contain lorawan with serial_rx enabled

Dependencies:   pulga-lorawan-drv SPI_MX25R Si1133 BME280

Revision:
62:89df9529dbb0
Parent:
61:65744bc8ab55
Child:
63:4ec1808fb547
--- a/main.cpp	Fri Jan 08 20:16:58 2021 +0000
+++ b/main.cpp	Fri Feb 26 17:07:12 2021 +0000
@@ -15,6 +15,7 @@
  * limitations under the License.
  */
 #include <stdio.h>
+#include "mbed.h"
 
 #include "lorawan/LoRaWANInterface.h"
 #include "lorawan/system/lorawan_data_structures.h"
@@ -26,8 +27,13 @@
 #include "lora_radio_helper.h"
 //#include "BME280.h"
 
+#include "serial.h"
+
 using namespace events;
 
+
+//mbed::RawSerial pc(P0_28, P0_25);
+//Ticker serial_rx_ticker;
 // Max payload size can be LORAMAC_PHY_MAXPAYLOAD.
 // This example only communicates with much shorter messages (<30 bytes).
 // If longer messages are used, these buffers must be changed accordingly.
@@ -69,6 +75,8 @@
 //        uint32_t amb = 0;
 //        float  sensor_get = 0;
 
+void serial_post_to_queue(void);
+
 /**
 * This event queue is the global event queue for both the
 * application and stack. To conserve memory, the stack is designed to run
@@ -153,17 +161,38 @@
 void GPS_Read(void)
 {
     gps_print_local();
-    printf ("gps longitude=%d \n",lon);
-    printf ("gps latitude=%d \n",lat);
+    pc.printf ("gps longitude=%d \n",lon);
+    pc.printf ("gps latitude=%d \n",lat);
     if(lat!=0 && lon!=0){
         longitude=lon;
         latitude=lat;
 //        led1 = !led1;
         }
      }
+     
+void serial_rx(){
+    if(pc.readable()){
+        pc.printf("rx: %c\n", pc.getc());
+    } 
+    pc.attach(&serial_post_to_queue, RawSerial::RxIrq);
+    return;
+}
+ 
+void serial_post_to_queue(void){
+    //disable serial rx interrupt
+    pc.attach(NULL, RawSerial::RxIrq);
+    //enqueue the serial rx reception as a normal task
+    ev_queue.call(SerialRx);
+    return;
+}
 
 int main(void)
 {
+    pc.printf("init\n");
+    pc.baud(9600);
+    pc.printf("config9600\n");
+    //enable serial rx interrupt
+    pc.attach(&serial_post_to_queue, RawSerial::RxIrq);
     gps_config();
     gps_leBootMsg();
     gps_config_gnss ();
@@ -214,7 +243,7 @@
     i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false);
 //    printf("Range GYR Selecionado a 250deg/s\n\r");
     
-    printf("BMX160 Configurado\n\r");
+    pc.printf("BMX160 Configurado\n\r");
     
     //########################################################
     // setup tracing
@@ -225,11 +254,11 @@
 
     // Initialize LoRaWAN stack
     if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
-        printf("\r\n LoRa initialization failed! \r\n");
+        pc.printf("\r\n LoRa initialization failed! \r\n");
         return -1;
     }
 
-    printf("\r\n Mbed LoRaWANStack initialized \r\n");
+    pc.printf("\r\n Mbed LoRaWANStack initialized \r\n");
 
     // prepare application callbacks
     callbacks.events = mbed::callback(lora_event_handler);
@@ -238,33 +267,33 @@
     // Set number of retries in case of CONFIRMED messages
     if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER)
             != LORAWAN_STATUS_OK) {
-        printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n");
+        pc.printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n");
         return -1;
     }
 
-    printf("\r\n CONFIRMED message retries : %d \r\n",
+    pc.printf("\r\n CONFIRMED message retries : %d \r\n",
            CONFIRMED_MSG_RETRY_COUNTER);
 
     // Enable adaptive data rate
     if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) {
-        printf("\r\n enable_adaptive_datarate failed! \r\n");
+        pc.printf("\r\n enable_adaptive_datarate failed! \r\n");
         return -1;
     }
 
-    printf("\r\n Adaptive data  rate (ADR) - Enabled \r\n");
+    pc.printf("\r\n Adaptive data  rate (ADR) - Enabled \r\n");
 
     retcode = lorawan.connect();
 
     if (retcode == LORAWAN_STATUS_OK ||
             retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
     } else {
-        printf("\r\n Connection error, code = %d \r\n", retcode);
+        pc.printf("\r\n Connection error, code = %d \r\n", retcode);
         return -1;
     }
 
-    printf("\r\n Connection - In Progress ...\r\n");
+    pc.printf("\r\n Connection - In Progress ...\r\n");
     
-_actuated_led =0;
+    _actuated_led =0;
     // make your event queue dispatching events forever
     ev_queue.dispatch_forever();
 
@@ -287,8 +316,8 @@
                            MSG_UNCONFIRMED_FLAG);
 
     if (retcode < 0) {
-        retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n")
-        : printf("\r\n send() - Error code %d \r\n", retcode);
+        retcode == LORAWAN_STATUS_WOULD_BLOCK ? pc.printf("send - WOULD BLOCK\r\n")
+        : pc.printf("\r\n send() - Error code %d \r\n", retcode);
 
         if (retcode == LORAWAN_STATUS_WOULD_BLOCK) {
             //retry in 3 seconds
@@ -299,10 +328,10 @@
         return;
     }
 
-    printf("%2.2f;%04.2f;%2.2f;", getTemperature(), getPressure(), getHumidity());
+    pc.printf("%2.2f;%04.2f;%2.2f;", getTemperature(), getPressure(), getHumidity());
     BMX160Read();
-    printf ("%d;",lon);
-    printf ("%d \r\n",lat);
+    pc.printf ("%d;",lon);
+    pc.printf ("%d \r\n",lat);
 //    printf("\r\n %d bytes scheduled for transmission \r\n", retcode);
     memset(tx_buffer, 0, sizeof(tx_buffer));
 }
@@ -323,9 +352,9 @@
 
 //    printf(" RX Data on port %u (%d bytes): ", port, retcode);
     for (uint8_t i = 0; i < retcode; i++) {
-        printf("%02x ", rx_buffer[i]);
+        pc.printf("%02x ", rx_buffer[i]);
     }
-    printf("\r\n");
+    pc.printf("\r\n");
     
     memset(rx_buffer, 0, sizeof(rx_buffer));
 }
@@ -337,7 +366,7 @@
 {
     switch (event) {
         case CONNECTED:
-            printf("\r\n Connection - Successful \r\n");
+            pc.printf("\r\n Connection - Successful \r\n");
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
                 send_message();
             } else {
@@ -347,7 +376,7 @@
             break;
         case DISCONNECTED:
             ev_queue.break_dispatch();
-            printf("\r\n Disconnected Successfully \r\n");
+            pc.printf("\r\n Disconnected Successfully \r\n");
             break;
         case TX_DONE:
 //            printf("\r\n Message Sent to Network Server \r\n");
@@ -357,10 +386,13 @@
             break;
         case TX_TIMEOUT:
 //        printf("\r\n Transmission Error TX_Timeout");
+            break;
         case TX_ERROR:
 //        printf("\r\n Transmission Error TX_Error");
+            break;
         case TX_CRYPTO_ERROR:
 //        printf("\r\n Transmission Error TX_Crypto_Error");
+            break;
         case TX_SCHEDULING_ERROR:
 //            printf("\r\n Transmission Error - EventCode = %d \r\n", event);
             // try again
@@ -374,6 +406,7 @@
             break;
         case RX_TIMEOUT:
 //        printf("\r\n Transmission Error RX_Timeout");
+            break;
         case RX_ERROR:
 //            printf("\r\n Error in reception - Code = %d \r\n", event);
             break;
@@ -388,6 +421,7 @@
             break;
         default:
             MBED_ASSERT("Unknown Event");
+            break;
     }
 }