-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.c
409 lines (265 loc) · 10.1 KB
/
cmd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "cmd.h"
#include "usart.h"
#include "calc.h"
#include "display.h"
#include "gpio.h"
#include "ble.h"
char CMD_MSG_CURSOR[] PROGMEM = "> ";
char CMD_MSG_CRLF[] PROGMEM = "\r\n";
char CMD_MSG_OK[] PROGMEM = "OK\r\n";
char CMD_MSG_INVALID[] PROGMEM = "Invalid entry\r\n";
char CMD_MSG_VER[] PROGMEM = "NIXIETACH II Command-Line Interface\r\n" __DATE__ " " __TIME__ "\r\n";
char CMD_MSG_8CYL[] PROGMEM = "8 cylinders\r\n";
char CMD_MSG_6CYL[] PROGMEM = "6 cylinders\r\n";
char CMD_MSG_4CYL[] PROGMEM = "4 cylinders\r\n";
char CMD_MSG_LOG_SEG1[] PROGMEM = "RPM: ";
char CMD_MSG_LOG_SEG2[] PROGMEM = ", dwell: ";
char CMD_MSG_LOG_SEG3[] PROGMEM = " deg.\r\n";
char CMD_MSG_LOG_SEGx[] PROGMEM = "---";
char CMD_MSG_LOG_START[] PROGMEM = "Logging started\r\n";
char CMD_MSG_LOG_STOP[] PROGMEM = "Logging stopped\r\n";
char CMD_MSG_DISP_ON[] PROGMEM = "Display is on\r\n";
char CMD_MSG_DISP_OFF[] PROGMEM = "Display is off\r\n";
char CMD_MSG_BLE_ERROR[] PROGMEM = "Unexpected response from BLE module\r\n";
char CMD_TOK_VER[] PROGMEM = "VER";
char CMD_TOK_CYL[] PROGMEM = "CYL";
char CMD_TOK_RPM[] PROGMEM = "RPM";
char CMD_TOK_START[] PROGMEM = "START";
char CMD_TOK_STOP[] PROGMEM = "STOP";
char CMD_TOK_DISP[] PROGMEM = "DISP";
char CMD_TOK_ON[] PROGMEM = "ON";
char CMD_TOK_OFF[] PROGMEM = "OFF";
char CMD_TOK_RESET[] PROGMEM = "RESET";
char CMD_TOK_BLE[] PROGMEM = "BLE";
static uint8_t cmd_log_target;
uint8_t cmd_char_process (uint8_t cmd_wptr, char cmd_char, char *cmd_buffer, uint8_t cmd_intf)
{
// interpret character
switch (cmd_char)
{
case 0x0D: // carriage return
case 0x0A: // line feed
// append null terminator
cmd_buffer[cmd_wptr] = '\0';
// execute buffer contents
cmd_str_exec (cmd_buffer, cmd_intf);
// reset pointer
return (0);
case 0x7F: // backspace
// protect against under-run
if (cmd_wptr > 0)
{
// walk cursor back (terminal only)
if (cmd_intf == CMD_INTF_USART) usart_print_char (0x7F);
// decrement pointer
return (cmd_wptr - 1);
} else {
// leave pointer unchanged
return (cmd_wptr);
} // if (pointer)
default: // all other characters
// protect against over-run
if (cmd_wptr < CMD_BUFFER_SIZE)
{
// store character
cmd_buffer[cmd_wptr] = cmd_char;
// echo character (terminal only)
if (cmd_intf == CMD_INTF_USART) usart_print_char (cmd_char);
// increment pointer
return (cmd_wptr + 1);
} else {
// leave pointer unchanged
return (cmd_wptr);
} // if (pointer)
} // switch (character)
}
void cmd_str_exec (char *cmd_str, uint8_t cmd_intf)
{
char *cmd_token;
cmd_token = strtok (cmd_str, " ");
// force carriage return + line feed (terminal only)
if (cmd_intf == CMD_INTF_USART) usart_print_str_P (CMD_MSG_CRLF);
// interpret command
if (!cmd_token)
{
// print cursor (terminal only)
if (cmd_intf == CMD_INTF_USART) usart_print_str_P (CMD_MSG_CURSOR);
} else if (!(strcmp_P (strupr (cmd_token), CMD_TOK_VER))) {
// print version info
cmd_print_str_P (CMD_MSG_VER, cmd_intf);
// print cursor (terminal only)
if (cmd_intf == CMD_INTF_USART) usart_print_str_P (CMD_MSG_CURSOR);
} else if (!(strcmp_P (strupr (cmd_token), CMD_TOK_CYL))) {
// print state of cylinder-select switch
switch (CSW_STATE)
{
case CSW_STATE_8CYL:
cmd_print_str_P (CMD_MSG_8CYL, cmd_intf);
break;
case CSW_STATE_6CYL:
cmd_print_str_P (CMD_MSG_6CYL, cmd_intf);
break;
case CSW_STATE_4CYL:
cmd_print_str_P (CMD_MSG_4CYL, cmd_intf);
break;
default:
cmd_print_str_P (CMD_MSG_INVALID, cmd_intf);
} // switch (cylinder select)
// print cursor (terminal only)
if (cmd_intf == CMD_INTF_USART) usart_print_str_P (CMD_MSG_CURSOR);
} else if (!(strcmp_P (strupr (cmd_token), CMD_TOK_RPM))) {
// retrieve next argument
cmd_token = strtok (NULL, " ");
// interpret argument
if (!cmd_token)
{
// print RPM and dwell once
cmd_log (cmd_intf);
// print cursor (terminal only)
if (cmd_intf == CMD_INTF_USART) usart_print_str_P (CMD_MSG_CURSOR);
} else if (!(strcmp_P (strupr (cmd_token), CMD_TOK_START))) {
// set selected interface as logging target interface
cmd_log_target_set (cmd_intf, cmd_intf);
} else if (!(strcmp_P (strupr (cmd_token), CMD_TOK_STOP))) {
// reset logging target interface
cmd_log_target_set (CMD_INTF_NONE, cmd_intf);
} else {
// print error message
cmd_print_str_P (CMD_MSG_INVALID, cmd_intf);
// print cursor (terminal only)
if (cmd_intf == CMD_INTF_USART) usart_print_str_P (CMD_MSG_CURSOR);
} // if (argument)
} else if (!(strcmp_P (strupr (cmd_token), CMD_TOK_DISP))) {
// retrieve next argument
cmd_token = strtok (NULL, " ");
// interpret argument
if (!cmd_token)
{
// print display state
if (DISPLAY_STATE == DISPLAY_STATE_ON) cmd_print_str_P (CMD_MSG_DISP_ON, cmd_intf); else cmd_print_str_P (CMD_MSG_DISP_OFF, cmd_intf);
} else if (!(strcmp_P (strupr (cmd_token), CMD_TOK_ON))) {
// enable display
display_force_on ();
// print confirmation message
cmd_print_str_P (CMD_MSG_DISP_ON, cmd_intf);
} else if (!(strcmp_P (strupr (cmd_token), CMD_TOK_OFF))) {
// disable display
display_force_off ();
// print confirmation message
cmd_print_str_P (CMD_MSG_DISP_OFF, cmd_intf);
} else if (!(strcmp_P (strupr (cmd_token), CMD_TOK_RESET))) {
// reset display mode
display_mode_set (DISPLAY_MODE_NORM);
// print confirmation message
cmd_print_str_P (CMD_MSG_OK, cmd_intf);
} else {
// change display mode
display_mode_set (DISPLAY_MODE_DEMO);
// display argument
display_print_num (atoi (cmd_token));
// print confirmation message
cmd_print_str_P (CMD_MSG_OK, cmd_intf);
} // if (argument)
// print cursor (terminal only)
if (cmd_intf == CMD_INTF_USART) usart_print_str_P (CMD_MSG_CURSOR);
} else if (!(strcmp_P (strupr (cmd_token), CMD_TOK_BLE))) {
// prevent BLE commands from being sent over BLE
if (cmd_intf == CMD_INTF_BLE)
{
// display error message
ble_send_cmd_P (BLE_CMD_TYPE_UARTTX, CMD_MSG_INVALID);
} else {
// retrieve next argument
cmd_token = strtok (NULL, " ");
// interpret argument
if (!(strcmp_P (strupr (cmd_token), CMD_TOK_RESET)))
{
// toggle reset
ble_reset ();
// print confirmation message
usart_print_str_P (CMD_MSG_OK);
} else {
// send AT command
ble_send_cmd (BLE_CMD_TYPE_ATWRAP, cmd_token);
} // if (argument)
// print cursor
usart_print_str_P (CMD_MSG_CURSOR);
} // if (interface)
} else {
// print error message
cmd_print_str_P (CMD_MSG_INVALID, cmd_intf);
// print cursor (terminal only)
if (cmd_intf == CMD_INTF_USART) usart_print_str_P (CMD_MSG_CURSOR);
} // if (command)
}
void cmd_print_str (char *print_str, uint8_t cmd_intf)
{
// print string to selected interface
switch (cmd_intf)
{
case CMD_INTF_USART:
usart_print_str (print_str);
break;
case CMD_INTF_BLE:
ble_send_cmd (BLE_CMD_TYPE_UARTTX, print_str);
break;
} // switch (interface)
}
void cmd_print_str_P (char *print_str, uint8_t cmd_intf)
{
// print string (from program memory) to selected interface
switch (cmd_intf)
{
case CMD_INTF_USART:
usart_print_str_P (print_str);
break;
case CMD_INTF_BLE:
ble_send_cmd_P (BLE_CMD_TYPE_UARTTX, print_str);
break;
} // switch (interface)
}
void cmd_log (uint8_t cmd_intf)
{
char temp_str[8];
// print leading segment
cmd_print_str_P (CMD_MSG_LOG_SEG1, cmd_intf);
// print RPM (unless engine is stalled)
if (calc_rpm_get ()) utoa (calc_rpm_get (), temp_str, 10); else strcpy_P (temp_str, CMD_MSG_LOG_SEGx);
cmd_print_str (temp_str, cmd_intf);
// print middle segment
cmd_print_str_P (CMD_MSG_LOG_SEG2, cmd_intf);
// print dwell (unless engine is stalled)
if (calc_dwell_get ()) utoa (calc_dwell_get (), temp_str, 10); else strcpy_P (temp_str, CMD_MSG_LOG_SEGx);
cmd_print_str (temp_str, cmd_intf);
// print trailing segment
cmd_print_str_P (CMD_MSG_LOG_SEG3, cmd_intf);
}
void cmd_log_target_set (uint8_t log_target, uint8_t cmd_intf)
{
// set data logging target interface
cmd_log_target = log_target;
// print appropriate confirmation message
switch (cmd_log_target)
{
case CMD_INTF_USART:
case CMD_INTF_BLE:
cmd_print_str_P (CMD_MSG_LOG_START, cmd_intf);
break;
case CMD_INTF_NONE:
cmd_print_str_P (CMD_MSG_LOG_STOP, cmd_intf);
// print cursor (terminal only)
if (cmd_intf == CMD_INTF_USART) usart_print_str_P (CMD_MSG_CURSOR);
break;
} // switch (interface)
}
uint8_t cmd_log_target_get (void)
{
// get data logging target interface
return (cmd_log_target);
}