HRV -> Mood

Dependencies:   MAX30101 Hexi_KW40Z Hexi_OLED_SSD1351

Committer:
jeannie9809
Date:
Sat Mar 16 02:51:04 2019 +0000
Revision:
2:3389bdfd9afa
Parent:
1:eabf219849ab
Child:
3:0da9235c9069
before i add that devil for loop

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jeannie9809 0:338c50c9c8cd 1 #include "mbed.h"
jeannie9809 0:338c50c9c8cd 2 #include "mbed_events.h"
jeannie9809 0:338c50c9c8cd 3 #include "MAX30101.h"
jeannie9809 0:338c50c9c8cd 4 #include "string.h"
jeannie9809 0:338c50c9c8cd 5 #include "Hexi_OLED_SSD1351.h"
jeannie9809 0:338c50c9c8cd 6 #include "Hexi_KW40Z.h"
jeannie9809 0:338c50c9c8cd 7 #include <math.h>
jeannie9809 0:338c50c9c8cd 8 #define FIFO_DATA_MAX 288
jeannie9809 0:338c50c9c8cd 9 void UpdateSensorData(void);
jeannie9809 0:338c50c9c8cd 10 void StartHaptic(void);
jeannie9809 0:338c50c9c8cd 11 void StopHaptic(void const *n);
jeannie9809 0:338c50c9c8cd 12 void txTask(void);
jeannie9809 0:338c50c9c8cd 13
jeannie9809 0:338c50c9c8cd 14 DigitalOut pwr1v8(PTA29);
jeannie9809 0:338c50c9c8cd 15 DigitalOut pwr3v3b(PTC13);
jeannie9809 0:338c50c9c8cd 16 DigitalOut pwr15v(PTB12);
jeannie9809 0:338c50c9c8cd 17 I2C i2c0(PTB1, PTB0);
jeannie9809 0:338c50c9c8cd 18 InterruptIn maximInterrupt(PTB18);
jeannie9809 0:338c50c9c8cd 19 Serial pc(USBTX, USBRX);
jeannie9809 0:338c50c9c8cd 20 SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15);
jeannie9809 0:338c50c9c8cd 21 KW40Z kw40z_device(PTE24, PTE25);
jeannie9809 0:338c50c9c8cd 22 DigitalOut haptic(PTB9);
jeannie9809 0:338c50c9c8cd 23 EventQueue evqueue(32 * EVENTS_EVENT_SIZE);
jeannie9809 0:338c50c9c8cd 24 Thread t;
jeannie9809 0:338c50c9c8cd 25 /* Define timer for haptic feedback */
jeannie9809 0:338c50c9c8cd 26 RtosTimer hapticTimer(StopHaptic, osTimerOnce);
jeannie9809 0:338c50c9c8cd 27
jeannie9809 0:338c50c9c8cd 28 /*Create a Thread to handle sending BLE Sensor Data */
jeannie9809 0:338c50c9c8cd 29 Thread txThread;
jeannie9809 0:338c50c9c8cd 30 MAX30101 hr(i2c0);
jeannie9809 1:eabf219849ab 31 int ppg_single_sample;
jeannie9809 0:338c50c9c8cd 32 int mask_ppg = 0;
jeannie9809 0:338c50c9c8cd 33 uint32_t count = 0;
jeannie9809 0:338c50c9c8cd 34 uint32_t num;
jeannie9809 0:338c50c9c8cd 35 uint8_t testsignal = 60;
jeannie9809 0:338c50c9c8cd 36
jeannie9809 2:3389bdfd9afa 37 // I added this
jeannie9809 0:338c50c9c8cd 38 const int num_samples = 2129;
jeannie9809 0:338c50c9c8cd 39 int index = 0;
jeannie9809 0:338c50c9c8cd 40 int ppg[num_samples];
jeannie9809 1:eabf219849ab 41 int SDNN_n, SDNN;
jeannie9809 0:338c50c9c8cd 42 bool first_sample_set = true;
jeannie9809 1:eabf219849ab 43 double arousal, valence, HF_LF, HF_LF_n;
jeannie9809 0:338c50c9c8cd 44
jeannie9809 0:338c50c9c8cd 45 void StartHaptic(void) {
jeannie9809 0:338c50c9c8cd 46 hapticTimer.start(50);
jeannie9809 0:338c50c9c8cd 47 haptic = 1;
jeannie9809 0:338c50c9c8cd 48 }
jeannie9809 0:338c50c9c8cd 49 void ButtonRight(void)
jeannie9809 0:338c50c9c8cd 50 {
jeannie9809 0:338c50c9c8cd 51 StartHaptic();
jeannie9809 0:338c50c9c8cd 52 kw40z_device.ToggleAdvertisementMode();
jeannie9809 0:338c50c9c8cd 53 }
jeannie9809 0:338c50c9c8cd 54
jeannie9809 0:338c50c9c8cd 55 void ButtonLeft(void)
jeannie9809 0:338c50c9c8cd 56 {
jeannie9809 0:338c50c9c8cd 57 StartHaptic();
jeannie9809 0:338c50c9c8cd 58 kw40z_device.ToggleAdvertisementMode();
jeannie9809 0:338c50c9c8cd 59 }
jeannie9809 0:338c50c9c8cd 60
jeannie9809 0:338c50c9c8cd 61 void StopHaptic(void const *n) {
jeannie9809 0:338c50c9c8cd 62 haptic = 0;
jeannie9809 0:338c50c9c8cd 63 hapticTimer.stop();
jeannie9809 0:338c50c9c8cd 64 }
jeannie9809 0:338c50c9c8cd 65 void txTask(void){
jeannie9809 0:338c50c9c8cd 66
jeannie9809 0:338c50c9c8cd 67 while (true)
jeannie9809 0:338c50c9c8cd 68 {
jeannie9809 0:338c50c9c8cd 69 UpdateSensorData();
jeannie9809 0:338c50c9c8cd 70
jeannie9809 0:338c50c9c8cd 71 /*Notify Hexiwear App that it is running Sensor Tag mode*/
jeannie9809 0:338c50c9c8cd 72 kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG);
jeannie9809 0:338c50c9c8cd 73 //send heartrate
jeannie9809 0:338c50c9c8cd 74 kw40z_device.SendHeartRate(testsignal);
jeannie9809 0:338c50c9c8cd 75 /*The following is sending dummy data over BLE. Replace with real data*/
jeannie9809 0:338c50c9c8cd 76
jeannie9809 0:338c50c9c8cd 77 /*Send Battery Level for 20%
jeannie9809 0:338c50c9c8cd 78 kw40z_device.SendBatteryLevel(battery);
jeannie9809 0:338c50c9c8cd 79
jeannie9809 0:338c50c9c8cd 80 Send Ambient Light Level at 50%
jeannie9809 0:338c50c9c8cd 81 kw40z_device.SendAmbientLight(light);*/
jeannie9809 0:338c50c9c8cd 82
jeannie9809 0:338c50c9c8cd 83 /*Send Humidity at 90% */
jeannie9809 0:338c50c9c8cd 84 //kw40z_device.SendHumidity(humidity);
jeannie9809 0:338c50c9c8cd 85
jeannie9809 0:338c50c9c8cd 86 /*Send Temperature at 25 degrees Celsius
jeannie9809 0:338c50c9c8cd 87 kw40z_device.SendTemperature(temperature);
jeannie9809 0:338c50c9c8cd 88
jeannie9809 0:338c50c9c8cd 89 /*Send Pressure at 100kPA */
jeannie9809 0:338c50c9c8cd 90 //kw40z_device.SendPressure(pressure);
jeannie9809 0:338c50c9c8cd 91
jeannie9809 0:338c50c9c8cd 92 /*Send Mag,Accel,Gyro Data.
jeannie9809 0:338c50c9c8cd 93 kw40z_device.SendGyro(x,y,z);
jeannie9809 0:338c50c9c8cd 94 kw40z_device.SendAccel(z,x,y);
jeannie9809 0:338c50c9c8cd 95 kw40z_device.SendMag(y,z,x);*/
jeannie9809 0:338c50c9c8cd 96
jeannie9809 0:338c50c9c8cd 97 Thread::wait(1000);
jeannie9809 0:338c50c9c8cd 98 }
jeannie9809 0:338c50c9c8cd 99 }
jeannie9809 0:338c50c9c8cd 100 void UpdateSensorData(void)
jeannie9809 0:338c50c9c8cd 101 {
jeannie9809 0:338c50c9c8cd 102 testsignal+=1;
jeannie9809 0:338c50c9c8cd 103 /*battery -= 5;
jeannie9809 0:338c50c9c8cd 104 if(battery < 5) battery = 100;
jeannie9809 0:338c50c9c8cd 105
jeannie9809 0:338c50c9c8cd 106 light += 20;
jeannie9809 0:338c50c9c8cd 107 if(light > 100) light = 0;
jeannie9809 0:338c50c9c8cd 108
jeannie9809 0:338c50c9c8cd 109 humidity += 500;
jeannie9809 0:338c50c9c8cd 110 if(humidity > 8000) humidity = 2000;
jeannie9809 0:338c50c9c8cd 111
jeannie9809 0:338c50c9c8cd 112 temperature -= 200;
jeannie9809 0:338c50c9c8cd 113 if(temperature < 200) temperature = 4200;
jeannie9809 0:338c50c9c8cd 114
jeannie9809 0:338c50c9c8cd 115 pressure += 300;
jeannie9809 0:338c50c9c8cd 116 if(pressure > 10300) pressure = 7500;
jeannie9809 0:338c50c9c8cd 117
jeannie9809 0:338c50c9c8cd 118 x += 1400;
jeannie9809 0:338c50c9c8cd 119 y -= 2300;
jeannie9809 0:338c50c9c8cd 120 z += 1700;*/
jeannie9809 0:338c50c9c8cd 121 }
jeannie9809 0:338c50c9c8cd 122
jeannie9809 0:338c50c9c8cd 123 void interruptHandlerQueued() {
jeannie9809 0:338c50c9c8cd 124
jeannie9809 0:338c50c9c8cd 125 MAX30101::InterruptBitField_u interruptStatus;
jeannie9809 0:338c50c9c8cd 126 hr.getInterruptStatus(interruptStatus);
jeannie9809 0:338c50c9c8cd 127 // printf("Interrupt Status: 0x%02x\r\n", interruptStatus.all);
jeannie9809 0:338c50c9c8cd 128
jeannie9809 0:338c50c9c8cd 129 if (interruptStatus.bits.pwr_rdy == 0x1) {
jeannie9809 0:338c50c9c8cd 130 // printf("Powered on\r\n");
jeannie9809 0:338c50c9c8cd 131
jeannie9809 0:338c50c9c8cd 132 // Soft reset
jeannie9809 0:338c50c9c8cd 133 MAX30101::ModeConfiguration_u modeConf;
jeannie9809 0:338c50c9c8cd 134 modeConf.all = 0;
jeannie9809 0:338c50c9c8cd 135 modeConf.bits.reset = 1;
jeannie9809 0:338c50c9c8cd 136 hr.setModeConfiguration(modeConf);
jeannie9809 0:338c50c9c8cd 137 wait(0.01);
jeannie9809 0:338c50c9c8cd 138
jeannie9809 0:338c50c9c8cd 139 // Configure FIFO
jeannie9809 0:338c50c9c8cd 140 MAX30101::FIFO_Configuration_u fifoConf;
jeannie9809 0:338c50c9c8cd 141 hr.getFIFOConfiguration(fifoConf);
jeannie9809 0:338c50c9c8cd 142 // pc.printf("FIFO Configuration: 0x%02x\r\n", fifoConf.all);
jeannie9809 0:338c50c9c8cd 143
jeannie9809 0:338c50c9c8cd 144 // Set LED power
jeannie9809 0:338c50c9c8cd 145 hr.setLEDPulseAmplitude(MAX30101::LED1_PA, 0x0C);
jeannie9809 0:338c50c9c8cd 146 hr.setLEDPulseAmplitude(MAX30101::ProxModeLED_PA, 0x19);
jeannie9809 0:338c50c9c8cd 147 // pc.printf("LED set\r\n");
jeannie9809 0:338c50c9c8cd 148
jeannie9809 0:338c50c9c8cd 149 MAX30101::SpO2Configuration_u spo2Conf;
jeannie9809 0:338c50c9c8cd 150 hr.getSpO2Configuration(spo2Conf);
jeannie9809 0:338c50c9c8cd 151 spo2Conf.bits.led_pw = MAX30101::PW_1;
jeannie9809 0:338c50c9c8cd 152 spo2Conf.bits.spo2_sr = MAX30101::SR_100_Hz;
jeannie9809 0:338c50c9c8cd 153 hr.setSpO2Configuration(spo2Conf);
jeannie9809 0:338c50c9c8cd 154 hr.getSpO2Configuration(spo2Conf);
jeannie9809 0:338c50c9c8cd 155 // pc.printf("SpO2 Configuration: 0x%02x\r\n", spo2Conf.all);
jeannie9809 0:338c50c9c8cd 156
jeannie9809 0:338c50c9c8cd 157 // Proximity settings
jeannie9809 0:338c50c9c8cd 158 hr.setProxIntThreshold(0x14);
jeannie9809 0:338c50c9c8cd 159
jeannie9809 0:338c50c9c8cd 160 // Enable HR mode
jeannie9809 0:338c50c9c8cd 161 modeConf.all = 0;
jeannie9809 0:338c50c9c8cd 162 modeConf.bits.mode = MAX30101::HeartRateMode;
jeannie9809 0:338c50c9c8cd 163 hr.setModeConfiguration(modeConf);
jeannie9809 0:338c50c9c8cd 164 // printf("Mode set\r\n");
jeannie9809 0:338c50c9c8cd 165 }
jeannie9809 0:338c50c9c8cd 166
jeannie9809 0:338c50c9c8cd 167 if (interruptStatus.bits.prox_int == 0x1) {
jeannie9809 0:338c50c9c8cd 168 // printf("Proximity Triggered, entered HR Mode.");
jeannie9809 0:338c50c9c8cd 169 }
jeannie9809 0:338c50c9c8cd 170
jeannie9809 0:338c50c9c8cd 171 if (interruptStatus.bits.ppg_rdy == 0x1) {
jeannie9809 0:338c50c9c8cd 172 // printf("PPG Ready.\r\n");
jeannie9809 0:338c50c9c8cd 173 mask_ppg = 1;
jeannie9809 0:338c50c9c8cd 174 }
jeannie9809 0:338c50c9c8cd 175
jeannie9809 0:338c50c9c8cd 176 if (interruptStatus.bits.a_full == 0x1) {
jeannie9809 0:338c50c9c8cd 177 // printf("FIFO Almost Full.\r\n");
jeannie9809 0:338c50c9c8cd 178 uint8_t data[FIFO_DATA_MAX];
jeannie9809 0:338c50c9c8cd 179 uint16_t readBytes = 0;
jeannie9809 0:338c50c9c8cd 180
jeannie9809 0:338c50c9c8cd 181
jeannie9809 2:3389bdfd9afa 182
jeannie9809 0:338c50c9c8cd 183 hr.readFIFO(MAX30101::OneLedChannel, data, readBytes);
jeannie9809 2:3389bdfd9afa 184 printf("data length: %u \r\n",readBytes);
jeannie9809 0:338c50c9c8cd 185 //printf("data length: %u \r\n",data);
jeannie9809 0:338c50c9c8cd 186 for (uint16_t i = 0; i < readBytes; i += 3) {
jeannie9809 0:338c50c9c8cd 187 uint8_t sample[4] = {0};
jeannie9809 0:338c50c9c8cd 188 sample[0] = data[i + 2];
jeannie9809 0:338c50c9c8cd 189 sample[1] = data[i + 1];
jeannie9809 0:338c50c9c8cd 190 sample[2] = data[i];
jeannie9809 0:338c50c9c8cd 191
jeannie9809 0:338c50c9c8cd 192 num = *(uint32_t *) sample;
jeannie9809 0:338c50c9c8cd 193 if (num < 310000){
jeannie9809 1:eabf219849ab 194 ppg_single_sample = 0;
jeannie9809 0:338c50c9c8cd 195 printf("keep closer to your hand \r\n");
jeannie9809 0:338c50c9c8cd 196 }
jeannie9809 0:338c50c9c8cd 197 else {
jeannie9809 0:338c50c9c8cd 198
jeannie9809 1:eabf219849ab 199 //ppg_single_sample = 65;
jeannie9809 1:eabf219849ab 200 ppg_single_sample = num;
jeannie9809 2:3389bdfd9afa 201 ppg[index] = num;
jeannie9809 2:3389bdfd9afa 202 // printf("%d\r\n", ppg_single_sample); // I commented this out
jeannie9809 0:338c50c9c8cd 203 }
jeannie9809 0:338c50c9c8cd 204 //printf("%u\r\n", num);
jeannie9809 0:338c50c9c8cd 205
jeannie9809 0:338c50c9c8cd 206
jeannie9809 0:338c50c9c8cd 207 }
jeannie9809 0:338c50c9c8cd 208 }
jeannie9809 0:338c50c9c8cd 209
jeannie9809 0:338c50c9c8cd 210 interruptStatus.all = 0xFF;
jeannie9809 0:338c50c9c8cd 211 if (mask_ppg == 1) {
jeannie9809 0:338c50c9c8cd 212 interruptStatus.bits.ppg_rdy = 0;
jeannie9809 0:338c50c9c8cd 213 }
jeannie9809 0:338c50c9c8cd 214 hr.enableInterrupts(interruptStatus);
jeannie9809 0:338c50c9c8cd 215 }
jeannie9809 0:338c50c9c8cd 216
jeannie9809 0:338c50c9c8cd 217 void interruptHandler() {
jeannie9809 0:338c50c9c8cd 218 evqueue.call(interruptHandlerQueued);
jeannie9809 0:338c50c9c8cd 219 }
jeannie9809 0:338c50c9c8cd 220
jeannie9809 0:338c50c9c8cd 221 // main() runs in its own thread in the OS
jeannie9809 0:338c50c9c8cd 222 int main() {
jeannie9809 0:338c50c9c8cd 223 // printf("Hello world.\r\n");
jeannie9809 0:338c50c9c8cd 224
jeannie9809 0:338c50c9c8cd 225 t.start(callback(&evqueue, &EventQueue::dispatch_forever));
jeannie9809 0:338c50c9c8cd 226 kw40z_device.attach_buttonLeft(&ButtonLeft);
jeannie9809 0:338c50c9c8cd 227 kw40z_device.attach_buttonRight(&ButtonRight);
jeannie9809 1:eabf219849ab 228
jeannie9809 0:338c50c9c8cd 229 pwr1v8 = 1;
jeannie9809 0:338c50c9c8cd 230 pwr3v3b = 1;
jeannie9809 0:338c50c9c8cd 231 pwr15v = 0;
jeannie9809 0:338c50c9c8cd 232
jeannie9809 0:338c50c9c8cd 233 maximInterrupt.fall(interruptHandler);
jeannie9809 0:338c50c9c8cd 234 maximInterrupt.enable_irq();
jeannie9809 0:338c50c9c8cd 235
jeannie9809 0:338c50c9c8cd 236 MAX30101::InterruptBitField_u interruptStatus;
jeannie9809 0:338c50c9c8cd 237 interruptStatus.all = 0xFF;
jeannie9809 0:338c50c9c8cd 238 hr.enableInterrupts(interruptStatus);
jeannie9809 0:338c50c9c8cd 239
jeannie9809 0:338c50c9c8cd 240 char text[20]; /* Text Buffer */
jeannie9809 0:338c50c9c8cd 241 oled_text_properties_t textProperties = {0};
jeannie9809 0:338c50c9c8cd 242 oled.GetTextProperties(&textProperties);
jeannie9809 0:338c50c9c8cd 243 /* Turn on the backlight of the OLED Display */
jeannie9809 0:338c50c9c8cd 244 oled.DimScreenON();
jeannie9809 0:338c50c9c8cd 245
jeannie9809 0:338c50c9c8cd 246 /* Fills the screen with solid black */
jeannie9809 0:338c50c9c8cd 247 oled.FillScreen(COLOR_BLACK);
jeannie9809 1:eabf219849ab 248 strcpy((char *) text, "Raw PPG:");
jeannie9809 0:338c50c9c8cd 249 oled.Label((uint8_t *)text,7,0);
jeannie9809 0:338c50c9c8cd 250
jeannie9809 1:eabf219849ab 251 strcpy((char *) text, "SDNN; LF/HF:");
jeannie9809 0:338c50c9c8cd 252 oled.Label((uint8_t *)text,7,40);
jeannie9809 0:338c50c9c8cd 253 //dynamic text setup
jeannie9809 0:338c50c9c8cd 254 textProperties.fontColor = COLOR_WHITE;
jeannie9809 0:338c50c9c8cd 255 textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT;
jeannie9809 0:338c50c9c8cd 256 oled.SetTextProperties(&textProperties);
jeannie9809 0:338c50c9c8cd 257
jeannie9809 0:338c50c9c8cd 258 txThread.start(txTask);
jeannie9809 0:338c50c9c8cd 259 while (true) {
jeannie9809 0:338c50c9c8cd 260
jeannie9809 0:338c50c9c8cd 261 /* Format the time reading */
jeannie9809 1:eabf219849ab 262 sprintf(text,"%d",ppg_single_sample);
jeannie9809 0:338c50c9c8cd 263
jeannie9809 0:338c50c9c8cd 264 /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
jeannie9809 0:338c50c9c8cd 265 oled.TextBox((uint8_t *)text,55,15,35,15); //Increase textbox for more digits
jeannie9809 1:eabf219849ab 266 if (ppg_single_sample > 45){
jeannie9809 1:eabf219849ab 267 sprintf(text,"%d; %.2f",SDNN, HF_LF);
jeannie9809 0:338c50c9c8cd 268
jeannie9809 0:338c50c9c8cd 269 /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
jeannie9809 0:338c50c9c8cd 270 oled.TextBox((uint8_t *)text,55,55,35,15); //Increase textbox for more digits
jeannie9809 0:338c50c9c8cd 271 }
jeannie9809 0:338c50c9c8cd 272 else{
jeannie9809 0:338c50c9c8cd 273 sprintf(text,"wait HR");
jeannie9809 0:338c50c9c8cd 274
jeannie9809 0:338c50c9c8cd 275 /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
jeannie9809 0:338c50c9c8cd 276 oled.TextBox((uint8_t *)text,55,55,35,15);
jeannie9809 0:338c50c9c8cd 277 }
jeannie9809 0:338c50c9c8cd 278
jeannie9809 0:338c50c9c8cd 279 Thread::wait(1000);
jeannie9809 0:338c50c9c8cd 280 }
jeannie9809 0:338c50c9c8cd 281 return 0;
jeannie9809 0:338c50c9c8cd 282 }
jeannie9809 0:338c50c9c8cd 283