yuhang zhu / Mbed 2 deprecated ADAM_menu

Dependencies:   mbed

main.cpp

Committer:
yuhangzhu
Date:
2013-07-13
Revision:
6:e510aab8f2ce
Parent:
5:50ea1502a949
Child:
7:c268547fb6c8

File content as of revision 6:e510aab8f2ce:

#include "mbed.h"
#include "menu.h"
#include "parameters.h"
#include "handlers.h"

Serial pc(USBTX, USBRX);

char recv;
int state;
char cha1_pha, cha2_pha;
char cha1_amp, cha2_amp;
int ret;

short spi_data;
char line_buf[4];   //line_buf[3] is used to indicate if it's empty 0 empty 1 not
int line_pt;        //line buffer pointer points to next buffer location it's going to write
int line_length;    //store the current line length

int main() {

    pc.baud(115200);  //config buad rate 
    state = MODE_IDLE;
    line_buf[3] = 0;
    line_buf[2] = 255;
    line_buf[1] = 255;
    line_buf[0] = 255;
    
    while(1)
    {
        switch(state)
        {
            case MODE_IDLE:
                pc.printf("%s", main_menu);
                recv = pc.getc();
                mode_idle_handler(&state, recv);
                //pc.printf("%d", state);
                break;
            case MODE_CHA1PHA:
            case MODE_CHA1AMP:
            case MODE_CHA2PHA:
            case MODE_CHA2AMP:
                if(state == MODE_CHA1PHA)
                    pc.printf("%s", cmd1);
                else if(state == MODE_CHA1AMP)
                    pc.printf("%s", cmd2);
                else if(state == MODE_CHA2PHA)
                    pc.printf("%s", cmd3);
                else
                    pc.printf("%s", cmd4);
                    
                line_buf[3] = 0;
                line_buf[2] = 255;
                line_buf[1] = 255;
                line_buf[0] = 255;
                line_pt = 0;
                line_length = 0;
                
                while(1)   //input into line buffer, terminated by enter key
                {
                    recv = pc.getc();
                    if(recv == 13)   //enter is pressed, break
                    {
                        if(line_length == 0)
                            line_buf[3] = LINEBUF_EMPTY;
                        else if(line_length > 3)
                            line_buf[3] = LINEBUF_TOOLONG;
                        else
                            line_buf[3] = LINEBUF_OK;
                            
                        //pc.printf("decimal output : %d %d %d %d", line_buf[3], line_buf[2], line_buf[1], line_buf[0]);
                        //pc.printf("char output: %c %c %c %c", line_buf[3], line_buf[2], line_buf[1], line_buf[0]);   
                        break;  
                    }
                    else if(recv == 8)   //backspace is pressed, delete one character both on screen and line buffer
                    {
                        pc.putc(8);
                        pc.putc(32);
                        pc.putc(8);
                        
                        if(line_length <= 3 && line_length>=1)   
                        {
                            line_pt --;
                            line_buf[line_pt] = 255;
                        }
                        if(line_length > 0)
                            line_length --;
                    }
                    else        //other character is pressed, record that in line buffer
                    {
                        pc.putc(recv);
                        line_length ++;
                        
                        if(line_length <= 3)
                        {
                            line_buf[line_pt] = recv;
                            line_pt ++;
                        }
                    }
                }
                //input over, now checking 
                
                if(state == MODE_CHA1PHA || state == MODE_CHA2PHA)
                {  
                   if(state == MODE_CHA1PHA)
                        ret = parse_phase(line_buf, &cha1_pha);
                   else
                        ret = parse_phase(line_buf, &cha2_pha);
                        
                   if(ret == PARSE_LONG)
                   { 
                        pc.printf("%s", msg_strlong);
                        pc.getc();
                   }
                   else if(ret == PARSE_ERR)
                   {
                        pc.printf("%s", msg_err);
                        pc.getc();
                   }
                   else if(ret == PARSE_ROUNDED)
                   {    
                        if(state == MODE_CHA1PHA)
                            pc.printf("%s -%d\n", msg_rounded, cha1_pha*7);
                        else if(state == MODE_CHA2PHA)
                            pc.printf("%s -%d\n", msg_rounded, cha2_pha*7);
                            
                        pc.printf("%s", msg_ret);
                        pc.getc();
                   }
                   else
                   {    
                        pc.printf("%s", msg_ok);
                        pc.printf("%d %d\n", cha1_pha*7, cha2_pha*7);
                        pc.printf("%s", msg_ret);
                        pc.getc();
                   }
                } 
                
                state = MODE_IDLE;
                break;
            
        }
        
        
    }
    
    
    return 0;
}