Display zeigt merkwürdige Zeichen

Dependencies:   mbed PinDetect TextLCD

Fork of FWFSU_Start_28102013 by KX-ONE

Revision:
19:a06d764e4f64
Parent:
17:ce5a80e8fc8e
Child:
20:e809ca4de724
--- a/main.cpp	Tue Aug 02 09:55:16 2016 +0000
+++ b/main.cpp	Thu Aug 04 10:56:41 2016 +0000
@@ -1,107 +1,10 @@
 #include "main.h"
 
-#define PING_TIMEOUT 50000 //5 seconds
+using namespace evtbus;
+using namespace evt;
 
 struct linked_list *times;
-
-/**
- * Controls the button presses:
- * 1st press: The starttime gets set and the counter begins to run
- * 2nd press: The first snapshot gets taken and displayed
- * 3rd press: The second snapshot gets taken and displayed
- * 4th press: The stoptime gets taken, the overall time is displayed
- * The Stop can also occure when received from the Stopp button over the rf modul.
- * This does not require the snapshots to be taken.
- */
-void btn_control() {
-    char nbuff[9];
-    btn_cnt++;
-    switch (btn_cnt) {
-        case 0:
-            
-            break;
-        case 1:
-            start_tm = systime;
-            comm_flags |= FLAG_START;
-            int_to_str(start_tm, nbuff);
-            ser.sendCode(MSG_CODE_TMS, nbuff);
-            break;
-        case 2:
-            snapshot0 = systime;
-            comm_flags |= FLAG_SNAP_0;
-            int_to_str(snapshot0, nbuff);
-            ser.sendCode(MSG_CODE_TML0, nbuff);
-            break;
-        case 3:
-            snapshot1 = systime;
-            comm_flags |= FLAG_SNAP_1;
-            int_to_str(snapshot1, nbuff);
-            ser.sendCode(MSG_CODE_TML1, nbuff);
-            break;
-        case 4:
-            end_tm = systime;
-            comm_flags |= FLAG_STOP;
-            int_to_str(end_tm, nbuff);
-            ser.sendCode(MSG_CODE_TMP, nbuff);
-            break;
-        case 5:
-            btn_cnt--; //since we increment first and then check, it is perfectly ok to decrement to 4
-            break;
-    }
-    ser.flush();
-}
-
-/**
- * Ping the other device. The Stop-Button must already fully be booted in order for this to
- * work, since only then does it start to check for ping requests.
- * For this to work the rf module must first be configured, obviously.
- */
-void ping() {
-    time_100us start;
-    tcode code;
-    char p_resp[10];
-    char rbuff[MSG_BUFFSIZE];
-    
-    //notify usr
-    cprintf("pinging...");
-    display();
-    
-    //send ping and wait for resp
-    start = systime;
-    ser.sendCode(MSG_CODE_PING, "g");
-    ser.flush();
-    while(!ser.codeAvailable() && systime - start < RATE_SHOW_INFO);
-    
-    //handle
-    if (ser.codeAvailable()) {
-        pin = systime - start;
-        pin = (int) (pin * PING_ERR_COMP); // should be to 10ms accurate
-        ser.skipToCode();
-        ser.readCode(&code, rbuff);
-        
-        if (code != MSG_CODE_PING || rbuff[0] != 'r') {
-            pin = 0;
-            cprintf("internal error");
-        } else {
-            p_resp[0] = 'p';
-            int_to_str(pin, &p_resp[1]);
-            ser.sendCode(MSG_CODE_PING, p_resp);
-            ser.flush();
-            cprintf("ping: %de2us", pin);
-        }
-    } else {
-        cprintf("couldn't reach\ndevice");
-        display();
-        wait(WAIT_INFO_TM);
-        cprintf("please turn on\nstop first");
-        display();
-        wait(WAIT_INFO_TM);
-        shutdown_seq();
-    }
-    
-    display();
-    wait(WAIT_INFO_TM);
-}
+EventBus ebus;
 
 /**
  * Synchronize with the slave
@@ -117,7 +20,7 @@
     
     if (times != NULL) {
         deleteList(times);
-        times = initNode();   
+        times = initNode();
     }
 }
 
@@ -132,76 +35,206 @@
     appendNode(times, initNode());
 }
 
-/**
- * Receive routine for serial data.
- * Must be attatched to the MsgSerial object.
- */
-void hdl_rec( void ) {
-    tcode rec_code;
-    char rec_buff[MSG_BUFFSIZE];
-        
-    if (ser.codeAvailable()) {
-        ser.skipToCode();
-        ser.readCode(&rec_code, rec_buff);
-        
-        switch(rec_code) {
-            case MSG_CODE_KILL:
-                deleteList(times);
-                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.
-                break;
-            case MSG_CODE_PING:
-                break;
-            case MSG_CODE_RESET:
-                reinit_list();
-                reset();
-                break;
-            case MSG_CODE_START:
-                // NOTHING
-                break;
-            case MSG_CODE_STOP:
-                //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
-                //end_tm = systime - pin;
-                if (comm_flags & FLAG_START) { //resetting and pressing stop right afterwards breaks the code, one would need to reset again to start the time.
-                    end_tm = systime;
-                    comm_flags |= FLAG_STOP;
-                    int_to_str(end_tm, rec_buff);
-                    ser.sendCode(MSG_CODE_TMP, rec_buff);
-                    ser.flush();
-                    btn_cnt = 5; //otherwise we could press stop again and the end_tm value would be replaced by systime once again
-                }
-                break;
-            case MSG_CODE_TOTAL_RESET:
-                // NOTHING
-                break;
-            case MSG_CODE_REQ_RES:
-                resync();
-                break;
-            default:
-                rec_code = 0;
-                rec_buff[0] = 0;
-                break;
-        }
-    }
-}
+/********************************************************************************************
+ INTERRUPT SERVICE ROUTINES (those raise all events!)
+ ********************************************************************************************/
 
 void bzr_pressed( void ) {
-    btn_control();
+    time_100us ts = systime;
+    ebus.raiseEvent(1, 0, ts, NULL);
 }
 
 void bzr_held_pressed( void ) {
-    reinit_list();
-    reset();
-    ser.sendCode(MSG_CODE_RESET, NULL);
-    ser.flush();
+    time_100us ts = systime;
+    ebus.raiseEvent(2, 0, ts, NULL);
 }
 
 void btn_pressed( void ) {
-    resync();
+    time_100us ts = systime;
+    ebus.raiseEvent(3, 0, ts, NULL);
 }
 
 void btn_held_released( void ) {
+    time_100us ts = systime;
+    ebus.raiseEvent(4, 0, ts, NULL);
+}
+
+void beam_pressed(void) {
+    time_100us ts = systime;
+    ebus.raiseEvent(5, 0, ts, NULL);
+}
+
+void hdl_rec( void ) {
+    time_100us ts = systime;
+    ebus.raiseEvent(6, 0, ts, NULL);
+}
+
+
+/********************************************************************************************
+ EVENT BUS FUNCTIONS
+ ********************************************************************************************/
+
+char handleEvent_01_BuzzerPressed(Event *e) {
+    if ((comm_flags & FLAG_START) == 0)
+        ebus.raiseEvent(7, 0, e->timestamp, NULL);
+    else if ((comm_flags & FLAG_SNAP_0) == 0)
+        ebus.raiseEvent(8, 0, e->timestamp, NULL);
+    else if ((comm_flags & FLAG_SNAP_1) == 0)
+        ebus.raiseEvent(9, 0, e->timestamp, NULL);
+    else if ((comm_flags & FLAG_STOP) == 0)
+        ebus.raiseEvent(10, 0, e->timestamp, NULL);
+        
+    delete e;
+    return 0;
+}
+
+char handleEvent_02_StartNextRun(Event *e) {
+    reinit_list();
+    reset();
+    ebus.raiseEvent(11, 0, e->timestamp, NULL);
+    
+    delete e;
+    return 0;
+}
+
+char handleEvent_03_Resync(Event *e) {
+    resync();
+    
+    delete e;
+    return 0;
+}
+
+char handleEvent_04_Shutdown(Event *e) {
     deleteList(times);
     shutdown_seq();
+    
+    delete e;
+    return 0;
+}
+
+char handleEvent_05_BeamPressed(Event *e) {
+    ebus.raiseEvent(7, 0, e->timestamp, NULL);
+    
+    delete e;
+    return 0;
+}
+
+char handleEvent_06_Receive(Event *e) {
+    tcode c;
+    char e_b[EVENT_DATA_LENGTH] = { 0 };
+    char rec_buff[MSG_BUFFSIZE];
+    
+    if (!ser.codeAvailable()) {
+        delete e;
+        return 1;
+    }
+    
+    ser.skipToCode();
+    ser.readCode(&c, rec_buff);
+    int_to_str(c, e_b);
+    strcat(e_b, rec_buff);
+    ebus.raiseEvent(12, 0, e->timestamp, e_b);
+    
+    delete e;
+    return 0;
+}
+
+char handleEvent_07_Start(Event *e) {
+    char b[9];
+    
+    if ((comm_flags & FLAG_START) == 0) {
+        start_tm = e->timestamp;
+        comm_flags |= FLAG_START;
+        
+        int_to_str(start_tm, b);
+        ser.sendCode(MSG_CODE_TMS, b);
+        ser.flush();
+    }
+    
+    delete e;
+    return 0;
+}
+
+char handleEvent_08_Snap0(Event *e) {
+    char b[9];
+    
+    if ((comm_flags & FLAG_SNAP_0) == 0) {
+        snapshot0 = e->timestamp;
+        comm_flags |= FLAG_SNAP_0;
+        
+        int_to_str(snapshot0, b);
+        ser.sendCode(MSG_CODE_TML0, b);
+        ser.flush();
+    }
+    
+    delete e;
+    return 0;
+}
+
+char handleEvent_09_Snap1(Event *e) {
+    char b[9];
+    
+    if ((comm_flags & FLAG_SNAP_1) == 0) {
+        snapshot1 = e->timestamp;
+        comm_flags |= FLAG_SNAP_1;
+        
+        int_to_str(snapshot1, b);
+        ser.sendCode(MSG_CODE_TML1, b);
+        ser.flush();
+    }
+    
+    delete e;
+    return 0;
+}
+
+char handleEvent_10_Stop(Event *e) {
+    char b[9];
+    
+    if ((comm_flags & FLAG_STOP) == 0) {
+        end_tm = e->timestamp;
+        comm_flags |= FLAG_STOP;
+        
+        int_to_str(end_tm, b);
+        ser.sendCode(MSG_CODE_TMP, b);
+        ser.flush();
+    }
+    
+    delete e;
+    return 0;
+}
+
+char handleEvent_11_SendReset(Event *e) {
+    char b[9];
+    int_to_str(D, b);
+    ser.sendCode(MSG_CODE_RESET, b);
+    ser.flush();
+    
+    delete e;
+    return 0;
+}
+
+char handleEvent_12_MessageReceived(Event *e) {
+    tcode c = str_to_int(e->data);
+    char *msg = &e->data[8];
+    
+    switch(c) {
+        case MSG_CODE_KILL:
+            ebus.raiseEvent(4, 0, e->timestamp, NULL);
+            break;
+        case MSG_CODE_RESET:
+            ebus.raiseEvent(2, 0, e->timestamp, NULL);
+            break;
+        case MSG_CODE_STOP:
+            ebus.raiseEvent(10, 0, e->timestamp, NULL);
+            break;
+        case MSG_CODE_REQ_RES:
+            //ebus.raiseEvent(3, 0, e->timestamp, NULL);
+            ebus.raiseEvent(2, 0, e->timestamp, NULL);
+            break;
+    }
+    
+    delete e;
+    return 0;
 }
 
 int main() {
@@ -217,7 +250,7 @@
     tick.attach_us(&handle_systime, 100); //every 100 us
     pb1.attach_asserted(&bzr_pressed);
     pb1.attach_asserted_held(&bzr_held_pressed);
-    pb1.setSamplesTillHeld(2000); //2000 samples ---> 1ms ---> 2 seconds
+    pb1.setSamplesTillHeld(1000); //1000 samples ---> 1ms ---> 1 second
     pb1.setSampleFrequency(1000); //1ms
     
     pb2.attach_asserted(&btn_pressed);
@@ -225,6 +258,23 @@
     pb2.setSamplesTillHeld(200);
     pb2.setSampleFrequency();
     
+    beam.attach_asserted(&beam_pressed);
+    beam.setSampleFrequency(1000);
+    
+    //register event handlers
+    ebus.registerEvent(1, &handleEvent_01_BuzzerPressed);
+    ebus.registerEvent(2, &handleEvent_02_StartNextRun);
+    ebus.registerEvent(3, &handleEvent_03_Resync);
+    ebus.registerEvent(4, &handleEvent_04_Shutdown);
+    ebus.registerEvent(5, &handleEvent_05_BeamPressed);
+    ebus.registerEvent(6, &handleEvent_06_Receive);
+    ebus.registerEvent(7, &handleEvent_07_Start);
+    ebus.registerEvent(8, &handleEvent_08_Snap0);
+    ebus.registerEvent(9, &handleEvent_09_Snap1);
+    ebus.registerEvent(10, &handleEvent_10_Stop);
+    ebus.registerEvent(11, &handleEvent_11_SendReset);
+    ebus.registerEvent(12, &handleEvent_12_MessageReceived);
+    
     //info
     cprintf(RUNMODE_SERVER);
     display();
@@ -237,12 +287,18 @@
     //start
     lowbatt();
     ser.registerRecFc(&hdl_rec);
-    resync();
 
     prev[0] = prev[1] = prev[2] = prev[3] = prev[4] = systime;
     
     times = initNode();
     
+    memset(bl0, 0, LCD_BUFFERLEN);
+    memset(bl1, 0, LCD_BUFFERLEN);
+    memset(bl0_, 0, LCD_BUFFERLEN);
+    memset(bl1_, 0, LCD_BUFFERLEN);
+    
+    ebus.raiseEvent(3, 0, systime, NULL);
+    
     while(1) {
         if (systime - prev[2] >= RATE_DISPLAY) {
             prev[2] = systime;
@@ -266,67 +322,11 @@
                 shutdown_seq();
             }
         }
+        
+        if (!ser.codeAvailable() && ser.getStatus() & MSG_ERR_ROVFL) {
+            ser.flush(MSG_FLUSH_IN);
+        }
+        
+        ebus.run();
     }
-}
-
-/*
-int main() {
-    //init
-    init_io();
-    init_values();
-    tick.attach_us(&handle_systime, 100); //every 100 us
-    pb1.attach_asserted(&bzr_pressed);
-    pb1.attach_asserted_held(&bzr_held_pressed);
-    pb1.setSamplesTillHeld(2000); //2000 samples ---> 1ms ---> 2 seconds
-    pb1.setSampleFrequency(1000); //1ms
-    
-    pb2.attach_asserted(&btn_pressed);
-    pb2.attach_deasserted_held(&btn_held_released);
-    pb2.setSamplesTillHeld(200);
-    pb2.setSampleFrequency();
-    
-    //info
-    cprintf(RUNMODE_SERVER);
-    display();
-    wait(WAIT_INFO_TM);
-    
-    //config
-    config_buttons();
-    config_rf_mod();
-    
-    //start
-    lowbatt();
-    //ping();
-    ser.registerRecFc(&hdl_rec);
-    
-    struct linked_list *base = initNode();
-    appendNode(base, initNode());
-    appendNode(base, initNode());
-    
-    base->data = (void *) "abc";
-    base->next->data = (void *) "def";
-    base->next->next->data = (void *) "ghi";
-    
-    struct linked_list *nd = initNode();
-    
-    nd->data = (void *)"123";
-    insertNode(base->next, nd);
-    
-    nd = base->next;
-    removeNode(nd);
-    nd = base;
-    while (1) {
-        if (nd->data != NULL)
-            cprintf("%s", (char *) nd->data);
-        else
-            cprintf("");
-            
-        display();
-        wait(2);
-        
-        nd = nd->next;
-        if (nd == NULL)
-            nd = base;
-    }
-}
-*/
\ No newline at end of file
+}
\ No newline at end of file