Gestion du Trafic Adaptatif - Code des mbed récepteurs

Dependencies:   XBeeLib mbed mbed-rtos

Revision:
21:2ad11875542c
Parent:
20:4e4d869ada85
Child:
22:d9287c62dd45
--- a/main.cpp	Sun Nov 18 16:42:35 2018 +0000
+++ b/main.cpp	Sun Nov 18 21:14:55 2018 +0000
@@ -2,6 +2,7 @@
 
 #include "mbed.h"
 #include "XBeeLib.h"
+#include "rtos.h"
 #if defined(ENABLE_LOGGING)
 #include "DigiLoggerMbedSerial.h"
 using namespace DigiLog;
@@ -25,6 +26,10 @@
     //vert
 DigitalOut feux_gauche_vert(p26);
 
+Thread t_nbVoiture;
+
+DigitalIn boutonPlus(p24);
+DigitalIn boutonMoins(p23);
     //liste des message a envoyer rouge et vert seulement pour le coordinateur
     ////////////////////////// 
     /////
@@ -34,8 +39,10 @@
     uint8_t ok = 0x02;
     //////////////////////////
 
+Serial *log_serial;
+XBeeZB xbee = XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
+const RemoteXBeeZB remoteDevice = RemoteXBeeZB(REMOTE_NODE_ADDR64);
 
-Serial *log_serial;
 
 /** Callback function, invoked at packet reception */
 static void receive_cb(const RemoteXBeeZB& remote, bool broadcast, const uint8_t *const data, uint16_t len)
@@ -44,8 +51,14 @@
 
     log_serial->printf("\r\nGot a %s RX packet [%08x:%08x|%04x], len %d\r\nData: ", broadcast ? "BROADCAST" : "UNICAST", UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64), remote.get_addr16(), len);
 
-    for (int i = 0; i < len; i++)
-        log_serial->printf("%02x ", data[i]);
+    if (data[0]==0x76){
+        feux_gauche_rouge = 1;
+        feux_gauche_vert = 0;
+        }
+    if (data[0]==0x72){
+        feux_gauche_rouge = 0;
+        feux_gauche_vert = 1;
+        }
         
     log_serial->printf("\r\n");
 }
@@ -59,49 +72,46 @@
     const uint16_t clusterID = 0x0011;
     const uint16_t profileID = 0xC105;
 
-    char data1[]="r";
-    char data2[]="v";
+    char data1[]="m"; // m comme "moins" - une voiture de moins
+    char data2[]="p"; // p comme "plus" - une voiture de plus
     char data3[]="e";
     if (message ==0){
         const TxStatus txStatus = xbee.send_data(RemoteDevice, dstEP, srcEP, clusterID, profileID, (const uint8_t *)data1, strlen(data1)); 
-        if (txStatus == TxStatusSuccess)
+        /*if (txStatus == TxStatusSuccess)
             log_serial->printf("send_explicit_data_to_remote_node OK\r\n");
         else
-            log_serial->printf("send_explicit_data_to_remote_node failed with %d\r\n", (int) txStatus);
+            log_serial->printf("send_explicit_data_to_remote_node failed with %d\r\n", (int) txStatus);*/
     }
     else if (message ==1){
         const TxStatus txStatus = xbee.send_data(RemoteDevice, dstEP, srcEP, clusterID, profileID, (const uint8_t *)data2, strlen(data2)); 
-        if (txStatus == TxStatusSuccess)
+        /*if (txStatus == TxStatusSuccess)
             log_serial->printf("send_explicit_data_to_remote_node OK\r\n");
         else
-            log_serial->printf("send_explicit_data_to_remote_node failed with %d\r\n", (int) txStatus);
+            log_serial->printf("send_explicit_data_to_remote_node failed with %d\r\n", (int) txStatus);*/
     }
     else {
         const TxStatus txStatus = xbee.send_data(RemoteDevice, dstEP, srcEP, clusterID, profileID, (const uint8_t *)data3, strlen(data3));
-        if (txStatus == TxStatusSuccess)
+        /*if (txStatus == TxStatusSuccess)
             log_serial->printf("send_explicit_data_to_remote_node OK\r\n");
         else
-            log_serial->printf("send_explicit_data_to_remote_node failed with %d\r\n", (int) txStatus);
+            log_serial->printf("send_explicit_data_to_remote_node failed with %d\r\n", (int) txStatus);*/
     }
    
     
 }
 
-
-int main()
+void connect_Xbee()
 {
     log_serial = new Serial(DEBUG_TX, DEBUG_RX);
     log_serial->baud(9600);
     log_serial->printf("Sample application to demo how to receive unicast and broadcast data with the XBeeZB\r\n\r\n");
     log_serial->printf(XB_LIB_BANNER);
     
-
-
-#if defined(ENABLE_LOGGING)
+    #if defined(ENABLE_LOGGING)
     new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
-#endif
+    #endif
 
-    XBeeZB xbee = XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
+    //xbee = XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
 
     /* Register callbacks */
     xbee.register_receive_cb(&receive_cb);
@@ -116,20 +126,45 @@
         log_serial->printf(".");
     }
     log_serial->printf("OK\r\n");
+    //return xbee;
+}
 
-    int i =0;
-    const RemoteXBeeZB remoteDevice = RemoteXBeeZB(REMOTE_NODE_ADDR64);
+void nbVoiture() {
+    //TO DO : faire un compteur de voitures interne au recepteur et envoyer ce nombre 
+    //au coordinateur -> plus fiable en cas de perte de donnees dans un envoi
+    while(1) {
+        if(boutonPlus){
+            send_explicit_data_to_remote_node(xbee, remoteDevice,1); 
+            Thread::wait(500);
+        }
+        if(boutonMoins){
+            send_explicit_data_to_remote_node(xbee, remoteDevice,0);  
+            Thread::wait(500);
+        }
+        
+    }
+}
+
+int main()
+{
+    connect_Xbee();
+    t_nbVoiture.start(nbVoiture);
     while (true) {
-        i++;
         uint32_t receive_value = xbee.process_rx_frames();
-        log_serial->printf("%d");
+        /*log_serial->printf("%d");
         wait_ms(100);
-        log_serial->printf(".");
+        log_serial->printf(".");*/
         
+        /*if(boutonPlus){
+            send_explicit_data_to_remote_node(xbee, remoteDevice,1); 
+        }
+        if(boutonMoins){
+            send_explicit_data_to_remote_node(xbee, remoteDevice,0);  
+        }*/
         
         //partie gestion des feux a mettre sur le coordinateur 
         /////////////////////////////////////////////
-        if(i==10){
+        /*if(i==10){
             send_explicit_data_to_remote_node(xbee, remoteDevice,1);
             feux_gauche_rouge = 0;
             feux_gauche_vert = 1;
@@ -139,7 +174,7 @@
             feux_gauche_rouge = 1;
             feux_gauche_vert = 0;
             i=0;
-            }
+            }*/
         ////////////////////////////////////////////
     }