yuhang zhu / Mbed 2 deprecated ADAM_menu

Dependencies:   mbed

main.cpp

Committer:
yuhangzhu
Date:
2013-07-13
Revision:
9:3eaacc1ad758
Parent:
8:f0c3d5911b87
Child:
10:ff8c2470f019

File content as of revision 9:3eaacc1ad758:

#include "mbed.h"
#include "menu.h"
#include "parameters.h"
#include "handlers.h"
//#define DEBUG

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;
                        #ifdef DEBUG    
                        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]);
                        #endif
                           
                        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 
                //check phase input
                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)   //Input string too long
                   { 
                        pc.printf("%s", msg_strlong);
                        pc.getc();
                   }
                   else if(ret == PARSE_EMPTY)
                   {
                        pc.printf("%s", msg_empty);
                        pc.printf("%s", msg_ret);
                        pc.getc();
                   }
                   else if(ret == PARSE_ERR) //Input string has error
                   {
                        pc.printf("%s", msg_err);
                        pc.getc();
                   }
                   else if(ret == PARSE_ROUNDED) //Input is rounded,show the rounded value
                   {    
                        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   //Input is good
                   {    
                        pc.printf("%s", msg_ok);
                        #ifdef DEBUG
                        pc.printf("%d %d\n", cha1_pha*7, cha2_pha*7);
                        #endif
                        pc.printf("%s", msg_ret);
                        pc.getc();
                   }
                } 
                
                //check attenuation input
                if(state == MODE_CHA1AMP || state == MODE_CHA2AMP)
                {
                    if(state == MODE_CHA1AMP)
                        ret = parse_amp(line_buf, &cha1_amp);
                    else
                        ret = parse_amp(line_buf, &cha2_amp);
                    
                   if(ret == PARSE_LONG)   //Input string too long
                   { 
                        pc.printf("%s", msg_strlong);
                        pc.getc();
                   }
                   else if(ret == PARSE_EMPTY)
                   {
                        pc.printf("%s", msg_empty);
                        pc.printf("%s", msg_ret);
                        pc.getc();
                   }
                   else if(ret == PARSE_ERR) //Input string has error
                   {
                        pc.printf("%s", msg_err);
                        pc.getc();
                   }
                   else if(ret == PARSE_ROUNDED) //Input is rounded, show the rounded value
                   {    
                        if(state == MODE_CHA1AMP)
                            pc.printf("%s %.1f\n", msg_rounded, cha1_amp*5.0/10.0);
                        else if(state == MODE_CHA2AMP)
                            pc.printf("%s %.1f\n", msg_rounded, cha2_amp*5.0/10.0);
                            
                        pc.printf("%s", msg_ret);
                        pc.getc();
                   }
                   else   //Input is good
                   {    
                        pc.printf("%s", msg_ok);
                        #ifdef DEBUG
                        pc.printf("%d %d\n", cha1_amp*5, cha2_amp*5);
                        #endif
                        pc.printf("%s", msg_ret);
                        pc.getc();
                   }
                }  
                state = MODE_IDLE;  //back to idle state after input
                break;
            
            // in display mode
            case MODE_DISPLAY:
                pc.printf("Current phase shift value for channel 1 is %d degrees\n", cha1_pha*-7);
                pc.printf("Current phase shift value for channel 2 is %d degrees\n", cha2_pha*-7);
                pc.printf("Current attentuation for channel 1 is %.1f dB\n", cha1_amp*5.0/10.0);
                pc.printf("Current attentuation for channel 2 is %.1f dB\n", cha2_amp*5.0/10.0);
                pc.printf("%s", msg_ret);
                pc.getc();
                state = MODE_IDLE;
                break;
                
            case MODE_HELP:
                pc.printf("%s", msg_help);
                pc.printf("%s", msg_ret);
                pc.getc();
                state = MODE_IDLE;
                break;
                
            default:
                state = MODE_IDLE;
        }
        
        
    }
    
    
    return 0;
}