Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- yuhangzhu
- Date:
- 2013-07-14
- Revision:
- 11:0e42dd431fb5
- Parent:
- 10:ff8c2470f019
- Child:
- 12:5e4cba1182ab
File content as of revision 11:0e42dd431fb5:
#include "mbed.h" #include "menu.h" #include "parameters.h" #include "handlers.h" //#define DEBUG Serial pc(USBTX, USBRX); char recv; char temp; int state; char cha1_pha, cha2_pha; char cha1_amp, cha2_amp; int ret; unsigned short spi_stream; 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 i; int main() { pc.baud(19200); //config buad rate state = MODE_IDLE; line_buf[3] = 0; line_buf[2] = 255; line_buf[1] = 255; line_buf[0] = 255; cha1_pha = 0; cha2_pha = 0; cha1_amp = 0; cha2_amp = 0; 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("Phase shift value for channel 1 is %d degrees\n", cha1_pha*-7); pc.printf("Phase shift value for channel 2 is %d degrees\n", cha2_pha*-7); pc.printf("Attentuation for channel 1 is %.1f dB\n", cha1_amp*5.0/10.0); pc.printf("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; case MODE_SEND: pc.printf("Phase shift value for channel 1 is %d degrees\n", cha1_pha*-7); pc.printf("Phase shift value for channel 2 is %d degrees\n", cha2_pha*-7); pc.printf("Attentuation for channel 1 is %.1f dB\n", cha1_amp*5.0/10.0); pc.printf("Attentuation for channel 2 is %.1f dB\n", cha2_amp*5.0/10.0); pc.printf("\nSPI bit stream:\n"); for(i=15; i>=0; i--) pc.printf("Bit%d ", i); pc.printf("\n"); spi_stream = 0; spi_stream = (cha2_amp << 12) | (cha2_pha << 8) | (cha1_amp << 4) | (cha1_pha); for(i=0; i<=15; i++) { if(spi_stream >= 0x8000) { if(i < 6) pc.printf("%d ", 1); else pc.printf("%d ", 1); } else { if(i < 6) pc.printf("%d ", 0); else pc.printf("%d ", 0); } spi_stream = spi_stream << 1; } pc.printf("\n"); pc.printf("%s", msg_ret); pc.getc(); state = MODE_IDLE; break; default: state = MODE_IDLE; } } return 0; }