This program shows how to use the MODSERIAL library by Andy Kirkham to do canonical input processing. This allows you to enter and edit a command line (in the same way that you do in DOS or Linux) without tying up your main routine.
main.cpp
00001 /* Test of canonical input processing using MODSERIAL library by Andy Kirkham. 00002 * 00003 * The RX callback feature is used to echo input and perform basic line editing. 00004 * This happens outside of the main routine which is free to do other things. 00005 * 00006 * Backspace removes previous character from buffer and updates terminal. 00007 * Delete flushes the buffer and updates terminal. 00008 * CR or LF causes an "end of line" flag to be set. 00009 * Other characters are put into the buffer and echoed on terminal. 00010 */ 00011 00012 #define DATE "21 Apr 2011" 00013 #define VERSION "1.00" 00014 00015 #include "mbed.h" 00016 #include "MODSERIAL.h" 00017 00018 void serial_command_handler(void); 00019 void flashes3(void); 00020 void flashes4(void); 00021 void rxCallback(MODSERIAL_IRQ_INFO *info); 00022 00023 DigitalOut led1(LED1); 00024 DigitalOut led2(LED2); 00025 DigitalOut led3(LED3); 00026 DigitalOut led4(LED4); 00027 00028 MODSERIAL pc(USBTX, USBRX); 00029 00030 bool eol_flag = false; 00031 char buf[100]; 00032 00033 int main() { 00034 00035 pc.printf("%c[2J", 0x1B); //VT100 erase screen 00036 pc.printf("%c[H", 0x1B); //VT100 home 00037 pc.printf("MODSERIAL test program (Version %s, %s)\n", VERSION, DATE); 00038 00039 //set up serial rx callback 00040 pc.baud(9600); 00041 pc.attach(&rxCallback, MODSERIAL::RxIrq); 00042 00043 pc.printf("> "); 00044 while(1) { //main loop 00045 if (eol_flag) { //service command line 00046 serial_command_handler(); 00047 eol_flag = false; 00048 } 00049 //service an "event" 00050 flashes3(); 00051 //and service another one 00052 flashes4(); 00053 } 00054 } 00055 00056 // Serial command handler 00057 00058 void serial_command_handler(void) { 00059 char *cp; 00060 00061 led2 = 1; //light LED2 to show command processing 00062 pc.printf("\n"); 00063 //read input into buffer (gets() does not seem to be supported!) 00064 cp = buf; 00065 while (pc.readable() ) { 00066 *cp++ = pc.getc(); 00067 } 00068 --cp; //move back to CR or LF 00069 *cp = '\0'; //terminate buffer 00070 //print buffer length and contents in both ASCII and hex 00071 int i = (int) (cp - buf); 00072 pc.printf("cnt = %d, buf = <%s>\n", i, buf); 00073 cp = buf; 00074 while (i--) { 00075 pc.printf("%02x ", (int) *cp++); 00076 } 00077 pc.printf("\n> "); 00078 led2 = 0; 00079 } 00080 00081 // Dummy event handlers - flash LED3 or LED4 for a few seconds 00082 00083 void flashes3(void) { 00084 for (int i = 4; i > 0; i--) { 00085 led3 = 1; 00086 wait(0.25); 00087 led3 = 0; 00088 wait(0.25); 00089 } 00090 } 00091 00092 void flashes4(void) { 00093 for (int i = 3; i > 0; i--) { 00094 led4 = 1; 00095 wait(0.4); 00096 led4 = 0; 00097 wait(0.4); 00098 } 00099 } 00100 00101 // Rx callback routine 00102 00103 void rxCallback(MODSERIAL_IRQ_INFO *info) { 00104 00105 MODSERIAL *pc = info->serial; 00106 00107 led1 = !led1; //toggle LED1 to show char received 00108 char c = pc->rxGetLastChar(); 00109 if (c == 0x7f) { //flush input buffer and erase line on terminal 00110 int i = pc->rxBufferGetCount(); 00111 pc->rxBufferFlush(); 00112 while (i--) { 00113 pc->putc(0x8); 00114 pc->putc(' '); 00115 pc->putc(0x8); 00116 } 00117 } 00118 else if (c == 0x8) { //remove last char from buffer and erase on terminal 00119 //remove the BS and the previous char 00120 info->rxDiscardLastChar(); 00121 info->rxDiscardLastChar(); 00122 pc->putc(0x8); 00123 pc->putc(' '); 00124 pc->putc(0x8); 00125 } 00126 else if (c == '\n' || c == '\r') { //end of line, signal command available 00127 eol_flag = true; 00128 } 00129 else { //echo char on terminal 00130 pc->putc(c); 00131 } 00132 } 00133 00134 // END
Generated on Fri Jul 15 2022 13:04:34 by 1.7.2