Code to be run for the project Group Fitness

Dependencies:   FXOS8700 Hexi_KW40Z Hexi_OLED_SSD1351 MAX30101

Fork of final_project_ee119 by Wenfei Lu

Files at this revision

API Documentation at this revision

Comitter:
trhackett
Date:
Fri Jun 15 05:59:16 2018 +0000
Parent:
6:328ef7a29083
Child:
8:f5a21cfd3833
Commit message:
done

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Fri Jun 15 03:38:32 2018 +0000
+++ b/main.cpp	Fri Jun 15 05:59:16 2018 +0000
@@ -11,9 +11,18 @@
 void StopHaptic(void const *n);
 float Filter(int s);
 void AlertReceived(uint8_t *data, uint8_t length);
+void clearScreen();
 
+void displayString();
 void UpdateSensorData(void);
-void txTask(void);
+void BTTask(void);
+void dataTask(void);
+
+void ButtonLeft();
+void ButtonRight();
+void ButtonDown();
+void ButtonUp();
+void ButtonLeft(void);
 
 FXOS8700 accel(PTC11, PTC10);
 
@@ -21,12 +30,17 @@
 KW40Z kw40z_device(PTE24, PTE25);
 
 /* Instantiate the SSD1351 OLED Driver */ 
-SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); /* (MOSI,SCLK,POWER,CS,RST,DC) */
+SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15);
+
+RtosTimer hapticTimer(StopHaptic, osTimerOnce);
+DigitalOut haptic(PTB9);
 
-/*Create a Thread to handle sending BLE Sensor Data */ 
-Thread txThread;
+/* Create a Thread to handle sending BLE Sensor Data */ 
+Thread bluetoothThread;
+Thread dataThread;
 
-Thread t;
+Thread displayThread;
+EventQueue displayEventQueue;
 
 char text[20]; 
 
@@ -46,67 +60,44 @@
 
 float filter_buf[75];
 
-
-
-/****************************Call Back Functions*******************************/
-void ButtonUp(void) {
-    StartHaptic();
-    flag=1;
-}
-
-void ButtonDown(void) {
-    StartHaptic();
-    flag=2;
-}
-
-void ButtonRight(void) {
-    StartHaptic();
-    flag=3;
-}
+uint8_t result[1]={0};
 
 
-// just write the received data to the screen
-void displayString() {
-    if (!processedReceivedData) {
-        clearScreen();
-        
-        processedReceivedData = true;
-        oled_text_properties_t textProperties = {0};
-        oled.GetTextProperties(&textProperties);
-        
-        textProperties.fontColor   = COLOR_BLUE;
-        oled.SetTextProperties(&textProperties);
-        
-        sprintf(text, "USER: %s\0",user);
-        oled.Label((uint8_t*)text,0,0);
-        
-        sprintf(text, "MEAN HR: %s\0",mean);
-        oled.Label((uint8_t*)text,0,15);
-        
-        sprintf(text, "MAX HR: %s\0",max);
-        oled.Label((uint8_t*)text,0,30);
-        
-        sprintf(text, "MIN HR: %s\0",min);
-        oled.Label((uint8_t*)text,0,45);
-        
-        sprintf(text, "STEPS: %s\0",steps);
-        oled.Label((uint8_t*)text,0,60);
-    }
-}
+// variable to assist with viewing the other users' data
+char user[2];
+char mean[4];
+char max[4];
+char min[4];
+char steps[6];
+// boolean flag that says whether or not we've processed new data from pi
+// default to true because not data from pi until we ask for it
+bool processedReceivedData = true;
 
+int flag = 0;
+int userChosen = flag;
 
 // main() runs in its own thread in the OS
 int main() {
+    kw40z_device.attach_buttonLeft(&ButtonLeft);
+    kw40z_device.attach_buttonRight(&ButtonRight);
+    kw40z_device.attach_buttonDown(&ButtonDown);
+    kw40z_device.attach_buttonUp(&ButtonUp);
+    kw40z_device.attach_alert(&AlertReceived);
+    
     accel.accel_config();
     
-    txThread.start(txTask); /*Start transmitting Sensor Tag Data */
+    // open up the display queue so that at any point in the program,
+    // we can put things inside of it and they'll be executed eventually
+    displayThread.start(callback(&displayEventQueue, &EventQueue::dispatch_forever));
+    displayEventQueue.call(&clearScreen);
     
-    while (true) {  
-        Thread::wait(50);
-    }
+    dataThread.start(BTTask);
+    bluetoothThread.start(dataTask);
+    
+    wait(osWaitForever); 
 }
 
-void txTask(void){
+void BTTask(void){
    
    while (true) 
    {
@@ -114,61 +105,60 @@
             kw40z_device.ToggleAdvertisementMode();
          }
         
-        UpdateSensorData();
-        
         /*Notify Hexiwear App that it is running Sensor Tag mode*/
         kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG);
         
         kw40z_device.SendAlert(result, 2);
         kw40z_device.SendBatteryLevel(StepNumber);
 
-        Thread::wait(10);                 
+        Thread::wait(1000);                 
     }
 }
 
-void UpdateSensorData(void)
-{    
-    if(flag == 1) {
-        result[1]=1;
-        flag=0;
-        userChosen = 1;
-    }
-    
-    if(flag == 2) { 
-        result[1]=2;
-        flag=0;
-        userChosen = 2;
-    }
-    
-    if(flag == 3){ 
-        result[1]=3;
-        flag=0;
-        userChosen = 3;
+void dataTask(void) {    
+    while (true) {
+        if(flag == 1) {
+            result[1] = 1;
+            flag = 0;
+            userChosen = 1;
+        }
+        
+        if(flag == 2) { 
+            result[1] = 2;
+            flag = 0;
+            userChosen = 2;
+        }
+        
+        if(flag == 3){ 
+            result[1] = 3;
+            flag = 0;
+            userChosen = 3;
+        }
+        
+        accel.acquire_accel_data_g(accel_data);
+        ax = Filter(0);
+        ay = Filter(1);
+        az = Filter(2);  
+        wait(0.02);           
+        accel_rms = sqrt((ax*ax)+(ay*ay)+(az*az)/3);
+        dot = (old_accx * ax)+(old_accy * ay)+(old_accz * az);
+        old_acc = abs(sqrt(old_accx*old_accx+old_accy*old_accy+old_accz*old_accz));
+        new_acc = abs(sqrt(ax*ax+ay*ay+az*az));
+        dot /= (old_acc * new_acc);
+        
+        /* Display Legends */ 
+        StepNum = StepNumber;
+        if(abs(dot - old_dot) >= 0.05 && abs(dot - old_dot) <= 0.10) {
+            StepNumber += 1;
+        }
+        
+        old_accx = ax;
+        old_accy = ay;
+        old_accz = az;
+        old_dot = dot;
+        
+        Thread::wait(250);
     }
-    
-    accel.acquire_accel_data_g(accel_data);
-    ax = Filter(0);
-    ay = Filter(1);
-    az = Filter(2);  
-    wait(0.02);           
-    accel_rms = sqrt((ax*ax)+(ay*ay)+(az*az)/3);
-    dot = (old_accx * ax)+(old_accy * ay)+(old_accz * az);
-    old_acc = abs(sqrt(old_accx*old_accx+old_accy*old_accy+old_accz*old_accz));
-    new_acc = abs(sqrt(ax*ax+ay*ay+az*az));
-    dot /= (old_acc * new_acc);
-    
-    /* Display Legends */ 
-    StepNum = StepNumber;
-    if(abs(dot - old_dot) >= 0.05 && abs(dot - old_dot) <= 0.10) {
-        StepNumber += 1;
-    }
-    
-    old_accx = ax;
-    old_accy = ay;
-    old_accz = az;
-    old_dot = dot;
-    
-    Thread::wait(250);
 }
 
 void StartHaptic(void)  {
@@ -200,18 +190,6 @@
     
     StartHaptic();
     data[19] = 0;
-  
-    // 1: copy data into a global array
-    if (processedReceivedData) {
-        processedReceivedData = false;
-        for (int i = 0; i < 9; i++) {
-            receivedSummaryData1[i] = data[i];   
-        }
-        
-        for (int i = 9; i < 18; i++) {
-            receivedSummaryData2[i-9] = data[i];
-        }
-    }
     
     user[0] = '0' + userChosen;
     
@@ -239,12 +217,68 @@
     min[3] = 0;
     steps[5] = 0;
     
-    receivedSummaryData1[9] = 0;
-    receivedSummaryData2[9] = 0;
-    
     // if you haven't yet processed the data that pi sent
     // you in the past, then don't do anything.
     
     // 2: queue up the displaying of that string
     displayEventQueue.call(&displayString);
+}
+
+
+/****************************Call Back Functions*******************************/
+
+// just write the received data to the screen
+void displayString() {
+    if (!processedReceivedData) {
+        clearScreen();
+        
+        processedReceivedData = true;
+        oled_text_properties_t textProperties = {0};
+        oled.GetTextProperties(&textProperties);
+        
+        textProperties.fontColor   = COLOR_BLUE;
+        oled.SetTextProperties(&textProperties);
+        
+        sprintf(text, "USER: %s\0",user);
+        oled.Label((uint8_t*)text,0,0);
+        
+        sprintf(text, "MEAN HR: %s\0",mean);
+        oled.Label((uint8_t*)text,0,15);
+        
+        sprintf(text, "MAX HR: %s\0",max);
+        oled.Label((uint8_t*)text,0,30);
+        
+        sprintf(text, "MIN HR: %s\0",min);
+        oled.Label((uint8_t*)text,0,45);
+        
+        sprintf(text, "STEPS: %s\0",steps);
+        oled.Label((uint8_t*)text,0,60);
+    }
+}
+
+void ButtonUp(void) {
+    StartHaptic();
+    flag = 1;
+}
+
+void ButtonDown(void) {
+    StartHaptic();
+    flag = 2;
+}
+
+void ButtonRight(void) {
+    StartHaptic();
+    flag = 3;
+}
+
+void ButtonLeft(void) {
+    StartHaptic();
+    kw40z_device.ToggleAdvertisementMode();
+}
+
+
+
+// initialize the screen to black
+void clearScreen() {    
+    oled.FillScreen(COLOR_BLACK);
 }
\ No newline at end of file