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