Copy off node 1 to behave node 2

Dependencies:   mbed RF24Network RF24

Committer:
Giamarchi
Date:
Thu May 20 09:57:23 2021 +0000
Revision:
5:48f3f58ebd13
Parent:
4:c6de6d47c54a
Child:
6:6a3d4e0f6799
new version

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
akashvibhute 3:d605536db315 25 RF24 radio(spi_MOSI, spi_MISO, spi_SCK, nrf_CE, nrf_CSN );
akashvibhute 0:3982c0e9eda1 26
akashvibhute 0:3982c0e9eda1 27 // Network uses that radio
akashvibhute 0:3982c0e9eda1 28 RF24Network network(radio);
akashvibhute 0:3982c0e9eda1 29
akashvibhute 0:3982c0e9eda1 30 // Address of our node
akashvibhute 3:d605536db315 31 const uint16_t this_node = 01;
akashvibhute 0:3982c0e9eda1 32
akashvibhute 0:3982c0e9eda1 33 // Address of the other node
akashvibhute 3:d605536db315 34 const uint16_t other_node = 00;
akashvibhute 0:3982c0e9eda1 35
akashvibhute 2:926b93a68399 36 // How often to send payload packet to the other unit
Giamarchi 4:c6de6d47c54a 37 const unsigned long interval = 1000; //ms
akashvibhute 0:3982c0e9eda1 38
akashvibhute 0:3982c0e9eda1 39 // When did we last send?
akashvibhute 0:3982c0e9eda1 40 unsigned long last_sent;
akashvibhute 1:5be48a9550c3 41 Timer t;
akashvibhute 0:3982c0e9eda1 42
akashvibhute 0:3982c0e9eda1 43 // How many have we sent already
akashvibhute 0:3982c0e9eda1 44 unsigned long packets_sent;
akashvibhute 1:5be48a9550c3 45 Timer t_packet;
akashvibhute 0:3982c0e9eda1 46
akashvibhute 0:3982c0e9eda1 47 // Structure of our payload
akashvibhute 2:926b93a68399 48 struct payload_t
akashvibhute 0:3982c0e9eda1 49 {
Giamarchi 4:c6de6d47c54a 50 uint8_t src; // Source du message
Giamarchi 4:c6de6d47c54a 51 uint8_t dest; // Destination du message
Giamarchi 4:c6de6d47c54a 52 uint8_t data; // Data du message
akashvibhute 0:3982c0e9eda1 53 };
akashvibhute 0:3982c0e9eda1 54
Giamarchi 4:c6de6d47c54a 55 struct _node
Giamarchi 4:c6de6d47c54a 56 {
Giamarchi 4:c6de6d47c54a 57 uint8_t src; // Source du message
Giamarchi 4:c6de6d47c54a 58 uint8_t dest; // Destination du message
Giamarchi 4:c6de6d47c54a 59 uint8_t del;
Giamarchi 4:c6de6d47c54a 60 };
akashvibhute 0:3982c0e9eda1 61
akashvibhute 2:926b93a68399 62 int main()
akashvibhute 0:3982c0e9eda1 63 {
Giamarchi 4:c6de6d47c54a 64 _node node;
Giamarchi 4:c6de6d47c54a 65
Giamarchi 5:48f3f58ebd13 66 BTN1.mode(PullUp); // Resist interne Pull up
Giamarchi 4:c6de6d47c54a 67 // pc.baud(9600);
akashvibhute 0:3982c0e9eda1 68 wait_ms(1000);
akashvibhute 2:926b93a68399 69
Giamarchi 4:c6de6d47c54a 70 pc.printf("mBed RF24Network node : 01\r\n");
akashvibhute 0:3982c0e9eda1 71 radio.begin();
akashvibhute 0:3982c0e9eda1 72 network.begin(/*channel*/ 90, /*node address*/ this_node);
akashvibhute 0:3982c0e9eda1 73 wait_ms(2000);
akashvibhute 1:5be48a9550c3 74 t.start();
akashvibhute 1:5be48a9550c3 75 t_packet.start();
Giamarchi 4:c6de6d47c54a 76
Giamarchi 4:c6de6d47c54a 77
akashvibhute 2:926b93a68399 78 while(1)
akashvibhute 0:3982c0e9eda1 79 {
Giamarchi 4:c6de6d47c54a 80 // Refresh Dels
Giamarchi 5:48f3f58ebd13 81 // Set_8Dels(node.del);
Giamarchi 4:c6de6d47c54a 82
akashvibhute 0:3982c0e9eda1 83 // Pump the network regularly
akashvibhute 0:3982c0e9eda1 84 network.update();
akashvibhute 2:926b93a68399 85
akashvibhute 1:5be48a9550c3 86 /* If it's time to send a message, send it! */
Giamarchi 4:c6de6d47c54a 87 /* unsigned long now = t.read_ms();
akashvibhute 2:926b93a68399 88 if ( now >= interval )
akashvibhute 1:5be48a9550c3 89 {
akashvibhute 1:5be48a9550c3 90 t.reset();
akashvibhute 1:5be48a9550c3 91
Giamarchi 4:c6de6d47c54a 92 pc.printf("Envoi...");
akashvibhute 1:5be48a9550c3 93 payload_t payload_tx;
Giamarchi 4:c6de6d47c54a 94 if(!BP)
Giamarchi 4:c6de6d47c54a 95 payload_tx.num = 01;
Giamarchi 4:c6de6d47c54a 96 else
Giamarchi 4:c6de6d47c54a 97 payload_tx.num = 00;
Giamarchi 4:c6de6d47c54a 98
Giamarchi 4:c6de6d47c54a 99 payload_tx.del++;
akashvibhute 2:926b93a68399 100
akashvibhute 2:926b93a68399 101
Giamarchi 4:c6de6d47c54a 102 RF24NetworkHeader header_tx(other_node); // vers node "other_node"
akashvibhute 1:5be48a9550c3 103 bool ok = network.write(header_tx,&payload_tx,sizeof(payload_tx));
akashvibhute 1:5be48a9550c3 104 if (ok)
Giamarchi 4:c6de6d47c54a 105 pc.printf("ok.\r\n");
akashvibhute 1:5be48a9550c3 106 else
Giamarchi 4:c6de6d47c54a 107 pc.printf("erreur.\r\n");
Giamarchi 4:c6de6d47c54a 108
Giamarchi 4:c6de6d47c54a 109
akashvibhute 2:926b93a68399 110 }
Giamarchi 4:c6de6d47c54a 111 */
Giamarchi 4:c6de6d47c54a 112 while ( network.available() )
Giamarchi 4:c6de6d47c54a 113 {
Giamarchi 4:c6de6d47c54a 114 // If so, grab it and print it out
Giamarchi 4:c6de6d47c54a 115 RF24NetworkHeader header_rx;
Giamarchi 4:c6de6d47c54a 116 payload_t payload_rx;
Giamarchi 4:c6de6d47c54a 117 network.read(header_rx,&payload_rx,sizeof(payload_rx));
akashvibhute 2:926b93a68399 118
Giamarchi 4:c6de6d47c54a 119 pc.printf("Node 01 recu de node %d, data : %d\r\n",payload_rx.src,payload_rx.data);
Giamarchi 4:c6de6d47c54a 120
Giamarchi 4:c6de6d47c54a 121 node.del = payload_rx.data;
Giamarchi 4:c6de6d47c54a 122 }
Giamarchi 5:48f3f58ebd13 123 if(!BTN1)
Giamarchi 4:c6de6d47c54a 124 {
Giamarchi 4:c6de6d47c54a 125 payload_t payload_tx;
Giamarchi 4:c6de6d47c54a 126 payload_tx.src = this_node;
Giamarchi 4:c6de6d47c54a 127 payload_tx.dest = 02;
Giamarchi 4:c6de6d47c54a 128 payload_tx.data = node.del%5;
Giamarchi 4:c6de6d47c54a 129 pc.printf("Envoi...");
akashvibhute 2:926b93a68399 130
Giamarchi 4:c6de6d47c54a 131 RF24NetworkHeader header_tx(other_node); // vers node "other_node"
Giamarchi 4:c6de6d47c54a 132 bool ok = network.write(header_tx,&payload_tx,sizeof(payload_tx));
Giamarchi 4:c6de6d47c54a 133 if (ok)
Giamarchi 4:c6de6d47c54a 134 pc.printf("ok.\r\n");
Giamarchi 4:c6de6d47c54a 135 else
Giamarchi 4:c6de6d47c54a 136 pc.printf("erreur.\r\n");
Giamarchi 4:c6de6d47c54a 137 wait_ms(2000);
Giamarchi 4:c6de6d47c54a 138 }
akashvibhute 0:3982c0e9eda1 139 }
akashvibhute 0:3982c0e9eda1 140 }