Copy off node 1 to behave node 2

Dependencies:   mbed RF24Network RF24

Committer:
Giamarchi
Date:
Fri May 21 07:52:27 2021 +0000
Revision:
6:6a3d4e0f6799
Parent:
5:48f3f58ebd13
Child:
7:1f768ebd2742
Correctif

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Giamarchi 5:48f3f58ebd13 1 /*************************************/
Giamarchi 5:48f3f58ebd13 2 // YNCREA ISEN CSI3
Giamarchi 5:48f3f58ebd13 3 // Electronique Numérique
Giamarchi 5:48f3f58ebd13 4 //
Giamarchi 5:48f3f58ebd13 5 // Cours : Systèmes à microcontrôleur
Giamarchi 5:48f3f58ebd13 6 // Prof : Frédéric Giamarchi
Giamarchi 5:48f3f58ebd13 7 //
Giamarchi 5:48f3f58ebd13 8 // Projet 2021 : Jeu type Simon's pour la rééducation
Giamarchi 5:48f3f58ebd13 9 //
Giamarchi 5:48f3f58ebd13 10 // Note : Utilisation de Timer et du SPI
Giamarchi 5:48f3f58ebd13 11 /*************************************/
akashvibhute 0:3982c0e9eda1 12 #include "mbed.h"
akashvibhute 0:3982c0e9eda1 13 #include <RF24Network.h>
akashvibhute 2:926b93a68399 14 #include <RF24.h>
Giamarchi 5:48f3f58ebd13 15 #include "isen32_board.h"
akashvibhute 0:3982c0e9eda1 16
Giamarchi 5:48f3f58ebd13 17 Serial pc(USBTX, USBRX); // 9600 baud par défaut
Giamarchi 4:c6de6d47c54a 18
Giamarchi 5:48f3f58ebd13 19 #define nrf_CE PB_3 // D3 sur connecteur Arduino
Giamarchi 5:48f3f58ebd13 20 #define nrf_CSN PB_12
Giamarchi 5:48f3f58ebd13 21 #define spi_MOSI PB_15
Giamarchi 5:48f3f58ebd13 22 #define spi_MISO PB_14
Giamarchi 5:48f3f58ebd13 23 #define spi_SCK PB_13
akashvibhute 0:3982c0e9eda1 24
Giamarchi 6:6a3d4e0f6799 25 // Module nRF24 sur SPI2
akashvibhute 3:d605536db315 26 RF24 radio(spi_MOSI, spi_MISO, spi_SCK, nrf_CE, nrf_CSN );
akashvibhute 0:3982c0e9eda1 27
akashvibhute 0:3982c0e9eda1 28 // Network uses that radio
akashvibhute 0:3982c0e9eda1 29 RF24Network network(radio);
akashvibhute 0:3982c0e9eda1 30
akashvibhute 0:3982c0e9eda1 31 // Address of our node
akashvibhute 3:d605536db315 32 const uint16_t this_node = 01;
akashvibhute 0:3982c0e9eda1 33
akashvibhute 0:3982c0e9eda1 34 // Address of the other node
akashvibhute 3:d605536db315 35 const uint16_t other_node = 00;
akashvibhute 0:3982c0e9eda1 36
akashvibhute 2:926b93a68399 37 // How often to send payload packet to the other unit
Giamarchi 4:c6de6d47c54a 38 const unsigned long interval = 1000; //ms
akashvibhute 0:3982c0e9eda1 39
akashvibhute 0:3982c0e9eda1 40 // When did we last send?
akashvibhute 0:3982c0e9eda1 41 unsigned long last_sent;
akashvibhute 1:5be48a9550c3 42 Timer t;
akashvibhute 0:3982c0e9eda1 43
akashvibhute 0:3982c0e9eda1 44 // How many have we sent already
akashvibhute 0:3982c0e9eda1 45 unsigned long packets_sent;
akashvibhute 1:5be48a9550c3 46 Timer t_packet;
akashvibhute 0:3982c0e9eda1 47
akashvibhute 0:3982c0e9eda1 48 // Structure of our payload
akashvibhute 2:926b93a68399 49 struct payload_t
akashvibhute 0:3982c0e9eda1 50 {
Giamarchi 4:c6de6d47c54a 51 uint8_t src; // Source du message
Giamarchi 4:c6de6d47c54a 52 uint8_t dest; // Destination du message
Giamarchi 4:c6de6d47c54a 53 uint8_t data; // Data du message
akashvibhute 0:3982c0e9eda1 54 };
akashvibhute 0:3982c0e9eda1 55
Giamarchi 4:c6de6d47c54a 56 struct _node
Giamarchi 4:c6de6d47c54a 57 {
Giamarchi 4:c6de6d47c54a 58 uint8_t src; // Source du message
Giamarchi 4:c6de6d47c54a 59 uint8_t dest; // Destination du message
Giamarchi 4:c6de6d47c54a 60 uint8_t del;
Giamarchi 4:c6de6d47c54a 61 };
akashvibhute 0:3982c0e9eda1 62
akashvibhute 2:926b93a68399 63 int main()
akashvibhute 0:3982c0e9eda1 64 {
Giamarchi 4:c6de6d47c54a 65 _node node;
Giamarchi 4:c6de6d47c54a 66
Giamarchi 5:48f3f58ebd13 67 BTN1.mode(PullUp); // Resist interne Pull up
Giamarchi 4:c6de6d47c54a 68 // pc.baud(9600);
akashvibhute 0:3982c0e9eda1 69 wait_ms(1000);
akashvibhute 2:926b93a68399 70
Giamarchi 4:c6de6d47c54a 71 pc.printf("mBed RF24Network node : 01\r\n");
akashvibhute 0:3982c0e9eda1 72 radio.begin();
akashvibhute 0:3982c0e9eda1 73 network.begin(/*channel*/ 90, /*node address*/ this_node);
akashvibhute 0:3982c0e9eda1 74 wait_ms(2000);
akashvibhute 1:5be48a9550c3 75 t.start();
akashvibhute 1:5be48a9550c3 76 t_packet.start();
Giamarchi 4:c6de6d47c54a 77
Giamarchi 4:c6de6d47c54a 78
akashvibhute 2:926b93a68399 79 while(1)
akashvibhute 0:3982c0e9eda1 80 {
Giamarchi 4:c6de6d47c54a 81 // Refresh Dels
Giamarchi 5:48f3f58ebd13 82 // Set_8Dels(node.del);
Giamarchi 4:c6de6d47c54a 83
akashvibhute 0:3982c0e9eda1 84 // Pump the network regularly
akashvibhute 0:3982c0e9eda1 85 network.update();
akashvibhute 2:926b93a68399 86
akashvibhute 1:5be48a9550c3 87 /* If it's time to send a message, send it! */
Giamarchi 4:c6de6d47c54a 88 /* unsigned long now = t.read_ms();
akashvibhute 2:926b93a68399 89 if ( now >= interval )
akashvibhute 1:5be48a9550c3 90 {
akashvibhute 1:5be48a9550c3 91 t.reset();
akashvibhute 1:5be48a9550c3 92
Giamarchi 4:c6de6d47c54a 93 pc.printf("Envoi...");
akashvibhute 1:5be48a9550c3 94 payload_t payload_tx;
Giamarchi 4:c6de6d47c54a 95 if(!BP)
Giamarchi 4:c6de6d47c54a 96 payload_tx.num = 01;
Giamarchi 4:c6de6d47c54a 97 else
Giamarchi 4:c6de6d47c54a 98 payload_tx.num = 00;
Giamarchi 4:c6de6d47c54a 99
Giamarchi 4:c6de6d47c54a 100 payload_tx.del++;
akashvibhute 2:926b93a68399 101
akashvibhute 2:926b93a68399 102
Giamarchi 4:c6de6d47c54a 103 RF24NetworkHeader header_tx(other_node); // vers node "other_node"
akashvibhute 1:5be48a9550c3 104 bool ok = network.write(header_tx,&payload_tx,sizeof(payload_tx));
akashvibhute 1:5be48a9550c3 105 if (ok)
Giamarchi 4:c6de6d47c54a 106 pc.printf("ok.\r\n");
akashvibhute 1:5be48a9550c3 107 else
Giamarchi 4:c6de6d47c54a 108 pc.printf("erreur.\r\n");
Giamarchi 4:c6de6d47c54a 109
Giamarchi 4:c6de6d47c54a 110
akashvibhute 2:926b93a68399 111 }
Giamarchi 4:c6de6d47c54a 112 */
Giamarchi 4:c6de6d47c54a 113 while ( network.available() )
Giamarchi 4:c6de6d47c54a 114 {
Giamarchi 4:c6de6d47c54a 115 // If so, grab it and print it out
Giamarchi 4:c6de6d47c54a 116 RF24NetworkHeader header_rx;
Giamarchi 4:c6de6d47c54a 117 payload_t payload_rx;
Giamarchi 4:c6de6d47c54a 118 network.read(header_rx,&payload_rx,sizeof(payload_rx));
akashvibhute 2:926b93a68399 119
Giamarchi 4:c6de6d47c54a 120 pc.printf("Node 01 recu de node %d, data : %d\r\n",payload_rx.src,payload_rx.data);
Giamarchi 4:c6de6d47c54a 121
Giamarchi 4:c6de6d47c54a 122 node.del = payload_rx.data;
Giamarchi 4:c6de6d47c54a 123 }
Giamarchi 5:48f3f58ebd13 124 if(!BTN1)
Giamarchi 4:c6de6d47c54a 125 {
Giamarchi 4:c6de6d47c54a 126 payload_t payload_tx;
Giamarchi 4:c6de6d47c54a 127 payload_tx.src = this_node;
Giamarchi 4:c6de6d47c54a 128 payload_tx.dest = 02;
Giamarchi 4:c6de6d47c54a 129 payload_tx.data = node.del%5;
Giamarchi 4:c6de6d47c54a 130 pc.printf("Envoi...");
akashvibhute 2:926b93a68399 131
Giamarchi 4:c6de6d47c54a 132 RF24NetworkHeader header_tx(other_node); // vers node "other_node"
Giamarchi 4:c6de6d47c54a 133 bool ok = network.write(header_tx,&payload_tx,sizeof(payload_tx));
Giamarchi 4:c6de6d47c54a 134 if (ok)
Giamarchi 4:c6de6d47c54a 135 pc.printf("ok.\r\n");
Giamarchi 4:c6de6d47c54a 136 else
Giamarchi 4:c6de6d47c54a 137 pc.printf("erreur.\r\n");
Giamarchi 4:c6de6d47c54a 138 wait_ms(2000);
Giamarchi 4:c6de6d47c54a 139 }
akashvibhute 0:3982c0e9eda1 140 }
akashvibhute 0:3982c0e9eda1 141 }