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