Gestion du Trafic Adaptatif - Code des mbed récepteurs

Dependencies:   XBeeLib mbed mbed-rtos

main.cpp

Committer:
leomerel
Date:
2018-11-22
Revision:
22:d9287c62dd45
Parent:
21:2ad11875542c
Child:
23:296a2145c0fc

File content as of revision 22:d9287c62dd45:

//YOYOYO

#include "mbed.h"
#include "XBeeLib.h"
#include "rtos.h"
#if defined(ENABLE_LOGGING)
#include "DigiLoggerMbedSerial.h"
using namespace DigiLog;
#endif


#define REMOTE_NODE_ADDR64_MSB  ((uint32_t)0x0013A200)

//#error "Replace next define with the LSB of the remote module's 64-bit address (SL parameter)"
#define REMOTE_NODE_ADDR64_LSB  ((uint32_t)0x40E779AF)

#define REMOTE_NODE_ADDR64      UINT64(REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB)

using namespace XBeeLib;

//feux gauche
    //rouge
DigitalOut feu_rouge(p30);
    //orange
DigitalOut feu_orange(p28);
    //vert
DigitalOut feu_vert(p26);

Thread t_nbVoiture;
Thread t_feuRouge;

DigitalIn boutonPlus(p24);
DigitalIn boutonMoins(p23);

Serial *log_serial;
XBeeZB xbee = XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
const RemoteXBeeZB remoteDevice = RemoteXBeeZB(REMOTE_NODE_ADDR64);

/** Callback function, invoked at packet reception */
static void receive_cb(const RemoteXBeeZB& remote, bool broadcast, const uint8_t *const data, uint16_t len)
{
    const uint64_t remote_addr64 = remote.get_addr64();

    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);

    if (data[0]==0x76){
        t_feuRouge.signal_set(0x1);
    }
    else if (data[0]==0x72){
        feu_rouge = 0;
        feu_orange = 0;
        feu_vert = 1;
    }
        
    log_serial->printf("\r\n");
}


static void send_explicit_data_to_remote_node(XBeeZB& xbee, const RemoteXBeeZB& RemoteDevice,int message)
{
    const uint8_t dstEP = 0xE8;
    const uint8_t srcEP = 0xE8;
    const uint16_t clusterID = 0x0011;
    const uint16_t profileID = 0xC105;

    char data0[]="m"; // m comme "moins" - une voiture de moins
    char data1[]="p"; // p comme "plus" - une voiture de plus
    char data2[]="r"; // r comme "rouge" - confirmation que le feu est rouge
    char data3[]="e";
    if (message ==0){
        const TxStatus txStatus = xbee.send_data(RemoteDevice, dstEP, srcEP, clusterID, profileID, (const uint8_t *)data0, strlen(data0)); 
    }
    else if (message ==1){
        const TxStatus txStatus = xbee.send_data(RemoteDevice, dstEP, srcEP, clusterID, profileID, (const uint8_t *)data1, strlen(data1)); 
    }
    else if (message ==2){
        const TxStatus txStatus = xbee.send_data(RemoteDevice, dstEP, srcEP, clusterID, profileID, (const uint8_t *)data2, strlen(data2)); 
    }
    else {
        const TxStatus txStatus = xbee.send_data(RemoteDevice, dstEP, srcEP, clusterID, profileID, (const uint8_t *)data3, strlen(data3));
    }
   
    
}

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)
    new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
    #endif

    /* Register callbacks */
    xbee.register_receive_cb(&receive_cb);

    RadioStatus const radioStatus = xbee.init();
    MBED_ASSERT(radioStatus == Success);

    /* Wait until the device has joined the network */
    log_serial->printf("Waiting for device to join the network: ");
    while (!xbee.is_joined()) {
        wait_ms(1000);
        log_serial->printf(".");
    }
    log_serial->printf("OK\r\n");
}

void set_feuRouge()
{
   while(1)
   {
      Thread::signal_wait(0x1);
      feu_vert = 0;
      feu_orange = 1;
      Thread::wait(2000);
      feu_orange = 0;
      feu_rouge = 1;
      send_explicit_data_to_remote_node(xbee, remoteDevice,2);
   }
}

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
    // - faire un buffer pour envoyer les donnees
    //
    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);
    t_feuRouge.start(set_feuRouge);
    while (true) {
        uint32_t receive_value = xbee.process_rx_frames();
    }
}