Code for autonomous rover for Sparkfun AVC. DataBus won 3rd in 2012 and the same code was used on Troubled Child, a 1986 Jeep Grand Wagoneer to win 1st in 2014.
Dependencies: mbed Watchdog SDFileSystem DigoleSerialDisp
UI/shell.cpp@12:5dfa1ab47838, 2018-11-29 (annotated)
- Committer:
- shimniok
- Date:
- Thu Nov 29 16:50:46 2018 +0000
- Revision:
- 12:5dfa1ab47838
- Parent:
- 0:a6a169de725f
- Child:
- 15:01fb4916a5cd
Updated sensors, removed FAT filesystem class (now part of mbed I think), Updated Buttons and Shell
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shimniok | 0:a6a169de725f | 1 | #include <stdio.h> |
shimniok | 12:5dfa1ab47838 | 2 | #include <string.h> |
shimniok | 0:a6a169de725f | 3 | #include "mbed.h" |
shimniok | 12:5dfa1ab47838 | 4 | #include "globals.h" |
shimniok | 12:5dfa1ab47838 | 5 | #include "updater.h" |
shimniok | 12:5dfa1ab47838 | 6 | #include "print.h" |
shimniok | 0:a6a169de725f | 7 | #include "DirHandle.h" |
shimniok | 12:5dfa1ab47838 | 8 | #include "SDFileSystem.h" |
shimniok | 0:a6a169de725f | 9 | #include "util.h" |
shimniok | 0:a6a169de725f | 10 | #include "Buttons.h" |
shimniok | 0:a6a169de725f | 11 | |
shimniok | 12:5dfa1ab47838 | 12 | #define MAXBUF 128 |
shimniok | 12:5dfa1ab47838 | 13 | #define MAXCMDARR 21 |
shimniok | 12:5dfa1ab47838 | 14 | |
shimniok | 12:5dfa1ab47838 | 15 | char cwd[MAXBUF]; |
shimniok | 12:5dfa1ab47838 | 16 | char buf[MAXBUF]; |
shimniok | 12:5dfa1ab47838 | 17 | char path[MAXBUF]; |
shimniok | 12:5dfa1ab47838 | 18 | int status; |
shimniok | 12:5dfa1ab47838 | 19 | bool debug=false; |
shimniok | 12:5dfa1ab47838 | 20 | bool done=false; |
shimniok | 0:a6a169de725f | 21 | |
shimniok | 12:5dfa1ab47838 | 22 | typedef struct { |
shimniok | 12:5dfa1ab47838 | 23 | const char *cmd; |
shimniok | 12:5dfa1ab47838 | 24 | int (*f)(char *arg0); |
shimniok | 12:5dfa1ab47838 | 25 | const char *desc; |
shimniok | 12:5dfa1ab47838 | 26 | } cmd; |
shimniok | 0:a6a169de725f | 27 | |
shimniok | 12:5dfa1ab47838 | 28 | extern int autonomousMode(); |
shimniok | 12:5dfa1ab47838 | 29 | extern int resetMe(); |
shimniok | 12:5dfa1ab47838 | 30 | extern int gyroSwing(); |
shimniok | 12:5dfa1ab47838 | 31 | |
shimniok | 12:5dfa1ab47838 | 32 | extern "C" { |
shimniok | 12:5dfa1ab47838 | 33 | |
shimniok | 12:5dfa1ab47838 | 34 | void shell(void *args); |
shimniok | 12:5dfa1ab47838 | 35 | void docmd(char *cmdline); |
shimniok | 0:a6a169de725f | 36 | void termInput(char *cmd); |
shimniok | 0:a6a169de725f | 37 | void resolveDirectory(char *newpath, char *path); |
shimniok | 12:5dfa1ab47838 | 38 | void splitName(const char *path, char *dirname, char *basename); |
shimniok | 12:5dfa1ab47838 | 39 | int dols(char *arg); |
shimniok | 12:5dfa1ab47838 | 40 | int docd(char *path); |
shimniok | 12:5dfa1ab47838 | 41 | int dopwd(char *s); |
shimniok | 12:5dfa1ab47838 | 42 | int dotouch(char *path); |
shimniok | 12:5dfa1ab47838 | 43 | int dorm(char *path); |
shimniok | 12:5dfa1ab47838 | 44 | int domkdir(char *path); |
shimniok | 12:5dfa1ab47838 | 45 | int dohead(char *path); |
shimniok | 12:5dfa1ab47838 | 46 | int docat(char *path); |
shimniok | 12:5dfa1ab47838 | 47 | int dosend(char *path); |
shimniok | 12:5dfa1ab47838 | 48 | int doprintfree(char *path); |
shimniok | 12:5dfa1ab47838 | 49 | int doexit(char *s); |
shimniok | 12:5dfa1ab47838 | 50 | int dodebug(char *s); |
shimniok | 12:5dfa1ab47838 | 51 | int dohelp(char *s); |
shimniok | 12:5dfa1ab47838 | 52 | int dogyroswing(char *s); |
shimniok | 12:5dfa1ab47838 | 53 | int doreset(char *s); |
shimniok | 12:5dfa1ab47838 | 54 | int doautonomous(char *s); |
shimniok | 12:5dfa1ab47838 | 55 | int dospeed(char *arg); |
shimniok | 12:5dfa1ab47838 | 56 | int dosteer(char *arg); |
shimniok | 12:5dfa1ab47838 | 57 | int dotimes(char *arg); |
shimniok | 0:a6a169de725f | 58 | |
shimniok | 12:5dfa1ab47838 | 59 | // TODO 3 multiple arguments |
shimniok | 0:a6a169de725f | 60 | |
shimniok | 12:5dfa1ab47838 | 61 | const cmd command[MAXCMDARR] = { |
shimniok | 12:5dfa1ab47838 | 62 | { "help", dohelp, "print this help" }, |
shimniok | 12:5dfa1ab47838 | 63 | { "ls", dols, "list files" }, |
shimniok | 12:5dfa1ab47838 | 64 | { "cd", docd, "change directory" }, |
shimniok | 12:5dfa1ab47838 | 65 | { "pwd", dopwd, "print working directory" }, |
shimniok | 12:5dfa1ab47838 | 66 | { "touch", dotouch, "create, update file" }, |
shimniok | 12:5dfa1ab47838 | 67 | { "mkdir", domkdir, "make directory" }, |
shimniok | 12:5dfa1ab47838 | 68 | { "head", dohead, "output first part of file" }, |
shimniok | 12:5dfa1ab47838 | 69 | { "cat", docat, "output file" }, |
shimniok | 12:5dfa1ab47838 | 70 | { "send", dosend, "send file to terminal" }, |
shimniok | 12:5dfa1ab47838 | 71 | { "rm", dorm, "remove file" }, |
shimniok | 12:5dfa1ab47838 | 72 | { "debug", dodebug, "toggle debug mode" }, |
shimniok | 12:5dfa1ab47838 | 73 | { "gyro", dogyroswing, "gyro swing" }, |
shimniok | 12:5dfa1ab47838 | 74 | { "auto", doautonomous, "run autonomous mode" }, |
shimniok | 12:5dfa1ab47838 | 75 | { "reset", doreset, "reset the MCU" }, |
shimniok | 12:5dfa1ab47838 | 76 | { "free", doprintfree, "heap bytes available" }, |
shimniok | 12:5dfa1ab47838 | 77 | { "speed", dospeed, "set speed servo output" }, |
shimniok | 12:5dfa1ab47838 | 78 | { "steer", dosteer, "set steering servo output" }, |
shimniok | 12:5dfa1ab47838 | 79 | { "time", dotimes, "display update timing stats" }, |
shimniok | 12:5dfa1ab47838 | 80 | // { "exit", doexit, "exit shell" }, |
shimniok | 12:5dfa1ab47838 | 81 | { 0, 0, 0 } |
shimniok | 12:5dfa1ab47838 | 82 | }; |
shimniok | 0:a6a169de725f | 83 | |
shimniok | 12:5dfa1ab47838 | 84 | void shell(void *args) { |
shimniok | 12:5dfa1ab47838 | 85 | char cmdline[MAXBUF]; |
shimniok | 12:5dfa1ab47838 | 86 | |
shimniok | 12:5dfa1ab47838 | 87 | pc.baud(115200); |
shimniok | 12:5dfa1ab47838 | 88 | |
shimniok | 0:a6a169de725f | 89 | strcpy(cwd, "/log"); |
shimniok | 0:a6a169de725f | 90 | |
shimniok | 12:5dfa1ab47838 | 91 | fputs("Type help for assistance.\n", stdout); |
shimniok | 12:5dfa1ab47838 | 92 | |
shimniok | 12:5dfa1ab47838 | 93 | status=0; |
shimniok | 12:5dfa1ab47838 | 94 | done=false; |
shimniok | 0:a6a169de725f | 95 | while (!done) { |
shimniok | 0:a6a169de725f | 96 | termInput(cmdline); |
shimniok | 12:5dfa1ab47838 | 97 | |
shimniok | 12:5dfa1ab47838 | 98 | // interrupt operation if keypad button is pressed |
shimniok | 0:a6a169de725f | 99 | if (keypad.pressed) { |
shimniok | 0:a6a169de725f | 100 | keypad.pressed = false; |
shimniok | 0:a6a169de725f | 101 | break; |
shimniok | 0:a6a169de725f | 102 | } |
shimniok | 12:5dfa1ab47838 | 103 | |
shimniok | 12:5dfa1ab47838 | 104 | docmd(cmdline); |
shimniok | 12:5dfa1ab47838 | 105 | } |
shimniok | 12:5dfa1ab47838 | 106 | fputs("exiting shell\n", stdout); |
shimniok | 12:5dfa1ab47838 | 107 | |
shimniok | 12:5dfa1ab47838 | 108 | return; |
shimniok | 12:5dfa1ab47838 | 109 | } |
shimniok | 12:5dfa1ab47838 | 110 | |
shimniok | 12:5dfa1ab47838 | 111 | |
shimniok | 12:5dfa1ab47838 | 112 | /** docmd |
shimniok | 12:5dfa1ab47838 | 113 | * Run a command by looking it up the command requested on the shell command line in the |
shimniok | 12:5dfa1ab47838 | 114 | * command array. If it's found, run the associated function. If not, print an error. |
shimniok | 12:5dfa1ab47838 | 115 | */ |
shimniok | 12:5dfa1ab47838 | 116 | void docmd(char *cmdline) { |
shimniok | 12:5dfa1ab47838 | 117 | char *arg; |
shimniok | 12:5dfa1ab47838 | 118 | char cmd[MAXBUF]; |
shimniok | 12:5dfa1ab47838 | 119 | bool found = false; |
shimniok | 12:5dfa1ab47838 | 120 | |
shimniok | 12:5dfa1ab47838 | 121 | arg = split(cmd, cmdline, MAXBUF, ' '); |
shimniok | 12:5dfa1ab47838 | 122 | if (strlen(cmd) > 0) { |
shimniok | 12:5dfa1ab47838 | 123 | //if (debug) fprintf(stdout, "cmdline:<%s> cmd:<%s> arg:<%s>\n", cmdline, cmd, arg); |
shimniok | 12:5dfa1ab47838 | 124 | |
shimniok | 12:5dfa1ab47838 | 125 | for (int i=0; command[i].cmd; i++) { |
shimniok | 12:5dfa1ab47838 | 126 | if (!strcmp(cmd, command[i].cmd)) { |
shimniok | 12:5dfa1ab47838 | 127 | found = true; |
shimniok | 12:5dfa1ab47838 | 128 | command[i].f(arg); |
shimniok | 12:5dfa1ab47838 | 129 | } |
shimniok | 12:5dfa1ab47838 | 130 | } |
shimniok | 12:5dfa1ab47838 | 131 | |
shimniok | 12:5dfa1ab47838 | 132 | if (!found) { |
shimniok | 12:5dfa1ab47838 | 133 | fputs(cmd, stdout); |
shimniok | 12:5dfa1ab47838 | 134 | fputs(": command not found\n", stdout); |
shimniok | 0:a6a169de725f | 135 | } |
shimniok | 0:a6a169de725f | 136 | } |
shimniok | 0:a6a169de725f | 137 | |
shimniok | 0:a6a169de725f | 138 | return; |
shimniok | 0:a6a169de725f | 139 | } |
shimniok | 0:a6a169de725f | 140 | |
shimniok | 0:a6a169de725f | 141 | |
shimniok | 0:a6a169de725f | 142 | /** termInput |
shimniok | 12:5dfa1ab47838 | 143 | * read input from the terminal |
shimniok | 0:a6a169de725f | 144 | */ |
shimniok | 0:a6a169de725f | 145 | void termInput(char *cmd) { |
shimniok | 0:a6a169de725f | 146 | int i=0; |
shimniok | 0:a6a169de725f | 147 | char c; |
shimniok | 0:a6a169de725f | 148 | bool done = false; |
shimniok | 0:a6a169de725f | 149 | |
shimniok | 12:5dfa1ab47838 | 150 | memset(cmd, 0, MAXBUF); |
shimniok | 12:5dfa1ab47838 | 151 | fputc('(', stdout); |
shimniok | 12:5dfa1ab47838 | 152 | fputs(cwd, stdout); |
shimniok | 12:5dfa1ab47838 | 153 | fputs(")# ", stdout); |
shimniok | 0:a6a169de725f | 154 | do { |
shimniok | 0:a6a169de725f | 155 | cmd[i] = 0; |
shimniok | 12:5dfa1ab47838 | 156 | c = fgetc(stdin); |
shimniok | 0:a6a169de725f | 157 | if (c == '\r') { // if return is hit, we're done, don't add \r to cmd |
shimniok | 0:a6a169de725f | 158 | done = true; |
shimniok | 12:5dfa1ab47838 | 159 | } else if (i < MAXBUF-1) { |
shimniok | 0:a6a169de725f | 160 | if (c == 0x7f || c == '\b') { // backspace or delete |
shimniok | 0:a6a169de725f | 161 | if (i > 0) { // if we're at the beginning, do nothing |
shimniok | 0:a6a169de725f | 162 | i--; |
shimniok | 12:5dfa1ab47838 | 163 | fputs("\b \b", stdout); |
shimniok | 12:5dfa1ab47838 | 164 | |
shimniok | 0:a6a169de725f | 165 | } |
shimniok | 0:a6a169de725f | 166 | } else { |
shimniok | 12:5dfa1ab47838 | 167 | fputc(c, stdout); |
shimniok | 0:a6a169de725f | 168 | cmd[i++] = c; |
shimniok | 0:a6a169de725f | 169 | } |
shimniok | 0:a6a169de725f | 170 | } |
shimniok | 0:a6a169de725f | 171 | } while (!done); |
shimniok | 12:5dfa1ab47838 | 172 | fputc('\n', stdout); |
shimniok | 12:5dfa1ab47838 | 173 | |
shimniok | 12:5dfa1ab47838 | 174 | |
shimniok | 0:a6a169de725f | 175 | } |
shimniok | 0:a6a169de725f | 176 | |
shimniok | 0:a6a169de725f | 177 | /** resolveDirectory |
shimniok | 0:a6a169de725f | 178 | * resolve the directory path provided, given the cwd |
shimniok | 0:a6a169de725f | 179 | */ |
shimniok | 0:a6a169de725f | 180 | void resolveDirectory(char *newpath, char *path) { |
shimniok | 0:a6a169de725f | 181 | char dirname[32], basename[16]; |
shimniok | 0:a6a169de725f | 182 | |
shimniok | 0:a6a169de725f | 183 | /** absolute path */ |
shimniok | 0:a6a169de725f | 184 | if (path[0] == '/') { |
shimniok | 0:a6a169de725f | 185 | strcpy(newpath, path); |
shimniok | 0:a6a169de725f | 186 | } |
shimniok | 0:a6a169de725f | 187 | /** relative path */ |
shimniok | 0:a6a169de725f | 188 | else { |
shimniok | 0:a6a169de725f | 189 | strcpy(newpath, cwd); |
shimniok | 0:a6a169de725f | 190 | if (path[0] != 0) { |
shimniok | 0:a6a169de725f | 191 | if (newpath[strlen(newpath)-1] != '/') |
shimniok | 0:a6a169de725f | 192 | strcat(newpath, "/"); |
shimniok | 0:a6a169de725f | 193 | strcat(newpath, path); |
shimniok | 0:a6a169de725f | 194 | } |
shimniok | 0:a6a169de725f | 195 | /** Resolve .. references */ |
shimniok | 0:a6a169de725f | 196 | splitName(newpath, dirname, basename); |
shimniok | 0:a6a169de725f | 197 | if (!strcmp(basename, "..")) { |
shimniok | 0:a6a169de725f | 198 | splitName(dirname, newpath, basename); |
shimniok | 0:a6a169de725f | 199 | } |
shimniok | 0:a6a169de725f | 200 | } |
shimniok | 0:a6a169de725f | 201 | } |
shimniok | 0:a6a169de725f | 202 | |
shimniok | 12:5dfa1ab47838 | 203 | /** splitCmd |
shimniok | 12:5dfa1ab47838 | 204 | * copy t to s until space is reached |
shimniok | 12:5dfa1ab47838 | 205 | * return location of delimiter+1 in t |
shimniok | 12:5dfa1ab47838 | 206 | * if space not found, return t pointing to end of string |
shimniok | 12:5dfa1ab47838 | 207 | * if s or t null, return null |
shimniok | 12:5dfa1ab47838 | 208 | */ |
shimniok | 0:a6a169de725f | 209 | char *splitCmd(char *s, char *t, int max) |
shimniok | 0:a6a169de725f | 210 | { |
shimniok | 0:a6a169de725f | 211 | int i = 0; |
shimniok | 0:a6a169de725f | 212 | |
shimniok | 0:a6a169de725f | 213 | if (s == 0 || t == 0) |
shimniok | 0:a6a169de725f | 214 | return 0; |
shimniok | 0:a6a169de725f | 215 | |
shimniok | 0:a6a169de725f | 216 | while (*t != 0 && i < max) { |
shimniok | 0:a6a169de725f | 217 | *s++ = *t++; |
shimniok | 0:a6a169de725f | 218 | i++; |
shimniok | 0:a6a169de725f | 219 | if (*t == ' ') { |
shimniok | 0:a6a169de725f | 220 | t++; |
shimniok | 0:a6a169de725f | 221 | break; |
shimniok | 0:a6a169de725f | 222 | } |
shimniok | 0:a6a169de725f | 223 | } |
shimniok | 0:a6a169de725f | 224 | *s = 0; |
shimniok | 12:5dfa1ab47838 | 225 | |
shimniok | 0:a6a169de725f | 226 | return t; |
shimniok | 0:a6a169de725f | 227 | } |
shimniok | 0:a6a169de725f | 228 | |
shimniok | 0:a6a169de725f | 229 | /** splitName |
shimniok | 0:a6a169de725f | 230 | * split the path into a dirname and a |
shimniok | 0:a6a169de725f | 231 | */ |
shimniok | 12:5dfa1ab47838 | 232 | void splitName(const char *path, char *dirname, char *basename) { |
shimniok | 0:a6a169de725f | 233 | int sep; |
shimniok | 0:a6a169de725f | 234 | |
shimniok | 0:a6a169de725f | 235 | sep = 0; |
shimniok | 12:5dfa1ab47838 | 236 | //if (debug) fprintf(stdout, "%d\n", strlen(path)); |
shimniok | 0:a6a169de725f | 237 | for (int i=strlen(path)-1; i >= 0; i--) { |
shimniok | 12:5dfa1ab47838 | 238 | //if (debug) fprintf(stdout, "- %c\n", path[i]); |
shimniok | 0:a6a169de725f | 239 | sep = i; |
shimniok | 0:a6a169de725f | 240 | if (path[i] == '/') break; |
shimniok | 0:a6a169de725f | 241 | } |
shimniok | 0:a6a169de725f | 242 | for (int j=0; j < sep; j++) { |
shimniok | 12:5dfa1ab47838 | 243 | //if (debug) fprintf(stdout, "> %c\n", path[j]); |
shimniok | 0:a6a169de725f | 244 | dirname[j] = path[j]; |
shimniok | 0:a6a169de725f | 245 | dirname[j+1] = 0; |
shimniok | 0:a6a169de725f | 246 | } |
shimniok | 12:5dfa1ab47838 | 247 | for (unsigned int k=sep+1; k != strlen(path); k++) { |
shimniok | 12:5dfa1ab47838 | 248 | //if (debug) fprintf(stdout, "* %c\n", path[k]); |
shimniok | 0:a6a169de725f | 249 | basename[k-(sep+1)] = path[k]; |
shimniok | 0:a6a169de725f | 250 | basename[k-sep] = 0; |
shimniok | 0:a6a169de725f | 251 | } |
shimniok | 12:5dfa1ab47838 | 252 | //if (debug) fprintf(stdout, "d:<%s> b:<%s>\n", dirname, basename); |
shimniok | 12:5dfa1ab47838 | 253 | |
shimniok | 12:5dfa1ab47838 | 254 | |
shimniok | 0:a6a169de725f | 255 | } |
shimniok | 0:a6a169de725f | 256 | |
shimniok | 0:a6a169de725f | 257 | /** ls |
shimniok | 0:a6a169de725f | 258 | * lists files in the current working directory, 4 columns |
shimniok | 0:a6a169de725f | 259 | */ |
shimniok | 12:5dfa1ab47838 | 260 | int dols(char *arg) { |
shimniok | 12:5dfa1ab47838 | 261 | //if (debug) fprintf(stdout, "%s\n", cwd); |
shimniok | 0:a6a169de725f | 262 | DIR *d; |
shimniok | 0:a6a169de725f | 263 | struct dirent *p; |
shimniok | 12:5dfa1ab47838 | 264 | |
shimniok | 12:5dfa1ab47838 | 265 | resolveDirectory(path, arg); |
shimniok | 12:5dfa1ab47838 | 266 | |
shimniok | 0:a6a169de725f | 267 | int count=0; |
shimniok | 0:a6a169de725f | 268 | if ((d = opendir(path)) != NULL) { |
shimniok | 0:a6a169de725f | 269 | while ((p = readdir(d)) != NULL) { |
shimniok | 12:5dfa1ab47838 | 270 | int pad = 20 - strlen(p->d_name); |
shimniok | 12:5dfa1ab47838 | 271 | while (pad-- > 0) fputc(' ', stdout); |
shimniok | 12:5dfa1ab47838 | 272 | fputs(p->d_name, stdout); |
shimniok | 0:a6a169de725f | 273 | if (count++ >= 3) { |
shimniok | 0:a6a169de725f | 274 | count = 0; |
shimniok | 12:5dfa1ab47838 | 275 | fputc('\n', stdout); |
shimniok | 0:a6a169de725f | 276 | } |
shimniok | 0:a6a169de725f | 277 | } |
shimniok | 12:5dfa1ab47838 | 278 | fputc('\n', stdout); |
shimniok | 0:a6a169de725f | 279 | if (count < 3) |
shimniok | 12:5dfa1ab47838 | 280 | fputc('\n', stdout); |
shimniok | 0:a6a169de725f | 281 | closedir(d); |
shimniok | 12:5dfa1ab47838 | 282 | status = 0; |
shimniok | 0:a6a169de725f | 283 | } else { |
shimniok | 12:5dfa1ab47838 | 284 | fputs(path, stdout); |
shimniok | 12:5dfa1ab47838 | 285 | fputs(": No such directory\n", stdout); |
shimniok | 12:5dfa1ab47838 | 286 | status = 1; |
shimniok | 0:a6a169de725f | 287 | } |
shimniok | 12:5dfa1ab47838 | 288 | |
shimniok | 12:5dfa1ab47838 | 289 | return status; |
shimniok | 0:a6a169de725f | 290 | } |
shimniok | 0:a6a169de725f | 291 | |
shimniok | 0:a6a169de725f | 292 | /** cd |
shimniok | 0:a6a169de725f | 293 | * changes current working directory |
shimniok | 0:a6a169de725f | 294 | */ |
shimniok | 12:5dfa1ab47838 | 295 | int docd(char *arg) { |
shimniok | 12:5dfa1ab47838 | 296 | |
shimniok | 12:5dfa1ab47838 | 297 | resolveDirectory(path, arg); |
shimniok | 0:a6a169de725f | 298 | strcpy(cwd, path); |
shimniok | 12:5dfa1ab47838 | 299 | |
shimniok | 12:5dfa1ab47838 | 300 | return 0; |
shimniok | 0:a6a169de725f | 301 | } |
shimniok | 0:a6a169de725f | 302 | |
shimniok | 0:a6a169de725f | 303 | /** pwd |
shimniok | 0:a6a169de725f | 304 | * print current working directory |
shimniok | 0:a6a169de725f | 305 | */ |
shimniok | 12:5dfa1ab47838 | 306 | int dopwd(char *arg) { |
shimniok | 12:5dfa1ab47838 | 307 | fputs(cwd, stdout); |
shimniok | 12:5dfa1ab47838 | 308 | fputc('\n', stdout); |
shimniok | 12:5dfa1ab47838 | 309 | //fprintf(stdout, "%s\n", cwd); |
shimniok | 12:5dfa1ab47838 | 310 | |
shimniok | 12:5dfa1ab47838 | 311 | return 0; |
shimniok | 0:a6a169de725f | 312 | } |
shimniok | 0:a6a169de725f | 313 | |
shimniok | 0:a6a169de725f | 314 | /** touch |
shimniok | 0:a6a169de725f | 315 | * create an empty file |
shimniok | 0:a6a169de725f | 316 | */ |
shimniok | 12:5dfa1ab47838 | 317 | int dotouch(char *arg) { |
shimniok | 0:a6a169de725f | 318 | FILE *fp; |
shimniok | 12:5dfa1ab47838 | 319 | |
shimniok | 12:5dfa1ab47838 | 320 | resolveDirectory(path, arg); |
shimniok | 0:a6a169de725f | 321 | if ((fp = fopen(path, "w")) != NULL) { |
shimniok | 0:a6a169de725f | 322 | fclose(fp); |
shimniok | 12:5dfa1ab47838 | 323 | status = 0; |
shimniok | 0:a6a169de725f | 324 | } else { |
shimniok | 12:5dfa1ab47838 | 325 | fputs(path, stdout); |
shimniok | 12:5dfa1ab47838 | 326 | fputs(": No such file\n", stdout); |
shimniok | 12:5dfa1ab47838 | 327 | status = 1; |
shimniok | 0:a6a169de725f | 328 | } |
shimniok | 12:5dfa1ab47838 | 329 | |
shimniok | 12:5dfa1ab47838 | 330 | return status; |
shimniok | 0:a6a169de725f | 331 | } |
shimniok | 0:a6a169de725f | 332 | |
shimniok | 12:5dfa1ab47838 | 333 | int dorm(char *arg) { |
shimniok | 12:5dfa1ab47838 | 334 | resolveDirectory(path, arg); |
shimniok | 12:5dfa1ab47838 | 335 | return remove(path); |
shimniok | 12:5dfa1ab47838 | 336 | } |
shimniok | 12:5dfa1ab47838 | 337 | |
shimniok | 12:5dfa1ab47838 | 338 | int domkdir(char *arg) { |
shimniok | 12:5dfa1ab47838 | 339 | resolveDirectory(path, arg); |
shimniok | 12:5dfa1ab47838 | 340 | return mkdir(path, S_IRWXU|S_IRWXG|S_IRWXO); |
shimniok | 12:5dfa1ab47838 | 341 | } |
shimniok | 12:5dfa1ab47838 | 342 | |
shimniok | 0:a6a169de725f | 343 | /** head |
shimniok | 0:a6a169de725f | 344 | * print the first 10 lines of a file |
shimniok | 0:a6a169de725f | 345 | */ |
shimniok | 12:5dfa1ab47838 | 346 | int dohead(char *arg) { |
shimniok | 0:a6a169de725f | 347 | FILE *fp; |
shimniok | 0:a6a169de725f | 348 | char line = 0; |
shimniok | 12:5dfa1ab47838 | 349 | |
shimniok | 12:5dfa1ab47838 | 350 | resolveDirectory(path, arg); |
shimniok | 0:a6a169de725f | 351 | if ((fp = fopen(path, "r")) != NULL) { |
shimniok | 0:a6a169de725f | 352 | while (!feof(fp) && line++ < 10) { |
shimniok | 0:a6a169de725f | 353 | fgets(buf, 128, fp); |
shimniok | 12:5dfa1ab47838 | 354 | fputs(buf, stdout); |
shimniok | 0:a6a169de725f | 355 | } |
shimniok | 0:a6a169de725f | 356 | fclose(fp); |
shimniok | 12:5dfa1ab47838 | 357 | status = 0; |
shimniok | 0:a6a169de725f | 358 | } else { |
shimniok | 12:5dfa1ab47838 | 359 | fputs(path, stdout); |
shimniok | 12:5dfa1ab47838 | 360 | fputs(": No such file\n", stdout); |
shimniok | 12:5dfa1ab47838 | 361 | status = 1; |
shimniok | 0:a6a169de725f | 362 | } |
shimniok | 12:5dfa1ab47838 | 363 | |
shimniok | 12:5dfa1ab47838 | 364 | return status; |
shimniok | 0:a6a169de725f | 365 | } |
shimniok | 0:a6a169de725f | 366 | |
shimniok | 0:a6a169de725f | 367 | /** cat |
shimniok | 0:a6a169de725f | 368 | * display the content of a file |
shimniok | 0:a6a169de725f | 369 | */ |
shimniok | 12:5dfa1ab47838 | 370 | int docat(char *arg) { |
shimniok | 0:a6a169de725f | 371 | FILE *fp; |
shimniok | 0:a6a169de725f | 372 | |
shimniok | 12:5dfa1ab47838 | 373 | resolveDirectory(path, arg); |
shimniok | 0:a6a169de725f | 374 | if ((fp = fopen(path, "r")) != NULL) { |
shimniok | 12:5dfa1ab47838 | 375 | while (fgets(buf, 127, fp)) { |
shimniok | 12:5dfa1ab47838 | 376 | fputs(buf, stdout); |
shimniok | 0:a6a169de725f | 377 | } |
shimniok | 0:a6a169de725f | 378 | fclose(fp); |
shimniok | 12:5dfa1ab47838 | 379 | status = 0; |
shimniok | 0:a6a169de725f | 380 | } else { |
shimniok | 12:5dfa1ab47838 | 381 | fputs(path, stdout); |
shimniok | 12:5dfa1ab47838 | 382 | fputs(": No such file\n", stdout); |
shimniok | 12:5dfa1ab47838 | 383 | status = 1; |
shimniok | 0:a6a169de725f | 384 | } |
shimniok | 12:5dfa1ab47838 | 385 | |
shimniok | 12:5dfa1ab47838 | 386 | return status; |
shimniok | 0:a6a169de725f | 387 | } |
shimniok | 0:a6a169de725f | 388 | |
shimniok | 0:a6a169de725f | 389 | /** send |
shimniok | 0:a6a169de725f | 390 | * Simple serial file transfer protocol |
shimniok | 0:a6a169de725f | 391 | * Initiates escape sequence: ^A^B, sends filename, ^C, and then file |
shimniok | 0:a6a169de725f | 392 | * contents followed by ^D |
shimniok | 0:a6a169de725f | 393 | */ |
shimniok | 12:5dfa1ab47838 | 394 | int dosend(char *arg) { |
shimniok | 0:a6a169de725f | 395 | FILE *fp; |
shimniok | 0:a6a169de725f | 396 | char dirname[32], basename[16]; |
shimniok | 0:a6a169de725f | 397 | |
shimniok | 12:5dfa1ab47838 | 398 | resolveDirectory(path, arg); |
shimniok | 0:a6a169de725f | 399 | if ((fp = fopen(path, "r")) != NULL) { |
shimniok | 0:a6a169de725f | 400 | splitName(path, dirname, basename); |
shimniok | 12:5dfa1ab47838 | 401 | fputc(0x01, stdout); |
shimniok | 12:5dfa1ab47838 | 402 | fputc(0x02, stdout); |
shimniok | 12:5dfa1ab47838 | 403 | fputs(basename, stdout); |
shimniok | 12:5dfa1ab47838 | 404 | fputc(0x03, stdout); |
shimniok | 0:a6a169de725f | 405 | while (!feof(fp)) { |
shimniok | 0:a6a169de725f | 406 | if (fgets(buf, 127, fp) != NULL) |
shimniok | 12:5dfa1ab47838 | 407 | fputs(buf, stdout); |
shimniok | 0:a6a169de725f | 408 | } |
shimniok | 0:a6a169de725f | 409 | fclose(fp); |
shimniok | 12:5dfa1ab47838 | 410 | fputc(0x04, stdout); |
shimniok | 12:5dfa1ab47838 | 411 | status = 0; |
shimniok | 0:a6a169de725f | 412 | } else { |
shimniok | 12:5dfa1ab47838 | 413 | fputs(path, stdout); |
shimniok | 12:5dfa1ab47838 | 414 | fputs(": No such file\n", stdout); |
shimniok | 12:5dfa1ab47838 | 415 | status = 1; |
shimniok | 12:5dfa1ab47838 | 416 | } |
shimniok | 12:5dfa1ab47838 | 417 | |
shimniok | 12:5dfa1ab47838 | 418 | return status; |
shimniok | 12:5dfa1ab47838 | 419 | } |
shimniok | 12:5dfa1ab47838 | 420 | |
shimniok | 12:5dfa1ab47838 | 421 | int doprintfree(char *arg) { |
shimniok | 12:5dfa1ab47838 | 422 | //printInt(stdout, xPortGetFreeHeapSize()); |
shimniok | 12:5dfa1ab47838 | 423 | fputs(" bytes free.\n", stdout); |
shimniok | 12:5dfa1ab47838 | 424 | |
shimniok | 12:5dfa1ab47838 | 425 | return 0; |
shimniok | 12:5dfa1ab47838 | 426 | } |
shimniok | 12:5dfa1ab47838 | 427 | |
shimniok | 12:5dfa1ab47838 | 428 | /** doexit |
shimniok | 12:5dfa1ab47838 | 429 | * set a flag to exit the shell |
shimniok | 12:5dfa1ab47838 | 430 | */ |
shimniok | 12:5dfa1ab47838 | 431 | int doexit(char *arg) { |
shimniok | 12:5dfa1ab47838 | 432 | done = true; |
shimniok | 12:5dfa1ab47838 | 433 | |
shimniok | 12:5dfa1ab47838 | 434 | return 0; |
shimniok | 12:5dfa1ab47838 | 435 | } |
shimniok | 12:5dfa1ab47838 | 436 | |
shimniok | 12:5dfa1ab47838 | 437 | /** dodebug |
shimniok | 12:5dfa1ab47838 | 438 | * toggle the debug state variable |
shimniok | 12:5dfa1ab47838 | 439 | */ |
shimniok | 12:5dfa1ab47838 | 440 | int dodebug(char *arg) { |
shimniok | 12:5dfa1ab47838 | 441 | debug = !debug; |
shimniok | 12:5dfa1ab47838 | 442 | |
shimniok | 12:5dfa1ab47838 | 443 | return 0; |
shimniok | 12:5dfa1ab47838 | 444 | } |
shimniok | 12:5dfa1ab47838 | 445 | |
shimniok | 12:5dfa1ab47838 | 446 | /** dohelp |
shimniok | 12:5dfa1ab47838 | 447 | * print the list of commands and descriptions |
shimniok | 12:5dfa1ab47838 | 448 | */ |
shimniok | 12:5dfa1ab47838 | 449 | int dohelp(char *arg) { |
shimniok | 12:5dfa1ab47838 | 450 | for (int i=0; command[i].cmd; i++) { |
shimniok | 12:5dfa1ab47838 | 451 | int pad = 10 - strlen(command[i].cmd); |
shimniok | 12:5dfa1ab47838 | 452 | while (pad--) fputc(' ', stdout); |
shimniok | 12:5dfa1ab47838 | 453 | fputs(command[i].cmd, stdout); |
shimniok | 12:5dfa1ab47838 | 454 | fputs(": ", stdout); |
shimniok | 12:5dfa1ab47838 | 455 | fputs(command[i].desc, stdout); |
shimniok | 12:5dfa1ab47838 | 456 | fputc('\n', stdout); |
shimniok | 0:a6a169de725f | 457 | } |
shimniok | 12:5dfa1ab47838 | 458 | |
shimniok | 12:5dfa1ab47838 | 459 | return 0; |
shimniok | 0:a6a169de725f | 460 | } |
shimniok | 12:5dfa1ab47838 | 461 | |
shimniok | 12:5dfa1ab47838 | 462 | /** dogyroswing |
shimniok | 12:5dfa1ab47838 | 463 | * perform gyro swing, call external function |
shimniok | 12:5dfa1ab47838 | 464 | */ |
shimniok | 12:5dfa1ab47838 | 465 | int dogyroswing(char *arg) { |
shimniok | 12:5dfa1ab47838 | 466 | gyroSwing(); |
shimniok | 12:5dfa1ab47838 | 467 | |
shimniok | 12:5dfa1ab47838 | 468 | return 0; |
shimniok | 12:5dfa1ab47838 | 469 | } |
shimniok | 12:5dfa1ab47838 | 470 | |
shimniok | 12:5dfa1ab47838 | 471 | /** doreset |
shimniok | 12:5dfa1ab47838 | 472 | * reset the processor |
shimniok | 12:5dfa1ab47838 | 473 | */ |
shimniok | 12:5dfa1ab47838 | 474 | int doreset(char *arg) { |
shimniok | 12:5dfa1ab47838 | 475 | resetMe(); |
shimniok | 12:5dfa1ab47838 | 476 | return 0; // won't ever reach this line... |
shimniok | 12:5dfa1ab47838 | 477 | } |
shimniok | 12:5dfa1ab47838 | 478 | |
shimniok | 12:5dfa1ab47838 | 479 | /** doautonomous |
shimniok | 12:5dfa1ab47838 | 480 | * call external doAutonomous mode to perform an autonomous run |
shimniok | 12:5dfa1ab47838 | 481 | */ |
shimniok | 12:5dfa1ab47838 | 482 | int doautonomous(char *arg) { |
shimniok | 12:5dfa1ab47838 | 483 | autonomousMode(); |
shimniok | 12:5dfa1ab47838 | 484 | |
shimniok | 12:5dfa1ab47838 | 485 | return 0; |
shimniok | 12:5dfa1ab47838 | 486 | } |
shimniok | 12:5dfa1ab47838 | 487 | |
shimniok | 12:5dfa1ab47838 | 488 | |
shimniok | 12:5dfa1ab47838 | 489 | int dospeed(char *arg) { |
shimniok | 12:5dfa1ab47838 | 490 | // TODO 3 dospeed() |
shimniok | 12:5dfa1ab47838 | 491 | // int v = atoi(arg); |
shimniok | 12:5dfa1ab47838 | 492 | // if (config.escMin < v && v < config.escMax { |
shimniok | 12:5dfa1ab47838 | 493 | // fputs("speed=", stdout); |
shimniok | 12:5dfa1ab47838 | 494 | // setThrottle(v); |
shimniok | 12:5dfa1ab47838 | 495 | // printFloat(stdout, getThrottle(), 4); |
shimniok | 12:5dfa1ab47838 | 496 | // fputc('\n', stdout); |
shimniok | 12:5dfa1ab47838 | 497 | // } |
shimniok | 12:5dfa1ab47838 | 498 | return 0; |
shimniok | 12:5dfa1ab47838 | 499 | } |
shimniok | 12:5dfa1ab47838 | 500 | |
shimniok | 12:5dfa1ab47838 | 501 | int dosteer(char *arg) { |
shimniok | 12:5dfa1ab47838 | 502 | float v = cvstof(arg); |
shimniok | 12:5dfa1ab47838 | 503 | fputs("angle=", stdout); |
shimniok | 12:5dfa1ab47838 | 504 | printFloat(stdout, v, 4); |
shimniok | 12:5dfa1ab47838 | 505 | fputs(" servo=", stdout); |
shimniok | 12:5dfa1ab47838 | 506 | setSteering(v); |
shimniok | 12:5dfa1ab47838 | 507 | // TODO 4 printFloat(stdout, getSteering(), 4); |
shimniok | 12:5dfa1ab47838 | 508 | fputc('\n', stdout); |
shimniok | 12:5dfa1ab47838 | 509 | return 0; |
shimniok | 12:5dfa1ab47838 | 510 | } |
shimniok | 12:5dfa1ab47838 | 511 | |
shimniok | 12:5dfa1ab47838 | 512 | int dotimes(char *arg) { |
shimniok | 12:5dfa1ab47838 | 513 | int i; |
shimniok | 12:5dfa1ab47838 | 514 | for (i = 1; i < 8; i++) { |
shimniok | 12:5dfa1ab47838 | 515 | printInt(stdout, i); |
shimniok | 12:5dfa1ab47838 | 516 | fputc(':', stdout); |
shimniok | 12:5dfa1ab47838 | 517 | // TODO 4 printInt(stdout, getUpdateTime(i)-getUpdateTime(i-1)); |
shimniok | 12:5dfa1ab47838 | 518 | fputc('\n', stdout); |
shimniok | 12:5dfa1ab47838 | 519 | } |
shimniok | 12:5dfa1ab47838 | 520 | fputs("total:", stdout); |
shimniok | 12:5dfa1ab47838 | 521 | // TODO 4 printInt(stdout, getUpdateTime(7)-getUpdateTime(0)); |
shimniok | 12:5dfa1ab47838 | 522 | fputc('\n', stdout); |
shimniok | 12:5dfa1ab47838 | 523 | return 0; |
shimniok | 12:5dfa1ab47838 | 524 | } |
shimniok | 12:5dfa1ab47838 | 525 | |
shimniok | 12:5dfa1ab47838 | 526 | } // extern C |