![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
dd
Dependencies: xtoff2 RF24Network mbed
Fork of xtoff3 by
main.cpp@14:3e69c8a9c730, 2018-07-12 (annotated)
- Committer:
- pietor
- Date:
- Thu Jul 12 08:56:54 2018 +0000
- Revision:
- 14:3e69c8a9c730
- Parent:
- 13:f54dfb74247e
- Child:
- 15:f8aad6d1db68
f
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 | 14:3e69c8a9c730 | 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 | 14:3e69c8a9c730 | 61 | IF_PRINT_ENABLE( pc.printf("\r\nVOntvanger: \r\n"); |
pietor | 14:3e69c8a9c730 | 62 | pc.printf("Vtesting 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 | 14:3e69c8a9c730 | 69 | IF_PRINT_ENABLE(pc.printf("XInit 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 | 14:3e69c8a9c730 | 74 | setCurrentState(testConnection_state); |
pietor | 14:3e69c8a9c730 | 75 | break; |
pietor | 14:3e69c8a9c730 | 76 | |
pietor | 14:3e69c8a9c730 | 77 | case testConnection_state: |
pietor | 14:3e69c8a9c730 | 78 | IF_PRINT_ENABLE(pc.printf("XTesting State\n\r");); |
pietor | 14:3e69c8a9c730 | 79 | packet.setState = 't'; |
pietor | 14:3e69c8a9c730 | 80 | |
pietor | 14:3e69c8a9c730 | 81 | if (receive.write(packet)){ |
pietor | 14:3e69c8a9c730 | 82 | pc.printf("VConnection test passed.\n\r"); |
pietor | 14:3e69c8a9c730 | 83 | setCurrentState(receive_state); |
pietor | 14:3e69c8a9c730 | 84 | } |
pietor | 14:3e69c8a9c730 | 85 | else |
pietor | 14:3e69c8a9c730 | 86 | IF_PRINT_ENABLE(pc.printf("VConnection failed, trying again...\n\r");); |
pietor | 14:3e69c8a9c730 | 87 | |
pietor | 14:3e69c8a9c730 | 88 | wait_ms(500); |
pietor | 7:8515f205483b | 89 | break; |
pietor | 7:8515f205483b | 90 | |
pietor | 7:8515f205483b | 91 | case send_state: |
pietor | 14:3e69c8a9c730 | 92 | IF_PRINT_ENABLE(pc.printf("SSend State\n\r");); |
pietor | 12:a3dd2ebf8f93 | 93 | if(c == 's') { |
pietor | 14:3e69c8a9c730 | 94 | IF_PRINT_ENABLE(pc.printf("VSend state: Tare\n\r");); |
pietor | 13:f54dfb74247e | 95 | packet.setState = 's'; |
pietor | 13:f54dfb74247e | 96 | } |
pietor | 13:f54dfb74247e | 97 | if (c == 'c') { |
pietor | 14:3e69c8a9c730 | 98 | IF_PRINT_ENABLE(pc.printf("VSend state: Calibrate\n\r");); |
pietor | 13:f54dfb74247e | 99 | packet.setState = 'c'; |
pietor | 12:a3dd2ebf8f93 | 100 | } |
pietor | 7:8515f205483b | 101 | bool ok = receive.write(packet); |
pietor | 7:8515f205483b | 102 | if (ok) |
pietor | 14:3e69c8a9c730 | 103 | IF_PRINT_ENABLE(pc.printf("Vok.\n\r");); |
pietor | 7:8515f205483b | 104 | else |
pietor | 14:3e69c8a9c730 | 105 | IF_PRINT_ENABLE(pc.printf("Vfailed.\n\r");); |
pietor | 7:8515f205483b | 106 | wait(0.1); |
pietor | 7:8515f205483b | 107 | setCurrentState(receive_state); |
pietor | 7:8515f205483b | 108 | break; |
pietor | 7:8515f205483b | 109 | |
pietor | 7:8515f205483b | 110 | case receive_state: |
pietor | 12:a3dd2ebf8f93 | 111 | pc.printf("");//print niet weg doen, één of andere reden werkt het niet zonder |
pietor | 7:8515f205483b | 112 | if (receive.available()) { |
pietor | 7:8515f205483b | 113 | payload_t payload; |
pietor | 7:8515f205483b | 114 | payload = receive.read(); |
pietor | 13:f54dfb74247e | 115 | if(!payload.messageAvailable) { |
pietor | 13:f54dfb74247e | 116 | pc.printf("%f\r\n", payload.gram); |
pietor | 13:f54dfb74247e | 117 | if (payload.reedsensor) { |
pietor | 13:f54dfb74247e | 118 | acitvateLed(); |
pietor | 13:f54dfb74247e | 119 | } |
pietor | 7:8515f205483b | 120 | } |
pietor | 7:8515f205483b | 121 | } |
pietor | 7:8515f205483b | 122 | |
pietor | 12:a3dd2ebf8f93 | 123 | if (t.read() <= 1) { |
pietor | 7:8515f205483b | 124 | myled1 = myled2 = myled3 = myled4 = 1; |
pietor | 12:a3dd2ebf8f93 | 125 | } else |
pietor | 7:8515f205483b | 126 | myled1 = myled2 = myled3 = myled4 = 0; |
pietor | 7:8515f205483b | 127 | |
pietor | 7:8515f205483b | 128 | if(pc.readable()) { |
pietor | 12:a3dd2ebf8f93 | 129 | c = pc.getc(); |
pietor | 12:a3dd2ebf8f93 | 130 | if(c == 's' or c == 'c') |
pietor | 7:8515f205483b | 131 | setCurrentState(send_state); |
pietor | 7:8515f205483b | 132 | wait(0.5); |
pietor | 7:8515f205483b | 133 | } |
pietor | 7:8515f205483b | 134 | break; |
akashvibhute | 0:3982c0e9eda1 | 135 | } |
akashvibhute | 0:3982c0e9eda1 | 136 | } |
akashvibhute | 0:3982c0e9eda1 | 137 | } |