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.
Dependencies: NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed
Fork of ICE by
src/CommandParser/cmd.cpp@13:c80c283f9db2, 2016-09-07 (annotated)
- Committer:
- jmarkel44
- Date:
- Wed Sep 07 13:52:25 2016 +0000
- Revision:
- 13:c80c283f9db2
- Parent:
- 12:ea87887ca7ad
- Child:
- 14:cc916fa8dd11
general cleanup and usage or GLOBAL_mdot->resetCpu();
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jmarkel44 | 0:65cfa4873284 | 1 | /* |
jmarkel44 | 0:65cfa4873284 | 2 | * =============================================================== |
jmarkel44 | 0:65cfa4873284 | 3 | * Natural Tiny Shell (NT-Shell) Application example. |
jmarkel44 | 0:65cfa4873284 | 4 | * Version 0.0.6 |
jmarkel44 | 0:65cfa4873284 | 5 | * =============================================================== |
jmarkel44 | 0:65cfa4873284 | 6 | * Copyright (c) 2010-2011 Shinichiro Nakamura |
jmarkel44 | 0:65cfa4873284 | 7 | * |
jmarkel44 | 0:65cfa4873284 | 8 | * Permission is hereby granted, free of charge, to any person |
jmarkel44 | 0:65cfa4873284 | 9 | * obtaining a copy of this software and associated documentation |
jmarkel44 | 0:65cfa4873284 | 10 | * files (the "Software"), to deal in the Software without |
jmarkel44 | 0:65cfa4873284 | 11 | * restriction, including without limitation the rights to use, |
jmarkel44 | 0:65cfa4873284 | 12 | * copy, modify, merge, publish, distribute, sublicense, and/or |
jmarkel44 | 0:65cfa4873284 | 13 | * sell copies of the Software, and to permit persons to whom the |
jmarkel44 | 0:65cfa4873284 | 14 | * Software is furnished to do so, subject to the following |
jmarkel44 | 0:65cfa4873284 | 15 | * conditions: |
jmarkel44 | 0:65cfa4873284 | 16 | * |
jmarkel44 | 0:65cfa4873284 | 17 | * The above copyright notice and this permission notice shall be |
jmarkel44 | 0:65cfa4873284 | 18 | * included in all copies or substantial portions of the Software. |
jmarkel44 | 0:65cfa4873284 | 19 | * |
jmarkel44 | 0:65cfa4873284 | 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
jmarkel44 | 0:65cfa4873284 | 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
jmarkel44 | 0:65cfa4873284 | 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
jmarkel44 | 0:65cfa4873284 | 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
jmarkel44 | 0:65cfa4873284 | 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
jmarkel44 | 0:65cfa4873284 | 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
jmarkel44 | 0:65cfa4873284 | 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
jmarkel44 | 0:65cfa4873284 | 27 | * OTHER DEALINGS IN THE SOFTWARE. |
jmarkel44 | 0:65cfa4873284 | 28 | * =============================================================== |
jmarkel44 | 0:65cfa4873284 | 29 | */ |
jmarkel44 | 0:65cfa4873284 | 30 | |
jmarkel44 | 0:65cfa4873284 | 31 | #include "cmd.h" |
jmarkel44 | 0:65cfa4873284 | 32 | #include <mbed.h> |
jmarkel44 | 0:65cfa4873284 | 33 | #include "ntshell.h" |
jmarkel44 | 0:65cfa4873284 | 34 | #include "ntopt.h" |
jmarkel44 | 0:65cfa4873284 | 35 | #include "global.h" |
jmarkel44 | 0:65cfa4873284 | 36 | #include "ConfigurationHandler.h" |
jmarkel44 | 3:8ea4db957749 | 37 | #include "mDot.h" |
jmarkel44 | 0:65cfa4873284 | 38 | |
jmarkel44 | 0:65cfa4873284 | 39 | Serial serial(USBTX, USBRX); |
jmarkel44 | 0:65cfa4873284 | 40 | ntshell_t ntshell; |
jmarkel44 | 0:65cfa4873284 | 41 | |
jmarkel44 | 0:65cfa4873284 | 42 | typedef struct { |
jmarkel44 | 0:65cfa4873284 | 43 | char *command; // command (from shell) |
jmarkel44 | 0:65cfa4873284 | 44 | char *description; // descrption |
jmarkel44 | 0:65cfa4873284 | 45 | void (*func)(int argc, char **argv); // callback function |
jmarkel44 | 0:65cfa4873284 | 46 | } command_table_t; |
jmarkel44 | 0:65cfa4873284 | 47 | |
jmarkel44 | 0:65cfa4873284 | 48 | // see cmd.h |
jmarkel44 | 0:65cfa4873284 | 49 | const command_table_t cmdlist[] = { |
jmarkel44 | 0:65cfa4873284 | 50 | {"?", "help command", cmd_help }, |
jmarkel44 | 0:65cfa4873284 | 51 | {"create", "create a control", cmd_create }, |
jmarkel44 | 0:65cfa4873284 | 52 | {"destroy", "destroy a control", cmd_destroy }, |
jmarkel44 | 0:65cfa4873284 | 53 | {"heap", "show heap statistics", cmd_heap }, |
jmarkel44 | 0:65cfa4873284 | 54 | {"help", "help command", cmd_help }, |
jmarkel44 | 0:65cfa4873284 | 55 | {"modify", "modify a control", cmd_modify }, |
jmarkel44 | 0:65cfa4873284 | 56 | {"reset", "reset the controller", cmd_reset }, |
jmarkel44 | 0:65cfa4873284 | 57 | {"showControls", "display active controls", cmd_ShowControls }, |
jmarkel44 | 0:65cfa4873284 | 58 | {NULL, NULL, NULL} |
jmarkel44 | 0:65cfa4873284 | 59 | }; |
jmarkel44 | 0:65cfa4873284 | 60 | |
jmarkel44 | 0:65cfa4873284 | 61 | int func_read(char *buf, int cnt); |
jmarkel44 | 0:65cfa4873284 | 62 | int func_write(const char *buf, int cnt); |
jmarkel44 | 0:65cfa4873284 | 63 | int func_cb_ntshell(const char *text); |
jmarkel44 | 0:65cfa4873284 | 64 | void func_cb_ntopt(int argc, char **argv); |
jmarkel44 | 0:65cfa4873284 | 65 | |
jmarkel44 | 0:65cfa4873284 | 66 | /** |
jmarkel44 | 0:65cfa4873284 | 67 | * Serial read function. |
jmarkel44 | 0:65cfa4873284 | 68 | */ |
jmarkel44 | 0:65cfa4873284 | 69 | int func_read(char *buf, int cnt) |
jmarkel44 | 0:65cfa4873284 | 70 | { |
jmarkel44 | 0:65cfa4873284 | 71 | for (int i = 0; i < cnt; i++) { |
jmarkel44 | 0:65cfa4873284 | 72 | buf[i] = serial.getc(); |
jmarkel44 | 0:65cfa4873284 | 73 | } |
jmarkel44 | 0:65cfa4873284 | 74 | return 0; |
jmarkel44 | 0:65cfa4873284 | 75 | } |
jmarkel44 | 0:65cfa4873284 | 76 | |
jmarkel44 | 0:65cfa4873284 | 77 | /** |
jmarkel44 | 0:65cfa4873284 | 78 | * Serial write function. |
jmarkel44 | 0:65cfa4873284 | 79 | */ |
jmarkel44 | 0:65cfa4873284 | 80 | int func_write(const char *buf, int cnt) |
jmarkel44 | 0:65cfa4873284 | 81 | { |
jmarkel44 | 0:65cfa4873284 | 82 | for (int i = 0; i < cnt; i++) { |
jmarkel44 | 0:65cfa4873284 | 83 | serial.putc(buf[i]); |
jmarkel44 | 0:65cfa4873284 | 84 | } |
jmarkel44 | 0:65cfa4873284 | 85 | return 0; |
jmarkel44 | 0:65cfa4873284 | 86 | } |
jmarkel44 | 0:65cfa4873284 | 87 | |
jmarkel44 | 0:65cfa4873284 | 88 | /** |
jmarkel44 | 0:65cfa4873284 | 89 | * Callback function for ntshell module. |
jmarkel44 | 0:65cfa4873284 | 90 | */ |
jmarkel44 | 0:65cfa4873284 | 91 | int func_cb_ntshell(const char *text) |
jmarkel44 | 0:65cfa4873284 | 92 | { |
jmarkel44 | 0:65cfa4873284 | 93 | return ntopt_parse(text, func_cb_ntopt); |
jmarkel44 | 0:65cfa4873284 | 94 | } |
jmarkel44 | 0:65cfa4873284 | 95 | |
jmarkel44 | 0:65cfa4873284 | 96 | /** |
jmarkel44 | 0:65cfa4873284 | 97 | * Callback function for ntopt module. |
jmarkel44 | 0:65cfa4873284 | 98 | */ |
jmarkel44 | 0:65cfa4873284 | 99 | void func_cb_ntopt(int argc, char **argv) |
jmarkel44 | 0:65cfa4873284 | 100 | { |
jmarkel44 | 0:65cfa4873284 | 101 | if (argc == 0) { |
jmarkel44 | 0:65cfa4873284 | 102 | return; |
jmarkel44 | 0:65cfa4873284 | 103 | } |
jmarkel44 | 0:65cfa4873284 | 104 | int execnt = 0; |
jmarkel44 | 0:65cfa4873284 | 105 | const command_table_t *p = &cmdlist[0]; |
jmarkel44 | 0:65cfa4873284 | 106 | while (p->command != NULL) { |
jmarkel44 | 0:65cfa4873284 | 107 | if (strcmp(argv[0], p->command) == 0) { |
jmarkel44 | 0:65cfa4873284 | 108 | p->func(argc, argv); |
jmarkel44 | 0:65cfa4873284 | 109 | execnt++; |
jmarkel44 | 0:65cfa4873284 | 110 | } |
jmarkel44 | 0:65cfa4873284 | 111 | p++; |
jmarkel44 | 0:65cfa4873284 | 112 | } |
jmarkel44 | 0:65cfa4873284 | 113 | if (execnt == 0) { |
jmarkel44 | 0:65cfa4873284 | 114 | printf("Command not found.\r\n"); |
jmarkel44 | 0:65cfa4873284 | 115 | } |
jmarkel44 | 0:65cfa4873284 | 116 | wait_ms(250); |
jmarkel44 | 0:65cfa4873284 | 117 | } |
jmarkel44 | 0:65cfa4873284 | 118 | |
jmarkel44 | 0:65cfa4873284 | 119 | /************************* callback functions *******************************/ |
jmarkel44 | 0:65cfa4873284 | 120 | void cmd_help(int argc, char **argv) |
jmarkel44 | 0:65cfa4873284 | 121 | { |
jmarkel44 | 0:65cfa4873284 | 122 | UNUSED(argc); |
jmarkel44 | 0:65cfa4873284 | 123 | UNUSED(argv); |
jmarkel44 | 0:65cfa4873284 | 124 | |
jmarkel44 | 0:65cfa4873284 | 125 | const command_table_t *tblPtr = cmdlist; |
jmarkel44 | 0:65cfa4873284 | 126 | |
jmarkel44 | 0:65cfa4873284 | 127 | while (tblPtr->command) { |
jmarkel44 | 0:65cfa4873284 | 128 | printf("\r%-32s:\t%s\n", tblPtr->command, tblPtr->description); |
jmarkel44 | 0:65cfa4873284 | 129 | tblPtr++; |
jmarkel44 | 0:65cfa4873284 | 130 | } |
jmarkel44 | 0:65cfa4873284 | 131 | printf("\r\n"); |
jmarkel44 | 0:65cfa4873284 | 132 | } |
jmarkel44 | 0:65cfa4873284 | 133 | |
jmarkel44 | 0:65cfa4873284 | 134 | |
jmarkel44 | 0:65cfa4873284 | 135 | void cmd_ShowControls(int argc, char **argv) |
jmarkel44 | 0:65cfa4873284 | 136 | { |
jmarkel44 | 0:65cfa4873284 | 137 | UNUSED(argc); |
jmarkel44 | 0:65cfa4873284 | 138 | UNUSED(argv); |
jmarkel44 | 12:ea87887ca7ad | 139 | ConfigurationHandler_showControls(); |
jmarkel44 | 0:65cfa4873284 | 140 | } |
jmarkel44 | 0:65cfa4873284 | 141 | |
jmarkel44 | 0:65cfa4873284 | 142 | void cmd_reset(int argc, char **argv) |
jmarkel44 | 0:65cfa4873284 | 143 | { |
jmarkel44 | 0:65cfa4873284 | 144 | UNUSED(argc); |
jmarkel44 | 0:65cfa4873284 | 145 | UNUSED(argv); |
jmarkel44 | 13:c80c283f9db2 | 146 | //NVIC_SystemReset(); |
jmarkel44 | 13:c80c283f9db2 | 147 | GLOBAL_mdot->resetCpu(); |
jmarkel44 | 0:65cfa4873284 | 148 | } |
jmarkel44 | 0:65cfa4873284 | 149 | |
jmarkel44 | 0:65cfa4873284 | 150 | void cmd_create(int argc, char **argv) |
jmarkel44 | 0:65cfa4873284 | 151 | { |
jmarkel44 | 0:65cfa4873284 | 152 | if ( argc != 3 ) { |
jmarkel44 | 0:65cfa4873284 | 153 | printf("\r\nusage: create [controlName] [controlType]\n"); |
jmarkel44 | 0:65cfa4873284 | 154 | printf("\rcontrolType-> 0=timer, 1=PID, 2=setpoint, 3=composite, 4=manual\n"); |
jmarkel44 | 0:65cfa4873284 | 155 | return; |
jmarkel44 | 0:65cfa4873284 | 156 | } |
jmarkel44 | 0:65cfa4873284 | 157 | // send a message to the configuration handler to create the control |
jmarkel44 | 0:65cfa4873284 | 158 | Message_t *msg = MailBox.alloc(); |
jmarkel44 | 0:65cfa4873284 | 159 | memset(msg, 0, sizeof(Message_t)); |
jmarkel44 | 0:65cfa4873284 | 160 | msg->action = ACTION_CREATE; |
jmarkel44 | 0:65cfa4873284 | 161 | msg->control = (Control_t) atoi(argv[2]); |
jmarkel44 | 0:65cfa4873284 | 162 | strncpy(msg->controlFile, argv[1], sizeof(msg->controlFile)-1); |
jmarkel44 | 0:65cfa4873284 | 163 | |
jmarkel44 | 0:65cfa4873284 | 164 | printf("%s: Sending a create request for control %s type = %u\r\n", |
jmarkel44 | 0:65cfa4873284 | 165 | __func__, msg->controlFile, msg->control); |
jmarkel44 | 0:65cfa4873284 | 166 | |
jmarkel44 | 0:65cfa4873284 | 167 | MailBox.put(msg); |
jmarkel44 | 0:65cfa4873284 | 168 | return; |
jmarkel44 | 0:65cfa4873284 | 169 | } |
jmarkel44 | 0:65cfa4873284 | 170 | |
jmarkel44 | 0:65cfa4873284 | 171 | void cmd_destroy(int argc, char **argv) |
jmarkel44 | 0:65cfa4873284 | 172 | { |
jmarkel44 | 12:ea87887ca7ad | 173 | if ( argc != 3 ) { |
jmarkel44 | 12:ea87887ca7ad | 174 | printf("\r\nusage: destroy [controlName] [controlType]\n"); |
jmarkel44 | 12:ea87887ca7ad | 175 | printf("\rcontrolType-> 0=timer, 1=PID, 2=setpoint, 3=composite, 4=manual\n"); |
jmarkel44 | 0:65cfa4873284 | 176 | return; |
jmarkel44 | 0:65cfa4873284 | 177 | } |
jmarkel44 | 0:65cfa4873284 | 178 | |
jmarkel44 | 0:65cfa4873284 | 179 | // send a message to the configuration handler to destroy the control |
jmarkel44 | 0:65cfa4873284 | 180 | Message_t *msg = MailBox.alloc(); |
jmarkel44 | 0:65cfa4873284 | 181 | memset(msg, 0, sizeof(Message_t)); |
jmarkel44 | 0:65cfa4873284 | 182 | msg->action = ACTION_DESTROY; |
jmarkel44 | 12:ea87887ca7ad | 183 | msg->control = (Control_t) atoi(argv[2]); |
jmarkel44 | 0:65cfa4873284 | 184 | strncpy(msg->controlFile, argv[1], sizeof(msg->controlFile)-1); |
jmarkel44 | 0:65cfa4873284 | 185 | |
jmarkel44 | 0:65cfa4873284 | 186 | printf("%s: Sending a destroy request for control %s\r\n", |
jmarkel44 | 0:65cfa4873284 | 187 | __func__, msg->controlFile); |
jmarkel44 | 0:65cfa4873284 | 188 | |
jmarkel44 | 0:65cfa4873284 | 189 | MailBox.put(msg); |
jmarkel44 | 0:65cfa4873284 | 190 | return; |
jmarkel44 | 0:65cfa4873284 | 191 | } |
jmarkel44 | 0:65cfa4873284 | 192 | |
jmarkel44 | 0:65cfa4873284 | 193 | void cmd_heap(int argc, char **argv) |
jmarkel44 | 0:65cfa4873284 | 194 | { |
jmarkel44 | 0:65cfa4873284 | 195 | UNUSED(argc), UNUSED(argv); |
jmarkel44 | 0:65cfa4873284 | 196 | __heapstats((__heapprt)fprintf,stderr); // print initial free heap size |
jmarkel44 | 0:65cfa4873284 | 197 | } |
jmarkel44 | 0:65cfa4873284 | 198 | |
jmarkel44 | 0:65cfa4873284 | 199 | void cmd_modify(int argc, char **argv) |
jmarkel44 | 0:65cfa4873284 | 200 | { |
jmarkel44 | 0:65cfa4873284 | 201 | // stubbed |
jmarkel44 | 3:8ea4db957749 | 202 | printf("\rNot yet implemented.\n"); |
jmarkel44 | 0:65cfa4873284 | 203 | return; |
jmarkel44 | 0:65cfa4873284 | 204 | } |
jmarkel44 | 0:65cfa4873284 | 205 | |
jmarkel44 | 0:65cfa4873284 | 206 | |
jmarkel44 | 0:65cfa4873284 | 207 | |
jmarkel44 | 0:65cfa4873284 | 208 |