HRV -> Mood

Dependencies:   MAX30101 Hexi_KW40Z Hexi_OLED_SSD1351

Committer:
jeannie9809
Date:
Sat Mar 16 02:20:01 2019 +0000
Revision:
0:338c50c9c8cd
Child:
1:eabf219849ab
very beginning + var

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 0:338c50c9c8cd 32 int realHeartRate;
jeannie9809 0:338c50c9c8cd 33 float calorie;
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 0:338c50c9c8cd 39 // i added this
jeannie9809 0:338c50c9c8cd 40 const int num_samples = 2129;
jeannie9809 0:338c50c9c8cd 41 int index = 0;
jeannie9809 0:338c50c9c8cd 42 int ppg[num_samples];
jeannie9809 0:338c50c9c8cd 43 int SDNN_n, SDNN, HF_LF_n, HF_LF;
jeannie9809 0:338c50c9c8cd 44 bool first_sample_set = true;
jeannie9809 0:338c50c9c8cd 45 double arousal, valence;
jeannie9809 0:338c50c9c8cd 46
jeannie9809 0:338c50c9c8cd 47 void StartHaptic(void) {
jeannie9809 0:338c50c9c8cd 48 hapticTimer.start(50);
jeannie9809 0:338c50c9c8cd 49 haptic = 1;
jeannie9809 0:338c50c9c8cd 50 }
jeannie9809 0:338c50c9c8cd 51 void ButtonRight(void)
jeannie9809 0:338c50c9c8cd 52 {
jeannie9809 0:338c50c9c8cd 53 StartHaptic();
jeannie9809 0:338c50c9c8cd 54 kw40z_device.ToggleAdvertisementMode();
jeannie9809 0:338c50c9c8cd 55 }
jeannie9809 0:338c50c9c8cd 56
jeannie9809 0:338c50c9c8cd 57 void ButtonLeft(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 StopHaptic(void const *n) {
jeannie9809 0:338c50c9c8cd 64 haptic = 0;
jeannie9809 0:338c50c9c8cd 65 hapticTimer.stop();
jeannie9809 0:338c50c9c8cd 66 }
jeannie9809 0:338c50c9c8cd 67 void txTask(void){
jeannie9809 0:338c50c9c8cd 68
jeannie9809 0:338c50c9c8cd 69 while (true)
jeannie9809 0:338c50c9c8cd 70 {
jeannie9809 0:338c50c9c8cd 71 UpdateSensorData();
jeannie9809 0:338c50c9c8cd 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 0:338c50c9c8cd 76 kw40z_device.SendHeartRate(testsignal);
jeannie9809 0:338c50c9c8cd 77 /*The following is sending dummy data over BLE. Replace with real data*/
jeannie9809 0:338c50c9c8cd 78
jeannie9809 0:338c50c9c8cd 79 /*Send Battery Level for 20%
jeannie9809 0:338c50c9c8cd 80 kw40z_device.SendBatteryLevel(battery);
jeannie9809 0:338c50c9c8cd 81
jeannie9809 0:338c50c9c8cd 82 Send Ambient Light Level at 50%
jeannie9809 0:338c50c9c8cd 83 kw40z_device.SendAmbientLight(light);*/
jeannie9809 0:338c50c9c8cd 84
jeannie9809 0:338c50c9c8cd 85 /*Send Humidity at 90% */
jeannie9809 0:338c50c9c8cd 86 //kw40z_device.SendHumidity(humidity);
jeannie9809 0:338c50c9c8cd 87
jeannie9809 0:338c50c9c8cd 88 /*Send Temperature at 25 degrees Celsius
jeannie9809 0:338c50c9c8cd 89 kw40z_device.SendTemperature(temperature);
jeannie9809 0:338c50c9c8cd 90
jeannie9809 0:338c50c9c8cd 91 /*Send Pressure at 100kPA */
jeannie9809 0:338c50c9c8cd 92 //kw40z_device.SendPressure(pressure);
jeannie9809 0:338c50c9c8cd 93
jeannie9809 0:338c50c9c8cd 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 0:338c50c9c8cd 99 Thread::wait(1000);
jeannie9809 0:338c50c9c8cd 100 }
jeannie9809 0:338c50c9c8cd 101 }
jeannie9809 0:338c50c9c8cd 102 void UpdateSensorData(void)
jeannie9809 0:338c50c9c8cd 103 {
jeannie9809 0:338c50c9c8cd 104 testsignal+=1;
jeannie9809 0:338c50c9c8cd 105 /*battery -= 5;
jeannie9809 0:338c50c9c8cd 106 if(battery < 5) battery = 100;
jeannie9809 0:338c50c9c8cd 107
jeannie9809 0:338c50c9c8cd 108 light += 20;
jeannie9809 0:338c50c9c8cd 109 if(light > 100) light = 0;
jeannie9809 0:338c50c9c8cd 110
jeannie9809 0:338c50c9c8cd 111 humidity += 500;
jeannie9809 0:338c50c9c8cd 112 if(humidity > 8000) humidity = 2000;
jeannie9809 0:338c50c9c8cd 113
jeannie9809 0:338c50c9c8cd 114 temperature -= 200;
jeannie9809 0:338c50c9c8cd 115 if(temperature < 200) temperature = 4200;
jeannie9809 0:338c50c9c8cd 116
jeannie9809 0:338c50c9c8cd 117 pressure += 300;
jeannie9809 0:338c50c9c8cd 118 if(pressure > 10300) pressure = 7500;
jeannie9809 0:338c50c9c8cd 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 0:338c50c9c8cd 125 void interruptHandlerQueued() {
jeannie9809 0:338c50c9c8cd 126
jeannie9809 0:338c50c9c8cd 127 MAX30101::InterruptBitField_u interruptStatus;
jeannie9809 0:338c50c9c8cd 128 hr.getInterruptStatus(interruptStatus);
jeannie9809 0:338c50c9c8cd 129 // printf("Interrupt Status: 0x%02x\r\n", interruptStatus.all);
jeannie9809 0:338c50c9c8cd 130
jeannie9809 0:338c50c9c8cd 131 if (interruptStatus.bits.pwr_rdy == 0x1) {
jeannie9809 0:338c50c9c8cd 132 // printf("Powered on\r\n");
jeannie9809 0:338c50c9c8cd 133
jeannie9809 0:338c50c9c8cd 134 // Soft reset
jeannie9809 0:338c50c9c8cd 135 MAX30101::ModeConfiguration_u modeConf;
jeannie9809 0:338c50c9c8cd 136 modeConf.all = 0;
jeannie9809 0:338c50c9c8cd 137 modeConf.bits.reset = 1;
jeannie9809 0:338c50c9c8cd 138 hr.setModeConfiguration(modeConf);
jeannie9809 0:338c50c9c8cd 139 wait(0.01);
jeannie9809 0:338c50c9c8cd 140
jeannie9809 0:338c50c9c8cd 141 // Configure FIFO
jeannie9809 0:338c50c9c8cd 142 MAX30101::FIFO_Configuration_u fifoConf;
jeannie9809 0:338c50c9c8cd 143 hr.getFIFOConfiguration(fifoConf);
jeannie9809 0:338c50c9c8cd 144 // pc.printf("FIFO Configuration: 0x%02x\r\n", fifoConf.all);
jeannie9809 0:338c50c9c8cd 145
jeannie9809 0:338c50c9c8cd 146 // Set LED power
jeannie9809 0:338c50c9c8cd 147 hr.setLEDPulseAmplitude(MAX30101::LED1_PA, 0x0C);
jeannie9809 0:338c50c9c8cd 148 hr.setLEDPulseAmplitude(MAX30101::ProxModeLED_PA, 0x19);
jeannie9809 0:338c50c9c8cd 149 // pc.printf("LED set\r\n");
jeannie9809 0:338c50c9c8cd 150
jeannie9809 0:338c50c9c8cd 151 MAX30101::SpO2Configuration_u spo2Conf;
jeannie9809 0:338c50c9c8cd 152 hr.getSpO2Configuration(spo2Conf);
jeannie9809 0:338c50c9c8cd 153 spo2Conf.bits.led_pw = MAX30101::PW_1;
jeannie9809 0:338c50c9c8cd 154 spo2Conf.bits.spo2_sr = MAX30101::SR_100_Hz;
jeannie9809 0:338c50c9c8cd 155 hr.setSpO2Configuration(spo2Conf);
jeannie9809 0:338c50c9c8cd 156 hr.getSpO2Configuration(spo2Conf);
jeannie9809 0:338c50c9c8cd 157 // pc.printf("SpO2 Configuration: 0x%02x\r\n", spo2Conf.all);
jeannie9809 0:338c50c9c8cd 158
jeannie9809 0:338c50c9c8cd 159 // Proximity settings
jeannie9809 0:338c50c9c8cd 160 hr.setProxIntThreshold(0x14);
jeannie9809 0:338c50c9c8cd 161
jeannie9809 0:338c50c9c8cd 162 // Enable HR mode
jeannie9809 0:338c50c9c8cd 163 modeConf.all = 0;
jeannie9809 0:338c50c9c8cd 164 modeConf.bits.mode = MAX30101::HeartRateMode;
jeannie9809 0:338c50c9c8cd 165 hr.setModeConfiguration(modeConf);
jeannie9809 0:338c50c9c8cd 166 // printf("Mode set\r\n");
jeannie9809 0:338c50c9c8cd 167 }
jeannie9809 0:338c50c9c8cd 168
jeannie9809 0:338c50c9c8cd 169 if (interruptStatus.bits.prox_int == 0x1) {
jeannie9809 0:338c50c9c8cd 170 // printf("Proximity Triggered, entered HR Mode.");
jeannie9809 0:338c50c9c8cd 171 }
jeannie9809 0:338c50c9c8cd 172
jeannie9809 0:338c50c9c8cd 173 if (interruptStatus.bits.ppg_rdy == 0x1) {
jeannie9809 0:338c50c9c8cd 174 // printf("PPG Ready.\r\n");
jeannie9809 0:338c50c9c8cd 175 mask_ppg = 1;
jeannie9809 0:338c50c9c8cd 176 }
jeannie9809 0:338c50c9c8cd 177
jeannie9809 0:338c50c9c8cd 178 if (interruptStatus.bits.a_full == 0x1) {
jeannie9809 0:338c50c9c8cd 179 // printf("FIFO Almost Full.\r\n");
jeannie9809 0:338c50c9c8cd 180 uint8_t data[FIFO_DATA_MAX];
jeannie9809 0:338c50c9c8cd 181 uint16_t readBytes = 0;
jeannie9809 0:338c50c9c8cd 182
jeannie9809 0:338c50c9c8cd 183
jeannie9809 0:338c50c9c8cd 184 hr.readFIFO(MAX30101::OneLedChannel, data, readBytes);
jeannie9809 0:338c50c9c8cd 185 //printf("data length: %u \r\n",readBytes);
jeannie9809 0:338c50c9c8cd 186 //printf("data length: %u \r\n",data);
jeannie9809 0:338c50c9c8cd 187 for (uint16_t i = 0; i < readBytes; i += 3) {
jeannie9809 0:338c50c9c8cd 188 uint8_t sample[4] = {0};
jeannie9809 0:338c50c9c8cd 189 sample[0] = data[i + 2];
jeannie9809 0:338c50c9c8cd 190 sample[1] = data[i + 1];
jeannie9809 0:338c50c9c8cd 191 sample[2] = data[i];
jeannie9809 0:338c50c9c8cd 192
jeannie9809 0:338c50c9c8cd 193 num = *(uint32_t *) sample;
jeannie9809 0:338c50c9c8cd 194 if (num < 310000){
jeannie9809 0:338c50c9c8cd 195 realHeartRate = 0;
jeannie9809 0:338c50c9c8cd 196 printf("keep closer to your hand \r\n");
jeannie9809 0:338c50c9c8cd 197 }
jeannie9809 0:338c50c9c8cd 198 else {
jeannie9809 0:338c50c9c8cd 199
jeannie9809 0:338c50c9c8cd 200 //realHeartRate = 65;
jeannie9809 0:338c50c9c8cd 201 realHeartRate = (num - 310000)/100;
jeannie9809 0:338c50c9c8cd 202 if (realHeartRate >45){
jeannie9809 0:338c50c9c8cd 203 printf("%d\r\n", realHeartRate);
jeannie9809 0:338c50c9c8cd 204 }
jeannie9809 0:338c50c9c8cd 205 }
jeannie9809 0:338c50c9c8cd 206 //printf("%u\r\n", num);
jeannie9809 0:338c50c9c8cd 207
jeannie9809 0:338c50c9c8cd 208
jeannie9809 0:338c50c9c8cd 209 }
jeannie9809 0:338c50c9c8cd 210 }
jeannie9809 0:338c50c9c8cd 211
jeannie9809 0:338c50c9c8cd 212 interruptStatus.all = 0xFF;
jeannie9809 0:338c50c9c8cd 213 if (mask_ppg == 1) {
jeannie9809 0:338c50c9c8cd 214 interruptStatus.bits.ppg_rdy = 0;
jeannie9809 0:338c50c9c8cd 215 }
jeannie9809 0:338c50c9c8cd 216 hr.enableInterrupts(interruptStatus);
jeannie9809 0:338c50c9c8cd 217 }
jeannie9809 0:338c50c9c8cd 218
jeannie9809 0:338c50c9c8cd 219 void interruptHandler() {
jeannie9809 0:338c50c9c8cd 220 evqueue.call(interruptHandlerQueued);
jeannie9809 0:338c50c9c8cd 221 }
jeannie9809 0:338c50c9c8cd 222
jeannie9809 0:338c50c9c8cd 223 // main() runs in its own thread in the OS
jeannie9809 0:338c50c9c8cd 224 int main() {
jeannie9809 0:338c50c9c8cd 225 // printf("Hello world.\r\n");
jeannie9809 0:338c50c9c8cd 226
jeannie9809 0:338c50c9c8cd 227 t.start(callback(&evqueue, &EventQueue::dispatch_forever));
jeannie9809 0:338c50c9c8cd 228 kw40z_device.attach_buttonLeft(&ButtonLeft);
jeannie9809 0:338c50c9c8cd 229 kw40z_device.attach_buttonRight(&ButtonRight);
jeannie9809 0:338c50c9c8cd 230
jeannie9809 0:338c50c9c8cd 231 pwr1v8 = 1;
jeannie9809 0:338c50c9c8cd 232 pwr3v3b = 1;
jeannie9809 0:338c50c9c8cd 233 pwr15v = 0;
jeannie9809 0:338c50c9c8cd 234
jeannie9809 0:338c50c9c8cd 235 maximInterrupt.fall(interruptHandler);
jeannie9809 0:338c50c9c8cd 236 maximInterrupt.enable_irq();
jeannie9809 0:338c50c9c8cd 237
jeannie9809 0:338c50c9c8cd 238 MAX30101::InterruptBitField_u interruptStatus;
jeannie9809 0:338c50c9c8cd 239 interruptStatus.all = 0xFF;
jeannie9809 0:338c50c9c8cd 240 hr.enableInterrupts(interruptStatus);
jeannie9809 0:338c50c9c8cd 241
jeannie9809 0:338c50c9c8cd 242 char text[20]; /* Text Buffer */
jeannie9809 0:338c50c9c8cd 243 oled_text_properties_t textProperties = {0};
jeannie9809 0:338c50c9c8cd 244 oled.GetTextProperties(&textProperties);
jeannie9809 0:338c50c9c8cd 245 /* Turn on the backlight of the OLED Display */
jeannie9809 0:338c50c9c8cd 246 oled.DimScreenON();
jeannie9809 0:338c50c9c8cd 247
jeannie9809 0:338c50c9c8cd 248 /* Fills the screen with solid black */
jeannie9809 0:338c50c9c8cd 249 oled.FillScreen(COLOR_BLACK);
jeannie9809 0:338c50c9c8cd 250 strcpy((char *) text, "Heart Rate:");
jeannie9809 0:338c50c9c8cd 251 oled.Label((uint8_t *)text,7,0);
jeannie9809 0:338c50c9c8cd 252
jeannie9809 0:338c50c9c8cd 253 strcpy((char *) text, "Calorie/Hr:");
jeannie9809 0:338c50c9c8cd 254 oled.Label((uint8_t *)text,7,40);
jeannie9809 0:338c50c9c8cd 255 //dynamic text setup
jeannie9809 0:338c50c9c8cd 256 textProperties.fontColor = COLOR_WHITE;
jeannie9809 0:338c50c9c8cd 257 textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT;
jeannie9809 0:338c50c9c8cd 258 oled.SetTextProperties(&textProperties);
jeannie9809 0:338c50c9c8cd 259
jeannie9809 0:338c50c9c8cd 260 txThread.start(txTask);
jeannie9809 0:338c50c9c8cd 261 while (true) {
jeannie9809 0:338c50c9c8cd 262
jeannie9809 0:338c50c9c8cd 263 /* Format the time reading */
jeannie9809 0:338c50c9c8cd 264 sprintf(text,"%d",realHeartRate);
jeannie9809 0:338c50c9c8cd 265
jeannie9809 0:338c50c9c8cd 266 /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
jeannie9809 0:338c50c9c8cd 267 oled.TextBox((uint8_t *)text,55,15,35,15); //Increase textbox for more digits
jeannie9809 0:338c50c9c8cd 268 if (realHeartRate > 45){
jeannie9809 0:338c50c9c8cd 269 calorie = (-55.1 + (0.6309*realHeartRate)+(0.1988*75)+(0.2017*25))*60/4.184 ;
jeannie9809 0:338c50c9c8cd 270 sprintf(text,"%0.2f",calorie);
jeannie9809 0:338c50c9c8cd 271
jeannie9809 0:338c50c9c8cd 272 /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
jeannie9809 0:338c50c9c8cd 273 oled.TextBox((uint8_t *)text,55,55,35,15); //Increase textbox for more digits
jeannie9809 0:338c50c9c8cd 274 }
jeannie9809 0:338c50c9c8cd 275 else{
jeannie9809 0:338c50c9c8cd 276 sprintf(text,"wait HR");
jeannie9809 0:338c50c9c8cd 277
jeannie9809 0:338c50c9c8cd 278 /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
jeannie9809 0:338c50c9c8cd 279 oled.TextBox((uint8_t *)text,55,55,35,15);
jeannie9809 0:338c50c9c8cd 280 }
jeannie9809 0:338c50c9c8cd 281
jeannie9809 0:338c50c9c8cd 282 Thread::wait(1000);
jeannie9809 0:338c50c9c8cd 283 }
jeannie9809 0:338c50c9c8cd 284 return 0;
jeannie9809 0:338c50c9c8cd 285 }
jeannie9809 0:338c50c9c8cd 286