Display zeigt merkwürdige Zeichen

Dependencies:   mbed PinDetect TextLCD

Fork of FWFSU_Start_28102013 by KX-ONE

main.cpp

Committer:
manuelfederanko
Date:
2016-07-26
Revision:
13:a45461fbb77f
Parent:
12:3f53322a5270
Child:
14:90b2af48767a

File content as of revision 13:a45461fbb77f:

#include "common.h"

#define PING_TIMEOUT 50000 //5 seconds

/**
 * 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);
}

/**
 * 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:
                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:
                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;
                comm_flags |= FLAG_STOP;
                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;
            default:
                rec_code = 0;
                rec_buff[0] = 0;
                break;
        }
    }
}

/**
 * Synchronize with the slave
 */
void resync( void ) {
    ser.sendCode(MSG_CODE_TOTAL_RESET, NULL);
    ser.flush();
    btn_cnt = 0;
    comm_flags = 0;
    buzzer_state = 0;
    button_state = 0;
    D = 1;    
}

void bzr_pressed( void ) {
    btn_control();
}

void bzr_held_pressed( void ) {
    reset();
    ser.sendCode(MSG_CODE_RESET, NULL);
    ser.flush();
}

void btn_pressed( void ) {
    resync();
}

void btn_held_released( void ) {
    shutdown_seq();
}

int main() {
    //| Element 0 | Element 1 | Element 2 | Element 3 |
    //|-----------|-----------|-----------|-----------|
    //| Akkucheck | LED blink | Disp-Rate | Btn. Rate |
    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.setSampleFrequency();
    
    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);
    resync();

    prev[0] = prev[1] = prev[2] = prev[3] = prev[4] = systime;
    
    
    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 (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();
            }
        }
    }
}