Set telemetry rate to 100ms
[fw/altos] / ao_cmd.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19
20 __xdata uint16_t ao_cmd_lex_i;
21 __xdata uint8_t ao_cmd_lex_c;
22 __xdata enum ao_cmd_status ao_cmd_status;
23 static __xdata uint8_t  lex_echo;
24
25 #define CMD_LEN 32
26
27 static __xdata uint8_t  cmd_line[CMD_LEN];
28 static __xdata uint8_t  cmd_len;
29 static __xdata uint8_t  cmd_i;
30
31 static void
32 put_string(char *s)
33 {
34         __xdata uint8_t c;
35         while (c = *s++)
36                 putchar(c);
37 }
38
39 static void
40 readline(void)
41 {
42         __xdata uint8_t c;
43         if (lex_echo)
44                 put_string("> ");
45         cmd_len = 0;
46         for (;;) {
47                 flush();
48                 c = getchar();
49                 /* backspace/delete */
50                 if (c == '\010' || c == '\177') {
51                         if (cmd_len != 0) {
52                                 if (lex_echo)
53                                         put_string("\010 \010");
54                                 --cmd_len;
55                         }
56                         continue;
57                 }
58
59                 /* ^U */
60                 if (c == '\025') {
61                         while (cmd_len != 0) {
62                                 if (lex_echo)
63                                         put_string("\010 \010");
64                                 --cmd_len;
65                         }
66                         continue;
67                 }
68
69                 /* map CR to NL */
70                 if (c == '\r')
71                         c = '\n';
72                 
73                 if (c == '\n') {
74                         if (lex_echo)
75                                 putchar('\n');
76                         break;
77                 }
78
79                 if (cmd_len >= CMD_LEN - 2) {
80                         if (lex_echo)
81                                 putchar('\007');
82                         continue;
83                 }
84                 cmd_line[cmd_len++] = c;
85                 if (lex_echo)
86                         putchar(c);
87         }
88         cmd_line[cmd_len++] = '\n';
89         cmd_line[cmd_len++] = '\0';
90         cmd_i = 0;
91 }
92
93 void
94 ao_cmd_lex(void)
95 {
96         ao_cmd_lex_c = '\n';
97         if (cmd_i < cmd_len)
98                 ao_cmd_lex_c = cmd_line[cmd_i++];
99 }
100
101 static void
102 putnibble(uint8_t v)
103 {
104         if (v < 10)
105                 putchar(v + '0');
106         else
107                 putchar(v + ('a' - 10));
108 }
109
110 void
111 ao_cmd_put16(uint16_t v)
112 {
113         int8_t i;
114         for (i = 3; i >= 0; i--)
115                 putnibble((v >> (i << 2)) & 0xf);
116 }
117
118 void
119 ao_cmd_put8(uint8_t v)
120 {
121         putnibble((v >> 4) & 0xf);
122         putnibble(v & 0xf);
123 }
124
125 void
126 ao_cmd_white(void)
127 {
128         while (ao_cmd_lex_c == ' ' || ao_cmd_lex_c == '\t')
129                 ao_cmd_lex();
130 }
131
132 void
133 ao_cmd_hex(void)
134 {
135         __xdata uint8_t r = ao_cmd_lex_error;
136         
137         ao_cmd_lex_i = 0;
138         ao_cmd_white();
139         for(;;) {
140                 if ('0' <= ao_cmd_lex_c && ao_cmd_lex_c <= '9')
141                         ao_cmd_lex_i = (ao_cmd_lex_i << 4) | (ao_cmd_lex_c - '0');
142                 else if ('a' <= ao_cmd_lex_c && ao_cmd_lex_c <= 'f')
143                         ao_cmd_lex_i = (ao_cmd_lex_i << 4) | (ao_cmd_lex_c - 'a' + 10);
144                 else if ('A' <= ao_cmd_lex_c && ao_cmd_lex_c <= 'F')
145                         ao_cmd_lex_i = (ao_cmd_lex_i << 4) | (ao_cmd_lex_c - 'A' + 10);
146                 else
147                         break;
148                 r = ao_cmd_success;
149                 ao_cmd_lex();
150         }
151         if (r != ao_cmd_success)
152                 ao_cmd_status = r;
153 }
154
155 #if 0
156 static void
157 decimal(void)
158 {
159         __xdata uint8_t r = ao_cmd_lex_error;
160         
161         ao_cmd_lex_i = 0;
162         ao_cmd_white();
163         for(;;) {
164                 if ('0' <= ao_cmd_lex_c && ao_cmd_lex_c <= '9')
165                         ao_cmd_lex_i = (ao_cmd_lex_i * 10 ) | (ao_cmd_lex_c - '0');
166                 else
167                         break;
168                 r = ao_cmd_success;
169                 ao_cmd_lex();
170         }
171         if (r != ao_cmd_success)
172                 ao_cmd_status = r;
173 }
174 #endif
175
176 static void
177 eol(void)
178 {
179         while (ao_cmd_lex_c != '\n')
180                 ao_cmd_lex();
181 }
182
183 static void
184 dump(void)
185 {
186         __xdata uint16_t c;
187         __xdata uint8_t * __xdata start, * __xdata end;
188
189         ao_cmd_hex();
190         start = (uint8_t __xdata *) ao_cmd_lex_i;
191         ao_cmd_hex();
192         end = (uint8_t __xdata *) ao_cmd_lex_i;
193         if (ao_cmd_status != ao_cmd_success)
194                 return;
195         c = 0;
196         while (start <= end) {
197                 if ((c & 7) == 0) {
198                         if (c)
199                                 putchar('\n');
200                         ao_cmd_put16((uint16_t) start);
201                 }
202                 putchar(' ');
203                 ao_cmd_put8(*start);
204                 ++c;
205                 start++;
206         }
207         putchar('\n');
208 }
209
210 static void
211 echo(void)
212 {
213         ao_cmd_hex();
214         lex_echo = ao_cmd_lex_i != 0;
215 }
216
217 static const uint8_t help_txt[] = "All numbers are in hex";
218
219 #define NUM_CMDS        8
220
221 static __code struct ao_cmds    *__xdata (ao_cmds[NUM_CMDS]);
222 static __xdata uint8_t          ao_ncmds;
223
224 static void
225 help(void)
226 {
227         __xdata uint8_t cmds;
228         __xdata uint8_t cmd;
229         __code struct ao_cmds * __xdata cs;
230         puts(help_txt);
231         for (cmds = 0; cmds < ao_ncmds; cmds++) {
232                 cs = ao_cmds[cmds];
233                 for (cmd = 0; cs[cmd].cmd != '\0'; cmd++)
234                         puts(cs[cmd].help);
235         }
236 }
237
238 static void
239 report(void)
240 {
241         switch(ao_cmd_status) {
242         case ao_cmd_lex_error:
243         case ao_cmd_syntax_error:
244                 puts("Syntax error");
245                 ao_cmd_status = 0;
246                 break;
247         }
248 }
249
250 void
251 ao_cmd_register(__code struct ao_cmds *cmds)
252 {
253         if (ao_ncmds >= NUM_CMDS)
254                 ao_panic(AO_PANIC_CMD);
255         ao_cmds[ao_ncmds++] = cmds;
256 }
257
258 void
259 ao_cmd(void *parameters)
260 {
261         __xdata uint8_t c;
262         __xdata uint8_t cmd, cmds;
263         __code struct ao_cmds * __xdata cs;
264         void (*__xdata func)(void);
265         (void) parameters;
266
267         lex_echo = 1;
268         for (;;) {
269                 readline();
270                 ao_cmd_lex();
271                 ao_cmd_white();
272                 c = ao_cmd_lex_c;
273                 ao_cmd_lex();
274                 if (c == '\r' || c == '\n')
275                         continue;
276                 func = (void (*)(void)) NULL;
277                 for (cmds = 0; cmds < ao_ncmds; cmds++) {
278                         cs = ao_cmds[cmds];
279                         for (cmd = 0; cs[cmd].cmd != '\0'; cmd++)
280                                 if (cs[cmd].cmd == c) {
281                                         func = cs[cmd].func;
282                                         break;
283                                 }
284                         if (func)
285                                 break;
286                 }
287                 if (func)
288                         (*func)();
289                 else
290                         ao_cmd_status = ao_cmd_syntax_error;
291                 report();
292         }
293 }
294
295 __xdata struct ao_task ao_cmd_task;
296
297 __code struct ao_cmds   ao_base_cmds[] = {
298         { '?', help,            "?                                  Print this message" },
299         { 'T', ao_task_info,    "T                                  Show task states" },
300         { 'E', echo,            "E <0 off, 1 on>                    Set command echo mode" },
301         { 'd', dump,            "d <start> <end>                    Dump memory" },
302         { 0,    help,   NULL },
303 };
304
305 void
306 ao_cmd_init(void)
307 {
308         ao_cmd_register(&ao_base_cmds[0]);
309         ao_add_task(&ao_cmd_task, ao_cmd, "cmd");
310 }