Orange_IoT / Mbed OS liveobjects-iotsoftbox-basic

Dependencies:   MQTTPacket

Revision:
4:919c25da5ec8
Parent:
3:a348ae67024e
Child:
8:82317399e4ce
--- a/main.cpp	Wed Feb 08 16:24:49 2017 +0100
+++ b/main.cpp	Thu Feb 09 12:45:03 2017 +0100
@@ -22,7 +22,13 @@
 #include "mbed.h"
 #include "rtos.h"
 
-static const char* appv_version = "MBED SAMPLE V03.01";
+static const char* appv_version = "MBED SAMPLE V03.02";
+
+#define MEASURES_ENABLED_AT_INIT     0
+#define STREAM_PREFIX                1
+#define USE_BUTTON_SWITCH            1         //D7 := PTC3
+#define USE_BUTTON_SW2               1
+#define MEASURES_ENABLED_CTL_ON_SW2  1
 
 #if 0
 #define DBG_DFT_MAIN_LOG_LEVEL    3
@@ -226,7 +232,9 @@
 // ----------------------------------------------------------
 // STATUS data
 //
+#if USE_BUTTON_SW2
 uint32_t appv_status_sw2_cnt = 0;
+#endif
 int32_t  appv_status_counter = 0;
 char     appv_status_message[150] = "READY";
 
@@ -234,7 +242,9 @@
 LiveObjectsD_Data_t appv_set_status[] = {
     { LOD_TYPE_STRING_C, "sample_version" ,  (void*)appv_version },
     { LOD_TYPE_INT32,    "sample_counter" ,  &appv_status_counter},
+#if USE_BUTTON_SW2
     { LOD_TYPE_UINT32,   "sample_sw2_cnt" ,  &appv_status_sw2_cnt},
+#endif
     { LOD_TYPE_STRING_C, "sample_message" ,  appv_status_message}
 };
 #define SET_STATUS_NB (sizeof(appv_set_status) / sizeof(LiveObjectsD_Data_t))
@@ -245,10 +255,10 @@
 // ----------------------------------------------------------
 // 'COLLECTED DATA'
 //
-#define STREAM_PREFIX   0
 
-
-uint8_t  appv_measures_enabled = 1;
+// Measures
+// --------
+uint8_t  appv_measures_enabled = MEASURES_ENABLED_AT_INIT;
 
 int32_t  appv_measures_temp_grad = -1;
 float    appv_measures_volt_grad = -0.2;
@@ -272,6 +282,20 @@
 
 int      appv_hdl_data = -1;
 
+// Button states
+// -------------
+
+#if USE_BUTTON_SW2 || USE_BUTTON_SWITCH
+uint8_t appv_button_state_sw2 = 0;
+uint8_t appv_button_state_switch = 0;
+
+LiveObjectsD_Data_t appv_set_button_states[] = {
+    { LOD_TYPE_BOOL, "SW2" ,     &appv_button_state_sw2 },
+    { LOD_TYPE_BOOL, "Switch" ,  &appv_button_state_switch }
+};
+#define SET_DATA_STATES_NB (sizeof(appv_set_button_states) / sizeof(LiveObjectsD_Data_t))
+int appv_hdl_states = -1;
+#endif
 
 // ----------------------------------------------------------
 // CONFIGURATION data
@@ -688,48 +712,63 @@
 }
 
 // ==========================================================
-//
+// BUTTONS
+
+// ----------------------------------------------------------
+// Button Interrupt SW2
+#if USE_BUTTON_SW2
+short       appv_button_sw2_raise = 0;
+InterruptIn appv_button_sw2_int(SW2);
+
+void app_sw2_release(void)
+{
+    //output.printf("On-board button SW2 was released - state=%u cnt=%u\r\n", appv_sw2_raise, appv_status_sw2_cnt);
+	appv_button_sw2_raise = 1;
+}
+#endif /* USE_BUTTON_SW2 */
 
 // ----------------------------------------------------------
-// Interrupts and buttons
-short   appv_sw2_raise = 0;
-short   appv_sw2_state = 0;
-
-InterruptIn app_int_sw2(SW2);
-
+// Button Switch D7
+#if USE_BUTTON_SWITCH
+DigitalIn   appv_button_switch_in(D7, PullDown);    //D7 := PTC3  (D8 := PTA0)
+#endif
 
-void app_sw2_test(void) {
-    if (appv_sw2_raise) {
-        int sw2_state = (appv_sw2_state) ? 0 : 1;
-        appv_sw2_raise = 0;
-        output.printf("  *** FLIP-FLOP STATE OF SENSOR SWITCH  %u -> %u\r\n", appv_sw2_state , sw2_state);
-        appv_sw2_state = sw2_state;
-
-        if  ((appv_log_level > 1) && ((appv_hdl_status >= 0) || (appv_hdl_data >= 0))) {
-            if (appv_hdl_status >= 0) {
-                output.printf("  *** Push Status\r\n");
-            }
-            if (appv_hdl_data >= 0) {
-                output.printf("  *** PushData\r\n");
-            }
-        }
-
+void app_button_test(void)
+{
+#if USE_BUTTON_SW2 || USE_BUTTON_SWITCH
+    uint8_t change = 0;
+    uint8_t button_state;
+#if USE_BUTTON_SW2
+    if (appv_button_sw2_raise) {
+        appv_button_sw2_raise = 0;
+        button_state = (appv_button_state_sw2) ? 0 : 1;
+        output.printf("  *** FLIP-FLOP STATE OF SW2  %u -> %u\r\n", appv_button_state_sw2 , button_state);
+        appv_button_state_sw2 = button_state;
+#if MEASURES_ENABLED_CTL_ON_SW2
+        appv_measures_enabled = (appv_measures_enabled) ? 0 : 1;
+        output.printf("  ==> Now MEASURES PUBLISH is %s\r\n", appv_measures_enabled ? "enabled" : "disabled");
+#endif
+        change ++;
         if (appv_hdl_status >= 0) {
             appv_status_sw2_cnt++;
             LiveObjectsClient_PushStatus(appv_hdl_status);
         }
-        if (appv_hdl_data >= 0) {
-            appv_measures_counter++;
-            LiveObjectsClient_PushData(appv_hdl_data);
-        }
     }
+#endif
+#if USE_BUTTON_SWITCH
+    button_state = appv_button_switch_in.read() ? 0 : 1;
+    if (appv_button_state_switch != button_state) {
+        change ++;
+        output.printf("  *** BUTTON STATE CHANGE %d -> %d \r\n", appv_button_state_switch, button_state);
+        appv_button_state_switch = button_state;
+    }
+#endif
+    if ((change) && (appv_hdl_states >= 0)) {
+        LiveObjectsClient_PushData(appv_hdl_states);
+    }
+#endif
 }
 
-void app_sw2_release(void)
-{
-    //output.printf("On-board button SW2 was released - state=%u cnt=%u\r\n", appv_sw2_raise, appv_status_sw2_cnt);
-    appv_sw2_raise = 1;
-}
 
 // ==========================================================
 
@@ -762,8 +801,8 @@
 
         loop_delay += 100;
 
-        // Process the SW2 button interrupt
-        app_sw2_test();
+        // Process  button state change
+        app_button_test();
 
         // Process the LED command response if pending.
         main_cmd_delayed_resp_LED();
@@ -940,10 +979,17 @@
     output.printf("\r\n\r\n");
     output.printf("Starting LiveObject Client Example %s  (tid=x%p) ...\r\n", appv_version, appv_thread_id);
 
+#if USE_BUTTON_SW2
     // Enable interrupts: SW2
      output.printf("Enable SW2 ..\r\n");
-     app_int_sw2.rise(&app_sw2_release);
+     appv_button_sw2_int.rise(&app_sw2_release);
+     appv_button_state_sw2 = 0;
+#endif
 
+#if USE_BUTTON_SWITCH
+     appv_button_state_switch = appv_button_switch_in.read() ? 0 : 1;
+     output.printf("Initial state of SWITCH button (D7) = %u\r\n", appv_button_state_switch);
+#endif
 
     app_trace_setup();
 
@@ -1001,11 +1047,17 @@
             appv_hdl_status = LiveObjectsClient_AttachStatus(appv_set_status, SET_STATUS_NB);
             if (appv_hdl_status) output.printf(" !!! ERROR (%d) to attach status !\r\n", appv_hdl_status);
 
-            // Attach one set of collected data to the LiveObjects Client instance
+            // Attach 2 sets of collected data to the LiveObjects Client instance
             // --------------------------------------------------------------------
             appv_hdl_data = LiveObjectsClient_AttachData(STREAM_PREFIX, "LO_sample_measures", "mV1","\"Test\"", NULL, appv_set_measures, SET_MEASURES_NB);
             if (appv_hdl_data < 0) output.printf(" !!! ERROR (%d) to attach a collected data stream !\r\n", appv_hdl_data);
 
+#if USE_BUTTON_SW2 || USE_BUTTON_SWITCH
+            appv_hdl_states = LiveObjectsClient_AttachData(STREAM_PREFIX,
+                    "LO_sample_events","mv1","\"Test\",\"EVT\"", NULL, appv_set_button_states, SET_DATA_STATES_NB);
+            if (appv_hdl_states < 0) output.printf(" !!! ERROR (%d) to attach a collected data stream -  STATES !\r\n", appv_hdl_states);
+#endif
+
             // ==================================
             // User Application part.
 #if 1