HRV -> Mood
Dependencies: MAX30101 Hexi_KW40Z Hexi_OLED_SSD1351
8cee5929f4d8/main.cpp@7:7ae14b3b00d1, 2019-03-16 (annotated)
- Committer:
- jeannie9809
- Date:
- Sat Mar 16 04:19:57 2019 +0000
- Revision:
- 7:7ae14b3b00d1
- Parent:
- 6:ff1c3560db84
- Child:
- 8:f5bd13e53c38
before SDNN
Who changed what in which revision?
| User | Revision | Line number | New 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 | 6:ff1c3560db84 | 8 | #include <vector> |
| jeannie9809 | 6:ff1c3560db84 | 9 | using namespace std; |
| jeannie9809 | 0:338c50c9c8cd | 10 | #define FIFO_DATA_MAX 288 |
| jeannie9809 | 0:338c50c9c8cd | 11 | void UpdateSensorData(void); |
| jeannie9809 | 0:338c50c9c8cd | 12 | void StartHaptic(void); |
| jeannie9809 | 0:338c50c9c8cd | 13 | void StopHaptic(void const *n); |
| jeannie9809 | 0:338c50c9c8cd | 14 | void txTask(void); |
| jeannie9809 | 0:338c50c9c8cd | 15 | |
| jeannie9809 | 0:338c50c9c8cd | 16 | DigitalOut pwr1v8(PTA29); |
| jeannie9809 | 0:338c50c9c8cd | 17 | DigitalOut pwr3v3b(PTC13); |
| jeannie9809 | 0:338c50c9c8cd | 18 | DigitalOut pwr15v(PTB12); |
| jeannie9809 | 0:338c50c9c8cd | 19 | I2C i2c0(PTB1, PTB0); |
| jeannie9809 | 0:338c50c9c8cd | 20 | InterruptIn maximInterrupt(PTB18); |
| jeannie9809 | 0:338c50c9c8cd | 21 | Serial pc(USBTX, USBRX); |
| jeannie9809 | 0:338c50c9c8cd | 22 | SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); |
| jeannie9809 | 0:338c50c9c8cd | 23 | KW40Z kw40z_device(PTE24, PTE25); |
| jeannie9809 | 0:338c50c9c8cd | 24 | DigitalOut haptic(PTB9); |
| jeannie9809 | 0:338c50c9c8cd | 25 | EventQueue evqueue(32 * EVENTS_EVENT_SIZE); |
| jeannie9809 | 0:338c50c9c8cd | 26 | Thread t; |
| jeannie9809 | 0:338c50c9c8cd | 27 | /* Define timer for haptic feedback */ |
| jeannie9809 | 0:338c50c9c8cd | 28 | RtosTimer hapticTimer(StopHaptic, osTimerOnce); |
| jeannie9809 | 0:338c50c9c8cd | 29 | |
| jeannie9809 | 3:0da9235c9069 | 30 | /*Create a Thread to handle sending BLE Sensor Data */ |
| jeannie9809 | 0:338c50c9c8cd | 31 | Thread txThread; |
| jeannie9809 | 0:338c50c9c8cd | 32 | MAX30101 hr(i2c0); |
| jeannie9809 | 1:eabf219849ab | 33 | int ppg_single_sample; |
| jeannie9809 | 0:338c50c9c8cd | 34 | int mask_ppg = 0; |
| jeannie9809 | 0:338c50c9c8cd | 35 | uint32_t count = 0; |
| jeannie9809 | 0:338c50c9c8cd | 36 | uint32_t num; |
| jeannie9809 | 0:338c50c9c8cd | 37 | uint8_t testsignal = 60; |
| jeannie9809 | 0:338c50c9c8cd | 38 | |
| jeannie9809 | 2:3389bdfd9afa | 39 | // I added this |
| jeannie9809 | 4:94190624967a | 40 | const int num_samples = 1200; // for 30 sec |
| jeannie9809 | 4:94190624967a | 41 | double TIME[num_samples]; |
| jeannie9809 | 0:338c50c9c8cd | 42 | int ppg[num_samples]; |
| jeannie9809 | 1:eabf219849ab | 43 | int SDNN_n, SDNN; |
| jeannie9809 | 0:338c50c9c8cd | 44 | bool first_sample_set = true; |
| jeannie9809 | 1:eabf219849ab | 45 | double arousal, valence, HF_LF, HF_LF_n; |
| jeannie9809 | 0:338c50c9c8cd | 46 | |
| jeannie9809 | 3:0da9235c9069 | 47 | void StartHaptic(void) |
| jeannie9809 | 3:0da9235c9069 | 48 | { |
| jeannie9809 | 0:338c50c9c8cd | 49 | hapticTimer.start(50); |
| jeannie9809 | 0:338c50c9c8cd | 50 | haptic = 1; |
| jeannie9809 | 0:338c50c9c8cd | 51 | } |
| jeannie9809 | 0:338c50c9c8cd | 52 | void ButtonRight(void) |
| jeannie9809 | 0:338c50c9c8cd | 53 | { |
| jeannie9809 | 0:338c50c9c8cd | 54 | StartHaptic(); |
| jeannie9809 | 0:338c50c9c8cd | 55 | kw40z_device.ToggleAdvertisementMode(); |
| jeannie9809 | 0:338c50c9c8cd | 56 | } |
| jeannie9809 | 0:338c50c9c8cd | 57 | |
| jeannie9809 | 0:338c50c9c8cd | 58 | void ButtonLeft(void) |
| jeannie9809 | 0:338c50c9c8cd | 59 | { |
| jeannie9809 | 0:338c50c9c8cd | 60 | StartHaptic(); |
| jeannie9809 | 0:338c50c9c8cd | 61 | kw40z_device.ToggleAdvertisementMode(); |
| jeannie9809 | 0:338c50c9c8cd | 62 | } |
| jeannie9809 | 0:338c50c9c8cd | 63 | |
| jeannie9809 | 3:0da9235c9069 | 64 | void StopHaptic(void const *n) |
| jeannie9809 | 3:0da9235c9069 | 65 | { |
| jeannie9809 | 0:338c50c9c8cd | 66 | haptic = 0; |
| jeannie9809 | 0:338c50c9c8cd | 67 | hapticTimer.stop(); |
| jeannie9809 | 0:338c50c9c8cd | 68 | } |
| jeannie9809 | 3:0da9235c9069 | 69 | void txTask(void) |
| jeannie9809 | 3:0da9235c9069 | 70 | { |
| jeannie9809 | 3:0da9235c9069 | 71 | |
| jeannie9809 | 3:0da9235c9069 | 72 | while (true) { |
| jeannie9809 | 0:338c50c9c8cd | 73 | UpdateSensorData(); |
| jeannie9809 | 3:0da9235c9069 | 74 | |
| jeannie9809 | 0:338c50c9c8cd | 75 | /*Notify Hexiwear App that it is running Sensor Tag mode*/ |
| jeannie9809 | 0:338c50c9c8cd | 76 | kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG); |
| jeannie9809 | 0:338c50c9c8cd | 77 | //send heartrate |
| jeannie9809 | 3:0da9235c9069 | 78 | kw40z_device.SendHeartRate(testsignal); |
| jeannie9809 | 0:338c50c9c8cd | 79 | /*The following is sending dummy data over BLE. Replace with real data*/ |
| jeannie9809 | 3:0da9235c9069 | 80 | |
| jeannie9809 | 3:0da9235c9069 | 81 | /*Send Battery Level for 20% |
| jeannie9809 | 0:338c50c9c8cd | 82 | kw40z_device.SendBatteryLevel(battery); |
| jeannie9809 | 3:0da9235c9069 | 83 | |
| jeannie9809 | 3:0da9235c9069 | 84 | Send Ambient Light Level at 50% |
| jeannie9809 | 0:338c50c9c8cd | 85 | kw40z_device.SendAmbientLight(light);*/ |
| jeannie9809 | 3:0da9235c9069 | 86 | |
| jeannie9809 | 0:338c50c9c8cd | 87 | /*Send Humidity at 90% */ |
| jeannie9809 | 0:338c50c9c8cd | 88 | //kw40z_device.SendHumidity(humidity); |
| jeannie9809 | 3:0da9235c9069 | 89 | |
| jeannie9809 | 3:0da9235c9069 | 90 | /*Send Temperature at 25 degrees Celsius |
| jeannie9809 | 0:338c50c9c8cd | 91 | kw40z_device.SendTemperature(temperature); |
| jeannie9809 | 0:338c50c9c8cd | 92 | |
| jeannie9809 | 3:0da9235c9069 | 93 | /*Send Pressure at 100kPA */ |
| jeannie9809 | 0:338c50c9c8cd | 94 | //kw40z_device.SendPressure(pressure); |
| jeannie9809 | 3:0da9235c9069 | 95 | |
| jeannie9809 | 3:0da9235c9069 | 96 | /*Send Mag,Accel,Gyro Data. |
| jeannie9809 | 0:338c50c9c8cd | 97 | kw40z_device.SendGyro(x,y,z); |
| jeannie9809 | 0:338c50c9c8cd | 98 | kw40z_device.SendAccel(z,x,y); |
| jeannie9809 | 0:338c50c9c8cd | 99 | kw40z_device.SendMag(y,z,x);*/ |
| jeannie9809 | 0:338c50c9c8cd | 100 | |
| jeannie9809 | 3:0da9235c9069 | 101 | Thread::wait(1000); |
| jeannie9809 | 0:338c50c9c8cd | 102 | } |
| jeannie9809 | 0:338c50c9c8cd | 103 | } |
| jeannie9809 | 0:338c50c9c8cd | 104 | void UpdateSensorData(void) |
| jeannie9809 | 3:0da9235c9069 | 105 | { |
| jeannie9809 | 3:0da9235c9069 | 106 | testsignal+=1; |
| jeannie9809 | 0:338c50c9c8cd | 107 | /*battery -= 5; |
| jeannie9809 | 0:338c50c9c8cd | 108 | if(battery < 5) battery = 100; |
| jeannie9809 | 3:0da9235c9069 | 109 | |
| jeannie9809 | 0:338c50c9c8cd | 110 | light += 20; |
| jeannie9809 | 0:338c50c9c8cd | 111 | if(light > 100) light = 0; |
| jeannie9809 | 3:0da9235c9069 | 112 | |
| jeannie9809 | 0:338c50c9c8cd | 113 | humidity += 500; |
| jeannie9809 | 0:338c50c9c8cd | 114 | if(humidity > 8000) humidity = 2000; |
| jeannie9809 | 3:0da9235c9069 | 115 | |
| jeannie9809 | 0:338c50c9c8cd | 116 | temperature -= 200; |
| jeannie9809 | 0:338c50c9c8cd | 117 | if(temperature < 200) temperature = 4200; |
| jeannie9809 | 3:0da9235c9069 | 118 | |
| jeannie9809 | 0:338c50c9c8cd | 119 | pressure += 300; |
| jeannie9809 | 0:338c50c9c8cd | 120 | if(pressure > 10300) pressure = 7500; |
| jeannie9809 | 3:0da9235c9069 | 121 | |
| jeannie9809 | 0:338c50c9c8cd | 122 | x += 1400; |
| jeannie9809 | 0:338c50c9c8cd | 123 | y -= 2300; |
| jeannie9809 | 0:338c50c9c8cd | 124 | z += 1700;*/ |
| jeannie9809 | 0:338c50c9c8cd | 125 | } |
| jeannie9809 | 0:338c50c9c8cd | 126 | |
| jeannie9809 | 3:0da9235c9069 | 127 | void interruptHandlerQueued() |
| jeannie9809 | 3:0da9235c9069 | 128 | { |
| jeannie9809 | 4:94190624967a | 129 | // int temp_ppg[num_samples]; |
| jeannie9809 | 3:0da9235c9069 | 130 | for(int iter = 0; iter < num_samples; iter+=0) { |
| jeannie9809 | 0:338c50c9c8cd | 131 | |
| jeannie9809 | 3:0da9235c9069 | 132 | MAX30101::InterruptBitField_u interruptStatus; |
| jeannie9809 | 3:0da9235c9069 | 133 | hr.getInterruptStatus(interruptStatus); |
| jeannie9809 | 3:0da9235c9069 | 134 | // printf("Interrupt Status: 0x%02x\r\n", interruptStatus.all); |
| jeannie9809 | 3:0da9235c9069 | 135 | |
| jeannie9809 | 3:0da9235c9069 | 136 | if (interruptStatus.bits.pwr_rdy == 0x1) { |
| jeannie9809 | 3:0da9235c9069 | 137 | // printf("Powered on\r\n"); |
| jeannie9809 | 3:0da9235c9069 | 138 | |
| jeannie9809 | 3:0da9235c9069 | 139 | // Soft reset |
| jeannie9809 | 3:0da9235c9069 | 140 | MAX30101::ModeConfiguration_u modeConf; |
| jeannie9809 | 3:0da9235c9069 | 141 | modeConf.all = 0; |
| jeannie9809 | 3:0da9235c9069 | 142 | modeConf.bits.reset = 1; |
| jeannie9809 | 3:0da9235c9069 | 143 | hr.setModeConfiguration(modeConf); |
| jeannie9809 | 3:0da9235c9069 | 144 | wait(0.01); |
| jeannie9809 | 3:0da9235c9069 | 145 | |
| jeannie9809 | 3:0da9235c9069 | 146 | // Configure FIFO |
| jeannie9809 | 3:0da9235c9069 | 147 | MAX30101::FIFO_Configuration_u fifoConf; |
| jeannie9809 | 3:0da9235c9069 | 148 | hr.getFIFOConfiguration(fifoConf); |
| jeannie9809 | 0:338c50c9c8cd | 149 | // pc.printf("FIFO Configuration: 0x%02x\r\n", fifoConf.all); |
| jeannie9809 | 3:0da9235c9069 | 150 | |
| jeannie9809 | 3:0da9235c9069 | 151 | // Set LED power |
| jeannie9809 | 3:0da9235c9069 | 152 | hr.setLEDPulseAmplitude(MAX30101::LED1_PA, 0x0C); |
| jeannie9809 | 3:0da9235c9069 | 153 | hr.setLEDPulseAmplitude(MAX30101::ProxModeLED_PA, 0x19); |
| jeannie9809 | 0:338c50c9c8cd | 154 | // pc.printf("LED set\r\n"); |
| jeannie9809 | 3:0da9235c9069 | 155 | |
| jeannie9809 | 3:0da9235c9069 | 156 | MAX30101::SpO2Configuration_u spo2Conf; |
| jeannie9809 | 3:0da9235c9069 | 157 | hr.getSpO2Configuration(spo2Conf); |
| jeannie9809 | 3:0da9235c9069 | 158 | spo2Conf.bits.led_pw = MAX30101::PW_1; |
| jeannie9809 | 3:0da9235c9069 | 159 | spo2Conf.bits.spo2_sr = MAX30101::SR_100_Hz; |
| jeannie9809 | 3:0da9235c9069 | 160 | hr.setSpO2Configuration(spo2Conf); |
| jeannie9809 | 3:0da9235c9069 | 161 | hr.getSpO2Configuration(spo2Conf); |
| jeannie9809 | 0:338c50c9c8cd | 162 | // pc.printf("SpO2 Configuration: 0x%02x\r\n", spo2Conf.all); |
| jeannie9809 | 3:0da9235c9069 | 163 | |
| jeannie9809 | 3:0da9235c9069 | 164 | // Proximity settings |
| jeannie9809 | 3:0da9235c9069 | 165 | hr.setProxIntThreshold(0x14); |
| jeannie9809 | 3:0da9235c9069 | 166 | |
| jeannie9809 | 3:0da9235c9069 | 167 | // Enable HR mode |
| jeannie9809 | 3:0da9235c9069 | 168 | modeConf.all = 0; |
| jeannie9809 | 3:0da9235c9069 | 169 | modeConf.bits.mode = MAX30101::HeartRateMode; |
| jeannie9809 | 3:0da9235c9069 | 170 | hr.setModeConfiguration(modeConf); |
| jeannie9809 | 0:338c50c9c8cd | 171 | // printf("Mode set\r\n"); |
| jeannie9809 | 3:0da9235c9069 | 172 | } |
| jeannie9809 | 3:0da9235c9069 | 173 | |
| jeannie9809 | 3:0da9235c9069 | 174 | if (interruptStatus.bits.prox_int == 0x1) { |
| jeannie9809 | 0:338c50c9c8cd | 175 | // printf("Proximity Triggered, entered HR Mode."); |
| jeannie9809 | 3:0da9235c9069 | 176 | } |
| jeannie9809 | 3:0da9235c9069 | 177 | |
| jeannie9809 | 3:0da9235c9069 | 178 | if (interruptStatus.bits.ppg_rdy == 0x1) { |
| jeannie9809 | 0:338c50c9c8cd | 179 | // printf("PPG Ready.\r\n"); |
| jeannie9809 | 3:0da9235c9069 | 180 | mask_ppg = 1; |
| jeannie9809 | 3:0da9235c9069 | 181 | } |
| jeannie9809 | 3:0da9235c9069 | 182 | |
| jeannie9809 | 3:0da9235c9069 | 183 | if (interruptStatus.bits.a_full == 0x1) { |
| jeannie9809 | 0:338c50c9c8cd | 184 | // printf("FIFO Almost Full.\r\n"); |
| jeannie9809 | 3:0da9235c9069 | 185 | uint8_t data[FIFO_DATA_MAX]; |
| jeannie9809 | 3:0da9235c9069 | 186 | uint16_t readBytes = 0; |
| jeannie9809 | 3:0da9235c9069 | 187 | |
| jeannie9809 | 3:0da9235c9069 | 188 | hr.readFIFO(MAX30101::OneLedChannel, data, readBytes); |
| jeannie9809 | 3:0da9235c9069 | 189 | printf("data length: %u \r\n",readBytes); |
| jeannie9809 | 3:0da9235c9069 | 190 | //printf("data length: %u \r\n",data); |
| jeannie9809 | 3:0da9235c9069 | 191 | for (uint16_t i = 0; i < readBytes; i += 3) { |
| jeannie9809 | 3:0da9235c9069 | 192 | uint8_t sample[4] = {0}; |
| jeannie9809 | 3:0da9235c9069 | 193 | sample[0] = data[i + 2]; |
| jeannie9809 | 3:0da9235c9069 | 194 | sample[1] = data[i + 1]; |
| jeannie9809 | 3:0da9235c9069 | 195 | sample[2] = data[i]; |
| jeannie9809 | 3:0da9235c9069 | 196 | |
| jeannie9809 | 3:0da9235c9069 | 197 | num = *(uint32_t *) sample; |
| jeannie9809 | 3:0da9235c9069 | 198 | if (num < 310000) { |
| jeannie9809 | 3:0da9235c9069 | 199 | ppg_single_sample = 0; |
| jeannie9809 | 3:0da9235c9069 | 200 | printf("keep closer to your hand \r\n"); |
| jeannie9809 | 3:0da9235c9069 | 201 | } else { |
| jeannie9809 | 3:0da9235c9069 | 202 | |
| jeannie9809 | 3:0da9235c9069 | 203 | //ppg_single_sample = 65; |
| jeannie9809 | 6:ff1c3560db84 | 204 | ppg_single_sample = num; |
| jeannie9809 | 3:0da9235c9069 | 205 | if(iter < num_samples) |
| jeannie9809 | 3:0da9235c9069 | 206 | ppg[iter] = num; |
| jeannie9809 | 3:0da9235c9069 | 207 | // printf("%d\r\n", ppg_single_sample); // I commented this out |
| jeannie9809 | 3:0da9235c9069 | 208 | iter++; |
| jeannie9809 | 3:0da9235c9069 | 209 | } |
| jeannie9809 | 4:94190624967a | 210 | printf("%d; %d\r\n", iter, ppg[iter-1]); |
| jeannie9809 | 3:0da9235c9069 | 211 | |
| jeannie9809 | 3:0da9235c9069 | 212 | |
| jeannie9809 | 0:338c50c9c8cd | 213 | } |
| jeannie9809 | 0:338c50c9c8cd | 214 | } |
| jeannie9809 | 3:0da9235c9069 | 215 | |
| jeannie9809 | 3:0da9235c9069 | 216 | interruptStatus.all = 0xFF; |
| jeannie9809 | 4:94190624967a | 217 | |
| jeannie9809 | 3:0da9235c9069 | 218 | if (mask_ppg == 1) { |
| jeannie9809 | 3:0da9235c9069 | 219 | interruptStatus.bits.ppg_rdy = 0; |
| jeannie9809 | 3:0da9235c9069 | 220 | } |
| jeannie9809 | 3:0da9235c9069 | 221 | hr.enableInterrupts(interruptStatus); |
| jeannie9809 | 0:338c50c9c8cd | 222 | } |
| jeannie9809 | 4:94190624967a | 223 | /* |
| jeannie9809 | 4:94190624967a | 224 | for(int i = 0; i < num_samples; i++) { |
| jeannie9809 | 4:94190624967a | 225 | ppg[i] = temp_ppg[i]; |
| jeannie9809 | 4:94190624967a | 226 | } |
| jeannie9809 | 4:94190624967a | 227 | */ |
| jeannie9809 | 4:94190624967a | 228 | //ppg = temp_ppg; |
| jeannie9809 | 0:338c50c9c8cd | 229 | } |
| jeannie9809 | 0:338c50c9c8cd | 230 | |
| jeannie9809 | 3:0da9235c9069 | 231 | void interruptHandler() |
| jeannie9809 | 3:0da9235c9069 | 232 | { |
| jeannie9809 | 0:338c50c9c8cd | 233 | evqueue.call(interruptHandlerQueued); |
| jeannie9809 | 3:0da9235c9069 | 234 | |
| jeannie9809 | 4:94190624967a | 235 | // moving average |
| jeannie9809 | 4:94190624967a | 236 | double movave[num_samples]; |
| jeannie9809 | 4:94190624967a | 237 | int avecap = 25; |
| jeannie9809 | 4:94190624967a | 238 | int i = 0; |
| jeannie9809 | 4:94190624967a | 239 | int j = 0; |
| jeannie9809 | 4:94190624967a | 240 | for(i = 0; i < (avecap-1)/2; i++) { |
| jeannie9809 | 4:94190624967a | 241 | movave[i] = ppg[0]; |
| jeannie9809 | 4:94190624967a | 242 | for(j = 1; j < 1+(avecap-1)/2; j++) { |
| jeannie9809 | 4:94190624967a | 243 | movave[i] = movave[i] + ppg[j]; |
| jeannie9809 | 4:94190624967a | 244 | } |
| jeannie9809 | 4:94190624967a | 245 | movave[i] = (double)(movave[i]/(i+(avecap-1)/2-1)); |
| jeannie9809 | 4:94190624967a | 246 | } |
| jeannie9809 | 4:94190624967a | 247 | for(i = (num_samples-(avecap-1)/2); i < num_samples; i++) { |
| jeannie9809 | 4:94190624967a | 248 | movave[i] = ppg[i-(avecap-1)/2-1]; |
| jeannie9809 | 4:94190624967a | 249 | for(j = (i-(avecap-1)/2); j < num_samples; j++) { |
| jeannie9809 | 4:94190624967a | 250 | movave[i] = movave[i] + ppg[j]; |
| jeannie9809 | 4:94190624967a | 251 | } |
| jeannie9809 | 4:94190624967a | 252 | movave[i] = (double)(movave[i]/(num_samples-i+(avecap-1)/2)); |
| jeannie9809 | 4:94190624967a | 253 | } |
| jeannie9809 | 4:94190624967a | 254 | for(i = (avecap-1)/2; i < (num_samples-(avecap-1)/2); i++) { |
| jeannie9809 | 4:94190624967a | 255 | movave[i] = ppg[i-(avecap-1)/2-1]; |
| jeannie9809 | 4:94190624967a | 256 | for(j = i-(avecap-1)/2; j < i+(avecap-1)/2; j++) { |
| jeannie9809 | 4:94190624967a | 257 | movave[i] = movave[i] + ppg[j]; |
| jeannie9809 | 4:94190624967a | 258 | } |
| jeannie9809 | 4:94190624967a | 259 | movave[i] = (double)(movave[i]/avecap); |
| jeannie9809 | 4:94190624967a | 260 | } |
| jeannie9809 | 4:94190624967a | 261 | // normalize ppg |
| jeannie9809 | 4:94190624967a | 262 | for(i = 0; i < num_samples; i++) { |
| jeannie9809 | 4:94190624967a | 263 | ppg[i] = ppg[i] - movave[i]; |
| jeannie9809 | 4:94190624967a | 264 | } |
| jeannie9809 | 5:a6b6d0e5a69f | 265 | |
| jeannie9809 | 5:a6b6d0e5a69f | 266 | // smoothing curve |
| jeannie9809 | 5:a6b6d0e5a69f | 267 | for(i = 1; i < num_samples; i++) { |
| jeannie9809 | 5:a6b6d0e5a69f | 268 | ppg[i] = ppg[i] + ppg[i-1]; |
| jeannie9809 | 5:a6b6d0e5a69f | 269 | } |
| jeannie9809 | 5:a6b6d0e5a69f | 270 | |
| jeannie9809 | 5:a6b6d0e5a69f | 271 | // AMPD Algorithm |
| jeannie9809 | 5:a6b6d0e5a69f | 272 | const int kcap = 25; |
| jeannie9809 | 5:a6b6d0e5a69f | 273 | int m[kcap][num_samples]; |
| jeannie9809 | 5:a6b6d0e5a69f | 274 | for(int k = 0; k < kcap; k++) { |
| jeannie9809 | 5:a6b6d0e5a69f | 275 | for(i = 1; i < num_samples; i++) { |
| jeannie9809 | 5:a6b6d0e5a69f | 276 | if(i-1 < 0 || i-k-1 < 0 || i+k-1 >= num_samples) |
| jeannie9809 | 5:a6b6d0e5a69f | 277 | m[k][i] = 0; |
| jeannie9809 | 5:a6b6d0e5a69f | 278 | else if(ppg[i-1] > ppg[i-k-1] && ppg[i-1] > ppg[i+k-1]) |
| jeannie9809 | 5:a6b6d0e5a69f | 279 | m[k][i] = 1; |
| jeannie9809 | 5:a6b6d0e5a69f | 280 | else |
| jeannie9809 | 5:a6b6d0e5a69f | 281 | m[k][i] = 0; |
| jeannie9809 | 5:a6b6d0e5a69f | 282 | } |
| jeannie9809 | 5:a6b6d0e5a69f | 283 | } |
| jeannie9809 | 5:a6b6d0e5a69f | 284 | int max_mult[num_samples]; |
| jeannie9809 | 5:a6b6d0e5a69f | 285 | for(i = 0; i < num_samples; i++) { // max_mult = m(1,:)'; |
| jeannie9809 | 5:a6b6d0e5a69f | 286 | max_mult[i] = m[0][i]; |
| jeannie9809 | 5:a6b6d0e5a69f | 287 | } |
| jeannie9809 | 5:a6b6d0e5a69f | 288 | for(int k = 1; k < kcap; k++) { |
| jeannie9809 | 5:a6b6d0e5a69f | 289 | for(i = 0; i < num_samples; i++) { |
| jeannie9809 | 5:a6b6d0e5a69f | 290 | max_mult[i] = max_mult[i]*m[k][i]; |
| jeannie9809 | 5:a6b6d0e5a69f | 291 | } |
| jeannie9809 | 5:a6b6d0e5a69f | 292 | } |
| jeannie9809 | 5:a6b6d0e5a69f | 293 | |
| jeannie9809 | 6:ff1c3560db84 | 294 | // extract times that are max |
| jeannie9809 | 6:ff1c3560db84 | 295 | int num_max = 0; |
| jeannie9809 | 6:ff1c3560db84 | 296 | for(i = 0; i < num_samples; i++) { |
| jeannie9809 | 6:ff1c3560db84 | 297 | num_max = max_mult[i]; |
| jeannie9809 | 6:ff1c3560db84 | 298 | } |
| jeannie9809 | 6:ff1c3560db84 | 299 | vector<double> time_of_max; |
| jeannie9809 | 6:ff1c3560db84 | 300 | vector<int> index_of_max; |
| jeannie9809 | 6:ff1c3560db84 | 301 | vector<int> max_points; |
| jeannie9809 | 6:ff1c3560db84 | 302 | for(i = 0; i < num_samples; i++) { |
| jeannie9809 | 6:ff1c3560db84 | 303 | if(max_mult[i] == 1) { |
| jeannie9809 | 6:ff1c3560db84 | 304 | time_of_max.push_back(TIME[i-1]); |
| jeannie9809 | 6:ff1c3560db84 | 305 | index_of_max.push_back(i-1); |
| jeannie9809 | 6:ff1c3560db84 | 306 | max_points.push_back(ppg[i-1]); |
| jeannie9809 | 6:ff1c3560db84 | 307 | } |
| jeannie9809 | 6:ff1c3560db84 | 308 | } |
| jeannie9809 | 7:7ae14b3b00d1 | 309 | |
| jeannie9809 | 7:7ae14b3b00d1 | 310 | // calculating HRV |
| jeannie9809 | 7:7ae14b3b00d1 | 311 | vector<double> r; |
| jeannie9809 | 7:7ae14b3b00d1 | 312 | vector<int> index_r; |
| jeannie9809 | 7:7ae14b3b00d1 | 313 | double mean_inter_time = 0; |
| jeannie9809 | 7:7ae14b3b00d1 | 314 | for(i = 0; i < num_max-1; i++) { |
| jeannie9809 | 7:7ae14b3b00d1 | 315 | r.push_back(time_of_max.at(i+1)-time_of_max.at(i)); |
| jeannie9809 | 7:7ae14b3b00d1 | 316 | index_r.push_back(index_of_max.at(i+1) - index_of_max.at(i)); |
| jeannie9809 | 7:7ae14b3b00d1 | 317 | mean_inter_time = mean_inter_time + r.at(i); |
| jeannie9809 | 7:7ae14b3b00d1 | 318 | } |
| jeannie9809 | 7:7ae14b3b00d1 | 319 | mean_inter_time = (double)(mean_inter_time/(num_max-1)); |
| jeannie9809 | 7:7ae14b3b00d1 | 320 | |
| jeannie9809 | 7:7ae14b3b00d1 | 321 | // getting rid of outlier points in r |
| jeannie9809 | 7:7ae14b3b00d1 | 322 | for(i = 0; i < num_max-1; i++) { |
| jeannie9809 | 7:7ae14b3b00d1 | 323 | if(r.at(i) > mean_inter_time + 0.11) |
| jeannie9809 | 7:7ae14b3b00d1 | 324 | r.at(i) = mean_inter_time + 0.11; |
| jeannie9809 | 7:7ae14b3b00d1 | 325 | else if(r.at(i) < mean_inter_time - 0.11) |
| jeannie9809 | 7:7ae14b3b00d1 | 326 | r.at(i) = mean_inter_time - 0.11; |
| jeannie9809 | 7:7ae14b3b00d1 | 327 | } |
| jeannie9809 | 7:7ae14b3b00d1 | 328 | |
| jeannie9809 | 7:7ae14b3b00d1 | 329 | // SDNN -- std of normal to normal R-R intervals |
| jeannie9809 | 0:338c50c9c8cd | 330 | } |
| jeannie9809 | 0:338c50c9c8cd | 331 | |
| jeannie9809 | 0:338c50c9c8cd | 332 | // main() runs in its own thread in the OS |
| jeannie9809 | 3:0da9235c9069 | 333 | int main() |
| jeannie9809 | 3:0da9235c9069 | 334 | { |
| jeannie9809 | 0:338c50c9c8cd | 335 | // printf("Hello world.\r\n"); |
| jeannie9809 | 4:94190624967a | 336 | for(int i = 0; i < num_samples; i++) { |
| jeannie9809 | 4:94190624967a | 337 | TIME[i] = (double)(i*(30.0/num_samples)); |
| jeannie9809 | 4:94190624967a | 338 | } |
| jeannie9809 | 4:94190624967a | 339 | |
| jeannie9809 | 0:338c50c9c8cd | 340 | t.start(callback(&evqueue, &EventQueue::dispatch_forever)); |
| jeannie9809 | 0:338c50c9c8cd | 341 | kw40z_device.attach_buttonLeft(&ButtonLeft); |
| jeannie9809 | 0:338c50c9c8cd | 342 | kw40z_device.attach_buttonRight(&ButtonRight); |
| jeannie9809 | 3:0da9235c9069 | 343 | |
| jeannie9809 | 0:338c50c9c8cd | 344 | pwr1v8 = 1; |
| jeannie9809 | 0:338c50c9c8cd | 345 | pwr3v3b = 1; |
| jeannie9809 | 0:338c50c9c8cd | 346 | pwr15v = 0; |
| jeannie9809 | 3:0da9235c9069 | 347 | |
| jeannie9809 | 0:338c50c9c8cd | 348 | maximInterrupt.fall(interruptHandler); |
| jeannie9809 | 0:338c50c9c8cd | 349 | maximInterrupt.enable_irq(); |
| jeannie9809 | 3:0da9235c9069 | 350 | |
| jeannie9809 | 0:338c50c9c8cd | 351 | MAX30101::InterruptBitField_u interruptStatus; |
| jeannie9809 | 0:338c50c9c8cd | 352 | interruptStatus.all = 0xFF; |
| jeannie9809 | 0:338c50c9c8cd | 353 | hr.enableInterrupts(interruptStatus); |
| jeannie9809 | 3:0da9235c9069 | 354 | |
| jeannie9809 | 3:0da9235c9069 | 355 | char text[20]; /* Text Buffer */ |
| jeannie9809 | 0:338c50c9c8cd | 356 | oled_text_properties_t textProperties = {0}; |
| jeannie9809 | 3:0da9235c9069 | 357 | oled.GetTextProperties(&textProperties); |
| jeannie9809 | 0:338c50c9c8cd | 358 | /* Turn on the backlight of the OLED Display */ |
| jeannie9809 | 0:338c50c9c8cd | 359 | oled.DimScreenON(); |
| jeannie9809 | 3:0da9235c9069 | 360 | |
| jeannie9809 | 3:0da9235c9069 | 361 | /* Fills the screen with solid black */ |
| jeannie9809 | 0:338c50c9c8cd | 362 | oled.FillScreen(COLOR_BLACK); |
| jeannie9809 | 1:eabf219849ab | 363 | strcpy((char *) text, "Raw PPG:"); |
| jeannie9809 | 0:338c50c9c8cd | 364 | oled.Label((uint8_t *)text,7,0); |
| jeannie9809 | 3:0da9235c9069 | 365 | |
| jeannie9809 | 1:eabf219849ab | 366 | strcpy((char *) text, "SDNN; LF/HF:"); |
| jeannie9809 | 0:338c50c9c8cd | 367 | oled.Label((uint8_t *)text,7,40); |
| jeannie9809 | 0:338c50c9c8cd | 368 | //dynamic text setup |
| jeannie9809 | 0:338c50c9c8cd | 369 | textProperties.fontColor = COLOR_WHITE; |
| jeannie9809 | 0:338c50c9c8cd | 370 | textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT; |
| jeannie9809 | 3:0da9235c9069 | 371 | oled.SetTextProperties(&textProperties); |
| jeannie9809 | 3:0da9235c9069 | 372 | |
| jeannie9809 | 0:338c50c9c8cd | 373 | txThread.start(txTask); |
| jeannie9809 | 0:338c50c9c8cd | 374 | while (true) { |
| jeannie9809 | 3:0da9235c9069 | 375 | |
| jeannie9809 | 0:338c50c9c8cd | 376 | /* Format the time reading */ |
| jeannie9809 | 1:eabf219849ab | 377 | sprintf(text,"%d",ppg_single_sample); |
| jeannie9809 | 3:0da9235c9069 | 378 | |
| jeannie9809 | 0:338c50c9c8cd | 379 | /* Display time reading in 35px by 15px textbox at(x=55, y=40) */ |
| jeannie9809 | 0:338c50c9c8cd | 380 | oled.TextBox((uint8_t *)text,55,15,35,15); //Increase textbox for more digits |
| jeannie9809 | 3:0da9235c9069 | 381 | if (ppg_single_sample > 45) { |
| jeannie9809 | 1:eabf219849ab | 382 | sprintf(text,"%d; %.2f",SDNN, HF_LF); |
| jeannie9809 | 3:0da9235c9069 | 383 | |
| jeannie9809 | 0:338c50c9c8cd | 384 | /* Display time reading in 35px by 15px textbox at(x=55, y=40) */ |
| jeannie9809 | 0:338c50c9c8cd | 385 | oled.TextBox((uint8_t *)text,55,55,35,15); //Increase textbox for more digits |
| jeannie9809 | 3:0da9235c9069 | 386 | } else { |
| jeannie9809 | 0:338c50c9c8cd | 387 | sprintf(text,"wait HR"); |
| jeannie9809 | 3:0da9235c9069 | 388 | |
| jeannie9809 | 0:338c50c9c8cd | 389 | /* Display time reading in 35px by 15px textbox at(x=55, y=40) */ |
| jeannie9809 | 0:338c50c9c8cd | 390 | oled.TextBox((uint8_t *)text,55,55,35,15); |
| jeannie9809 | 0:338c50c9c8cd | 391 | } |
| jeannie9809 | 3:0da9235c9069 | 392 | |
| jeannie9809 | 0:338c50c9c8cd | 393 | Thread::wait(1000); |
| jeannie9809 | 3:0da9235c9069 | 394 | } |
| jeannie9809 | 0:338c50c9c8cd | 395 | return 0; |
| jeannie9809 | 0:338c50c9c8cd | 396 | } |
| jeannie9809 | 0:338c50c9c8cd | 397 |