![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
dd
Dependencies: xtoff2 RF24Network mbed
Fork of xtoff3 by
main.cpp@11:77018fac195e, 2018-03-09 (annotated)
- Committer:
- pietor
- Date:
- Fri Mar 09 12:22:27 2018 +0000
- Revision:
- 11:77018fac195e
- Parent:
- 10:0a4c465f7838
- Child:
- 12:a3dd2ebf8f93
9/03 test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pietor | 4:7ebc3b421306 | 1 | /** |
pietor | 4:7ebc3b421306 | 2 | Main.cpp |
pietor | 4:7ebc3b421306 | 3 | Purpose: main class |
pietor | 4:7ebc3b421306 | 4 | |
pietor | 4:7ebc3b421306 | 5 | @author Pieter Berteloot |
pietor | 11:77018fac195e | 6 | |
pietor | 11:77018fac195e | 7 | RF24 PINOUT: |
pietor | 11:77018fac195e | 8 | ---- ----- |
pietor | 11:77018fac195e | 9 | |GND | VCC | |
pietor | 11:77018fac195e | 10 | |---- | |
pietor | 11:77018fac195e | 11 | |CE CSN | |
pietor | 11:77018fac195e | 12 | |SCK MOSI| |
pietor | 11:77018fac195e | 13 | |MISO IRQ | |
pietor | 11:77018fac195e | 14 | ---- ----- |
pietor | 11:77018fac195e | 15 | |
pietor | 11:77018fac195e | 16 | ---- ----- |
pietor | 11:77018fac195e | 17 | |GND | Vout| |
pietor | 11:77018fac195e | 18 | |---- | |
pietor | 11:77018fac195e | 19 | |p9 p8 | |
pietor | 11:77018fac195e | 20 | |p7 p5 | |
pietor | 11:77018fac195e | 21 | |p6 NC | |
pietor | 11:77018fac195e | 22 | ---- ----- |
pietor | 4:7ebc3b421306 | 23 | */ |
pietor | 11:77018fac195e | 24 | //uncommend #define PRINT_ENABLE to see informational prints |
pietor | 11:77018fac195e | 25 | //#define PRINT_ENABLE |
pietor | 11:77018fac195e | 26 | |
akashvibhute | 0:3982c0e9eda1 | 27 | #include "mbed.h" |
pietor | 4:7ebc3b421306 | 28 | #include "Ontvanger.h" |
pietor | 4:7ebc3b421306 | 29 | |
akashvibhute | 0:3982c0e9eda1 | 30 | Serial pc(USBTX, USBRX); |
pietor | 4:7ebc3b421306 | 31 | Ontvanger receive; |
pietor | 6:6a82110ff393 | 32 | Timer t; |
pietor | 6:6a82110ff393 | 33 | |
pietor | 9:e14f65b8ffc2 | 34 | DigitalOut myled1(LED1); |
pietor | 9:e14f65b8ffc2 | 35 | DigitalOut myled2(LED2); |
pietor | 9:e14f65b8ffc2 | 36 | DigitalOut myled3(LED3); |
pietor | 9:e14f65b8ffc2 | 37 | DigitalOut myled4(LED4); |
pietor | 7:8515f205483b | 38 | |
pietor | 7:8515f205483b | 39 | State2 current_state = init_state; |
pietor | 7:8515f205483b | 40 | |
pietor | 6:6a82110ff393 | 41 | void acitvateLed() |
pietor | 6:6a82110ff393 | 42 | { |
pietor | 6:6a82110ff393 | 43 | t.reset(); |
pietor | 6:6a82110ff393 | 44 | } |
pietor | 6:6a82110ff393 | 45 | |
pietor | 7:8515f205483b | 46 | void setCurrentState( State2 setState ) |
pietor | 7:8515f205483b | 47 | { |
pietor | 7:8515f205483b | 48 | current_state = setState; |
pietor | 7:8515f205483b | 49 | } |
pietor | 7:8515f205483b | 50 | |
pietor | 7:8515f205483b | 51 | |
pietor | 7:8515f205483b | 52 | State2 getCurrentState() |
pietor | 7:8515f205483b | 53 | { |
pietor | 7:8515f205483b | 54 | return current_state; |
pietor | 7:8515f205483b | 55 | } |
pietor | 7:8515f205483b | 56 | |
akashvibhute | 2:608cf8c5c55e | 57 | int main() |
akashvibhute | 0:3982c0e9eda1 | 58 | { |
pietor | 11:77018fac195e | 59 | IF_PRINT_ENABLE( pc.printf("\r\nOntvanger: \r\n"); |
pietor | 11:77018fac195e | 60 | pc.printf("testing Radio... %d\r\n", receive.isValid()); |
pietor | 11:77018fac195e | 61 | ); |
pietor | 10:0a4c465f7838 | 62 | |
pietor | 4:7ebc3b421306 | 63 | while(1) { |
pietor | 10:0a4c465f7838 | 64 | receive.update(); |
pietor | 7:8515f205483b | 65 | switch (current_state) { |
pietor | 7:8515f205483b | 66 | case init_state: |
pietor | 7:8515f205483b | 67 | pc.baud(9600); |
pietor | 7:8515f205483b | 68 | t.start(); |
pietor | 7:8515f205483b | 69 | state_Packet packet; |
pietor | 7:8515f205483b | 70 | payload_t payload; |
pietor | 7:8515f205483b | 71 | setCurrentState(receive_state); |
pietor | 7:8515f205483b | 72 | break; |
pietor | 7:8515f205483b | 73 | |
pietor | 7:8515f205483b | 74 | case send_state: |
pietor | 11:77018fac195e | 75 | IF_PRINT_ENABLE(pc.printf("Send State\n\r");); |
pietor | 7:8515f205483b | 76 | packet.setState = State_position; |
pietor | 7:8515f205483b | 77 | bool ok = receive.write(packet); |
pietor | 7:8515f205483b | 78 | if (ok) |
pietor | 11:77018fac195e | 79 | IF_PRINT_ENABLE(pc.printf("ok.\n\r");); |
pietor | 7:8515f205483b | 80 | else |
pietor | 11:77018fac195e | 81 | IF_PRINT_ENABLE(pc.printf("failed.\n\r");); |
pietor | 7:8515f205483b | 82 | wait(0.1); |
pietor | 7:8515f205483b | 83 | setCurrentState(receive_state); |
pietor | 7:8515f205483b | 84 | break; |
pietor | 7:8515f205483b | 85 | |
pietor | 7:8515f205483b | 86 | case receive_state: |
pietor | 7:8515f205483b | 87 | pc.printf("");//print niet weg doen, één of andere reden werkt het dan wel |
pietor | 7:8515f205483b | 88 | if (receive.available()) { |
pietor | 7:8515f205483b | 89 | payload_t payload; |
pietor | 7:8515f205483b | 90 | payload = receive.read(); |
pietor | 8:c43411433ce1 | 91 | pc.printf("%f\r\n", payload.milligram); |
pietor | 7:8515f205483b | 92 | if (payload.reedsensor) { |
pietor | 7:8515f205483b | 93 | acitvateLed(); |
pietor | 7:8515f205483b | 94 | } |
pietor | 7:8515f205483b | 95 | } |
pietor | 7:8515f205483b | 96 | |
pietor | 9:e14f65b8ffc2 | 97 | if (t.read() <= 1){ |
pietor | 7:8515f205483b | 98 | myled1 = myled2 = myled3 = myled4 = 1; |
pietor | 9:e14f65b8ffc2 | 99 | } |
pietor | 7:8515f205483b | 100 | else |
pietor | 7:8515f205483b | 101 | myled1 = myled2 = myled3 = myled4 = 0; |
pietor | 7:8515f205483b | 102 | |
pietor | 7:8515f205483b | 103 | if(pc.readable()) { |
pietor | 7:8515f205483b | 104 | char c = pc.getc(); |
pietor | 7:8515f205483b | 105 | if(c == 's') |
pietor | 7:8515f205483b | 106 | setCurrentState(send_state); |
pietor | 7:8515f205483b | 107 | wait(0.5); |
pietor | 7:8515f205483b | 108 | } |
pietor | 7:8515f205483b | 109 | break; |
akashvibhute | 0:3982c0e9eda1 | 110 | } |
akashvibhute | 0:3982c0e9eda1 | 111 | } |
akashvibhute | 0:3982c0e9eda1 | 112 | } |