When debugging code it can be handy to examine/alter variables and to check the state of input lines. So your main program can run and you have access to alter variables and run functions from a terminal. In this sample the main program is just a loop that flashes an LED. In that loop a periodic call is made to ShellTC which handles any commands from the serial terminal. The code is a bit quirky(it was originally written for a very resource limited device) but it works well enough to be useful, hence its published. More details in the main.cpp
mon.cpp
00001 /* Copyright (c) <year> <copyright holders>, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 /* 00019 * the monitor support functions .. this needs shell to work 00020 * this just provides an array of commands and functions and the functions 00021 * themselves 00022 00023 This is based on the ancient GB3H8 code from 1992!!!!!!!!!!!!!!!!!!! 00024 (c)jont@ninelocks.com 1992-2012 00025 00026 Nasty nasty code intedned to help debug other bits of the project 00027 If you dont like the code, fix it :-) 00028 00029 To add your functions you need to put them in 3 places 00030 00031 search for JJT1 JJT2 JJT3 in this file 00032 */ 00033 00034 00035 #include "ctype.h" 00036 #include "shell.h" 00037 #include "mon.h" 00038 #include "mbed.h" 00039 00040 00041 /*======================================================================*/ 00042 /* the externals */ 00043 /*======================================================================*/ 00044 /* 00045 Things in the main program you want to support and need access to 00046 could be done via a header... feel free to make it so 00047 */ 00048 00049 extern DigitalOut myled3; 00050 extern AnalogIn a2d1; 00051 extern AnalogIn a2d2; 00052 extern AnalogIn a2d3; 00053 extern AnalogIn a2d4; 00054 extern AnalogIn a2d5; 00055 extern AnalogIn a2d6; 00056 00057 /*==========================================================================================*/ 00058 /*==========================================================================================*/ 00059 /* the functions for the array of commands */ 00060 /*==========================================================================================*/ 00061 //JJT1 add the declaration of your function here 00062 /*==========================================================================================*/ 00063 void command_help(void); 00064 void command_ledon(void); 00065 void command_ledoff(void); 00066 void command_a2d(void); 00067 void command_time(void); //show rtc datetime 00068 void command_utime(void); //setunixtime 00069 00070 00071 /*======================================================================*/ 00072 /* other supporting functions */ 00073 /*======================================================================*/ 00074 int convertHex(char *); /* convert a buffer to ascii decimal from hex */ 00075 00076 00077 /*======================================================================*/ 00078 /* typdefs */ 00079 /*======================================================================*/ 00080 00081 /* an struct of string for the command and pointer to function */ 00082 00083 /*typedef struct 00084 { 00085 char *commandp; 00086 void (*func_p)(void); 00087 } cmd_entry; 00088 */ 00089 00090 /* 00091 JJT2 put the function in this table along with the associated command you want to use in the terminal. 00092 */ 00093 const cmd_entry command_table[] = 00094 { 00095 "TIME", command_time, //show real time clock as human readable 00096 "HELP",command_help, //list the available commads 00097 "A2D", command_a2d, //show value of analogue to digital convertor inputs 00098 "UTIME", command_utime, //set the realtime clock with a unix time string 00099 "LEDON", command_ledon, //switch test led on 00100 "LEDOFF", command_ledoff, //switch test led off 00101 "",0,0 /* this terminates table */ 00102 }; 00103 00104 00105 /*======================================================================*/ 00106 /* globals */ 00107 /*======================================================================*/ 00108 00109 char valueChange = 0; //used to point to where argument starts in the 00110 //command buffer 00111 00112 00113 /*======================================================================*/ 00114 /* support functions */ 00115 /*======================================================================*/ 00116 00117 00118 /*======================================================================*/ 00119 /*= convert a buffer in ascii hex to ascii decimal */ 00120 /*======================================================================*/ 00121 /* 00122 * if successfull returns TRUE else false 00123 * up to caller what to do about it 00124 */ 00125 int convertHex(char *buffer) 00126 { 00127 char *oldBuffer; 00128 int number = 0; 00129 int digit; 00130 int length; 00131 oldBuffer = buffer; /* so we can use it later */ 00132 00133 length = strlen(buffer); 00134 if ( length > 8 || length == 0) //was 4 jt2012 00135 { 00136 return(false); 00137 } 00138 00139 while (*buffer != '\0') 00140 { 00141 if (isxdigit(*buffer) == 0) 00142 { 00143 return(false); 00144 } 00145 digit = *buffer; 00146 digit -= 0x30; 00147 if (digit > 9) /* A - F */ 00148 { 00149 digit -=7; 00150 } 00151 00152 number <<=4; /* shift left */ 00153 number |= digit; 00154 buffer++; 00155 } 00156 sprintf(oldBuffer, "%d", number); 00157 return(true); 00158 } 00159 00160 00161 /*======================================================================*/ 00162 /* here are the command handlers */ 00163 /*======================================================================*/ 00164 00165 //JJT3 heres where you add your functions 00166 /*======================================================================*/ 00167 /* command_help */ 00168 /*======================================================================*/ 00169 void command_help(void) 00170 { 00171 /* dumps all commands but only those you can use with your 00172 current level 00173 */ 00174 int i= 0; 00175 int size; /* no of characters op */ 00176 char column = 0; /* which column on page */ 00177 printf("\n\r* * * * HELP * * * *\n\r"); 00178 do 00179 { 00180 00181 printf("%s", command_table[i].commandp); 00182 size = strlen(command_table[i].commandp); 00183 if ( size < 8 ) 00184 { 00185 for(; size < 8; size++) 00186 { 00187 printf("%c",0x20); 00188 } 00189 } 00190 column++; 00191 if (column >= 8) 00192 { 00193 printf("\n\r"); 00194 column = 0; 00195 } 00196 00197 i++; /* display next entry */ 00198 } 00199 while(strcmp( command_table[i].commandp, "") != 0); 00200 00201 } 00202 00203 00204 00205 /*======================================================================*/ 00206 /*command_ledon */ 00207 /*======================================================================*/ 00208 void command_ledon(void) 00209 { 00210 myled3 = 1; 00211 } 00212 /*======================================================================*/ 00213 /*command_ledon */ 00214 /*======================================================================*/ 00215 void command_ledoff(void) 00216 { 00217 myled3 = 0; 00218 } 00219 00220 /*======================================================================*/ 00221 /*command_time */ 00222 /*======================================================================*/ 00223 void command_time(void) 00224 { 00225 time_t seconds = time(NULL); 00226 printf("%s\n", ctime(&seconds)); 00227 00228 } 00229 00230 /*======================================================================*/ 00231 /*Show Values read from A:" convertor */ 00232 /*======================================================================*/ 00233 void command_a2d() 00234 { 00235 printf("A2D Readings %.2f %.2f %.2f %.2f %.2f %.2f", a2d1.read(),a2d2.read(),a2d3.read(),a2d4.read(),a2d5.read(),a2d6.read()); 00236 } 00237 00238 /*======================================================================*/ 00239 /* Settime using a unixtime */ 00240 /*======================================================================*/ 00241 //on your desktop you get uninxtime with date +%s 00242 void command_utime() 00243 { 00244 time_t seconds; 00245 00246 seconds = atol(comBuff + valueChange); 00247 set_time(seconds); 00248 printf("Setting time to %s",ctime(&seconds)); 00249 00250 }
Generated on Sat Jul 16 2022 22:10:25 by 1.7.2