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

Dependencies:   mbed

.

Committer:
jont
Date:
Fri Feb 06 09:46:44 2015 +0000
Revision:
4:107d2d3294da
Parent:
0:87e65dabdb95
Tweaked for GU Projects

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jont 0:87e65dabdb95 1 /* Copyright (c) <year> <copyright holders>, MIT License
jont 0:87e65dabdb95 2 *
jont 0:87e65dabdb95 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
jont 0:87e65dabdb95 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
jont 0:87e65dabdb95 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
jont 0:87e65dabdb95 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
jont 0:87e65dabdb95 7 * furnished to do so, subject to the following conditions:
jont 0:87e65dabdb95 8 *
jont 0:87e65dabdb95 9 * The above copyright notice and this permission notice shall be included in all copies or
jont 0:87e65dabdb95 10 * substantial portions of the Software.
jont 0:87e65dabdb95 11 *
jont 0:87e65dabdb95 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
jont 0:87e65dabdb95 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
jont 0:87e65dabdb95 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
jont 0:87e65dabdb95 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jont 0:87e65dabdb95 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
jont 0:87e65dabdb95 17 */
jont 0:87e65dabdb95 18 /*
jont 0:87e65dabdb95 19 * the monitor support functions .. this needs shell to work
jont 0:87e65dabdb95 20 * this just provides an array of commands and functions and the functions
jont 0:87e65dabdb95 21 * themselves
jont 0:87e65dabdb95 22
jont 0:87e65dabdb95 23 This is based on the ancient GB3H8 code from 1992!!!!!!!!!!!!!!!!!!!
jont 0:87e65dabdb95 24 (c)jont@ninelocks.com 1992-2012
jont 0:87e65dabdb95 25
jont 0:87e65dabdb95 26 Nasty nasty code intedned to help debug other bits of the project
jont 0:87e65dabdb95 27 If you dont like the code, fix it :-)
jont 0:87e65dabdb95 28
jont 0:87e65dabdb95 29 To add your functions you need to put them in 3 places
jont 0:87e65dabdb95 30
jont 0:87e65dabdb95 31 search for JJT1 JJT2 JJT3 in this file
jont 0:87e65dabdb95 32 */
jont 0:87e65dabdb95 33
jont 0:87e65dabdb95 34
jont 0:87e65dabdb95 35 #include "ctype.h"
jont 0:87e65dabdb95 36 #include "shell.h"
jont 0:87e65dabdb95 37 #include "mon.h"
jont 0:87e65dabdb95 38 #include "mbed.h"
jont 0:87e65dabdb95 39
jont 0:87e65dabdb95 40
jont 0:87e65dabdb95 41 /*======================================================================*/
jont 0:87e65dabdb95 42 /* the externals */
jont 0:87e65dabdb95 43 /*======================================================================*/
jont 0:87e65dabdb95 44 /*
jont 0:87e65dabdb95 45 Things in the main program you want to support and need access to
jont 0:87e65dabdb95 46 could be done via a header... feel free to make it so
jont 0:87e65dabdb95 47 */
jont 0:87e65dabdb95 48
jont 0:87e65dabdb95 49 extern DigitalOut myled3;
jont 0:87e65dabdb95 50 extern AnalogIn a2d1;
jont 0:87e65dabdb95 51 extern AnalogIn a2d2;
jont 0:87e65dabdb95 52 extern AnalogIn a2d3;
jont 0:87e65dabdb95 53 extern AnalogIn a2d4;
jont 0:87e65dabdb95 54 extern AnalogIn a2d5;
jont 0:87e65dabdb95 55 extern AnalogIn a2d6;
jont 0:87e65dabdb95 56
jont 0:87e65dabdb95 57 /*==========================================================================================*/
jont 0:87e65dabdb95 58 /*==========================================================================================*/
jont 0:87e65dabdb95 59 /* the functions for the array of commands */
jont 0:87e65dabdb95 60 /*==========================================================================================*/
jont 0:87e65dabdb95 61 //JJT1 add the declaration of your function here
jont 0:87e65dabdb95 62 /*==========================================================================================*/
jont 0:87e65dabdb95 63 void command_help(void);
jont 0:87e65dabdb95 64 void command_ledon(void);
jont 0:87e65dabdb95 65 void command_ledoff(void);
jont 0:87e65dabdb95 66 void command_a2d(void);
jont 0:87e65dabdb95 67 void command_time(void); //show rtc datetime
jont 0:87e65dabdb95 68 void command_utime(void); //setunixtime
jont 0:87e65dabdb95 69
jont 0:87e65dabdb95 70
jont 0:87e65dabdb95 71 /*======================================================================*/
jont 0:87e65dabdb95 72 /* other supporting functions */
jont 0:87e65dabdb95 73 /*======================================================================*/
jont 0:87e65dabdb95 74 int convertHex(char *); /* convert a buffer to ascii decimal from hex */
jont 0:87e65dabdb95 75
jont 0:87e65dabdb95 76
jont 0:87e65dabdb95 77 /*======================================================================*/
jont 0:87e65dabdb95 78 /* typdefs */
jont 0:87e65dabdb95 79 /*======================================================================*/
jont 0:87e65dabdb95 80
jont 0:87e65dabdb95 81 /* an struct of string for the command and pointer to function */
jont 0:87e65dabdb95 82
jont 0:87e65dabdb95 83 /*typedef struct
jont 0:87e65dabdb95 84 {
jont 0:87e65dabdb95 85 char *commandp;
jont 0:87e65dabdb95 86 void (*func_p)(void);
jont 0:87e65dabdb95 87 } cmd_entry;
jont 0:87e65dabdb95 88 */
jont 0:87e65dabdb95 89
jont 0:87e65dabdb95 90 /*
jont 0:87e65dabdb95 91 JJT2 put the function in this table along with the associated command you want to use in the terminal.
jont 0:87e65dabdb95 92 */
jont 0:87e65dabdb95 93 const cmd_entry command_table[] =
jont 0:87e65dabdb95 94 {
jont 0:87e65dabdb95 95 "TIME", command_time, //show real time clock as human readable
jont 0:87e65dabdb95 96 "HELP",command_help, //list the available commads
jont 0:87e65dabdb95 97 "A2D", command_a2d, //show value of analogue to digital convertor inputs
jont 0:87e65dabdb95 98 "UTIME", command_utime, //set the realtime clock with a unix time string
jont 0:87e65dabdb95 99 "LEDON", command_ledon, //switch test led on
jont 0:87e65dabdb95 100 "LEDOFF", command_ledoff, //switch test led off
jont 0:87e65dabdb95 101 "",0,0 /* this terminates table */
jont 0:87e65dabdb95 102 };
jont 0:87e65dabdb95 103
jont 0:87e65dabdb95 104
jont 0:87e65dabdb95 105 /*======================================================================*/
jont 0:87e65dabdb95 106 /* globals */
jont 0:87e65dabdb95 107 /*======================================================================*/
jont 0:87e65dabdb95 108
jont 0:87e65dabdb95 109 char valueChange = 0; //used to point to where argument starts in the
jont 0:87e65dabdb95 110 //command buffer
jont 0:87e65dabdb95 111
jont 0:87e65dabdb95 112
jont 0:87e65dabdb95 113 /*======================================================================*/
jont 0:87e65dabdb95 114 /* support functions */
jont 0:87e65dabdb95 115 /*======================================================================*/
jont 0:87e65dabdb95 116
jont 0:87e65dabdb95 117
jont 0:87e65dabdb95 118 /*======================================================================*/
jont 0:87e65dabdb95 119 /*= convert a buffer in ascii hex to ascii decimal */
jont 0:87e65dabdb95 120 /*======================================================================*/
jont 0:87e65dabdb95 121 /*
jont 0:87e65dabdb95 122 * if successfull returns TRUE else false
jont 0:87e65dabdb95 123 * up to caller what to do about it
jont 0:87e65dabdb95 124 */
jont 0:87e65dabdb95 125 int convertHex(char *buffer)
jont 0:87e65dabdb95 126 {
jont 0:87e65dabdb95 127 char *oldBuffer;
jont 0:87e65dabdb95 128 int number = 0;
jont 0:87e65dabdb95 129 int digit;
jont 0:87e65dabdb95 130 int length;
jont 0:87e65dabdb95 131 oldBuffer = buffer; /* so we can use it later */
jont 0:87e65dabdb95 132
jont 0:87e65dabdb95 133 length = strlen(buffer);
jont 0:87e65dabdb95 134 if ( length > 8 || length == 0) //was 4 jt2012
jont 0:87e65dabdb95 135 {
jont 0:87e65dabdb95 136 return(false);
jont 0:87e65dabdb95 137 }
jont 0:87e65dabdb95 138
jont 0:87e65dabdb95 139 while (*buffer != '\0')
jont 0:87e65dabdb95 140 {
jont 0:87e65dabdb95 141 if (isxdigit(*buffer) == 0)
jont 0:87e65dabdb95 142 {
jont 0:87e65dabdb95 143 return(false);
jont 0:87e65dabdb95 144 }
jont 0:87e65dabdb95 145 digit = *buffer;
jont 0:87e65dabdb95 146 digit -= 0x30;
jont 0:87e65dabdb95 147 if (digit > 9) /* A - F */
jont 0:87e65dabdb95 148 {
jont 0:87e65dabdb95 149 digit -=7;
jont 0:87e65dabdb95 150 }
jont 0:87e65dabdb95 151
jont 0:87e65dabdb95 152 number <<=4; /* shift left */
jont 0:87e65dabdb95 153 number |= digit;
jont 0:87e65dabdb95 154 buffer++;
jont 0:87e65dabdb95 155 }
jont 0:87e65dabdb95 156 sprintf(oldBuffer, "%d", number);
jont 0:87e65dabdb95 157 return(true);
jont 0:87e65dabdb95 158 }
jont 0:87e65dabdb95 159
jont 0:87e65dabdb95 160
jont 0:87e65dabdb95 161 /*======================================================================*/
jont 0:87e65dabdb95 162 /* here are the command handlers */
jont 0:87e65dabdb95 163 /*======================================================================*/
jont 0:87e65dabdb95 164
jont 0:87e65dabdb95 165 //JJT3 heres where you add your functions
jont 0:87e65dabdb95 166 /*======================================================================*/
jont 0:87e65dabdb95 167 /* command_help */
jont 0:87e65dabdb95 168 /*======================================================================*/
jont 0:87e65dabdb95 169 void command_help(void)
jont 0:87e65dabdb95 170 {
jont 0:87e65dabdb95 171 /* dumps all commands but only those you can use with your
jont 0:87e65dabdb95 172 current level
jont 0:87e65dabdb95 173 */
jont 0:87e65dabdb95 174 int i= 0;
jont 0:87e65dabdb95 175 int size; /* no of characters op */
jont 0:87e65dabdb95 176 char column = 0; /* which column on page */
jont 0:87e65dabdb95 177 printf("\n\r* * * * HELP * * * *\n\r");
jont 0:87e65dabdb95 178 do
jont 0:87e65dabdb95 179 {
jont 0:87e65dabdb95 180
jont 0:87e65dabdb95 181 printf("%s", command_table[i].commandp);
jont 0:87e65dabdb95 182 size = strlen(command_table[i].commandp);
jont 0:87e65dabdb95 183 if ( size < 8 )
jont 0:87e65dabdb95 184 {
jont 0:87e65dabdb95 185 for(; size < 8; size++)
jont 0:87e65dabdb95 186 {
jont 0:87e65dabdb95 187 printf("%c",0x20);
jont 0:87e65dabdb95 188 }
jont 0:87e65dabdb95 189 }
jont 0:87e65dabdb95 190 column++;
jont 0:87e65dabdb95 191 if (column >= 8)
jont 0:87e65dabdb95 192 {
jont 0:87e65dabdb95 193 printf("\n\r");
jont 0:87e65dabdb95 194 column = 0;
jont 0:87e65dabdb95 195 }
jont 0:87e65dabdb95 196
jont 0:87e65dabdb95 197 i++; /* display next entry */
jont 0:87e65dabdb95 198 }
jont 0:87e65dabdb95 199 while(strcmp( command_table[i].commandp, "") != 0);
jont 0:87e65dabdb95 200
jont 0:87e65dabdb95 201 }
jont 0:87e65dabdb95 202
jont 0:87e65dabdb95 203
jont 0:87e65dabdb95 204
jont 0:87e65dabdb95 205 /*======================================================================*/
jont 0:87e65dabdb95 206 /*command_ledon */
jont 0:87e65dabdb95 207 /*======================================================================*/
jont 0:87e65dabdb95 208 void command_ledon(void)
jont 0:87e65dabdb95 209 {
jont 0:87e65dabdb95 210 myled3 = 1;
jont 0:87e65dabdb95 211 }
jont 0:87e65dabdb95 212 /*======================================================================*/
jont 0:87e65dabdb95 213 /*command_ledon */
jont 0:87e65dabdb95 214 /*======================================================================*/
jont 0:87e65dabdb95 215 void command_ledoff(void)
jont 0:87e65dabdb95 216 {
jont 0:87e65dabdb95 217 myled3 = 0;
jont 0:87e65dabdb95 218 }
jont 0:87e65dabdb95 219
jont 0:87e65dabdb95 220 /*======================================================================*/
jont 0:87e65dabdb95 221 /*command_time */
jont 0:87e65dabdb95 222 /*======================================================================*/
jont 0:87e65dabdb95 223 void command_time(void)
jont 0:87e65dabdb95 224 {
jont 0:87e65dabdb95 225 time_t seconds = time(NULL);
jont 0:87e65dabdb95 226 printf("%s\n", ctime(&seconds));
jont 0:87e65dabdb95 227
jont 0:87e65dabdb95 228 }
jont 0:87e65dabdb95 229
jont 0:87e65dabdb95 230 /*======================================================================*/
jont 0:87e65dabdb95 231 /*Show Values read from A:" convertor */
jont 0:87e65dabdb95 232 /*======================================================================*/
jont 0:87e65dabdb95 233 void command_a2d()
jont 0:87e65dabdb95 234 {
jont 0:87e65dabdb95 235 printf("A2D Readings %.2f %.2f %.2f %.2f %.2f %.2f", a2d1.read(),a2d2.read(),a2d3.read(),a2d4.read(),a2d5.read(),a2d6.read());
jont 0:87e65dabdb95 236 }
jont 0:87e65dabdb95 237
jont 0:87e65dabdb95 238 /*======================================================================*/
jont 0:87e65dabdb95 239 /* Settime using a unixtime */
jont 0:87e65dabdb95 240 /*======================================================================*/
jont 0:87e65dabdb95 241 //on your desktop you get uninxtime with date +%s
jont 0:87e65dabdb95 242 void command_utime()
jont 0:87e65dabdb95 243 {
jont 0:87e65dabdb95 244 time_t seconds;
jont 0:87e65dabdb95 245
jont 0:87e65dabdb95 246 seconds = atol(comBuff + valueChange);
jont 0:87e65dabdb95 247 set_time(seconds);
jont 0:87e65dabdb95 248 printf("Setting time to %s",ctime(&seconds));
jont 0:87e65dabdb95 249
jont 0:87e65dabdb95 250 }