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.
serial_gui_if.cpp
00001 00002 #include "mbed.h" 00003 #include "max32630fthr.h" 00004 #include "USBSerial.h" 00005 #include <string> 00006 #include <sstream> 00007 #include <iostream> 00008 #include <stdarg.h> 00009 #include "serial_gui_if.h" 00010 00011 struct FUNCTION_TABLE{ 00012 char func_name[128]; 00013 int size_fname; 00014 int id; 00015 int argc; 00016 int arg_min[8]; 00017 int arg_max[8]; 00018 }func_table[128]; 00019 00020 //<mbed>command arg1 arg2 arg3 .... <end> 00021 00022 SERIAL_GUI_INTERFACE::SERIAL_GUI_INTERFACE(Serial *_serial) : 00023 serial(_serial) 00024 { 00025 memset(argv, NULL, sizeof(argv)); 00026 } 00027 00028 00029 SERIAL_GUI_INTERFACE::~SERIAL_GUI_INTERFACE() 00030 { 00031 delete serial; 00032 } 00033 00034 00035 int SERIAL_GUI_INTERFACE::set_function_info(char *_fname, 00036 int _arg, ...) 00037 //int SERIAL_GUI_INTERFACE::set_function_info( 00038 // char * _fname, 00039 // int _size_fname, 00040 // int _id, 00041 // int _argc 00042 // ) 00043 { 00044 int icnt, iarg; 00045 va_list ap; 00046 00047 va_start(ap, _arg); 00048 for(icnt = 0; icnt < 128; icnt++) { 00049 if(func_table[icnt].id == 0 ) { 00050 strcpy(func_table[icnt].func_name, _fname); 00051 func_table[icnt].size_fname = _arg; 00052 func_table[icnt].id = va_arg(ap, int); 00053 func_table[icnt].argc = va_arg(ap, int); 00054 for(iarg= 0; iarg < func_table[icnt].argc; iarg++) { 00055 func_table[icnt].arg_min[iarg] = va_arg(ap, int); 00056 func_table[icnt].arg_max[iarg] = va_arg(ap, int); 00057 } 00058 va_end(ap); 00059 return func_table[icnt].id; 00060 } 00061 } 00062 va_end(ap); 00063 return -1; 00064 } 00065 00066 int SERIAL_GUI_INTERFACE::get_serial_cmd_wo_head() 00067 { 00068 int ipos = 0; 00069 char tail = '\n'; 00070 char recvStr[128]; 00071 char seps[] = "\n\t;(,) "; 00072 00073 memset(recvStr, NULL, sizeof(recvStr)); 00074 memset(argv, NULL, sizeof(argv)); 00075 00076 //Waiting function 00077 while((recvStr[ipos] = serial->getc()) != tail){ 00078 //serial->printf("check_serial_cmd\n"); 00079 00080 if(++ipos >= sizeof(recvStr)){ 00081 serial->printf("Recv Buffer Size Error\n"); 00082 return -1; 00083 } 00084 } 00085 00086 ipos = 0; 00087 argv[ipos] = strtok( &recvStr[0], seps ); 00088 while( argv[ipos] != NULL ) { 00089 //serial->printf("arg[%d] = %s \n",ipos, argv[ipos]); 00090 argv[++ipos] = strtok( NULL, seps ); 00091 } 00092 00093 for(ipos = 0; ipos < 128; ipos++) { 00094 if(strncmp(argv[0], func_table[ipos].func_name, 00095 func_table[ipos].size_fname) == 0) { 00096 //serial->printf("found %s arg[%d] @ %d sizefname %d\n",argv[0], ipos, func_table[ipos].id, func_table[ipos].size_fname); 00097 return func_table[ipos].id; 00098 } 00099 } 00100 00101 return -1; 00102 } 00103 00104 int SERIAL_GUI_INTERFACE::get_serial_cmd() 00105 { 00106 int rval = 0; 00107 char recvStr[6], ihead = 0; 00108 char *head = "<mbed>"; 00109 char tail = '\n'; 00110 00111 memset(recvStr, NULL, sizeof(recvStr)); 00112 //Waiting Headers 00113 while(1){ 00114 00115 //serial->printf("check_serial\n"); 00116 00117 if((recvStr[ihead] = serial->getc())== tail) 00118 { 00119 ihead = 0; 00120 continue; 00121 } 00122 00123 if(++ihead == sizeof(recvStr)){ 00124 if(strncmp(head,recvStr,sizeof(recvStr)) == 0){ 00125 //serial->printf("!! find Head %s \n", recvStr); 00126 00127 rval = get_serial_cmd_wo_head(); 00128 break; 00129 } 00130 ihead = 0; 00131 } 00132 } 00133 serial->printf("id %d \n", rval); 00134 return rval; 00135 } 00136 00137 00138 void SERIAL_GUI_INTERFACE::req_function_info(void) 00139 { 00140 int icnt, iarg; 00141 00142 serial->printf("func name_size id argc arg(min, max)\n"); 00143 for(icnt = 0; icnt < 128; icnt++) { 00144 if(func_table[icnt].id <1) 00145 return; 00146 00147 serial->printf("%s %d 0x%x %d ", 00148 func_table[icnt].func_name, 00149 func_table[icnt].size_fname, 00150 func_table[icnt].id, 00151 func_table[icnt].argc 00152 ); 00153 00154 for(iarg = 0; iarg < func_table[icnt].argc; iarg++) { 00155 serial->printf(" 0x%x 0x%x", 00156 func_table[icnt].arg_min[iarg], 00157 func_table[icnt].arg_max[iarg] 00158 ); 00159 } 00160 serial->printf("\n"); 00161 } 00162 return; 00163 } 00164 00165 00166 int SERIAL_GUI_INTERFACE::get_argi(int _n) 00167 { 00168 unsigned int hex_value 00169 = std::strtoul(argv[_n], 0, 16); 00170 return 0; 00171 } 00172 00173 char* SERIAL_GUI_INTERFACE::get_arg(int _n) 00174 { 00175 return argv[_n]; 00176 } 00177 00178 00179 void SERIAL_GUI_INTERFACE::send_reg_value(int _reg, int _data) 00180 { 00181 serial->printf("0x%x = 0x%x", _reg, _data); 00182 }
Generated on Thu Jul 14 2022 01:21:39 by
1.7.2