Display zeigt merkwürdige Zeichen
Dependencies: mbed PinDetect TextLCD
Fork of FWFSU_Start_28102013 by
main.cpp@14:90b2af48767a, 2016-07-27 (annotated)
- Committer:
- manuelfederanko
- Date:
- Wed Jul 27 06:44:43 2016 +0000
- Revision:
- 14:90b2af48767a
- Parent:
- 13:a45461fbb77f
- Child:
- 15:16d4e268cb84
Changed the sample rate from 20ms to 1ms. This caused inaccuracies with the measured time.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
manuelfederanko | 13:a45461fbb77f | 1 | #include "common.h" |
joca89 | 0:097b334e1077 | 2 | |
manuelfederanko | 13:a45461fbb77f | 3 | #define PING_TIMEOUT 50000 //5 seconds |
joca89 | 0:097b334e1077 | 4 | |
manuelfederanko | 13:a45461fbb77f | 5 | /** |
manuelfederanko | 13:a45461fbb77f | 6 | * Controls the button presses: |
manuelfederanko | 13:a45461fbb77f | 7 | * 1st press: The starttime gets set and the counter begins to run |
manuelfederanko | 13:a45461fbb77f | 8 | * 2nd press: The first snapshot gets taken and displayed |
manuelfederanko | 13:a45461fbb77f | 9 | * 3rd press: The second snapshot gets taken and displayed |
manuelfederanko | 13:a45461fbb77f | 10 | * 4th press: The stoptime gets taken, the overall time is displayed |
manuelfederanko | 13:a45461fbb77f | 11 | * The Stop can also occure when received from the Stopp button over the rf modul. |
manuelfederanko | 13:a45461fbb77f | 12 | * This does not require the snapshots to be taken. |
manuelfederanko | 13:a45461fbb77f | 13 | */ |
manuelfederanko | 13:a45461fbb77f | 14 | void btn_control() { |
manuelfederanko | 13:a45461fbb77f | 15 | char nbuff[9]; |
manuelfederanko | 13:a45461fbb77f | 16 | btn_cnt++; |
manuelfederanko | 13:a45461fbb77f | 17 | switch (btn_cnt) { |
manuelfederanko | 13:a45461fbb77f | 18 | case 0: |
manuelfederanko | 13:a45461fbb77f | 19 | |
manuelfederanko | 13:a45461fbb77f | 20 | break; |
manuelfederanko | 13:a45461fbb77f | 21 | case 1: |
manuelfederanko | 13:a45461fbb77f | 22 | start_tm = systime; |
manuelfederanko | 13:a45461fbb77f | 23 | comm_flags |= FLAG_START; |
manuelfederanko | 13:a45461fbb77f | 24 | int_to_str(start_tm, nbuff); |
manuelfederanko | 13:a45461fbb77f | 25 | ser.sendCode(MSG_CODE_TMS, nbuff); |
manuelfederanko | 13:a45461fbb77f | 26 | break; |
manuelfederanko | 13:a45461fbb77f | 27 | case 2: |
manuelfederanko | 13:a45461fbb77f | 28 | snapshot0 = systime; |
manuelfederanko | 13:a45461fbb77f | 29 | comm_flags |= FLAG_SNAP_0; |
manuelfederanko | 13:a45461fbb77f | 30 | int_to_str(snapshot0, nbuff); |
manuelfederanko | 13:a45461fbb77f | 31 | ser.sendCode(MSG_CODE_TML0, nbuff); |
manuelfederanko | 13:a45461fbb77f | 32 | break; |
manuelfederanko | 13:a45461fbb77f | 33 | case 3: |
manuelfederanko | 13:a45461fbb77f | 34 | snapshot1 = systime; |
manuelfederanko | 13:a45461fbb77f | 35 | comm_flags |= FLAG_SNAP_1; |
manuelfederanko | 13:a45461fbb77f | 36 | int_to_str(snapshot1, nbuff); |
manuelfederanko | 13:a45461fbb77f | 37 | ser.sendCode(MSG_CODE_TML1, nbuff); |
manuelfederanko | 13:a45461fbb77f | 38 | break; |
manuelfederanko | 13:a45461fbb77f | 39 | case 4: |
manuelfederanko | 13:a45461fbb77f | 40 | end_tm = systime; |
manuelfederanko | 13:a45461fbb77f | 41 | comm_flags |= FLAG_STOP; |
manuelfederanko | 13:a45461fbb77f | 42 | int_to_str(end_tm, nbuff); |
manuelfederanko | 13:a45461fbb77f | 43 | ser.sendCode(MSG_CODE_TMP, nbuff); |
manuelfederanko | 13:a45461fbb77f | 44 | break; |
manuelfederanko | 13:a45461fbb77f | 45 | case 5: |
manuelfederanko | 13:a45461fbb77f | 46 | btn_cnt--; //since we increment first and then check, it is perfectly ok to decrement to 4 |
manuelfederanko | 13:a45461fbb77f | 47 | break; |
manuelfederanko | 13:a45461fbb77f | 48 | } |
manuelfederanko | 13:a45461fbb77f | 49 | ser.flush(); |
joca89 | 0:097b334e1077 | 50 | } |
joca89 | 0:097b334e1077 | 51 | |
manuelfederanko | 13:a45461fbb77f | 52 | /** |
manuelfederanko | 13:a45461fbb77f | 53 | * Ping the other device. The Stop-Button must already fully be booted in order for this to |
manuelfederanko | 13:a45461fbb77f | 54 | * work, since only then does it start to check for ping requests. |
manuelfederanko | 13:a45461fbb77f | 55 | * For this to work the rf module must first be configured, obviously. |
manuelfederanko | 13:a45461fbb77f | 56 | */ |
manuelfederanko | 13:a45461fbb77f | 57 | void ping() { |
manuelfederanko | 13:a45461fbb77f | 58 | time_100us start; |
manuelfederanko | 13:a45461fbb77f | 59 | tcode code; |
manuelfederanko | 13:a45461fbb77f | 60 | char p_resp[10]; |
manuelfederanko | 13:a45461fbb77f | 61 | char rbuff[MSG_BUFFSIZE]; |
manuelfederanko | 13:a45461fbb77f | 62 | |
manuelfederanko | 13:a45461fbb77f | 63 | //notify usr |
manuelfederanko | 13:a45461fbb77f | 64 | cprintf("pinging..."); |
manuelfederanko | 13:a45461fbb77f | 65 | display(); |
manuelfederanko | 13:a45461fbb77f | 66 | |
manuelfederanko | 13:a45461fbb77f | 67 | //send ping and wait for resp |
manuelfederanko | 13:a45461fbb77f | 68 | start = systime; |
manuelfederanko | 13:a45461fbb77f | 69 | ser.sendCode(MSG_CODE_PING, "g"); |
manuelfederanko | 13:a45461fbb77f | 70 | ser.flush(); |
manuelfederanko | 13:a45461fbb77f | 71 | while(!ser.codeAvailable() && systime - start < RATE_SHOW_INFO); |
manuelfederanko | 13:a45461fbb77f | 72 | |
manuelfederanko | 13:a45461fbb77f | 73 | //handle |
manuelfederanko | 13:a45461fbb77f | 74 | if (ser.codeAvailable()) { |
manuelfederanko | 13:a45461fbb77f | 75 | pin = systime - start; |
manuelfederanko | 13:a45461fbb77f | 76 | pin = (int) (pin * PING_ERR_COMP); // should be to 10ms accurate |
manuelfederanko | 13:a45461fbb77f | 77 | ser.skipToCode(); |
manuelfederanko | 13:a45461fbb77f | 78 | ser.readCode(&code, rbuff); |
manuelfederanko | 13:a45461fbb77f | 79 | |
manuelfederanko | 13:a45461fbb77f | 80 | if (code != MSG_CODE_PING || rbuff[0] != 'r') { |
manuelfederanko | 13:a45461fbb77f | 81 | pin = 0; |
manuelfederanko | 13:a45461fbb77f | 82 | cprintf("internal error"); |
manuelfederanko | 13:a45461fbb77f | 83 | } else { |
manuelfederanko | 13:a45461fbb77f | 84 | p_resp[0] = 'p'; |
manuelfederanko | 13:a45461fbb77f | 85 | int_to_str(pin, &p_resp[1]); |
manuelfederanko | 13:a45461fbb77f | 86 | ser.sendCode(MSG_CODE_PING, p_resp); |
manuelfederanko | 13:a45461fbb77f | 87 | ser.flush(); |
manuelfederanko | 13:a45461fbb77f | 88 | cprintf("ping: %de2us", pin); |
fox46 | 11:3d21939eb9bd | 89 | } |
manuelfederanko | 13:a45461fbb77f | 90 | } else { |
manuelfederanko | 13:a45461fbb77f | 91 | cprintf("couldn't reach\ndevice"); |
manuelfederanko | 13:a45461fbb77f | 92 | display(); |
manuelfederanko | 13:a45461fbb77f | 93 | wait(WAIT_INFO_TM); |
manuelfederanko | 13:a45461fbb77f | 94 | cprintf("please turn on\nstop first"); |
manuelfederanko | 13:a45461fbb77f | 95 | display(); |
manuelfederanko | 13:a45461fbb77f | 96 | wait(WAIT_INFO_TM); |
manuelfederanko | 13:a45461fbb77f | 97 | shutdown_seq(); |
fox46 | 11:3d21939eb9bd | 98 | } |
manuelfederanko | 13:a45461fbb77f | 99 | |
manuelfederanko | 13:a45461fbb77f | 100 | display(); |
manuelfederanko | 13:a45461fbb77f | 101 | wait(WAIT_INFO_TM); |
fox46 | 4:165fc296f7f5 | 102 | } |
fox46 | 7:016281ea7be6 | 103 | |
manuelfederanko | 13:a45461fbb77f | 104 | /** |
manuelfederanko | 13:a45461fbb77f | 105 | * Receive routine for serial data. |
manuelfederanko | 13:a45461fbb77f | 106 | * Must be attatched to the MsgSerial object. |
manuelfederanko | 13:a45461fbb77f | 107 | */ |
manuelfederanko | 13:a45461fbb77f | 108 | void hdl_rec( void ) { |
manuelfederanko | 13:a45461fbb77f | 109 | tcode rec_code; |
manuelfederanko | 13:a45461fbb77f | 110 | char rec_buff[MSG_BUFFSIZE]; |
manuelfederanko | 13:a45461fbb77f | 111 | |
manuelfederanko | 13:a45461fbb77f | 112 | if (ser.codeAvailable()) { |
manuelfederanko | 13:a45461fbb77f | 113 | ser.skipToCode(); |
manuelfederanko | 13:a45461fbb77f | 114 | ser.readCode(&rec_code, rec_buff); |
fox46 | 12:3f53322a5270 | 115 | |
manuelfederanko | 13:a45461fbb77f | 116 | switch(rec_code) { |
manuelfederanko | 13:a45461fbb77f | 117 | case MSG_CODE_KILL: |
manuelfederanko | 13:a45461fbb77f | 118 | shutdown_seq(); //the other device will already be shut down when we send the kill signal again, thus it doesn't matter if we do. |
manuelfederanko | 13:a45461fbb77f | 119 | break; |
manuelfederanko | 13:a45461fbb77f | 120 | case MSG_CODE_PING: |
manuelfederanko | 13:a45461fbb77f | 121 | break; |
manuelfederanko | 13:a45461fbb77f | 122 | case MSG_CODE_RESET: |
manuelfederanko | 13:a45461fbb77f | 123 | reset(); |
manuelfederanko | 13:a45461fbb77f | 124 | break; |
manuelfederanko | 13:a45461fbb77f | 125 | case MSG_CODE_START: |
manuelfederanko | 13:a45461fbb77f | 126 | // NOTHING |
manuelfederanko | 13:a45461fbb77f | 127 | break; |
manuelfederanko | 13:a45461fbb77f | 128 | case MSG_CODE_STOP: |
manuelfederanko | 13:a45461fbb77f | 129 | //pin is the time for a two way signal, if it just needs to travel from 1 device to the other, one must half it for the correct result |
manuelfederanko | 14:90b2af48767a | 130 | //end_tm = systime - pin; |
manuelfederanko | 14:90b2af48767a | 131 | end_tm = systime; |
manuelfederanko | 13:a45461fbb77f | 132 | comm_flags |= FLAG_STOP; |
manuelfederanko | 14:90b2af48767a | 133 | int_to_str(end_tm, rec_buff); |
manuelfederanko | 14:90b2af48767a | 134 | ser.sendCode(MSG_CODE_TMP, rec_buff); |
manuelfederanko | 14:90b2af48767a | 135 | ser.flush(); |
manuelfederanko | 13:a45461fbb77f | 136 | btn_cnt = 5; //otherwise we could press stop again and the end_tm value would be replaced by systime once again |
manuelfederanko | 13:a45461fbb77f | 137 | break; |
manuelfederanko | 13:a45461fbb77f | 138 | case MSG_CODE_TOTAL_RESET: |
manuelfederanko | 13:a45461fbb77f | 139 | // NOTHING |
manuelfederanko | 13:a45461fbb77f | 140 | break; |
manuelfederanko | 13:a45461fbb77f | 141 | default: |
manuelfederanko | 13:a45461fbb77f | 142 | rec_code = 0; |
manuelfederanko | 13:a45461fbb77f | 143 | rec_buff[0] = 0; |
manuelfederanko | 13:a45461fbb77f | 144 | break; |
fox46 | 11:3d21939eb9bd | 145 | } |
fox46 | 11:3d21939eb9bd | 146 | } |
fox46 | 12:3f53322a5270 | 147 | } |
joca89 | 0:097b334e1077 | 148 | |
manuelfederanko | 13:a45461fbb77f | 149 | /** |
manuelfederanko | 13:a45461fbb77f | 150 | * Synchronize with the slave |
manuelfederanko | 13:a45461fbb77f | 151 | */ |
manuelfederanko | 13:a45461fbb77f | 152 | void resync( void ) { |
manuelfederanko | 13:a45461fbb77f | 153 | ser.sendCode(MSG_CODE_TOTAL_RESET, NULL); |
manuelfederanko | 13:a45461fbb77f | 154 | ser.flush(); |
manuelfederanko | 13:a45461fbb77f | 155 | btn_cnt = 0; |
manuelfederanko | 13:a45461fbb77f | 156 | comm_flags = 0; |
manuelfederanko | 13:a45461fbb77f | 157 | buzzer_state = 0; |
manuelfederanko | 13:a45461fbb77f | 158 | button_state = 0; |
manuelfederanko | 13:a45461fbb77f | 159 | D = 1; |
manuelfederanko | 13:a45461fbb77f | 160 | } |
manuelfederanko | 13:a45461fbb77f | 161 | |
manuelfederanko | 13:a45461fbb77f | 162 | void bzr_pressed( void ) { |
manuelfederanko | 13:a45461fbb77f | 163 | btn_control(); |
manuelfederanko | 13:a45461fbb77f | 164 | } |
fox46 | 6:79bc3888c88c | 165 | |
manuelfederanko | 13:a45461fbb77f | 166 | void bzr_held_pressed( void ) { |
manuelfederanko | 13:a45461fbb77f | 167 | reset(); |
manuelfederanko | 13:a45461fbb77f | 168 | ser.sendCode(MSG_CODE_RESET, NULL); |
manuelfederanko | 13:a45461fbb77f | 169 | ser.flush(); |
manuelfederanko | 13:a45461fbb77f | 170 | } |
manuelfederanko | 13:a45461fbb77f | 171 | |
manuelfederanko | 13:a45461fbb77f | 172 | void btn_pressed( void ) { |
manuelfederanko | 13:a45461fbb77f | 173 | resync(); |
manuelfederanko | 13:a45461fbb77f | 174 | } |
manuelfederanko | 13:a45461fbb77f | 175 | |
manuelfederanko | 13:a45461fbb77f | 176 | void btn_held_released( void ) { |
manuelfederanko | 13:a45461fbb77f | 177 | shutdown_seq(); |
manuelfederanko | 13:a45461fbb77f | 178 | } |
manuelfederanko | 13:a45461fbb77f | 179 | |
manuelfederanko | 13:a45461fbb77f | 180 | int main() { |
manuelfederanko | 13:a45461fbb77f | 181 | //| Element 0 | Element 1 | Element 2 | Element 3 | |
manuelfederanko | 13:a45461fbb77f | 182 | //|-----------|-----------|-----------|-----------| |
manuelfederanko | 13:a45461fbb77f | 183 | //| Akkucheck | LED blink | Disp-Rate | Btn. Rate | |
manuelfederanko | 13:a45461fbb77f | 184 | time_100us prev[5]; |
fox46 | 12:3f53322a5270 | 185 | |
manuelfederanko | 13:a45461fbb77f | 186 | //init |
manuelfederanko | 13:a45461fbb77f | 187 | init_io(); |
manuelfederanko | 13:a45461fbb77f | 188 | init_values(); |
manuelfederanko | 13:a45461fbb77f | 189 | tick.attach_us(&handle_systime, 100); //every 100 us |
manuelfederanko | 13:a45461fbb77f | 190 | pb1.attach_asserted(&bzr_pressed); |
manuelfederanko | 13:a45461fbb77f | 191 | pb1.attach_asserted_held(&bzr_held_pressed); |
manuelfederanko | 14:90b2af48767a | 192 | pb1.setSamplesTillHeld(2000); //2000 samples ---> 1ms ---> 2 seconds |
manuelfederanko | 14:90b2af48767a | 193 | pb1.setSampleFrequency(1000); //1ms |
manuelfederanko | 13:a45461fbb77f | 194 | |
manuelfederanko | 13:a45461fbb77f | 195 | pb2.attach_asserted(&btn_pressed); |
manuelfederanko | 13:a45461fbb77f | 196 | pb2.attach_deasserted_held(&btn_held_released); |
manuelfederanko | 13:a45461fbb77f | 197 | pb2.setSamplesTillHeld(200); |
manuelfederanko | 13:a45461fbb77f | 198 | pb2.setSampleFrequency(); |
manuelfederanko | 13:a45461fbb77f | 199 | |
manuelfederanko | 13:a45461fbb77f | 200 | //info |
manuelfederanko | 13:a45461fbb77f | 201 | cprintf(RUNMODE_SERVER); |
manuelfederanko | 13:a45461fbb77f | 202 | display(); |
manuelfederanko | 13:a45461fbb77f | 203 | wait(WAIT_INFO_TM); |
manuelfederanko | 13:a45461fbb77f | 204 | |
manuelfederanko | 13:a45461fbb77f | 205 | //config |
manuelfederanko | 13:a45461fbb77f | 206 | config_buttons(); |
manuelfederanko | 13:a45461fbb77f | 207 | config_rf_mod(); |
manuelfederanko | 13:a45461fbb77f | 208 | |
manuelfederanko | 13:a45461fbb77f | 209 | //start |
manuelfederanko | 13:a45461fbb77f | 210 | lowbatt(); |
manuelfederanko | 13:a45461fbb77f | 211 | ping(); |
manuelfederanko | 13:a45461fbb77f | 212 | ser.registerRecFc(&hdl_rec); |
manuelfederanko | 13:a45461fbb77f | 213 | resync(); |
manuelfederanko | 13:a45461fbb77f | 214 | |
manuelfederanko | 13:a45461fbb77f | 215 | prev[0] = prev[1] = prev[2] = prev[3] = prev[4] = systime; |
manuelfederanko | 13:a45461fbb77f | 216 | |
manuelfederanko | 13:a45461fbb77f | 217 | |
manuelfederanko | 13:a45461fbb77f | 218 | while(1) { |
manuelfederanko | 13:a45461fbb77f | 219 | if (systime - prev[2] >= RATE_DISPLAY) { |
manuelfederanko | 13:a45461fbb77f | 220 | prev[2] = systime; |
manuelfederanko | 13:a45461fbb77f | 221 | render_time(); |
manuelfederanko | 13:a45461fbb77f | 222 | display(); |
fox46 | 12:3f53322a5270 | 223 | } |
manuelfederanko | 13:a45461fbb77f | 224 | if (systime - prev[1] >= ((akku_state >= AKKU_1)? RATE_LED_NOK : RATE_LED_OK)) { |
manuelfederanko | 13:a45461fbb77f | 225 | prev[1] = systime; |
manuelfederanko | 13:a45461fbb77f | 226 | stoppled.write(!stoppled.read()); |
manuelfederanko | 13:a45461fbb77f | 227 | } |
manuelfederanko | 13:a45461fbb77f | 228 | if (comm_flags & FLAG_INFO_LOCK && systime - info_tm >= RATE_SHOW_INFO) { |
manuelfederanko | 13:a45461fbb77f | 229 | //if the flag is set it means, that we wanna show something and the time has been set --> if the difference bigger than the rate, then clear the flag |
manuelfederanko | 13:a45461fbb77f | 230 | //the flag gets checked in the render_time function, to prevent that from overwriting the info text |
manuelfederanko | 13:a45461fbb77f | 231 | comm_flags &= ~FLAG_INFO_LOCK; |
manuelfederanko | 13:a45461fbb77f | 232 | } |
manuelfederanko | 13:a45461fbb77f | 233 | if (systime - prev[0] >= RATE_CHECK_AKKU) { |
manuelfederanko | 13:a45461fbb77f | 234 | prev[0] = systime; |
manuelfederanko | 13:a45461fbb77f | 235 | akkucheck(); |
fox46 | 10:27b3acc95b8f | 236 | |
manuelfederanko | 13:a45461fbb77f | 237 | if (akku_state == AKKU_CRITICAL) { |
manuelfederanko | 13:a45461fbb77f | 238 | shutdown_seq(); |
fox46 | 12:3f53322a5270 | 239 | } |
fox46 | 11:3d21939eb9bd | 240 | } |
fox46 | 8:9eccc5d25e8f | 241 | } |
manuelfederanko | 13:a45461fbb77f | 242 | } |