Hexiwear Code for Game Controls

Dependencies:   FXAS21002 FXOS8700 Hexi_KW40Z Hexi_OLED_SSD1351

Fork of Hexi_Final_Project by Destin Raymundo

Revision:
5:b479668a7373
Parent:
4:20d4eebfa986
Child:
6:8a9b0eb4835d
--- a/main.cpp	Fri May 04 08:56:11 2018 +0000
+++ b/main.cpp	Tue Jun 12 03:34:17 2018 +0000
@@ -4,14 +4,19 @@
 #include "OLED_types.h"
 #include "OpenSans_Font.h"
 #include "string.h"
+#include "FXAS21002.h"
+#include "FXOS8700.h"
+#include <cmath>
 
 #define LED_ON      0
 #define LED_OFF     1
 
 void UpdateSensorData(void);
+void GetSensorData(void);
 void StartHaptic(void);
 void StopHaptic(void const *n);
 void txTask(void);
+FXOS8700 accel(PTC11, PTC10);
 
 Serial pc(USBTX, USBRX);
 
@@ -36,25 +41,28 @@
 char text[20]; 
 
 uint8_t battery = 100;
-uint8_t light = 0;
-uint16_t humidity = 4500;
-uint16_t temperature = 2000;
-uint16_t pressure = 9000;
-uint16_t x = 0;
-uint16_t y = 5000;
-uint16_t z = 10000;
+int16_t x = 0;
+int16_t y = 0;
+int16_t z = 0;
+
+uint8_t abs_x = 0;
+uint8_t abs_y = 0;
+uint8_t abs_z = 0;
+float accel_data[3];
 
 /****************************Call Back Functions*******************************/
 void ButtonRight(void)
 {
     StartHaptic();
-    kw40z_device.ToggleAdvertisementMode();
+    kw40z_device.SendAccel(x,y,z);
+//    kw40z_device.ToggleAdvertisementMode();
 }
 
 void ButtonLeft(void)
 {
     StartHaptic();
-    kw40z_device.ToggleAdvertisementMode();
+    kw40z_device.SendAccel(x,y,z);
+//    kw40z_device.ToggleAdvertisementMode();
 }
 
 void PassKey(void)
@@ -103,6 +111,8 @@
     kw40z_device.attach_buttonRight(&ButtonRight);
     kw40z_device.attach_passkey(&PassKey);
     kw40z_device.attach_alert(&AlertReceived);
+    
+    accel.accel_config();
 
     pc.printf("hello\n\r");
     
@@ -147,13 +157,12 @@
 
 /******************************End of Main*************************************/
 
-
 /* txTask() transmits the sensor data */
 void txTask(void){
    
    while (true) 
    {
-        UpdateSensorData();
+        GetSensorData();
         
         /*Notify Hexiwear App that it is running Sensor Tag mode*/
         kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG);
@@ -161,49 +170,45 @@
         /*The following is sending dummy data over BLE. Replace with real data*/
     
         /*Send Battery Level for 20% */ 
-        kw40z_device.SendBatteryLevel(battery);
+        kw40z_device.SendBatteryLevel('x');
+        kw40z_device.SendBatteryLevel(abs_x);
+        kw40z_device.SendBatteryLevel('y');
+        kw40z_device.SendBatteryLevel(abs_y);
+        kw40z_device.SendBatteryLevel('z');
+        kw40z_device.SendBatteryLevel(abs_z);
                
         /*Send Ambient Light Level at 50% */ 
-        kw40z_device.SendAmbientLight(light);
+//        kw40z_device.SendAmbientLight(light);
         
         /*Send Humidity at 90% */
-        kw40z_device.SendHumidity(humidity);
+//        kw40z_device.SendHumidity(humidity);
         
         /*Send Temperature at 25 degrees Celsius */
-        kw40z_device.SendTemperature(temperature);
+//        kw40z_device.SendTemperature(temperature);
 
         /*Send Pressure at 100kPA */ 
-        kw40z_device.SendPressure(pressure);
+//        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);
+//        kw40z_device.SendGyro(x,y,z);
+        kw40z_device.SendAccel(x,y,z);
+//        kw40z_device.SendMag(y,z,x);
 
         Thread::wait(1000);                 
     }
 }
 
-void UpdateSensorData(void)
-{    
-    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 GetSensorData(void) {
+    accel.acquire_accel_data_g(accel_data);
+    x = (int16_t)(accel_data[0] * 100);
+    y = (int16_t)(accel_data[1] * 100);
+    z = (int16_t)(accel_data[2] * 100);
+    abs_x = abs(x);
+    abs_y = abs(y);
+    abs_z = abs(z);
+//    printf("ACCEL: %d %4.2f %4.2f\n",x, accel_data[1], accel_data[2]);
+//    printf("ACCEL (x100): X:%d Y:%d Z:%d\n",x,y,z);
+    printf("ACCEL (x100)(ABS): X:%d Y:%d Z:%d\n",abs_x,abs_y,abs_z);
 }
 
 void StartHaptic(void)  {