![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
dd
Dependencies: xtoff2 RF24Network mbed
Fork of xtoff3 by
main.cpp@10:0a4c465f7838, 2018-03-09 (annotated)
- Committer:
- pietor
- Date:
- Fri Mar 09 08:26:00 2018 +0000
- Revision:
- 10:0a4c465f7838
- Parent:
- 9:e14f65b8ffc2
- Child:
- 11:77018fac195e
9/03
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 | 4:7ebc3b421306 | 6 | */ |
akashvibhute | 0:3982c0e9eda1 | 7 | #include "mbed.h" |
pietor | 4:7ebc3b421306 | 8 | #include "Ontvanger.h" |
pietor | 4:7ebc3b421306 | 9 | |
akashvibhute | 0:3982c0e9eda1 | 10 | Serial pc(USBTX, USBRX); |
pietor | 4:7ebc3b421306 | 11 | Ontvanger receive; |
pietor | 6:6a82110ff393 | 12 | Timer t; |
pietor | 6:6a82110ff393 | 13 | |
pietor | 9:e14f65b8ffc2 | 14 | DigitalOut myled1(LED1); |
pietor | 9:e14f65b8ffc2 | 15 | DigitalOut myled2(LED2); |
pietor | 9:e14f65b8ffc2 | 16 | DigitalOut myled3(LED3); |
pietor | 9:e14f65b8ffc2 | 17 | DigitalOut myled4(LED4); |
pietor | 7:8515f205483b | 18 | |
pietor | 7:8515f205483b | 19 | State2 current_state = init_state; |
pietor | 7:8515f205483b | 20 | |
pietor | 6:6a82110ff393 | 21 | void acitvateLed() |
pietor | 6:6a82110ff393 | 22 | { |
pietor | 6:6a82110ff393 | 23 | t.reset(); |
pietor | 6:6a82110ff393 | 24 | } |
pietor | 6:6a82110ff393 | 25 | |
pietor | 7:8515f205483b | 26 | void setCurrentState( State2 setState ) |
pietor | 7:8515f205483b | 27 | { |
pietor | 7:8515f205483b | 28 | current_state = setState; |
pietor | 7:8515f205483b | 29 | } |
pietor | 7:8515f205483b | 30 | |
pietor | 7:8515f205483b | 31 | |
pietor | 7:8515f205483b | 32 | State2 getCurrentState() |
pietor | 7:8515f205483b | 33 | { |
pietor | 7:8515f205483b | 34 | return current_state; |
pietor | 7:8515f205483b | 35 | } |
pietor | 7:8515f205483b | 36 | |
akashvibhute | 2:608cf8c5c55e | 37 | int main() |
akashvibhute | 0:3982c0e9eda1 | 38 | { |
pietor | 10:0a4c465f7838 | 39 | pc.printf("\r\nOntvanger: \r\n"); |
pietor | 10:0a4c465f7838 | 40 | pc.printf("testing Radio... %d", receive.isValid()); |
pietor | 10:0a4c465f7838 | 41 | pc.printf("testing Carrier... %d", receive.testRPD()); |
pietor | 10:0a4c465f7838 | 42 | |
pietor | 10:0a4c465f7838 | 43 | |
pietor | 4:7ebc3b421306 | 44 | while(1) { |
pietor | 10:0a4c465f7838 | 45 | receive.update(); |
pietor | 7:8515f205483b | 46 | switch (current_state) { |
pietor | 7:8515f205483b | 47 | case init_state: |
pietor | 7:8515f205483b | 48 | pc.baud(9600); |
pietor | 7:8515f205483b | 49 | t.start(); |
pietor | 7:8515f205483b | 50 | state_Packet packet; |
pietor | 7:8515f205483b | 51 | payload_t payload; |
pietor | 7:8515f205483b | 52 | setCurrentState(receive_state); |
pietor | 7:8515f205483b | 53 | break; |
pietor | 7:8515f205483b | 54 | |
pietor | 7:8515f205483b | 55 | case send_state: |
pietor | 9:e14f65b8ffc2 | 56 | pc.printf("Send State\n\r"); |
pietor | 7:8515f205483b | 57 | packet.setState = State_position; |
pietor | 7:8515f205483b | 58 | bool ok = receive.write(packet); |
pietor | 7:8515f205483b | 59 | if (ok) |
pietor | 7:8515f205483b | 60 | pc.printf("ok.\n\r"); |
pietor | 7:8515f205483b | 61 | else |
pietor | 7:8515f205483b | 62 | pc.printf("failed.\n\r"); |
pietor | 7:8515f205483b | 63 | wait(0.1); |
pietor | 7:8515f205483b | 64 | setCurrentState(receive_state); |
pietor | 7:8515f205483b | 65 | break; |
pietor | 7:8515f205483b | 66 | |
pietor | 7:8515f205483b | 67 | case receive_state: |
pietor | 7:8515f205483b | 68 | pc.printf("");//print niet weg doen, één of andere reden werkt het dan wel |
pietor | 7:8515f205483b | 69 | if (receive.available()) { |
pietor | 7:8515f205483b | 70 | payload_t payload; |
pietor | 7:8515f205483b | 71 | payload = receive.read(); |
pietor | 8:c43411433ce1 | 72 | pc.printf("%f\r\n", payload.milligram); |
pietor | 7:8515f205483b | 73 | if (payload.reedsensor) { |
pietor | 7:8515f205483b | 74 | acitvateLed(); |
pietor | 7:8515f205483b | 75 | } |
pietor | 7:8515f205483b | 76 | } |
pietor | 7:8515f205483b | 77 | |
pietor | 9:e14f65b8ffc2 | 78 | if (t.read() <= 1){ |
pietor | 7:8515f205483b | 79 | myled1 = myled2 = myled3 = myled4 = 1; |
pietor | 9:e14f65b8ffc2 | 80 | } |
pietor | 7:8515f205483b | 81 | else |
pietor | 7:8515f205483b | 82 | myled1 = myled2 = myled3 = myled4 = 0; |
pietor | 7:8515f205483b | 83 | |
pietor | 7:8515f205483b | 84 | if(pc.readable()) { |
pietor | 7:8515f205483b | 85 | char c = pc.getc(); |
pietor | 7:8515f205483b | 86 | if(c == 's') |
pietor | 7:8515f205483b | 87 | setCurrentState(send_state); |
pietor | 7:8515f205483b | 88 | wait(0.5); |
pietor | 7:8515f205483b | 89 | } |
pietor | 7:8515f205483b | 90 | break; |
akashvibhute | 0:3982c0e9eda1 | 91 | } |
akashvibhute | 0:3982c0e9eda1 | 92 | } |
akashvibhute | 0:3982c0e9eda1 | 93 | } |