Hexiwear Code for Game Controls

Dependencies:   FXAS21002 FXOS8700 Hexi_KW40Z Hexi_OLED_SSD1351

Fork of Hexi_Final_Project by Destin Raymundo

Revision:
1:a0d9eeedb771
Parent:
0:c80666325948
Child:
3:c2ab3a0de448
--- a/main.cpp	Tue Sep 20 01:13:43 2016 +0000
+++ b/main.cpp	Tue Sep 20 22:40:39 2016 +0000
@@ -1,9 +1,3 @@
-/* Use this in conjunction with the iOS or Android Hexiwear App. 
-   This example currently shows the Sensor Tag functionality. 
-   Program the Hexiwear with the binary, then tap to turn on
-   Bluetooth.(Blue LED indicates BLE is on) Use Hexiwear app to 
-   connect. Pairing code will appear on the OLED screen. */ 
-
 #include "mbed.h"
 #include "Hexi_KW40Z.h"
 #include "Hexi_OLED_SSD1351.h"
@@ -16,6 +10,7 @@
    
 void StartHaptic(void);
 void StopHaptic(void const *n);
+void txTask(void);
 
 DigitalOut redLed(LED1,1);
 DigitalOut greenLed(LED2,1);
@@ -31,8 +26,13 @@
 /* Instantiate the SSD1351 OLED Driver */ 
 SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); /* (MOSI,SCLK,POWER,CS,RST,DC) */
 
-char text[20];  /* Text Buffer */ 
+/*Create a Thread to handle sending BLE Sensor Data */ 
+Thread txThread;
 
+ /* Text Buffer */ 
+char text[20]; 
+
+/****************************Call Back Functions*******************************/
 void ButtonRight(void)
 {
     StartHaptic();
@@ -48,16 +48,18 @@
 void PassKey(void)
 {
     StartHaptic();
-    
-    /* Display Text at (x=20,y=2) */
     strcpy((char *) text,"PAIR CODE");
-    oled.Label((uint8_t *)text,20,25);
+    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);
 }
-   
+
+/***********************End of Call Back Functions*****************************/
+
+/********************************Main******************************************/
+
 int main()
 {    
     /* Get OLED Class Default Text Properties */
@@ -74,7 +76,7 @@
     kw40z_device.attach_buttonLeft(&ButtonLeft);
     kw40z_device.attach_buttonRight(&ButtonRight);
     kw40z_device.attach_passkey(&PassKey);
-    
+
     /* Change font color to Blue */ 
     textProperties.fontColor   = COLOR_BLUE;
     oled.SetTextProperties(&textProperties);
@@ -88,20 +90,35 @@
     textProperties.alignParam = OLED_TEXT_ALIGN_CENTER;
     oled.SetTextProperties(&textProperties);
     
-     /* Display Label at x=22,y=80 */ 
+    /* Display Label at x=22,y=80 */ 
     strcpy((char *) text,"Tap Below");
     oled.Label((uint8_t *)text,22,80);
          
+    uint8_t prevLinkState = 0; 
+    uint8_t currLinkState = 0; 
+    txThread.start(txTask); /*Start transmitting Sensor Tag Data */
     
-    while (true) {
+    while (true) 
+    {
+        blueLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/   
+        Thread::wait(50);
+    }
+}
+
+/******************************End of Main*************************************/
+
+
+/* txTask() transmits the sensor data */
+void txTask(void){
+   
+   while (true) 
+   {
         
-        blueLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/
-        
-        /*Notify device that it is running Sensor Tag mode for the Hexiwear App*/
+        /*Notify Hexiwear App that it is running Sensor Tag mode*/
         kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG);
                 
-        /*The following is sending dummy data over Bluetooth LE. Replace with real data*/
-          
+        /*The following is sending dummy data over BLE. Replace with real data*/
+    
         /*Send Battery Level for 20% */ 
         kw40z_device.SendBatteryLevel(20);
                
@@ -117,18 +134,15 @@
         /*Send Pressure at 100kPA */ 
         kw40z_device.SendPressure(10000);
         
-        /*Send Mag,Accel,Gyro Data. Y-axis has offset in Hexiwear App*/
+        /*Send Mag,Accel,Gyro Data. */
         kw40z_device.SendGyro(0,0,0);
         kw40z_device.SendAccel(0,0,0);        
-        kw40z_device.SendMag(0,0,0);
-        
-        Thread::wait(1000);
-      
+        kw40z_device.SendMag(0,0,0);        
+        Thread::wait(1000);                 
     }
 }
 
-void StartHaptic(void)
-{
+void StartHaptic(void)  {
     hapticTimer.start(50);
     haptic = 1;
 }
@@ -137,3 +151,4 @@
     haptic = 0;
     hapticTimer.stop();
 }
+