SunTracker_BLE_Remote

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 X_NUCLEO_IKS01A1 mbed

Fork of SunTracker_BLE_Remote by ST Expansion SW Team

This application is the BLE Remote Control for the SunTracker demo application that you can find here.
Please refer to that page for further information.

Revision:
8:63b01a643f4d
Parent:
7:eb2215fe86b2
Child:
9:d530044d91b9
--- a/main.cpp	Tue Apr 19 08:53:53 2016 +0000
+++ b/main.cpp	Fri Apr 22 14:41:25 2016 +0000
@@ -76,10 +76,10 @@
 //const UUID::ShortUUIDBytes_t SENS_POSITION_CHAR_UUID    = 0xB003;
 //const UUID::ShortUUIDBytes_t SENS_SUNPANEL_CHAR_UUID    = 0xB004;
 const UUID::LongUUIDBytes_t CONTROL_COMMAND_CHAR_UUID = {0x00,0x00,0x00,0x02,0x00,0x0F,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
-const UUID::LongUUIDBytes_t SENS_STATUS_CHAR_UUID     = {0x00,0x00,0x04,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
-const UUID::LongUUIDBytes_t SENS_DIFFERENCE_CHAR_UUID = {0x00,0x00,0x08,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
-const UUID::LongUUIDBytes_t SENS_POSITION_CHAR_UUID   = {0x00,0x00,0x10,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
-const UUID::LongUUIDBytes_t SENS_SUNPANEL_CHAR_UUID   = {0x00,0x00,0x20,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
+const UUID::LongUUIDBytes_t SENS_STATUS_CHAR_UUID     = {0x00,0x00,0x08,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
+const UUID::LongUUIDBytes_t SENS_DIFFERENCE_CHAR_UUID = {0x00,0x00,0x10,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
+const UUID::LongUUIDBytes_t SENS_POSITION_CHAR_UUID   = {0x00,0x00,0x20,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
+const UUID::LongUUIDBytes_t SENS_SUNPANEL_CHAR_UUID   = {0x00,0x00,0x40,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
 
 DiscoveredCharacteristic command_bleCharacteristic;
 DiscoveredCharacteristic status_bleCharacteristic;
@@ -93,10 +93,10 @@
 
 uint8_t value_write[COMMAND_DATA_LEN];
 
-#define FeatureStatus      0x00000400
-#define FeatureDifference  0x00000800
-#define FeaturePosition    0x00001000
-#define FeatureSunPanel    0x00002000
+#define FeatureStatus      0x00000800
+#define FeatureDifference  0x00001000
+#define FeaturePosition    0x00002000
+#define FeatureSunPanel    0x00004000
 
 /* Variables ------------------------------------------------------------------*/
 
@@ -111,6 +111,7 @@
 };
 int trigger_Op = IDLE;
 bool trigger_button = false;
+int16_t Status_to_Write=0;  // to avoid to read the old Status by BLE before send the new Status by BLE
 
 int16_t Status=0;      // Status Shown on Display: 0 = Idle, 1 = Motor Speed, 2 = Solar Panel Value, 3 = Manual Control [--> Receive BLE]
 int16_t diff=0;        // Babybear or Accelerometer difference [--> Receive BLE]
@@ -133,20 +134,22 @@
 
 #endif
 
+DigitalOut myled(LED1, 0);
 InterruptIn mybutton(USER_BUTTON);
 
 /* User_Button_Pressed -------------------------------------------------------*/
 
 void User_Button_Pressed(void)
-{
+{ 
+
     trigger_button = true;
-    
     Status++;
+    Status_to_Write = Status;
 
 #ifdef Sensors
-    if (Status>3)  { Status=1; }
+    if (Status_to_Write>3)  { Status=1; Status_to_Write=1; }
 #else
-    if (Status>2)  { Status=1; }
+    if (Status_to_Write>2)  { Status=1; Status_to_Write=1; }
 #endif 
     
 }
@@ -379,6 +382,8 @@
     }
     
     printf("Remote Connected\n\r");
+    
+    myled = 1; // Power ON Led Green on CLIENT Board to indicate Connection between CLIENT and SERVER
 }
 
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
@@ -386,6 +391,8 @@
     (void)params;
     
     printf("Remote Disconnected\n\r");
+    
+    myled = 0; // Power OFF Led Green on CLIENT Board to indicate Disconnection between CLIENT and SERVER
 }
 
 /** 
@@ -435,12 +442,17 @@
             // Don't run more ble .read or .write in the same loop
             // because the callback is an interrupt and it doesn't work correctly if overloaded
             
-            if (Status == 1 || Status == 2)
-            { if (trigger_Op == WRITE_DIF) trigger_Op = READ_STA; } // if Display 1 or 2, skip WRITE_DIF
+            if (Status == 0)
+            { if (trigger_Op == READ_DIF) trigger_Op = WRITE_STA; } // if Display 0, skip READ_DIF,READ_POS,READ_SUN
+            
+            if (Status == 0 || Status == 1 || Status == 2)
+            { if (trigger_Op == WRITE_DIF) trigger_Op = READ_STA; } // if Display 0 or 1 or 2, skip WRITE_DIF
             
             if (Status == 3)
             { if (trigger_Op == READ_DIF) trigger_Op = READ_POS; } // if Display 3, skip READ_DIF
-                        
+            
+            if (trigger_button == true) { trigger_Op = WRITE_STA; } // jump directly to WRITE_STA
+              
             switch(trigger_Op)
             {
             case READ_STA:
@@ -461,7 +473,7 @@
                 break;
             case WRITE_STA:
                 if (trigger_button == true)
-                {                    
+                {                         
                     memset (value_write, 0, COMMAND_DATA_LEN);
                     //value_write = Feature (4) + Type (1) + Data (2)
                     value_write[0] = (FeatureStatus & 0xff000000) >> 24;
@@ -469,8 +481,8 @@
                     value_write[2] = (FeatureStatus & 0x0000ff00) >> 8;
                     value_write[3] = (FeatureStatus & 0x000000ff);
                     value_write[4] = (0x00);
-                    value_write[5] = (Status & 0xff00) >> 8;
-                    value_write[6] = (Status & 0x00ff);
+                    value_write[5] = (Status_to_Write & 0xff00) >> 8;
+                    value_write[6] = (Status_to_Write & 0x00ff);
                     command_bleCharacteristic.write(COMMAND_DATA_LEN, value_write);
                              
                     trigger_Op = IDLE;