![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
dd
Dependencies: xtoff2 RF24Network mbed
Fork of xtoff3 by
main.cpp@13:f54dfb74247e, 2018-03-21 (annotated)
- Committer:
- pietor
- Date:
- Wed Mar 21 16:22:28 2018 +0000
- Revision:
- 13:f54dfb74247e
- Parent:
- 12:a3dd2ebf8f93
- Child:
- 14:3e69c8a9c730
Werkend 21/03: Added Messages
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 | 12:a3dd2ebf8f93 | 6 | |
pietor | 11:77018fac195e | 7 | RF24 PINOUT: |
pietor | 11:77018fac195e | 8 | ---- ----- |
pietor | 12:a3dd2ebf8f93 | 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 | 12:a3dd2ebf8f93 | 15 | |
pietor | 11:77018fac195e | 16 | ---- ----- |
pietor | 12:a3dd2ebf8f93 | 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 | 12:a3dd2ebf8f93 | 22 | ---- ----- |
pietor | 4:7ebc3b421306 | 23 | */ |
pietor | 11:77018fac195e | 24 | //uncommend #define PRINT_ENABLE to see informational prints |
pietor | 13:f54dfb74247e | 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 | 12:a3dd2ebf8f93 | 39 | char c; |
pietor | 12:a3dd2ebf8f93 | 40 | |
pietor | 7:8515f205483b | 41 | State2 current_state = init_state; |
pietor | 7:8515f205483b | 42 | |
pietor | 6:6a82110ff393 | 43 | void acitvateLed() |
pietor | 6:6a82110ff393 | 44 | { |
pietor | 6:6a82110ff393 | 45 | t.reset(); |
pietor | 6:6a82110ff393 | 46 | } |
pietor | 6:6a82110ff393 | 47 | |
pietor | 7:8515f205483b | 48 | void setCurrentState( State2 setState ) |
pietor | 7:8515f205483b | 49 | { |
pietor | 7:8515f205483b | 50 | current_state = setState; |
pietor | 7:8515f205483b | 51 | } |
pietor | 7:8515f205483b | 52 | |
pietor | 7:8515f205483b | 53 | |
pietor | 7:8515f205483b | 54 | State2 getCurrentState() |
pietor | 7:8515f205483b | 55 | { |
pietor | 7:8515f205483b | 56 | return current_state; |
pietor | 7:8515f205483b | 57 | } |
pietor | 7:8515f205483b | 58 | |
akashvibhute | 2:608cf8c5c55e | 59 | int main() |
akashvibhute | 0:3982c0e9eda1 | 60 | { |
pietor | 12:a3dd2ebf8f93 | 61 | IF_PRINT_ENABLE( pc.printf("\r\nOntvanger: \r\n"); |
pietor | 12:a3dd2ebf8f93 | 62 | pc.printf("testing Radio... %d\r\n", receive.isValid()); |
pietor | 12:a3dd2ebf8f93 | 63 | ); |
pietor | 12:a3dd2ebf8f93 | 64 | |
pietor | 4:7ebc3b421306 | 65 | while(1) { |
pietor | 10:0a4c465f7838 | 66 | receive.update(); |
pietor | 7:8515f205483b | 67 | switch (current_state) { |
pietor | 7:8515f205483b | 68 | case init_state: |
pietor | 12:a3dd2ebf8f93 | 69 | IF_PRINT_ENABLE(pc.printf("Init State\n\r");); |
pietor | 7:8515f205483b | 70 | pc.baud(9600); |
pietor | 7:8515f205483b | 71 | t.start(); |
pietor | 7:8515f205483b | 72 | state_Packet packet; |
pietor | 7:8515f205483b | 73 | payload_t payload; |
pietor | 7:8515f205483b | 74 | setCurrentState(receive_state); |
pietor | 7:8515f205483b | 75 | break; |
pietor | 7:8515f205483b | 76 | |
pietor | 7:8515f205483b | 77 | case send_state: |
pietor | 11:77018fac195e | 78 | IF_PRINT_ENABLE(pc.printf("Send State\n\r");); |
pietor | 12:a3dd2ebf8f93 | 79 | if(c == 's') { |
pietor | 12:a3dd2ebf8f93 | 80 | IF_PRINT_ENABLE(pc.printf("Send state: Tare\n\r");); |
pietor | 13:f54dfb74247e | 81 | packet.setState = 's'; |
pietor | 13:f54dfb74247e | 82 | } |
pietor | 13:f54dfb74247e | 83 | if (c == 'c') { |
pietor | 12:a3dd2ebf8f93 | 84 | IF_PRINT_ENABLE(pc.printf("Send state: Calibrate\n\r");); |
pietor | 13:f54dfb74247e | 85 | packet.setState = 'c'; |
pietor | 12:a3dd2ebf8f93 | 86 | } |
pietor | 7:8515f205483b | 87 | bool ok = receive.write(packet); |
pietor | 7:8515f205483b | 88 | if (ok) |
pietor | 11:77018fac195e | 89 | IF_PRINT_ENABLE(pc.printf("ok.\n\r");); |
pietor | 7:8515f205483b | 90 | else |
pietor | 11:77018fac195e | 91 | IF_PRINT_ENABLE(pc.printf("failed.\n\r");); |
pietor | 7:8515f205483b | 92 | wait(0.1); |
pietor | 7:8515f205483b | 93 | setCurrentState(receive_state); |
pietor | 7:8515f205483b | 94 | break; |
pietor | 7:8515f205483b | 95 | |
pietor | 7:8515f205483b | 96 | case receive_state: |
pietor | 12:a3dd2ebf8f93 | 97 | pc.printf("");//print niet weg doen, één of andere reden werkt het niet zonder |
pietor | 7:8515f205483b | 98 | if (receive.available()) { |
pietor | 7:8515f205483b | 99 | payload_t payload; |
pietor | 7:8515f205483b | 100 | payload = receive.read(); |
pietor | 13:f54dfb74247e | 101 | if(!payload.messageAvailable) { |
pietor | 13:f54dfb74247e | 102 | pc.printf("%f\r\n", payload.gram); |
pietor | 13:f54dfb74247e | 103 | if (payload.reedsensor) { |
pietor | 13:f54dfb74247e | 104 | acitvateLed(); |
pietor | 13:f54dfb74247e | 105 | } |
pietor | 7:8515f205483b | 106 | } |
pietor | 7:8515f205483b | 107 | } |
pietor | 7:8515f205483b | 108 | |
pietor | 12:a3dd2ebf8f93 | 109 | if (t.read() <= 1) { |
pietor | 7:8515f205483b | 110 | myled1 = myled2 = myled3 = myled4 = 1; |
pietor | 12:a3dd2ebf8f93 | 111 | } else |
pietor | 7:8515f205483b | 112 | myled1 = myled2 = myled3 = myled4 = 0; |
pietor | 7:8515f205483b | 113 | |
pietor | 7:8515f205483b | 114 | if(pc.readable()) { |
pietor | 12:a3dd2ebf8f93 | 115 | c = pc.getc(); |
pietor | 12:a3dd2ebf8f93 | 116 | if(c == 's' or c == 'c') |
pietor | 7:8515f205483b | 117 | setCurrentState(send_state); |
pietor | 7:8515f205483b | 118 | wait(0.5); |
pietor | 7:8515f205483b | 119 | } |
pietor | 7:8515f205483b | 120 | break; |
akashvibhute | 0:3982c0e9eda1 | 121 | } |
akashvibhute | 0:3982c0e9eda1 | 122 | } |
akashvibhute | 0:3982c0e9eda1 | 123 | } |