Bluetooth Low Energy enabled device with "Switch" feature, compatible with BlueST Protocol.

Bluetooth Low Energy enabled device with "Switch" feature, compatible with BlueST Protocol.

Revision:
4:67ff1ddd11e2
Parent:
3:b880b9a9ccdf
Child:
5:1651815d2a74
diff -r b880b9a9ccdf -r 67ff1ddd11e2 source/main.cpp
--- a/source/main.cpp	Thu May 10 11:59:17 2018 +0000
+++ b/source/main.cpp	Thu May 31 18:30:29 2018 +0000
@@ -54,19 +54,10 @@
 #define DELAY_1 100
 
 
-/* Types ---------------------------------------------------------------------*/
-
-/* Toggle state. */
-typedef enum
-{
-    TOGGLE_OFF = 0,
-    TOGGLE_ON
-} toggle_t;
-
-
 /* Variables -----------------------------------------------------------------*/
 
-/* Button state. */
+/* Switch and toggle state. */
+CustomService::switch_state_t switch_state;
 bool toggle_state;
 
 /* LED to indicate system state. */
@@ -103,8 +94,8 @@
     if (params->handle == custom_service->getValueHandle()) {
         // Reading led state and sending it via bluetooth.
         if (BLE::Instance().getGapState().connected) {
-            //event_queue.call(Callback<void(uint16_t, uint8_t)>(custom_service, &CustomService::send_state), time_stamp++, led_state.read() ? CustomService::SWITCH_ON : CustomService::SWITCH_OFF);
-            event_queue.call(Callback<void(uint16_t, uint8_t)>(custom_service, &CustomService::send_state), time_stamp++, TOGGLE_OFF);  // To clean the BLE buffer.
+            switch_state = led_state.read() ? CustomService::SWITCH_ON : CustomService::SWITCH_OFF;
+            event_queue.call(Callback<void(uint16_t, uint8_t)>(custom_service, &CustomService::send_state), time_stamp++, switch_state);
         }
     }
 }
@@ -120,7 +111,7 @@
 
     if (params->handle == custom_service->getValueHandle()) {
         /* Receiving command via bluetooth and writing it to the led state. */
-        led_state.write(((CustomService::switch_state_t) ((uint8_t *) (params->data))[0]));
+        led_state.write(((CustomService::switch_state_t) ((uint8_t *) (params->data))[SWITCH_DATA_INDEX]));
     }
 }
 
@@ -134,7 +125,7 @@
     /* Reading toggle command and sending it via bluetooth. */
     if (toggle_state) {
         if (BLE::Instance().getGapState().connected) {
-            event_queue.call(Callback<void(uint16_t, uint8_t)>(custom_service, &CustomService::send_state), time_stamp++, toggle_state ? TOGGLE_ON : TOGGLE_OFF);
+            event_queue.call(Callback<void(uint16_t, uint8_t)>(custom_service, &CustomService::send_state), time_stamp++, switch_state);
             toggle_state = false;
         }
     }
@@ -192,6 +183,7 @@
 /* Custom service related functions ------------------------------------------*/
 
 void button_callback(void) {
+    switch_state = (CustomService::switch_state_t) (CustomService::SWITCH_ON - switch_state);
     toggle_state = true;
 }
 
@@ -214,8 +206,10 @@
     /* Attaching and enabling interrupt handlers. */
     event.fall(button_callback);
 
-    /* Setting up initial state: led OFF, no toggle state. */
-    led_state.write(CustomService::SWITCH_OFF);
+    /* Setting up initial state: switch feature OFF, i.e. led OFF, and no toggle
+       state triggered. */
+    switch_state = CustomService::SWITCH_OFF;
+    led_state.write(switch_state);
     toggle_state = false;
 
     /* Start. */