Display zeigt merkwürdige Zeichen

Dependencies:   mbed PinDetect TextLCD

Fork of FWFSU_Start_28102013 by KX-ONE

Committer:
manuelfederanko
Date:
Tue Jul 26 14:53:21 2016 +0000
Revision:
13:a45461fbb77f
Parent:
12:3f53322a5270
Child:
14:90b2af48767a
Reviewed code and fixed the inaccuracy between the times on both devices; Summary:;  + Added the class MsgSerial for easier transfer of data between devices.;  + Added the files common.h and common.cpp which contain functionality needed in both devices.

Who changed what in which revision?

UserRevisionLine numberNew 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 13:a45461fbb77f 130 end_tm = systime - pin;
manuelfederanko 13:a45461fbb77f 131 comm_flags |= FLAG_STOP;
manuelfederanko 13:a45461fbb77f 132 btn_cnt = 5; //otherwise we could press stop again and the end_tm value would be replaced by systime once again
manuelfederanko 13:a45461fbb77f 133 break;
manuelfederanko 13:a45461fbb77f 134 case MSG_CODE_TOTAL_RESET:
manuelfederanko 13:a45461fbb77f 135 // NOTHING
manuelfederanko 13:a45461fbb77f 136 break;
manuelfederanko 13:a45461fbb77f 137 default:
manuelfederanko 13:a45461fbb77f 138 rec_code = 0;
manuelfederanko 13:a45461fbb77f 139 rec_buff[0] = 0;
manuelfederanko 13:a45461fbb77f 140 break;
fox46 11:3d21939eb9bd 141 }
fox46 11:3d21939eb9bd 142 }
fox46 12:3f53322a5270 143 }
joca89 0:097b334e1077 144
manuelfederanko 13:a45461fbb77f 145 /**
manuelfederanko 13:a45461fbb77f 146 * Synchronize with the slave
manuelfederanko 13:a45461fbb77f 147 */
manuelfederanko 13:a45461fbb77f 148 void resync( void ) {
manuelfederanko 13:a45461fbb77f 149 ser.sendCode(MSG_CODE_TOTAL_RESET, NULL);
manuelfederanko 13:a45461fbb77f 150 ser.flush();
manuelfederanko 13:a45461fbb77f 151 btn_cnt = 0;
manuelfederanko 13:a45461fbb77f 152 comm_flags = 0;
manuelfederanko 13:a45461fbb77f 153 buzzer_state = 0;
manuelfederanko 13:a45461fbb77f 154 button_state = 0;
manuelfederanko 13:a45461fbb77f 155 D = 1;
manuelfederanko 13:a45461fbb77f 156 }
manuelfederanko 13:a45461fbb77f 157
manuelfederanko 13:a45461fbb77f 158 void bzr_pressed( void ) {
manuelfederanko 13:a45461fbb77f 159 btn_control();
manuelfederanko 13:a45461fbb77f 160 }
fox46 6:79bc3888c88c 161
manuelfederanko 13:a45461fbb77f 162 void bzr_held_pressed( void ) {
manuelfederanko 13:a45461fbb77f 163 reset();
manuelfederanko 13:a45461fbb77f 164 ser.sendCode(MSG_CODE_RESET, NULL);
manuelfederanko 13:a45461fbb77f 165 ser.flush();
manuelfederanko 13:a45461fbb77f 166 }
manuelfederanko 13:a45461fbb77f 167
manuelfederanko 13:a45461fbb77f 168 void btn_pressed( void ) {
manuelfederanko 13:a45461fbb77f 169 resync();
manuelfederanko 13:a45461fbb77f 170 }
manuelfederanko 13:a45461fbb77f 171
manuelfederanko 13:a45461fbb77f 172 void btn_held_released( void ) {
manuelfederanko 13:a45461fbb77f 173 shutdown_seq();
manuelfederanko 13:a45461fbb77f 174 }
manuelfederanko 13:a45461fbb77f 175
manuelfederanko 13:a45461fbb77f 176 int main() {
manuelfederanko 13:a45461fbb77f 177 //| Element 0 | Element 1 | Element 2 | Element 3 |
manuelfederanko 13:a45461fbb77f 178 //|-----------|-----------|-----------|-----------|
manuelfederanko 13:a45461fbb77f 179 //| Akkucheck | LED blink | Disp-Rate | Btn. Rate |
manuelfederanko 13:a45461fbb77f 180 time_100us prev[5];
fox46 12:3f53322a5270 181
manuelfederanko 13:a45461fbb77f 182 //init
manuelfederanko 13:a45461fbb77f 183 init_io();
manuelfederanko 13:a45461fbb77f 184 init_values();
manuelfederanko 13:a45461fbb77f 185 tick.attach_us(&handle_systime, 100); //every 100 us
manuelfederanko 13:a45461fbb77f 186 pb1.attach_asserted(&bzr_pressed);
manuelfederanko 13:a45461fbb77f 187 pb1.attach_asserted_held(&bzr_held_pressed);
manuelfederanko 13:a45461fbb77f 188 pb1.setSampleFrequency();
manuelfederanko 13:a45461fbb77f 189
manuelfederanko 13:a45461fbb77f 190 pb2.attach_asserted(&btn_pressed);
manuelfederanko 13:a45461fbb77f 191 pb2.attach_deasserted_held(&btn_held_released);
manuelfederanko 13:a45461fbb77f 192 pb2.setSamplesTillHeld(200);
manuelfederanko 13:a45461fbb77f 193 pb2.setSampleFrequency();
manuelfederanko 13:a45461fbb77f 194
manuelfederanko 13:a45461fbb77f 195 //info
manuelfederanko 13:a45461fbb77f 196 cprintf(RUNMODE_SERVER);
manuelfederanko 13:a45461fbb77f 197 display();
manuelfederanko 13:a45461fbb77f 198 wait(WAIT_INFO_TM);
manuelfederanko 13:a45461fbb77f 199
manuelfederanko 13:a45461fbb77f 200 //config
manuelfederanko 13:a45461fbb77f 201 config_buttons();
manuelfederanko 13:a45461fbb77f 202 config_rf_mod();
manuelfederanko 13:a45461fbb77f 203
manuelfederanko 13:a45461fbb77f 204 //start
manuelfederanko 13:a45461fbb77f 205 lowbatt();
manuelfederanko 13:a45461fbb77f 206 ping();
manuelfederanko 13:a45461fbb77f 207 ser.registerRecFc(&hdl_rec);
manuelfederanko 13:a45461fbb77f 208 resync();
manuelfederanko 13:a45461fbb77f 209
manuelfederanko 13:a45461fbb77f 210 prev[0] = prev[1] = prev[2] = prev[3] = prev[4] = systime;
manuelfederanko 13:a45461fbb77f 211
manuelfederanko 13:a45461fbb77f 212
manuelfederanko 13:a45461fbb77f 213 while(1) {
manuelfederanko 13:a45461fbb77f 214 if (systime - prev[2] >= RATE_DISPLAY) {
manuelfederanko 13:a45461fbb77f 215 prev[2] = systime;
manuelfederanko 13:a45461fbb77f 216 render_time();
manuelfederanko 13:a45461fbb77f 217 display();
fox46 12:3f53322a5270 218 }
manuelfederanko 13:a45461fbb77f 219 if (systime - prev[1] >= ((akku_state >= AKKU_1)? RATE_LED_NOK : RATE_LED_OK)) {
manuelfederanko 13:a45461fbb77f 220 prev[1] = systime;
manuelfederanko 13:a45461fbb77f 221 stoppled.write(!stoppled.read());
manuelfederanko 13:a45461fbb77f 222 }
manuelfederanko 13:a45461fbb77f 223 if (comm_flags & FLAG_INFO_LOCK && systime - info_tm >= RATE_SHOW_INFO) {
manuelfederanko 13:a45461fbb77f 224 //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 225 //the flag gets checked in the render_time function, to prevent that from overwriting the info text
manuelfederanko 13:a45461fbb77f 226 comm_flags &= ~FLAG_INFO_LOCK;
manuelfederanko 13:a45461fbb77f 227 }
manuelfederanko 13:a45461fbb77f 228 if (systime - prev[0] >= RATE_CHECK_AKKU) {
manuelfederanko 13:a45461fbb77f 229 prev[0] = systime;
manuelfederanko 13:a45461fbb77f 230 akkucheck();
fox46 10:27b3acc95b8f 231
manuelfederanko 13:a45461fbb77f 232 if (akku_state == AKKU_CRITICAL) {
manuelfederanko 13:a45461fbb77f 233 shutdown_seq();
fox46 12:3f53322a5270 234 }
fox46 11:3d21939eb9bd 235 }
fox46 8:9eccc5d25e8f 236 }
manuelfederanko 13:a45461fbb77f 237 }