Display zeigt merkwürdige Zeichen

Dependencies:   mbed PinDetect TextLCD

Fork of FWFSU_Start_28102013 by KX-ONE

main.cpp

Committer:
manuelfederanko
Date:
2016-08-12
Revision:
34:71b726a58856
Parent:
33:9c1ea15dc6f2
Child:
35:1fcb3101c2f9

File content as of revision 34:71b726a58856:

#include "main.h"

using namespace evtbus;
using namespace evt;
using namespace msg;

EventBus ebus;

time_100us ping_flag = 0;

/**
 * Synchronize with the slave
 */
void resync( void ) {
    Message *m = new Message(MSG_CODE_TOTAL_RESET, "");
    ser.putMsg(m);
    ser.flush();
    wait(FLUSH_DELAY);
    
    comm_flags = 0;
    buzzer_state = 0;
    button_state = 0;
    D = MIN_ROUND_CNT;
}

/********************************************************************************************
 INTERRUPT SERVICE ROUTINES (those raise all events!)
 ********************************************************************************************/

void bzr_pressed( void ) {
    time_100us ts = systime;
    ebus.setInterruptContext();
    ebus.raiseEvent(1, 0, ts, NULL);
}

void bzr_held_pressed( void ) {
    time_100us ts = systime;
    ebus.setInterruptContext();
    ebus.raiseEvent(2, 0, ts, NULL);
}

void btn_pressed( void ) {
    time_100us ts = systime;
    ebus.setInterruptContext();
    ebus.raiseEvent(3, 0, ts, NULL);
}

void btn_held_released( void ) {
    time_100us ts = systime;
    ebus.setInterruptContext();
    ebus.raiseEvent(4, 0, ts, NULL);
}

void beam_pressed(void) {
    time_100us ts = systime;
    ebus.setInterruptContext();
    ebus.raiseEvent(5, 0, ts, NULL);
}

void hdl_rec( void ) {
    time_100us ts = systime;
    ebus.setInterruptContext();
    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_STOP) == 0 && (comm_flags & FLAG_SNAP_0) == 0) //only when stop has not been pressed
        ebus.raiseEvent(8, 0, e->timestamp, NULL);
    else if ((comm_flags & FLAG_STOP) == 0 && (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) {
    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) {
    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) {
    Message *m;
    char e_b[EVENT_DATA_LENGTH] = { 0 };
    
    /*
    if (ser.available() == 0) {
        delete e;
        return 0;
    }
    */
    
    /*
    switch (ser.messageAvailable()) {
        case MSG_AV_FLAG_NONE:
            delete e;
            return 2;
        case MSG_AV_FLAG_MSG:
            //cprintf("MSG!!!!");
            //display();
            //wait(1);
            goto PROCESS_MSG;
        case MSG_AV_FLAG_RESEND:
            ser.resendMsg();
            ser.flush();
            delete e;
            return 1;
    }
    */
    
PROCESS_MSG:
    m = ser.getMessage();
    if (m == NULL) {
        delete e;
        return 2;
    }
    int_to_str(m->code, e_b);
    strcat(e_b, m->data);
    ebus.raiseEvent(12, 0, e->timestamp, e_b);
    delete m;
    delete e;
    return 0;
}

char handleEvent_07_Start(Event *e) {
    char b[9];
    Message *m;
    
    if ((comm_flags & FLAG_START) == 0) {
        start_tm = e->timestamp;
        comm_flags |= FLAG_START;
            
        int_to_str(start_tm, b);
        m = new Message(MSG_CODE_TMS, b);
        ser.putMsg(m);
        ser.flush();
        wait(FLUSH_DELAY);
        
        #ifdef DEBUG
        DPRINT("start");
        #endif
    }
    
    delete e;
    return 0;
}

char handleEvent_08_Snap0(Event *e) {
    char b[9];
    Message *m;
    
    if ((comm_flags & FLAG_SNAP_0) == 0) {
        snapshot0 = e->timestamp;
        comm_flags |= FLAG_SNAP_0;
            
        int_to_str(snapshot0, b);
        m = new Message(MSG_CODE_TML0, b);
        ser.putMsg(m);
        ser.flush();
        wait(FLUSH_DELAY);
        
        #ifdef DEBUG
        DPRINT("snap0");
        #endif
    }
    
    delete e;
    return 0;
}

char handleEvent_09_Snap1(Event *e) {
    char b[9];
    Message *m;
    
    if ((comm_flags & FLAG_SNAP_1) == 0) {
        snapshot1 = e->timestamp;
        comm_flags |= FLAG_SNAP_1;
        
        int_to_str(snapshot1, b);
        m = new Message(MSG_CODE_TML1, b);
        ser.putMsg(m);
        ser.flush();
        wait(FLUSH_DELAY);
        
        #ifdef DEBUG
        DPRINT("snap1");
        #endif
    }
    
    delete e;
    return 0;
}

char handleEvent_10_Stop(Event *e) {
    char b[9];
    Message *m;
    
    if ((comm_flags & FLAG_STOP) == 0) {
        end_tm = e->timestamp;
        comm_flags |= FLAG_STOP;
        
        int_to_str(end_tm, b);
        m = new Message(MSG_CODE_TMP, b);
        ser.putMsg(m);
        ser.flush();
        wait(FLUSH_DELAY);
        
        #ifdef DEBUG
        DPRINT("stop");
        #endif
    }
    
    delete e;
    return 0;
}

char handleEvent_11_SendReset(Event *e) {
    char b[9];
    Message *m;
    
    int_to_str(D, b);
    
    #ifdef DEBUG
    DPRINT("D (int)=%d\n(str)=%s", D, b);
    #endif
    
    m = new Message(MSG_CODE_RESET, b);
    ser.putMsg(m);
    ser.flush();
    wait(FLUSH_DELAY);
    
    //that here for not delaying the msg transfere
    #ifdef DEBUG
    DPRINT("reset");
    #endif
    
    delete e;
    return 0;
}

char handleEvent_12_MessageReceived(Event *e) {
    tcode c = str_to_int(e->data);
    char *msg = &e->data[8];
    
    #ifdef DEBUG
    //cprintf("%08x %s", c, msg);
    //display();
    //wait(3);
    #endif
    
    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(2, 0, e->timestamp, NULL);
            break;
        case MSG_CODE_PING:
            ebus.raiseEvent(14, 0, e->timestamp, NULL);
            break;
        case MSG_CODE_TOTAL_RESET:
            ebus.raiseEvent(3, 0, e->timestamp, NULL);
            break;
    }
    
    delete e;
    return 0;
}

char handleEvent_13_YouThere(Event *e) {
    Message *m;
    ping_flag = systime;
    
    m = new Message(MSG_CODE_PING, "");
    ser.putMsg(m);
    ser.flush();
    wait(FLUSH_DELAY);
    
    delete e;
    return 0;
}

char handleEvent_14_Hello(Event *e) {
    Message *m;
    if (ping_flag) {
        if ((comm_flags & FLAG_BROTHER_HERE) == 0) {
            cprintf("reconnected");
            display();
            wait(0.5);
            comm_flags |= FLAG_BROTHER_HERE;
        }
        ping_flag = 0;
    } else {
        m = new Message(MSG_CODE_PING, "");
        ser.putMsg(m);
        ser.flush();
        wait(FLUSH_DELAY);
    }
    
    delete e;
    return 0;
}

int main() {
    //| Element 0 | Element 1 | Element 2 | Element 3 |
    //|-----------|-----------|-----------|-----------|
    //| Akkucheck | LED blink | Disp-Rate |      Ping |
    time_100us prev[5];

    //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(1000); //1000 samples ---> 1ms ---> 1 second
    pb1.setSampleFrequency(1000); //1ms
    
    pb2.attach_deasserted(&btn_pressed);
    pb2.attach_deasserted_held(&btn_held_released);
    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);
    ebus.registerEvent(13, &handleEvent_13_YouThere);
    ebus.registerEvent(14, &handleEvent_14_Hello);
    
    //info
    cprintf(RUNMODE_SERVER);
    display();
    wait(WAIT_INFO_TM);
    
    //config
    config_buttons();
    config_rf_mod();
    
    //start
    lowbatt();
    ser.registerRcFc(&hdl_rec);

    prev[0] = prev[1] = prev[2] = prev[3] = prev[4] = systime;
    
    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;
            render_time();
            display();
        }
        if (systime - prev[1] >= ((akku_state >= AKKU_1)? RATE_LED_NOK : RATE_LED_OK)) {
            prev[1] = systime;
            stoppled.write(!stoppled.read());
        }
        if (systime - prev[3] >= 50000) { //every 5 secs ping
            prev[3] = systime;
            //ebus.raiseEvent(13, 0, systime, NULL);
        }
        if (comm_flags & FLAG_INFO_LOCK && systime - info_tm >= RATE_SHOW_INFO) {
            //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
            //the flag gets checked in the render_time function, to prevent that from overwriting the info text
            comm_flags &= ~FLAG_INFO_LOCK;
        }
        if (systime - prev[0] >= RATE_CHECK_AKKU) {
            prev[0] = systime;
            akkucheck();
            
            if (akku_state == AKKU_CRITICAL) {
                shutdown_seq();
            }
        }
        if (ping_flag != 0 && systime - ping_flag >= 30000) { //3 secs
            if (comm_flags & FLAG_BROTHER_HERE) {
                cprintf("connection lost");
                display();
                wait(0.5);
            }
            comm_flags &= ~FLAG_BROTHER_HERE;
            ping_flag = 0;
        }
        
        /*
        if (ser.available_()) {
            ser.read();
        }
        */
        
        //if (systime - prev[4] > 100) {
        //    prev[4] = systime;
            ebus.run();
        //}
    }
}