Code for the Hexiwear sensor system. Requires an Air Quality Click, Carbon Monoxide Click, and Buzzer Click for full functionality. Currently only reads values and displays to the OLED while testing and alerting the user of present threats. Future goals are to incorporate button presses with separate screens to display the data as well as using the KW40 drivers to transmit the data. Still in early stages of development, many unnecessary files will be removed and cleaned up once final product is completed within the next month. Driver.cpp is the main driver for the program and was written for purposes of this project. All other headers and functions were found on mbed.org from other developers repositories or provided by NXP Semiconductors for purposes of this project.

Dependencies:   Hexi_KW40Z images

Revision:
2:aa8c291b3f9a
Parent:
1:8ab4a1e27785
Child:
3:24b332b8e41d
--- a/driver.cpp	Mon Mar 27 17:36:47 2017 +0000
+++ b/driver.cpp	Tue Mar 28 21:41:06 2017 +0000
@@ -16,6 +16,7 @@
 void initModules();
 void StartHaptic(void);
 void StopHaptic(void const *n);
+void txTask(void);
 
 PwmOut Buzzer(PTA10);   /* Buzzer is attached to docking station port 1 */
 AnalogIn AQSensor(PTB3);   /* Air Quality sensor is attached to docking station port 2 */
@@ -33,16 +34,14 @@
 bool is_drawn1 = 0;      //detects if the screen is already drawn to the screen
 bool is_drawn2 = 0;
 bool is_drawn3 = 0;
+bool is_drawn4 = 0;
 
 /* Screens */
-const uint8_t *image1;
-//image1  = homescreen_bmp; 
+const uint8_t *image1;          //homescreen
     
-const uint8_t *image2;
-//image2  = airquality_bmp;
+const uint8_t *image2;         //air quality screen
 
-const uint8_t *image3;
-//image3  = co_bmp;
+const uint8_t *image3;         //co screen
 
 const int green = 5,
           red = 6,          /* LED Colors */
@@ -77,7 +76,10 @@
 uint16_t aq_total, /* Variable to store the AQ total */
          co_total; /* Variable to store the CO total */  
          
-/* Variables to handle the data queue algorithm */
+/*Create a Thread to handle sending BLE Sensor Data */ 
+Thread txThread;
+
+/* Variables to handle the circular array algorithm */
 const int SIZE = 500;
 double dataSet [SIZE];
 int pushIndex = 0;
@@ -87,62 +89,40 @@
 /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */ 
 KW40Z kw40z_device(PTE24, PTE25);
 
-/* Instantiate the SSD1351 OLED Driver */ 
-//SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15);
-/* Get OLED Class Default Text Properties */
-//oled_text_properties_t textProperties = {0};
-//oled.GetTextProperties(&textProperties);
-
 /* Below are the functions to handle button presses and screens */
-
 void ButtonRight(void)//CO screen button
 {   
     screen = 3;
-    //StartHaptic();
-    
-    //oled.DrawImage(image3,0,0);
-    
-    /* Get OLED Class Default Text Properties */
-    //oled_text_properties_t textProperties = {0};
-    //oled.GetTextProperties(&textProperties);
-    
-    //textProperties.fontColor = COLOR_WHITE;
-    //textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT;
-    //oled.SetTextProperties(&textProperties);
+    StartHaptic();
 }
 
 void ButtonLeft(void)//AQ screen button
 {
     screen = 2;
-    //StartHaptic();
-    
-    //oled.DrawImage(image2,0,0);
-    
-    /* Get OLED Class Default Text Properties */
-    //oled_text_properties_t textProperties = {0};
-    //oled.GetTextProperties(&textProperties);
-        
-    //textProperties.fontColor = COLOR_WHITE;
-    //textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT;
-    //oled.SetTextProperties(&textProperties); 
+    StartHaptic();
 }
 
 void ButtonDown(void)//home screen button
 {
     screen = 1;
+    StartHaptic();
+}
+
+void ButtonUp(void) {//toggles bluetooth mode
+    kw40z_device.ToggleAdvertisementMode();
+    StartHaptic();
+}
+
+void PassKey(void)
+{
+    screen = 4;
     //StartHaptic();
-    //const uint8_t *image1;
-    //image1 = homescreen_bmp;
-    
-    //oled.DrawImage(image1,0,0);
-    
-    /* Get OLED Class Default Text Properties */
-    //oled_text_properties_t textProperties = {0};
-    //oled.GetTextProperties(&textProperties);
-    
-    //textProperties.fontColor = COLOR_WHITE;
-    //textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT;
-    //oled.SetTextProperties(&textProperties);
+    //strcpy((char *) text,"PAIR CODE");
+    //oled.TextBox((uint8_t *)text,0,25,95,18);
+  
+    /* Display Bond Pass Key in a 95px by 18px textbox at x=0,y=40 */
+    //sprintf(text,"%d", kw40z_device.GetPassKey());
+    //oled.TextBox((uint8_t *)text,0,40,95,18);
 } 
       
 int main(){
@@ -202,9 +182,9 @@
     initModules();
     
     /* Subroutine Calls */
-    //sensor_read.attach(&readSensors, 1);   /* Read the sensor on a time interval */
-    //A2D_Convert.attach(&CalculatePPM, 1);  /* Convert the values read from the sensors to floating point ppm values */
-    //push.attach(&dataQueue, 1);            /* Push the value into the set and compute the average */
+    sensor_read.attach(&readSensors, 1);   /* Read the sensor on a time interval */
+    A2D_Convert.attach(&CalculatePPM, 1);  /* Convert the values read from the sensors to floating point ppm values */
+    push.attach(&dataQueue, 1);            /* Push the value into the set and compute the average */
     ledcolor.attach(&ledColor, 0.5);       /* Determine the LED color */
     led_flash.attach(&led_out, 0.25);      /* Flash LED based on sensor data */
     buzzer_sound.attach(&buzzer, 0.25);    /* Sensor values are sent to buzzer function */
@@ -213,8 +193,12 @@
     kw40z_device.attach_buttonDown(&ButtonDown);
     kw40z_device.attach_buttonLeft(&ButtonLeft);
     kw40z_device.attach_buttonRight(&ButtonRight);
+    kw40z_device.attach_buttonUp(&ButtonUp);
+    kw40z_device.attach_passkey(&PassKey);
     
-    while (1) { /* Loop to display data to the OLED */
+    txThread.start(txTask); /*Start transmitting Sensor Tag Data */
+    
+    while (1) { /* Loop to process and display data to the OLED */
     
         /* Get OLED Class Default Text Properties */
         oled_text_properties_t textProperties = {0};
@@ -225,16 +209,16 @@
         textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT;
         oled.SetTextProperties(&textProperties);
 
-        readSensors();
-        CalculatePPM();
-        dataQueue();
-        //ledColor();
-        //led_out();
-        //buzzer();
+        //readSensors();          //read the sensors
+        //CalculatePPM();         //convert to ppm
+        //dataQueue();            //push the value into the circular array
         if (screen == 2){   //Air Quality Screen
-            is_drawn1 = 0;
-            is_drawn3 = 0;
+            //is_drawn1 = 0;
+            //is_drawn3 = 0;
             if(!is_drawn2){
+                is_drawn1 = 0;
+                is_drawn3 = 0;
+                is_drawn4 = 0;
                 oled.DrawImage(image2,0,0);
                 is_drawn2 = 1;
             }
@@ -242,9 +226,12 @@
             oled.TextBox((uint8_t *)AQ_level,35,76,35,15);
         }
         else if (screen == 3){  //Carbon Monoxide Screen
-            is_drawn2 = 0;
-            is_drawn1 = 0;
+            //is_drawn2 = 0;
+            //is_drawn1 = 0;
             if (!is_drawn3){
+                is_drawn2 = 0;
+                is_drawn1 = 0;
+                is_drawn4 = 0;
                 oled.DrawImage(image3,0,0);
                 is_drawn3 = 1;
             }
@@ -252,9 +239,12 @@
             oled.TextBox((uint8_t *)CO_level,35,76,35,15);
         }
         else if (screen == 1) {   //Home Screen
-            is_drawn3 = 0;
-            is_drawn2 = 0;
+            //is_drawn3 = 0;
+            //is_drawn2 = 0;
             if (!is_drawn1){
+                is_drawn3 = 0;
+                is_drawn2 = 0;
+                is_drawn4 = 0;
                 oled.DrawImage(image1,0,0);
                 is_drawn1 = 1;
             }
@@ -263,12 +253,36 @@
             sprintf(total_total, "%.2f", total);   /* Print the total to the screen */
             oled.TextBox((uint8_t *)total_total,55,40,35,15);
         }
-        Thread::wait(100);
+        
+        else if (screen == 4){   //BLE Pairing Screen
+            if (!is_drawn4){
+                is_drawn3 = 0;
+                is_drawn2 = 0;
+                is_drawn1 = 0; 
+                oled.FillScreen(COLOR_BLACK);
+                is_drawn4 = 1;  
+            }
+            strcpy((char *) text,"PAIR CODE");
+            oled.TextBox((uint8_t *)text,0,25,95,18);  
+            /* Display Bond Pass Key in a 95px by 18px textbox at x=0,y=40 */
+            sprintf(text,"%d", kw40z_device.GetPassKey());
+            oled.TextBox((uint8_t *)text,0,40,95,18);
+        }
+        Thread::wait(500);
     }
     
     return 0;
 }
-
+void txTask(void){
+    while (1){
+        /*Notify Hexiwear App that it is running Sensor Tag mode*/
+        kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG);   
+        kw40z_device.SendTemperature(100*aq_ppm); //send ppm Air Quality Click value
+        kw40z_device.SendHumidity(100*co_ppm);  //send ppm CO click value
+        kw40z_device.SendPressure(100*total);     //send average
+        Thread::wait(1000);
+    }
+}
 void initModules() {    /* Function to initialize the system */
 
     /* Turns on the OLED Display*/
@@ -278,36 +292,10 @@
     image2 = airquality_bmp;
     image3  = co_bmp;
     
-    //oled.DrawImage(image1,0,0); 
-    //screen = 1;
-    
-    /* Register callbacks to button presses */
-    //kw40z_device.attach_buttonDown(&ButtonDown);
-    //kw40z_device.attach_buttonLeft(&ButtonLeft);
-    //kw40z_device.attach_buttonRight(&ButtonRight);
-    
     /* Set text properties to white and right aligned for the dynamic text */
     //textProperties.fontColor = COLOR_WHITE;
     //textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT;
     //oled.SetTextProperties(&textProperties);
-    
-    /* while (1) {
-        readSensors();
-        CalculatePPM();
-        dataQueue();
-        ledColor();
-        led_out();
-        buzzer();
-        
-        sprintf(average, "%.2f", avg);      /* Print the average to the screen */
-    
-       /* oled.TextBox((uint8_t *)average,55,20,35,15);
-        
-        sprintf(total_total, "%.2f", total);   /* Print the total to the screen */
-        
-       /* oled.TextBox((uint8_t *)total_total,55,40,35,15);
-        Thread::wait(50); */
-    //}
 }
 
 void readSensors(){  /* Function to read sensors */
@@ -393,12 +381,14 @@
     ratio = Rs/AQ_Rl;                             // Calculate ratio
     lgPPM = (log10(ratio) * -0.8) + 0.9;      
     aq_ppm = 6* pow(10,lgPPM) - 12;                 // Calculate air quality ppm
+    //aq_ppm = 24.1;     //used for BLE testing
     
     Vrl = (double)co_total * Vadc_33;            // For 3.3V Vcc use Vadc_33
     Rs = CO_Rl * (3.3 - Vrl)/Vrl;                 // Calculate sensor resistance
     ratio = Rs/CO_Rl;                             // Calculate ratio
     lgPPM = (log10(ratio) * -0.8) + 0.9;       
     co_ppm = 6* pow(10,lgPPM) - 12;                 // Calculate carbon monoxide ppm
+    //co_ppm = 11.0;  //used for BLE testing
     
      if (aq_ppm < 0)
         aq_ppm = 0;             //prevents underflow
@@ -448,7 +438,7 @@
 
 void StartHaptic(void)
 {
-    hapticTimer.start(100);
+    hapticTimer.start(75);
     haptic = 1;
 }