Fork for Get Started Demo

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

Fork of D7A_Demo_full by WizziLab

Revision:
13:218d2b6a9390
Parent:
12:d17ef6ce50c8
Child:
14:02bfe80b3db8
--- a/main.cpp	Tue Dec 01 11:09:29 2015 +0000
+++ b/main.cpp	Wed Jan 27 19:32:04 2016 +0000
@@ -9,7 +9,7 @@
 // Semaphore for notifiying button presses
 Semaphore button_user(1);
 
-alp_err_t d7_write_file(const uint8_t file_id,
+alp_err_t d7a_write_file(const uint8_t file_id,
                         const uint16_t offset,
                         const uint16_t file_size,
                         const uint8_t* const content)
@@ -51,7 +51,7 @@
     return err;
 }
 
-alp_err_t d7_read_file( const uint8_t file_id,
+alp_err_t d7a_read_file( const uint8_t file_id,
                         const uint16_t offset,
                         const uint16_t file_size,
                         uint8_t* buf)
@@ -89,30 +89,49 @@
     return err;
 }
 
-const d7_config_t shield1001_config = {
+const d7a_config_t shield1001_config = {
     .tx = PB_6,
     .rx = PA_10,
     .rts = PA_5,
     .cts = PC_7,
     .rx_buffer_size = 256,
-    .local_timeout = 200,
-    .distant_timeout = 3000,
-    .write = d7_write_file,
-    .read = d7_read_file,
+    .local_timeout = 3000,
+    .distant_timeout = 10000,
+    .write = d7a_write_file,
+    .read = d7a_read_file,
 };
 
 
 // Checks the status of the report send.
-void check_status( uint8_t status, char* message )
+void check_status( alp_response_t* response, char* message )
 {
-    switch (status)
+    if (response != NULL)
     {
-        case ALP_ERR_NO: // message is send and acknowleged
-            PRINT("%s OK\r\n", message);
-        break;
-        default:
-            PRINT("%s ERROR %d\r\n", message, status);
-        break;
+        /*
+            Accessing the list of acknowledges.
+        */
+        if (response->nb_ack == 0)
+        {
+            PRINT("%s NO RESPONSE!\r\n", message);
+        }
+        else
+        {
+            for(uint8_t i=0 ; i < response->nb_ack ; i++)
+            {
+                PRINT("%s -%d- ERR:0x%02X UID:%02X%02X%02X%02X%02X%02X%02X%02X\r\n", message, i+1, response->ack[i].err,
+                    response->ack[i].uid[0], response->ack[i].uid[1], response->ack[i].uid[2], response->ack[i].uid[3], 
+                    response->ack[i].uid[4], response->ack[i].uid[5], response->ack[i].uid[6], response->ack[i].uid[7]);
+            }
+        }
+        
+        /*
+            Do not forget to free the response!
+        */
+        FREE(response);
+    }
+    else
+    {
+        PRINT("%s ERROR!\r\n", message);
     }
 }
 
@@ -127,8 +146,12 @@
 void alarm_thread( const void* args )
 {    
     // Get modem
-    D7_modem* wm1001 = (D7_modem*)args;
-    uint8_t target_uid[8] = { 0x00, 0x1B, 0xC5, 0x0C, 0x70, 0x00, 0x06, 0x9B };
+    D7A_modem* wm1001 = (D7A_modem*)args;
+    uint8_t target_uid[8] = { 0x00, 0x1B, 0xC5, 0x0C, 0x70, 0x00, 0x06, 0xA7 };
+    uint8_t led_on_cmd[5] = { 0, 0, 0, 9, 2 };
+    uint8_t led_off_cmd[5] = { 0, 0, 0, 9, 3 };
+    uint8_t buf[20];
+    alp_response_t* response;
 
     // Enable interrupt on User button
     DebouncedInterrupt button(USER_BUTTON);
@@ -139,23 +162,45 @@
         // Wait for button press
         button_user.wait();
         
+        response = NULL;
+        
         // Invert alarm status
         alarm_data.status = !alarm_data.status;
         
         PRINT("NOTIFY ALARM STATE CHANGE %d\r\n", alarm_data.status);
         FLUSH();
-        
+                
         // Notify alarm status
         check_status(wm1001->notify_filechange(ALARM_DATA_FILE_ID), "ALARM REPORT");
         
         if (alarm_data.status)
         {
-            wm1001->io_set(target_uid, ACCESS_CLASS_SC, 9);
+            /*
+                Writes a file in broadcast and wait for the first response
+            */
+            response = wm1001->write(NULL, ACCESS_CLASS_SC, 80, 0, 5, led_on_cmd, RESP_ANYCAST);
+            
+            /*
+                Reads a file from the specified UID into 'buf'
+                When the UID is specified, the resp parameter is always ANYCAST
+            */
+            //response = wm1001->read(target_uid, ACCESS_CLASS_SC, ALARM_CMD_FILE_ID, 0, 1, buf);
         }
         else
         {
-            wm1001->io_clear(target_uid, ACCESS_CLASS_SC, 9);
+            /*
+                Writes a file in broadcast and wait for all the responses before timeout.
+            */
+            response = wm1001->write(NULL, ACCESS_CLASS_SC, 80, 0, 5, led_off_cmd, RESP_ALLCAST);
+            
+            /*
+                Reads a file in broadcast and wait for all the responses before timeout.
+                'buf' is filled with the data of the last response recieved.
+            */
+            //response = wm1001->read(NULL, ACCESS_CLASS_SC, ALARM_CMD_FILE_ID, 0, 1, buf, RESP_ALLCAST);
         }
+        
+        check_status(response, "CMD");
     }
 }
 
@@ -164,7 +209,7 @@
 void temp_thread( const void* args )
 {
     // Get modem
-    D7_modem* wm1001 = (D7_modem*)args;
+    D7A_modem* wm1001 = (D7A_modem*)args;
 
     while(true)
     {
@@ -173,7 +218,7 @@
         PRINT("NOTIFY TEMPERATURE %d C\r\n", temp_data.value);
         
         // Notify temp value
-        check_status(wm1001->notify_filechange(TEMP_DATA_FILE_ID), "TEMP REPORT");
+        //check_status(wm1001->notify_filechange(TEMP_DATA_FILE_ID), "TEMP REPORT");
 
         // Wait 5 minutes
         // The function Thread::wait(...) takes a uin32_t as parameter
@@ -194,24 +239,27 @@
     system_open();
 
     // Initialize ALP modem
-    D7_modem wm1001(&shield1001_config, PB_0);
+    //D7A_modem wm1001(&shield1001_config, PB_0);
+    D7A_modem wm1001(&shield1001_config);
     
     // Wait for modem to notify its files if there is any
-    Thread::wait(500);
+    //Thread::wait(500);
     
     // Register the files
-    wm1001.register_file(&revision_device_fh);
+    //wm1001.register_file(&revision_device_fh);
     wm1001.register_file(&alarm_data_fh);
     wm1001.register_file(&alarm_cmd_fh);
-    wm1001.register_file(&temp_data_fh);    
+    //wm1001.register_file(&temp_data_fh);
     
     // Send the Revision report
-    PRINT("NOTIFY REVISION\r\n");
-    check_status(wm1001.notify_filechange(REVISION_DEVICE_FILE_ID), "REPORT REVISION");
+    //PRINT("NOTIFY REVISION\r\n");
+    //check_status(wm1001.notify_filechange(REVISION_DEVICE_FILE_ID), "REPORT REVISION");
     
     // Init Threads
-    Thread t_alarm_thread(alarm_thread, &wm1001);
-    Thread t_temp_thread(temp_thread, &wm1001);
+    Thread t_alarm_thread(alarm_thread, &wm1001, osPriorityNormal, DEFAULT_STACK_SIZE*8);
+    //Thread t_temp_thread(temp_thread, &wm1001, osPriorityNormal, DEFAULT_STACK_SIZE*4);
+    
+    //alarm_thread(&wm1001);
     
     // Set main task to lowest priority
     osThreadSetPriority(osThreadGetId(), osPriorityIdle);