Fork for Get Started Demo

Dependencies:   DebouncedInterrupt dash7-alp mbed-rtos mbed wizzi-utils

Fork of D7A_Demo_full by WizziLab

Revision:
5:e18c38942326
Parent:
4:1edd956b015f
Child:
6:897d29f7c89a
--- a/main.cpp	Thu Nov 19 17:30:37 2015 +0000
+++ b/main.cpp	Fri Nov 20 15:25:06 2015 +0000
@@ -7,6 +7,7 @@
 #include "alp_file.h"
 #include "alp_report.h"
 #include "DebouncedInterrupt.h"
+#include "system.h"
 
 
 /*
@@ -14,10 +15,11 @@
 to identify the device
 */
 
-#define __MANUFACTURER_ID__         0x01234567 // Identify the manufacturer
+#define __MANUFACTURER_ID__         0x01BC50C7 // Identify the manufacturer
 #define __DEVICE_ID__               0x89ABCDEF // Identify the device type
 
-#define __FW_ID__                   0x01 // Firmware ID
+#define __FW_ID__                   0x01       // Firmware ID
+#define __HW_ID__                   0x01520C02 // Hardware ID
 
 // Firmware version
 #define __FW_MAJOR__                0x02
@@ -25,9 +27,8 @@
 #define __FW_PATCH__                0x0045
 #define __FW_HASH__                 0x86605dba
 
-#define __HW_ID__                   0x01520C02 // Hardware ID
 
-revision_t revision = {
+const revision_t revision = {
     // The two first bytes must be present in all reported files
     // They are parsed by the dash7board
     .nw_stat = 0,
@@ -74,6 +75,23 @@
     .cmd = 0
 };
 
+// Alarm data structure
+TYPEDEF_STRUCT_PACKED
+{
+    uint8_t  nw_stat;
+    uint8_t  nw_seq;
+    int8_t   value; // Temperature value in °C
+} temp_data_t;
+
+#define TEMP_DATA_FILE_ID           (226)
+#define TEMP_DATA_FILE_SIZE         ((uint16_t) sizeof(temp_data_t))
+
+temp_data_t temp_data = {
+    .nw_stat = 0,
+    .nw_seq = 255,
+    .value = 0
+};
+
 // Checks the status of the report send.
 void check_status( AlpReport* report, void* param )
 {
@@ -114,11 +132,20 @@
     DPRINT("SENDING REPORT ALARM %d\r\n", alarm->status);
 }
 
+// Updates the temperature value.
+void update_temp( AlpReport* report, void* param )
+{
+    temp_data_t* temp = (temp_data_t*)report->get_content();
+    
+    temp->value = system_get_temperature();
+    
+    DPRINT("SENDING REPORT TEMP %d C\r\n", temp->value);
+}
+
 // Interrupt Service Routine on button press.
 // Inverts alarm status and releases button Semaphore.
 void button_push_isr( void )
 {
-    DPRINT("BUTTON PUSH\r\n");
     // Retrieve file content
     alarm_data_t* alarm = (alarm_data_t*)alp_file_get_content(ALARM_DATA_FILE_ID);
     // Invert alarm status
@@ -133,6 +160,7 @@
 {
     // Get shield device
     AlpDevice* shield = (AlpDevice*)args;
+    uint8_t retries;
     
     // Create new report
     AlpReport report_alarm(shield, ALARM_DATA_FILE_ID);
@@ -140,7 +168,6 @@
     report_alarm.attach_after(check_status, (void*)"REPORT ALARM");
     
     // Enable interrupt on User button
-    //__enable_irq(); // Enable Interrupts
     DebouncedInterrupt button(USER_BUTTON);
     button.attach(&button_push_isr, IRQ_FALL, 200);
 
@@ -148,9 +175,67 @@
     {
         // Wait for button press
         button_user.wait();
+        
+        // Reset retries
+        retries = 0;
 
         // Notify alarm status
-        report_alarm.send();
+        if (report_alarm.send() != ALP_CMD_OK)
+        {
+            // Retry policy
+            while (retries < 3)
+            {
+                // retry 1 second later
+                Thread::wait(1000);
+                retries++;
+                DPRINT("RETRY %d: ", retries);
+                if (report_alarm.retry() == ALP_CMD_OK) {
+                    break;
+                }
+            }
+        }
+    }
+}
+
+// This Thread reads the chip temperature
+// and reports it every 5 minutes
+void temp_thread( const void* args )
+{
+    // Get shield device
+    AlpDevice* shield = (AlpDevice*)args;
+    uint8_t retries;
+    
+    // Create new report
+    AlpReport report_temp(shield, TEMP_DATA_FILE_ID);
+    report_temp.attach_before(update_temp, NULL);
+    report_temp.attach_after(check_status, (void*)"REPORT TEMP");
+
+    while(true)
+    {
+        // Reset retries
+        retries = 0;
+        // Notify temp value
+        if (report_temp.send() == ALP_CMD_OK)
+        {
+            // Wait 5 minutes
+            // The function Thread::wait(...) takes a uin32_t as parameter
+            // but the maximum value is uin16_t (65535)
+            for (uint8_t i=0 ; i<5 ; i++) { Thread::wait(60000); }
+        }
+        else
+        {
+            // Retry policy
+            while (retries < 3)
+            {
+                // retry 1 second later
+                Thread::wait(1000);
+                retries++;
+                DPRINT("RETRY %d: ", retries);
+                if (report_temp.retry() == ALP_CMD_OK) {
+                    break;
+                }
+            }
+        }
     }
 }
 
@@ -182,6 +267,9 @@
     // Declare Shield NRST pin
     DigitalOut shield_nrst(PB_0);
     
+    // Initialize system functions
+    system_open();
+    
     // Reset shield
     shield_nrst = 0;
     
@@ -205,6 +293,7 @@
     
     // Add the files to the file system
     alp_file_add(ALARM_CMD_FILE_ID,       ALARM_CMD_FILE_SIZE,       (uint8_t*)&alarm_cmd);
+    alp_file_add(TEMP_DATA_FILE_ID,       TEMP_DATA_FILE_SIZE,       (uint8_t*)&temp_data);
     alp_file_add(ALARM_DATA_FILE_ID,      ALARM_DATA_FILE_SIZE,      (uint8_t*)&alarm_data);
     alp_file_add(REVISION_DEVICE_FILE_ID, REVISION_DEVICE_FILE_SIZE, (uint8_t*)&revision);
     
@@ -222,8 +311,9 @@
     DPRINT("SENDING REPORT REVISION\r\n");
     report_revision.send();
     
-    // Init Thread
+    // Init Threads
     Thread t_alarm_thread(alarm_thread, &shield);
+    Thread t_temp_thread(temp_thread, &shield);
     
     // Set main task to lowest priority
     osThreadSetPriority(osThreadGetId(), osPriorityIdle);