Serial Interface for GUI.

Committer:
daniel_gs_jeong
Date:
Thu Dec 27 05:13:43 2018 +0000
Revision:
0:9eb0a8cff892
the 1stVersion: Serial GUI Interface.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
daniel_gs_jeong 0:9eb0a8cff892 1
daniel_gs_jeong 0:9eb0a8cff892 2 #include "mbed.h"
daniel_gs_jeong 0:9eb0a8cff892 3 #include "max32630fthr.h"
daniel_gs_jeong 0:9eb0a8cff892 4 #include "USBSerial.h"
daniel_gs_jeong 0:9eb0a8cff892 5 #include <string>
daniel_gs_jeong 0:9eb0a8cff892 6 #include <sstream>
daniel_gs_jeong 0:9eb0a8cff892 7 #include <iostream>
daniel_gs_jeong 0:9eb0a8cff892 8 #include <stdarg.h>
daniel_gs_jeong 0:9eb0a8cff892 9 #include "serial_gui_if.h"
daniel_gs_jeong 0:9eb0a8cff892 10
daniel_gs_jeong 0:9eb0a8cff892 11 struct FUNCTION_TABLE{
daniel_gs_jeong 0:9eb0a8cff892 12 char func_name[128];
daniel_gs_jeong 0:9eb0a8cff892 13 int size_fname;
daniel_gs_jeong 0:9eb0a8cff892 14 int id;
daniel_gs_jeong 0:9eb0a8cff892 15 int argc;
daniel_gs_jeong 0:9eb0a8cff892 16 int arg_min[8];
daniel_gs_jeong 0:9eb0a8cff892 17 int arg_max[8];
daniel_gs_jeong 0:9eb0a8cff892 18 }func_table[128];
daniel_gs_jeong 0:9eb0a8cff892 19
daniel_gs_jeong 0:9eb0a8cff892 20 //<mbed>command arg1 arg2 arg3 .... <end>
daniel_gs_jeong 0:9eb0a8cff892 21
daniel_gs_jeong 0:9eb0a8cff892 22 SERIAL_GUI_INTERFACE::SERIAL_GUI_INTERFACE(Serial *_serial) :
daniel_gs_jeong 0:9eb0a8cff892 23 serial(_serial)
daniel_gs_jeong 0:9eb0a8cff892 24 {
daniel_gs_jeong 0:9eb0a8cff892 25 memset(argv, NULL, sizeof(argv));
daniel_gs_jeong 0:9eb0a8cff892 26 }
daniel_gs_jeong 0:9eb0a8cff892 27
daniel_gs_jeong 0:9eb0a8cff892 28
daniel_gs_jeong 0:9eb0a8cff892 29 SERIAL_GUI_INTERFACE::~SERIAL_GUI_INTERFACE()
daniel_gs_jeong 0:9eb0a8cff892 30 {
daniel_gs_jeong 0:9eb0a8cff892 31 delete serial;
daniel_gs_jeong 0:9eb0a8cff892 32 }
daniel_gs_jeong 0:9eb0a8cff892 33
daniel_gs_jeong 0:9eb0a8cff892 34
daniel_gs_jeong 0:9eb0a8cff892 35 int SERIAL_GUI_INTERFACE::set_function_info(char *_fname,
daniel_gs_jeong 0:9eb0a8cff892 36 int _arg, ...)
daniel_gs_jeong 0:9eb0a8cff892 37 //int SERIAL_GUI_INTERFACE::set_function_info(
daniel_gs_jeong 0:9eb0a8cff892 38 // char * _fname,
daniel_gs_jeong 0:9eb0a8cff892 39 // int _size_fname,
daniel_gs_jeong 0:9eb0a8cff892 40 // int _id,
daniel_gs_jeong 0:9eb0a8cff892 41 // int _argc
daniel_gs_jeong 0:9eb0a8cff892 42 // )
daniel_gs_jeong 0:9eb0a8cff892 43 {
daniel_gs_jeong 0:9eb0a8cff892 44 int icnt, iarg;
daniel_gs_jeong 0:9eb0a8cff892 45 va_list ap;
daniel_gs_jeong 0:9eb0a8cff892 46
daniel_gs_jeong 0:9eb0a8cff892 47 va_start(ap, _arg);
daniel_gs_jeong 0:9eb0a8cff892 48 for(icnt = 0; icnt < 128; icnt++) {
daniel_gs_jeong 0:9eb0a8cff892 49 if(func_table[icnt].id == 0 ) {
daniel_gs_jeong 0:9eb0a8cff892 50 strcpy(func_table[icnt].func_name, _fname);
daniel_gs_jeong 0:9eb0a8cff892 51 func_table[icnt].size_fname = _arg;
daniel_gs_jeong 0:9eb0a8cff892 52 func_table[icnt].id = va_arg(ap, int);
daniel_gs_jeong 0:9eb0a8cff892 53 func_table[icnt].argc = va_arg(ap, int);
daniel_gs_jeong 0:9eb0a8cff892 54 for(iarg= 0; iarg < func_table[icnt].argc; iarg++) {
daniel_gs_jeong 0:9eb0a8cff892 55 func_table[icnt].arg_min[iarg] = va_arg(ap, int);
daniel_gs_jeong 0:9eb0a8cff892 56 func_table[icnt].arg_max[iarg] = va_arg(ap, int);
daniel_gs_jeong 0:9eb0a8cff892 57 }
daniel_gs_jeong 0:9eb0a8cff892 58 va_end(ap);
daniel_gs_jeong 0:9eb0a8cff892 59 return func_table[icnt].id;
daniel_gs_jeong 0:9eb0a8cff892 60 }
daniel_gs_jeong 0:9eb0a8cff892 61 }
daniel_gs_jeong 0:9eb0a8cff892 62 va_end(ap);
daniel_gs_jeong 0:9eb0a8cff892 63 return -1;
daniel_gs_jeong 0:9eb0a8cff892 64 }
daniel_gs_jeong 0:9eb0a8cff892 65
daniel_gs_jeong 0:9eb0a8cff892 66 int SERIAL_GUI_INTERFACE::get_serial_cmd_wo_head()
daniel_gs_jeong 0:9eb0a8cff892 67 {
daniel_gs_jeong 0:9eb0a8cff892 68 int ipos = 0;
daniel_gs_jeong 0:9eb0a8cff892 69 char tail = '\n';
daniel_gs_jeong 0:9eb0a8cff892 70 char recvStr[128];
daniel_gs_jeong 0:9eb0a8cff892 71 char seps[] = "\n\t;(,) ";
daniel_gs_jeong 0:9eb0a8cff892 72
daniel_gs_jeong 0:9eb0a8cff892 73 memset(recvStr, NULL, sizeof(recvStr));
daniel_gs_jeong 0:9eb0a8cff892 74 memset(argv, NULL, sizeof(argv));
daniel_gs_jeong 0:9eb0a8cff892 75
daniel_gs_jeong 0:9eb0a8cff892 76 //Waiting function
daniel_gs_jeong 0:9eb0a8cff892 77 while((recvStr[ipos] = serial->getc()) != tail){
daniel_gs_jeong 0:9eb0a8cff892 78 //serial->printf("check_serial_cmd\n");
daniel_gs_jeong 0:9eb0a8cff892 79
daniel_gs_jeong 0:9eb0a8cff892 80 if(++ipos >= sizeof(recvStr)){
daniel_gs_jeong 0:9eb0a8cff892 81 serial->printf("Recv Buffer Size Error\n");
daniel_gs_jeong 0:9eb0a8cff892 82 return -1;
daniel_gs_jeong 0:9eb0a8cff892 83 }
daniel_gs_jeong 0:9eb0a8cff892 84 }
daniel_gs_jeong 0:9eb0a8cff892 85
daniel_gs_jeong 0:9eb0a8cff892 86 ipos = 0;
daniel_gs_jeong 0:9eb0a8cff892 87 argv[ipos] = strtok( &recvStr[0], seps );
daniel_gs_jeong 0:9eb0a8cff892 88 while( argv[ipos] != NULL ) {
daniel_gs_jeong 0:9eb0a8cff892 89 //serial->printf("arg[%d] = %s \n",ipos, argv[ipos]);
daniel_gs_jeong 0:9eb0a8cff892 90 argv[++ipos] = strtok( NULL, seps );
daniel_gs_jeong 0:9eb0a8cff892 91 }
daniel_gs_jeong 0:9eb0a8cff892 92
daniel_gs_jeong 0:9eb0a8cff892 93 for(ipos = 0; ipos < 128; ipos++) {
daniel_gs_jeong 0:9eb0a8cff892 94 if(strncmp(argv[0], func_table[ipos].func_name,
daniel_gs_jeong 0:9eb0a8cff892 95 func_table[ipos].size_fname) == 0) {
daniel_gs_jeong 0:9eb0a8cff892 96 //serial->printf("found %s arg[%d] @ %d sizefname %d\n",argv[0], ipos, func_table[ipos].id, func_table[ipos].size_fname);
daniel_gs_jeong 0:9eb0a8cff892 97 return func_table[ipos].id;
daniel_gs_jeong 0:9eb0a8cff892 98 }
daniel_gs_jeong 0:9eb0a8cff892 99 }
daniel_gs_jeong 0:9eb0a8cff892 100
daniel_gs_jeong 0:9eb0a8cff892 101 return -1;
daniel_gs_jeong 0:9eb0a8cff892 102 }
daniel_gs_jeong 0:9eb0a8cff892 103
daniel_gs_jeong 0:9eb0a8cff892 104 int SERIAL_GUI_INTERFACE::get_serial_cmd()
daniel_gs_jeong 0:9eb0a8cff892 105 {
daniel_gs_jeong 0:9eb0a8cff892 106 int rval = 0;
daniel_gs_jeong 0:9eb0a8cff892 107 char recvStr[6], ihead = 0;
daniel_gs_jeong 0:9eb0a8cff892 108 char *head = "<mbed>";
daniel_gs_jeong 0:9eb0a8cff892 109 char tail = '\n';
daniel_gs_jeong 0:9eb0a8cff892 110
daniel_gs_jeong 0:9eb0a8cff892 111 memset(recvStr, NULL, sizeof(recvStr));
daniel_gs_jeong 0:9eb0a8cff892 112 //Waiting Headers
daniel_gs_jeong 0:9eb0a8cff892 113 while(1){
daniel_gs_jeong 0:9eb0a8cff892 114
daniel_gs_jeong 0:9eb0a8cff892 115 //serial->printf("check_serial\n");
daniel_gs_jeong 0:9eb0a8cff892 116
daniel_gs_jeong 0:9eb0a8cff892 117 if((recvStr[ihead] = serial->getc())== tail)
daniel_gs_jeong 0:9eb0a8cff892 118 {
daniel_gs_jeong 0:9eb0a8cff892 119 ihead = 0;
daniel_gs_jeong 0:9eb0a8cff892 120 continue;
daniel_gs_jeong 0:9eb0a8cff892 121 }
daniel_gs_jeong 0:9eb0a8cff892 122
daniel_gs_jeong 0:9eb0a8cff892 123 if(++ihead == sizeof(recvStr)){
daniel_gs_jeong 0:9eb0a8cff892 124 if(strncmp(head,recvStr,sizeof(recvStr)) == 0){
daniel_gs_jeong 0:9eb0a8cff892 125 //serial->printf("!! find Head %s \n", recvStr);
daniel_gs_jeong 0:9eb0a8cff892 126
daniel_gs_jeong 0:9eb0a8cff892 127 rval = get_serial_cmd_wo_head();
daniel_gs_jeong 0:9eb0a8cff892 128 break;
daniel_gs_jeong 0:9eb0a8cff892 129 }
daniel_gs_jeong 0:9eb0a8cff892 130 ihead = 0;
daniel_gs_jeong 0:9eb0a8cff892 131 }
daniel_gs_jeong 0:9eb0a8cff892 132 }
daniel_gs_jeong 0:9eb0a8cff892 133 serial->printf("id %d \n", rval);
daniel_gs_jeong 0:9eb0a8cff892 134 return rval;
daniel_gs_jeong 0:9eb0a8cff892 135 }
daniel_gs_jeong 0:9eb0a8cff892 136
daniel_gs_jeong 0:9eb0a8cff892 137
daniel_gs_jeong 0:9eb0a8cff892 138 void SERIAL_GUI_INTERFACE::req_function_info(void)
daniel_gs_jeong 0:9eb0a8cff892 139 {
daniel_gs_jeong 0:9eb0a8cff892 140 int icnt, iarg;
daniel_gs_jeong 0:9eb0a8cff892 141
daniel_gs_jeong 0:9eb0a8cff892 142 serial->printf("func name_size id argc arg(min, max)\n");
daniel_gs_jeong 0:9eb0a8cff892 143 for(icnt = 0; icnt < 128; icnt++) {
daniel_gs_jeong 0:9eb0a8cff892 144 if(func_table[icnt].id <1)
daniel_gs_jeong 0:9eb0a8cff892 145 return;
daniel_gs_jeong 0:9eb0a8cff892 146
daniel_gs_jeong 0:9eb0a8cff892 147 serial->printf("%s %d 0x%x %d ",
daniel_gs_jeong 0:9eb0a8cff892 148 func_table[icnt].func_name,
daniel_gs_jeong 0:9eb0a8cff892 149 func_table[icnt].size_fname,
daniel_gs_jeong 0:9eb0a8cff892 150 func_table[icnt].id,
daniel_gs_jeong 0:9eb0a8cff892 151 func_table[icnt].argc
daniel_gs_jeong 0:9eb0a8cff892 152 );
daniel_gs_jeong 0:9eb0a8cff892 153
daniel_gs_jeong 0:9eb0a8cff892 154 for(iarg = 0; iarg < func_table[icnt].argc; iarg++) {
daniel_gs_jeong 0:9eb0a8cff892 155 serial->printf(" 0x%x 0x%x",
daniel_gs_jeong 0:9eb0a8cff892 156 func_table[icnt].arg_min[iarg],
daniel_gs_jeong 0:9eb0a8cff892 157 func_table[icnt].arg_max[iarg]
daniel_gs_jeong 0:9eb0a8cff892 158 );
daniel_gs_jeong 0:9eb0a8cff892 159 }
daniel_gs_jeong 0:9eb0a8cff892 160 serial->printf("\n");
daniel_gs_jeong 0:9eb0a8cff892 161 }
daniel_gs_jeong 0:9eb0a8cff892 162 return;
daniel_gs_jeong 0:9eb0a8cff892 163 }
daniel_gs_jeong 0:9eb0a8cff892 164
daniel_gs_jeong 0:9eb0a8cff892 165
daniel_gs_jeong 0:9eb0a8cff892 166 int SERIAL_GUI_INTERFACE::get_argi(int _n)
daniel_gs_jeong 0:9eb0a8cff892 167 {
daniel_gs_jeong 0:9eb0a8cff892 168 unsigned int hex_value
daniel_gs_jeong 0:9eb0a8cff892 169 = std::strtoul(argv[_n], 0, 16);
daniel_gs_jeong 0:9eb0a8cff892 170 return 0;
daniel_gs_jeong 0:9eb0a8cff892 171 }
daniel_gs_jeong 0:9eb0a8cff892 172
daniel_gs_jeong 0:9eb0a8cff892 173 char* SERIAL_GUI_INTERFACE::get_arg(int _n)
daniel_gs_jeong 0:9eb0a8cff892 174 {
daniel_gs_jeong 0:9eb0a8cff892 175 return argv[_n];
daniel_gs_jeong 0:9eb0a8cff892 176 }
daniel_gs_jeong 0:9eb0a8cff892 177
daniel_gs_jeong 0:9eb0a8cff892 178
daniel_gs_jeong 0:9eb0a8cff892 179 void SERIAL_GUI_INTERFACE::send_reg_value(int _reg, int _data)
daniel_gs_jeong 0:9eb0a8cff892 180 {
daniel_gs_jeong 0:9eb0a8cff892 181 serial->printf("0x%x = 0x%x", _reg, _data);
daniel_gs_jeong 0:9eb0a8cff892 182 }