Copy off node 1 to behave node 2
Dependencies: mbed RF24Network RF24
main.cpp@7:1f768ebd2742, 2021-05-23 (annotated)
- Committer:
- Giamarchi
- Date:
- Sun May 23 23:41:26 2021 +0000
- Revision:
- 7:1f768ebd2742
- Parent:
- 6:6a3d4e0f6799
- Child:
- 8:5c8bf540ed8e
- Child:
- 9:0736b94c8a51
Up Date;
Who changed what in which revision?
User | Revision | Line number | New 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 |
akashvibhute | 3:d605536db315 | 34 | const uint16_t this_node = 01; |
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 | 4:c6de6d47c54a | 67 | _node node; |
Giamarchi | 4:c6de6d47c54a | 68 | |
Giamarchi | 5:48f3f58ebd13 | 69 | BTN1.mode(PullUp); // Resist interne Pull up |
Giamarchi | 4:c6de6d47c54a | 70 | // pc.baud(9600); |
akashvibhute | 0:3982c0e9eda1 | 71 | wait_ms(1000); |
akashvibhute | 2:926b93a68399 | 72 | |
Giamarchi | 4:c6de6d47c54a | 73 | pc.printf("mBed RF24Network node : 01\r\n"); |
akashvibhute | 0:3982c0e9eda1 | 74 | radio.begin(); |
akashvibhute | 0:3982c0e9eda1 | 75 | network.begin(/*channel*/ 90, /*node address*/ this_node); |
akashvibhute | 0:3982c0e9eda1 | 76 | wait_ms(2000); |
akashvibhute | 1:5be48a9550c3 | 77 | t.start(); |
akashvibhute | 1:5be48a9550c3 | 78 | t_packet.start(); |
Giamarchi | 4:c6de6d47c54a | 79 | |
Giamarchi | 4:c6de6d47c54a | 80 | |
akashvibhute | 2:926b93a68399 | 81 | while(1) |
akashvibhute | 0:3982c0e9eda1 | 82 | { |
Giamarchi | 4:c6de6d47c54a | 83 | // Refresh Dels |
Giamarchi | 5:48f3f58ebd13 | 84 | // Set_8Dels(node.del); |
Giamarchi | 4:c6de6d47c54a | 85 | |
akashvibhute | 0:3982c0e9eda1 | 86 | // Pump the network regularly |
akashvibhute | 0:3982c0e9eda1 | 87 | network.update(); |
akashvibhute | 2:926b93a68399 | 88 | |
akashvibhute | 1:5be48a9550c3 | 89 | /* If it's time to send a message, send it! */ |
Giamarchi | 4:c6de6d47c54a | 90 | /* unsigned long now = t.read_ms(); |
akashvibhute | 2:926b93a68399 | 91 | if ( now >= interval ) |
akashvibhute | 1:5be48a9550c3 | 92 | { |
akashvibhute | 1:5be48a9550c3 | 93 | t.reset(); |
akashvibhute | 1:5be48a9550c3 | 94 | |
Giamarchi | 4:c6de6d47c54a | 95 | pc.printf("Envoi..."); |
akashvibhute | 1:5be48a9550c3 | 96 | payload_t payload_tx; |
Giamarchi | 4:c6de6d47c54a | 97 | if(!BP) |
Giamarchi | 4:c6de6d47c54a | 98 | payload_tx.num = 01; |
Giamarchi | 4:c6de6d47c54a | 99 | else |
Giamarchi | 4:c6de6d47c54a | 100 | payload_tx.num = 00; |
Giamarchi | 4:c6de6d47c54a | 101 | |
Giamarchi | 4:c6de6d47c54a | 102 | payload_tx.del++; |
akashvibhute | 2:926b93a68399 | 103 | |
akashvibhute | 2:926b93a68399 | 104 | |
Giamarchi | 4:c6de6d47c54a | 105 | RF24NetworkHeader header_tx(other_node); // vers node "other_node" |
akashvibhute | 1:5be48a9550c3 | 106 | bool ok = network.write(header_tx,&payload_tx,sizeof(payload_tx)); |
akashvibhute | 1:5be48a9550c3 | 107 | if (ok) |
Giamarchi | 4:c6de6d47c54a | 108 | pc.printf("ok.\r\n"); |
akashvibhute | 1:5be48a9550c3 | 109 | else |
Giamarchi | 4:c6de6d47c54a | 110 | pc.printf("erreur.\r\n"); |
Giamarchi | 4:c6de6d47c54a | 111 | |
Giamarchi | 4:c6de6d47c54a | 112 | |
akashvibhute | 2:926b93a68399 | 113 | } |
Giamarchi | 4:c6de6d47c54a | 114 | */ |
Giamarchi | 4:c6de6d47c54a | 115 | while ( network.available() ) |
Giamarchi | 4:c6de6d47c54a | 116 | { |
Giamarchi | 4:c6de6d47c54a | 117 | // If so, grab it and print it out |
Giamarchi | 4:c6de6d47c54a | 118 | RF24NetworkHeader header_rx; |
Giamarchi | 4:c6de6d47c54a | 119 | payload_t payload_rx; |
Giamarchi | 4:c6de6d47c54a | 120 | network.read(header_rx,&payload_rx,sizeof(payload_rx)); |
akashvibhute | 2:926b93a68399 | 121 | |
Giamarchi | 4:c6de6d47c54a | 122 | pc.printf("Node 01 recu de node %d, data : %d\r\n",payload_rx.src,payload_rx.data); |
Giamarchi | 4:c6de6d47c54a | 123 | |
Giamarchi | 4:c6de6d47c54a | 124 | node.del = payload_rx.data; |
Giamarchi | 4:c6de6d47c54a | 125 | } |
Giamarchi | 5:48f3f58ebd13 | 126 | if(!BTN1) |
Giamarchi | 4:c6de6d47c54a | 127 | { |
Giamarchi | 4:c6de6d47c54a | 128 | payload_t payload_tx; |
Giamarchi | 4:c6de6d47c54a | 129 | payload_tx.src = this_node; |
Giamarchi | 7:1f768ebd2742 | 130 | payload_tx.dest = 00; |
Giamarchi | 4:c6de6d47c54a | 131 | payload_tx.data = node.del%5; |
Giamarchi | 4:c6de6d47c54a | 132 | pc.printf("Envoi..."); |
akashvibhute | 2:926b93a68399 | 133 | |
Giamarchi | 4:c6de6d47c54a | 134 | RF24NetworkHeader header_tx(other_node); // vers node "other_node" |
Giamarchi | 4:c6de6d47c54a | 135 | bool ok = network.write(header_tx,&payload_tx,sizeof(payload_tx)); |
Giamarchi | 4:c6de6d47c54a | 136 | if (ok) |
Giamarchi | 4:c6de6d47c54a | 137 | pc.printf("ok.\r\n"); |
Giamarchi | 4:c6de6d47c54a | 138 | else |
Giamarchi | 4:c6de6d47c54a | 139 | pc.printf("erreur.\r\n"); |
Giamarchi | 7:1f768ebd2742 | 140 | wait_ms(1000); |
Giamarchi | 4:c6de6d47c54a | 141 | } |
akashvibhute | 0:3982c0e9eda1 | 142 | } |
akashvibhute | 0:3982c0e9eda1 | 143 | } |