123

Dependencies:   Hexi_KW40Z Hexi_OLED_SSD1351 MAX30101

Fork of HeartRate by Xi Han

Committer:
haoshiz
Date:
Wed Jun 13 11:02:52 2018 +0000
Revision:
4:8cee5929f4d8
Parent:
3:2e12e0cd1f26
proj_v1;

Who changed what in which revision?

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