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