Datavenue Live Objects basic sample using liveobjects-iotsoftbox-mqtt library.

Dependencies:   MQTTPacket

Revision:
3:a348ae67024e
Parent:
0:92f8cff1cf35
Child:
4:919c25da5ec8
--- a/main.cpp	Fri Dec 02 15:04:26 2016 +0100
+++ b/main.cpp	Wed Feb 08 16:24:49 2017 +0100
@@ -22,7 +22,7 @@
 #include "mbed.h"
 #include "rtos.h"
 
-static const char* appv_version = "MBED SAMPLE V02.01";
+static const char* appv_version = "MBED SAMPLE V03.01";
 
 #if 0
 #define DBG_DFT_MAIN_LOG_LEVEL    3
@@ -226,14 +226,15 @@
 // ----------------------------------------------------------
 // STATUS data
 //
-
-int32_t appv_status_counter = 0;
-char    appv_status_message[150] = "READY";
+uint32_t appv_status_sw2_cnt = 0;
+int32_t  appv_status_counter = 0;
+char     appv_status_message[150] = "READY";
 
 /// Set of status
 LiveObjectsD_Data_t appv_set_status[] = {
     { LOD_TYPE_STRING_C, "sample_version" ,  (void*)appv_version },
     { LOD_TYPE_INT32,    "sample_counter" ,  &appv_status_counter},
+    { LOD_TYPE_UINT32,   "sample_sw2_cnt" ,  &appv_status_sw2_cnt},
     { LOD_TYPE_STRING_C, "sample_message" ,  appv_status_message}
 };
 #define SET_STATUS_NB (sizeof(appv_set_status) / sizeof(LiveObjectsD_Data_t))
@@ -348,9 +349,6 @@
 uint32_t appv_rsc_offset = 0;
 
 
-
-
-
 // ==========================================================
 // IotSoftbox-mqtt callback functions
 
@@ -690,6 +688,50 @@
 }
 
 // ==========================================================
+//
+
+// ----------------------------------------------------------
+// Interrupts and buttons
+short   appv_sw2_raise = 0;
+short   appv_sw2_state = 0;
+
+InterruptIn app_int_sw2(SW2);
+
+
+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");
+            }
+        }
+
+        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);
+        }
+    }
+}
+
+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;
+}
+
+// ==========================================================
 
 
 /// Counter ticker to increment periodically a application counter
@@ -706,6 +748,7 @@
  *   - Send the delayed LED command response (if pending)
  */
 void thread_appli(void) {
+    uint32_t loop_delay = 0;
     uint32_t loop_cnt = 0;
     osThreadId id = osThreadGetId();
     output.printf("thread_appli: running %x\r\n", id);
@@ -715,14 +758,24 @@
         if (delay == 0)          delay = 1000;   // min. 1 seconds
         else if (delay > 120000) delay = 120000; // max. 2 minutes
 
-        wait_ms(delay);
+        wait_ms(100);
+
+        loop_delay += 100;
+
+        // Process the SW2 button interrupt
+        app_sw2_test();
+
+        // Process the LED command response if pending.
+        main_cmd_delayed_resp_LED();
+
+        if (loop_delay < delay) {
+            continue;
+        }
+        loop_delay = 0;
 
         ++loop_cnt;
         if (appv_log_level > 1) output.printf("thread_appli: %u - period= %u ms\r\n", loop_cnt, delay);
 
-        // Process the LED command response if pending.
-        main_cmd_delayed_resp_LED();
-
         // Simulate measures : Voltage and Temperature ...
 
         if (appv_measures_volt <= 0.0)       appv_measures_volt_grad = 0.2;
@@ -741,7 +794,6 @@
         if (appv_measures_enabled) {
             LiveObjectsClient_PushData( appv_hdl_data );
         }
-
     }
 }
 
@@ -888,6 +940,11 @@
     output.printf("\r\n\r\n");
     output.printf("Starting LiveObject Client Example %s  (tid=x%p) ...\r\n", appv_version, appv_thread_id);
 
+    // Enable interrupts: SW2
+     output.printf("Enable SW2 ..\r\n");
+     app_int_sw2.rise(&app_sw2_release);
+
+
     app_trace_setup();
 
     // Start program only if LiveObjet Apikey parameter is well defined