Copy off node 1 to behave node 2
Dependencies: mbed RF24Network RF24
Diff: main.cpp
- Revision:
- 4:c6de6d47c54a
- Parent:
- 3:d605536db315
- Child:
- 5:48f3f58ebd13
diff -r d605536db315 -r c6de6d47c54a main.cpp --- a/main.cpp Thu Nov 05 05:59:18 2015 +0000 +++ b/main.cpp Thu May 20 09:26:58 2021 +0000 @@ -4,11 +4,17 @@ Serial pc(USBTX, USBRX); -#define nrf_CE D9 -#define nrf_CSN D10 -#define spi_SCK D3 -#define spi_MOSI D4 -#define spi_MISO D5 +DigitalOut LED_V(PA_12); +DigitalOut LED_J(PB_0); +DigitalOut LED_R(PB_7); + +DigitalIn BP(PA_0); + +#define nrf_CE PA_8 +#define nrf_CSN PA_11 +#define spi_MOSI PB_5 +#define spi_MISO PB_4 +#define spi_SCK PB_3 RF24 radio(spi_MOSI, spi_MISO, spi_SCK, nrf_CE, nrf_CSN ); @@ -22,7 +28,7 @@ const uint16_t other_node = 00; // How often to send payload packet to the other unit -const unsigned long interval = 100; //ms +const unsigned long interval = 1000; //ms // When did we last send? unsigned long last_sent; @@ -35,48 +41,96 @@ // Structure of our payload struct payload_t { - unsigned long ms; - unsigned long counter; + uint8_t src; // Source du message + uint8_t dest; // Destination du message + uint8_t data; // Data du message }; +struct _node +{ + uint8_t src; // Source du message + uint8_t dest; // Destination du message + uint8_t del; +}; int main() { - pc.baud(921600); + _node node; + + BP.mode(PullUp); // Resist interne Pull up +// pc.baud(9600); wait_ms(1000); - pc.printf("mBed RF24Network node: Tx\n"); + pc.printf("mBed RF24Network node : 01\r\n"); radio.begin(); network.begin(/*channel*/ 90, /*node address*/ this_node); wait_ms(2000); t.start(); t_packet.start(); + + while(1) { + // Refresh Dels + LED_R = node.del & 0x01; + LED_J = (node.del & 0x02)>>1; + LED_V = (node.del & 0x04)>>2; + // Pump the network regularly network.update(); /* If it's time to send a message, send it! */ - unsigned long now = t.read_ms(); +/* unsigned long now = t.read_ms(); if ( now >= interval ) { t.reset(); - pc.printf("Sending..."); + pc.printf("Envoi..."); payload_t payload_tx; - payload_tx.ms = t_packet.read_ms(); - payload_tx.counter = packets_sent++; + if(!BP) + payload_tx.num = 01; + else + payload_tx.num = 00; + + payload_tx.del++; - RF24NetworkHeader header_tx(/*to node*/ other_node); + RF24NetworkHeader header_tx(other_node); // vers node "other_node" bool ok = network.write(header_tx,&payload_tx,sizeof(payload_tx)); if (ok) - pc.printf("ok.\n"); + pc.printf("ok.\r\n"); else - pc.printf("failed.\n"); + pc.printf("erreur.\r\n"); + + } + */ + while ( network.available() ) + { + // If so, grab it and print it out + RF24NetworkHeader header_rx; + payload_t payload_rx; + network.read(header_rx,&payload_rx,sizeof(payload_rx)); + pc.printf("Node 01 recu de node %d, data : %d\r\n",payload_rx.src,payload_rx.data); + + node.del = payload_rx.data; + } + if(!BP) + { + payload_t payload_tx; + payload_tx.src = this_node; + payload_tx.dest = 02; + payload_tx.data = node.del%5; + pc.printf("Envoi..."); + RF24NetworkHeader header_tx(other_node); // vers node "other_node" + bool ok = network.write(header_tx,&payload_tx,sizeof(payload_tx)); + if (ok) + pc.printf("ok.\r\n"); + else + pc.printf("erreur.\r\n"); + wait_ms(2000); + } } - } \ No newline at end of file