FinT
Dependencies: mbed RF24Network RF24
main.cpp@6:4840dd0ef133, 2019-04-01 (annotated)
- Committer:
- guillaume6544
- Date:
- Mon Apr 01 11:52:07 2019 +0000
- Revision:
- 6:4840dd0ef133
- Parent:
- 5:ddab03ae19aa
nouveau rec
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
akashvibhute | 0:3982c0e9eda1 | 1 | #include "mbed.h" |
akashvibhute | 0:3982c0e9eda1 | 2 | #include <RF24Network.h> |
akashvibhute | 2:608cf8c5c55e | 3 | #include <RF24.h> |
guillaume6544 | 6:4840dd0ef133 | 4 | |
akashvibhute | 0:3982c0e9eda1 | 5 | Serial pc(USBTX, USBRX); |
guillaume6544 | 6:4840dd0ef133 | 6 | InterruptIn button(D4); |
guillaume6544 | 6:4840dd0ef133 | 7 | |
guillaume6544 | 5:ddab03ae19aa | 8 | PwmOut mypwm(D5); |
guillaume6544 | 6:4840dd0ef133 | 9 | |
wesleytiem | 4:93d0253a8a32 | 10 | RF24 radio(SPI_MOSI, SPI_MISO, SPI_SCK, D9, SPI_CS ); |
guillaume6544 | 6:4840dd0ef133 | 11 | |
akashvibhute | 0:3982c0e9eda1 | 12 | // Network uses that radio |
akashvibhute | 0:3982c0e9eda1 | 13 | RF24Network network(radio); |
guillaume6544 | 6:4840dd0ef133 | 14 | |
akashvibhute | 0:3982c0e9eda1 | 15 | // Address of our node |
guillaume6544 | 6:4840dd0ef133 | 16 | const uint16_t this_node = 03; |
guillaume6544 | 6:4840dd0ef133 | 17 | |
akashvibhute | 0:3982c0e9eda1 | 18 | // Address of the other node |
guillaume6544 | 6:4840dd0ef133 | 19 | const uint16_t other_node = 00; |
guillaume6544 | 6:4840dd0ef133 | 20 | |
akashvibhute | 0:3982c0e9eda1 | 21 | // When did we last send? |
akashvibhute | 0:3982c0e9eda1 | 22 | unsigned long last_sent; |
guillaume6544 | 6:4840dd0ef133 | 23 | |
akashvibhute | 0:3982c0e9eda1 | 24 | // How many have we sent already |
akashvibhute | 0:3982c0e9eda1 | 25 | unsigned long packets_sent; |
guillaume6544 | 6:4840dd0ef133 | 26 | |
akashvibhute | 0:3982c0e9eda1 | 27 | // Structure of our payload |
akashvibhute | 2:608cf8c5c55e | 28 | struct payload_t |
akashvibhute | 0:3982c0e9eda1 | 29 | { |
akashvibhute | 2:608cf8c5c55e | 30 | unsigned long ms; |
akashvibhute | 2:608cf8c5c55e | 31 | unsigned long counter; |
akashvibhute | 0:3982c0e9eda1 | 32 | }; |
guillaume6544 | 6:4840dd0ef133 | 33 | |
guillaume6544 | 5:ddab03ae19aa | 34 | //interruption du NRF24 |
guillaume6544 | 5:ddab03ae19aa | 35 | void pressed() |
guillaume6544 | 5:ddab03ae19aa | 36 | { |
guillaume6544 | 5:ddab03ae19aa | 37 | mypwm.write(0.5); |
guillaume6544 | 5:ddab03ae19aa | 38 | } |
guillaume6544 | 6:4840dd0ef133 | 39 | |
akashvibhute | 2:608cf8c5c55e | 40 | int main() |
akashvibhute | 0:3982c0e9eda1 | 41 | { |
guillaume6544 | 5:ddab03ae19aa | 42 | button.enable_irq(); |
guillaume6544 | 5:ddab03ae19aa | 43 | button.rise(&pressed); |
guillaume6544 | 6:4840dd0ef133 | 44 | mypwm.period_us(40); |
guillaume6544 | 6:4840dd0ef133 | 45 | mypwm.pulsewidth_us(20); |
guillaume6544 | 5:ddab03ae19aa | 46 | mypwm.write(0.0); |
guillaume6544 | 5:ddab03ae19aa | 47 | |
wesleytiem | 4:93d0253a8a32 | 48 | pc.baud(115200); |
guillaume6544 | 6:4840dd0ef133 | 49 | wait_ms(500); |
guillaume6544 | 6:4840dd0ef133 | 50 | |
akashvibhute | 2:608cf8c5c55e | 51 | pc.printf("mBed RF24Network node\n"); |
akashvibhute | 0:3982c0e9eda1 | 52 | radio.begin(); |
akashvibhute | 0:3982c0e9eda1 | 53 | network.begin(/*channel*/ 90, /*node address*/ this_node); |
guillaume6544 | 6:4840dd0ef133 | 54 | wait_ms(1000); |
guillaume6544 | 6:4840dd0ef133 | 55 | |
akashvibhute | 2:608cf8c5c55e | 56 | while(1) |
akashvibhute | 0:3982c0e9eda1 | 57 | { |
akashvibhute | 0:3982c0e9eda1 | 58 | // Pump the network regularly |
akashvibhute | 0:3982c0e9eda1 | 59 | network.update(); |
wesleytiem | 4:93d0253a8a32 | 60 | |
akashvibhute | 0:3982c0e9eda1 | 61 | // Is there anything ready for us? |
akashvibhute | 2:608cf8c5c55e | 62 | while ( network.available() ) |
akashvibhute | 0:3982c0e9eda1 | 63 | { |
akashvibhute | 0:3982c0e9eda1 | 64 | // If so, grab it and print it out |
akashvibhute | 0:3982c0e9eda1 | 65 | RF24NetworkHeader header_rx; |
akashvibhute | 0:3982c0e9eda1 | 66 | payload_t payload_rx; |
akashvibhute | 0:3982c0e9eda1 | 67 | network.read(header_rx,&payload_rx,sizeof(payload_rx)); |
wesleytiem | 4:93d0253a8a32 | 68 | pc.printf("Received packet # %d at %d ms\r\n",payload_rx.counter,payload_rx.ms); |
guillaume6544 | 5:ddab03ae19aa | 69 | |
guillaume6544 | 6:4840dd0ef133 | 70 | wait_us(1750); |
guillaume6544 | 5:ddab03ae19aa | 71 | mypwm.write(0.0); |
akashvibhute | 0:3982c0e9eda1 | 72 | } |
guillaume6544 | 6:4840dd0ef133 | 73 | |
akashvibhute | 0:3982c0e9eda1 | 74 | } |
guillaume6544 | 6:4840dd0ef133 | 75 | |
guillaume6544 | 6:4840dd0ef133 | 76 | } |
guillaume6544 | 6:4840dd0ef133 | 77 |