Copy off node 1 to behave node 2

Dependencies:   mbed RF24Network RF24

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