123

Dependencies:   Hexi_KW40Z Hexi_OLED_SSD1351 MAX30101

Fork of HeartRate by Xi Han

Revision:
4:8cee5929f4d8
Parent:
3:2e12e0cd1f26
--- a/main.cpp	Fri Jun 01 23:23:08 2018 +0000
+++ b/main.cpp	Wed Jun 13 11:02:52 2018 +0000
@@ -1,8 +1,14 @@
 #include "mbed.h"
 #include "mbed_events.h"
 #include "MAX30101.h"
-
+#include "string.h"
+#include "Hexi_OLED_SSD1351.h"
+#include "Hexi_KW40Z.h"
 #define FIFO_DATA_MAX 288
+void UpdateSensorData(void);
+void StartHaptic(void);
+void StopHaptic(void const *n);
+void txTask(void);
 
 DigitalOut pwr1v8(PTA29);
 DigitalOut pwr3v3b(PTC13);
@@ -10,14 +16,102 @@
 I2C i2c0(PTB1, PTB0);
 InterruptIn maximInterrupt(PTB18);
 Serial pc(USBTX, USBRX);
-
+SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15);
+KW40Z kw40z_device(PTE24, PTE25);
+DigitalOut haptic(PTB9);
 EventQueue evqueue(32 * EVENTS_EVENT_SIZE);
 Thread t;
+/* Define timer for haptic feedback */
+RtosTimer hapticTimer(StopHaptic, osTimerOnce);
 
+
+/*Create a Thread to handle sending BLE Sensor Data */ 
+Thread txThread;
 MAX30101 hr(i2c0);
-
+int realHeartRate;
+float calorie;
 int mask_ppg = 0;
 uint32_t count = 0;
+uint32_t num;
+uint8_t testsignal = 60;
+void StartHaptic(void)  {
+    hapticTimer.start(50);
+    haptic = 1;
+}
+void ButtonRight(void)
+{
+    StartHaptic();
+    kw40z_device.ToggleAdvertisementMode();
+}
+
+void ButtonLeft(void)
+{
+    StartHaptic();
+    kw40z_device.ToggleAdvertisementMode();
+}
+
+void StopHaptic(void const *n) {
+    haptic = 0;
+    hapticTimer.stop();
+}
+void txTask(void){
+   
+   while (true) 
+   {
+        UpdateSensorData();
+        
+        /*Notify Hexiwear App that it is running Sensor Tag mode*/
+        kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG);
+        //send heartrate
+        kw40z_device.SendHeartRate(testsignal);        
+        /*The following is sending dummy data over BLE. Replace with real data*/
+    
+        /*Send Battery Level for 20%  
+        kw40z_device.SendBatteryLevel(battery);
+               
+        Send Ambient Light Level at 50%  
+        kw40z_device.SendAmbientLight(light);*/
+        
+        /*Send Humidity at 90% */
+        //kw40z_device.SendHumidity(humidity);
+        
+        /*Send Temperature at 25 degrees Celsius 
+        kw40z_device.SendTemperature(temperature);
+
+        /*Send Pressure at 100kPA */ 
+        //kw40z_device.SendPressure(pressure);
+        
+        /*Send Mag,Accel,Gyro Data. 
+        kw40z_device.SendGyro(x,y,z);
+        kw40z_device.SendAccel(z,x,y);
+        kw40z_device.SendMag(y,z,x);*/
+
+        Thread::wait(1000);                 
+    }
+}
+void UpdateSensorData(void)
+{    
+    testsignal+=1; 
+    /*battery -= 5;
+    if(battery < 5) battery = 100;
+    
+    light += 20;
+    if(light > 100) light = 0;
+    
+    humidity += 500;
+    if(humidity > 8000) humidity = 2000;
+    
+    temperature -= 200;
+    if(temperature < 200) temperature = 4200;
+    
+    pressure += 300;
+    if(pressure > 10300) pressure = 7500;
+    
+    x += 1400;
+    y -= 2300;
+    z += 1700;*/
+}
+
 void interruptHandlerQueued() {
     
     MAX30101::InterruptBitField_u interruptStatus;
@@ -75,15 +169,33 @@
 //        printf("FIFO Almost Full.\r\n");
         uint8_t data[FIFO_DATA_MAX];
         uint16_t readBytes = 0;
+       
+        
         hr.readFIFO(MAX30101::OneLedChannel, data, readBytes);
-        
+        //printf("data length: %u \r\n",readBytes);
+        //printf("data length: %u \r\n",data);
         for (uint16_t i = 0; i < readBytes; i += 3) {
             uint8_t sample[4] = {0};
             sample[0] = data[i + 2];
             sample[1] = data[i + 1];
             sample[2] = data[i];
             
-            printf("%u\r\n", *(uint32_t *) sample);
+            num = *(uint32_t *) sample;
+            if (num < 310000){
+                realHeartRate = 0;
+                printf("keep closer to your hand \r\n");
+            }
+            else {
+                
+                //realHeartRate = 65;
+                realHeartRate = (num - 310000)/100;
+                if (realHeartRate >45){
+                    printf("%d\r\n", realHeartRate);
+                }
+            }
+            //printf("%u\r\n", num);
+            
+            
         }
     }
     
@@ -101,8 +213,10 @@
 // main() runs in its own thread in the OS
 int main() {
 //    printf("Hello world.\r\n");
-    
+
     t.start(callback(&evqueue, &EventQueue::dispatch_forever));
+    kw40z_device.attach_buttonLeft(&ButtonLeft);
+    kw40z_device.attach_buttonRight(&ButtonRight);
      
     pwr1v8 = 1;
     pwr3v3b = 1;
@@ -115,6 +229,48 @@
     interruptStatus.all = 0xFF;
     hr.enableInterrupts(interruptStatus);
     
+    char text[20];  /* Text Buffer */ 
+    oled_text_properties_t textProperties = {0};
+    oled.GetTextProperties(&textProperties);   
+    /* Turn on the backlight of the OLED Display */
+    oled.DimScreenON();
+    
+    /* Fills the screen with solid black */         
+    oled.FillScreen(COLOR_BLACK);
+    strcpy((char *) text, "Heart Rate:");
+    oled.Label((uint8_t *)text,7,0);
+    
+    strcpy((char *) text, "Calorie/Hr:");
+    oled.Label((uint8_t *)text,7,40);
+    //dynamic text setup
+    textProperties.fontColor = COLOR_WHITE;
+    textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT;
+    oled.SetTextProperties(&textProperties);     
+    
+    txThread.start(txTask);
+    while (true) {
+        
+        /* Format the time reading */
+        sprintf(text,"%d",realHeartRate);
+        
+        /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
+        oled.TextBox((uint8_t *)text,55,15,35,15); //Increase textbox for more digits
+        if (realHeartRate > 45){
+            calorie = (-55.1 + (0.6309*realHeartRate)+(0.1988*75)+(0.2017*25))*60/4.184 ;
+            sprintf(text,"%0.2f",calorie);
+            
+            /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
+            oled.TextBox((uint8_t *)text,55,55,35,15); //Increase textbox for more digits
+        }
+        else{
+            sprintf(text,"wait HR");
+            
+            /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
+            oled.TextBox((uint8_t *)text,55,55,35,15);
+        }
+             
+        Thread::wait(1000);
+    }    
     return 0;
 }