HRV -> Mood

Dependencies:   MAX30101 Hexi_KW40Z Hexi_OLED_SSD1351

Committer:
jeannie9809
Date:
Sat Mar 16 02:43:26 2019 +0000
Revision:
1:eabf219849ab
Parent:
0:338c50c9c8cd
Child:
2:3389bdfd9afa
hex screen output correct

Who changed what in which revision?

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