Complete sensor demo.

Dependencies:   modem_ref_helper CRC X_NUCLEO_IKS01A1 DebouncedInterrupt

Revision:
7:b94971f119a5
Parent:
6:c17f7cbdeb1a
Child:
9:6d14fe774883
--- a/main.cpp	Thu Sep 21 11:23:13 2017 +0000
+++ b/main.cpp	Thu Dec 21 15:25:21 2017 +0000
@@ -10,6 +10,7 @@
 #include "modem_callbacks.h"
 #include "files.h"
 
+#define MIN_REPORT_PERIOD   (10) // Seconds
 
 enum {
     MODEM_RESP_NO,
@@ -23,21 +24,34 @@
 sensor_config_t g_light_config;
 Queue<void, 8> g_file_modified;
 
+static bool report_ok(uint32_t last_report_time)
+{
+    // Do not send a report if it's been less than MIN_REPORT_PERIOD since the last report
+    if ((last_report_time/1000) < MIN_REPORT_PERIOD)
+    {
+        PRINT("Report Skipped, next in %ds min\n", MIN_REPORT_PERIOD - (last_report_time/1000));
+        return false;
+    }
+    
+    return true;
+}
+
 // Check parameters to see if data should be send
 static bool report_needed(sensor_config_t* config, int32_t value, int32_t last_value, uint32_t last_report_time, uint8_t user_id)
 {
+
     switch (config->report_type)
     {
         case REPORT_ALWAYS:
             // Send a report at each measure
             PRINT("Report[%d] always\r\n", user_id);
-            return true;
+            return report_ok(last_report_time);
         case REPORT_ON_DIFFERENCE:
             // Send a report when the difference between the last reported measure and the current mesure is greater than max_diff
             if (abs(last_value - value) >= config->max_diff && config->max_diff)
             {
                 PRINT("Report[%d] on difference (last:%d new:%d max_diff:%d)\r\n", user_id, last_value, value, config->max_diff);
-                return true;
+                return report_ok(last_report_time);
             }
             break;
         case REPORT_ON_THRESHOLD:
@@ -48,7 +62,7 @@
                 || (value > config->threshold_low   && last_value <= config->threshold_low))
             {
                 PRINT("Report[%d] on threshold (last:%d new:%d th:%d tl:%d)\r\n", user_id, last_value, value, config->threshold_high, config->threshold_low);
-                return true;
+                return report_ok(last_report_time);
             }
             break;
         default:
@@ -59,7 +73,7 @@
     if (((last_report_time/1000) >= config->max_period) && config->max_period)
     {
         PRINT("Report[%d] on period (max_period:%d time:%d)\r\n", user_id, config->max_period, last_report_time);
-        return true;
+        return report_ok(last_report_time);
     }
 
     return false;
@@ -391,7 +405,12 @@
 #else
     DBG_OPEN(NC);
 #endif
-    PRINT("\r\n--- Starting new run ---\r\n");
+    PRINT("\n"
+          "-----------------------------------------\n"
+          "------------- Demo Sensors --------------\n"
+          "-----------------------------------------\n");
+          
+          
     FPRINT("(id:0x%08x)\r\n", osThreadGetId());
     
     modem_helper_open(&callbacks);