-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.cpp
304 lines (235 loc) · 5.44 KB
/
commands.cpp
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
#include <Arduino.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <SdFat.h>
#include "common.h"
#include "sd_log.h"
#include "timers.h"
static int scan_time(char *date, char *time,
unsigned short *y, tmElements_t *tm)
{
uint16_t numbers[3];
char *c;
int i = 0;
c = strtok(date, "-");
while (c && (i < 3)) {
numbers[i] = atoi(c);
c = strtok(NULL, "-");
i++;
}
if (i != 3)
return 1;
*y = numbers[0];
tm->Month = numbers[1];
tm->Day = numbers[2];
i = 0;
c = strtok(time, ":");
while (c && (i < 3)) {
numbers[i] = atoi(c);
c = strtok(NULL, ":");
i++;
}
if (i != 3)
return 1;
tm->Hour = numbers[0];
tm->Minute = numbers[1];
tm->Second = numbers[2];
return 0;
}
static char cmd_time(char argc, char *argv[]) {
unsigned short int y;
tmElements_t tm;
if (argc == 0) {
printTime(now());
return 0;
}
if (argc != 2) {
Serial.println(F("Usage: time YYYY-MM-DD HH:MM:SS"));
return 1;
}
if (scan_time(argv[0], argv[1], &y, &tm)) {
Serial.println(F("Cannot parse time"));
return 1;
}
tm.Year = y - 1970;
if (!RTC.write(tm))
Serial.println(F("RTC error"));
setTime(makeTime(tm));
if (!RTC.read(tm))
Serial.println(F("RTC error"));
printTime(now());
return 0;
}
static char cmd_ls(char, char *[]) {
if (!isSdReady())
return 1;
root.ls(LS_SIZE);
return 0;
}
static SdFile file;
static char filename[13];
static char cmd_dumpall(char, char *[])
{
if (!isSdReady())
return 1;
root.close();
root.open(sd_counter_dir, O_READ);
while (file.openNext(&root, O_RDONLY)) {
if (file.isDir())
goto next_file;
if (!file.getName(filename, sizeof(filename)))
goto next_file;
if (!strstr(filename, ".CSV"))
goto next_file;
Serial.println(filename);
dumpSdLog(filename);
next_file:
file.close();
}
return 0;
}
static char cmd_dump(char argc, char *argv[]) {
if (argc == 1)
return dumpSdLog(argv[0]);
else
return dumpSdLog(NULL);
return 0;
}
static char cmd_raw(char, char *[]) {
raw_measuring = !raw_measuring;
if (raw_measuring) {
if (raw_measuring)
stop_timer(IDLE_TIMEOUT_TIMER);
else
schedule_timer(IDLE_TIMEOUT_TIMER, IDLE_TIMEOUT_MS);
}
return 0;
}
static char cmd_batt(char, char *[]) {
Serial.println(readBattery_mV());
return 0;
}
static char cmd_poff(char, char *[]) {
digitalWrite(LED_PIN, 1);
digitalWrite(PWR_ON_PIN, 0);
return 0;
}
static char cmd_bt(char argc, char *argv[]) {
if (argc != 1) {
Serial.print(F("Bluetooth "));
if (bluetooth_mode == BT_PERMANENT)
Serial.println(F("ON"));
else
Serial.println(F("AUTO"));
} else {
if (!strcmp(argv[0], "on")) {
set_bluetooth_mode(BT_PERMANENT);
} else if (!strcmp(argv[0], "auto")) {
set_bluetooth_mode(BT_AUTO);
}
}
return 0;
}
static char cmd_ver(char, char *[])
{
Serial.println(F("Build: " __DATE__ " " __TIME__));
return 0;
}
static char cmd_help(char, char *[]) {
puts_P(PSTR(
"Commands:\n"
" time [yyyy-mm-dd HH:MM:SS] \t - get/set time\n"
" raw\t\t\t\t - toggle raw dump\n"
" ls\t\t\t\t - list files on SD\n"
" dump [file]\t\t\t - dump file content\n"
" dumpall [file]\t\t\t - dump all logs content\n"
" batt\t\t\t\t - show battery voltage\n"
" bt [on|auto]\t\t\t - bluetooth enabled permanently/on demand (by magnet)\n"
" ver \t\t\t\t – show firmware version\n"
));
return 0;
}
typedef char (*cmd_handler_t)(char argc, char *argv[]);
struct cmd_table_entry {
PGM_P cmd;
cmd_handler_t handler;
};
static const char cmd_time_name[] PROGMEM = "time";
static const char cmd_ls_name[] PROGMEM = "ls";
static const char cmd_dump_name[] PROGMEM = "dump";
static const char cmd_dumpall_name[] PROGMEM = "dumpall";
static const char cmd_raw_name[] PROGMEM = "raw";
static const char cmd_poff_name[] PROGMEM = "poff";
static const char cmd_batt_name[] PROGMEM = "batt";
static const char cmd_bt_name[] PROGMEM = "bt";
static const char cmd_ver_name[] PROGMEM = "ver";
static const char cmd_help_name[] PROGMEM = "help";
#define CMD(name) {cmd_ ## name ## _name, cmd_ ## name}
static PROGMEM const struct cmd_table_entry cmd_table[] = {
CMD(time),
CMD(ls),
CMD(dump),
CMD(dumpall),
CMD(raw),
CMD(poff),
CMD(batt),
CMD(bt),
CMD(ver),
CMD(help),
};
#define MAX_CMD_ARGS 3
void parse_cmdline(char *buf, uint8_t) {
char *cmd = strtok(buf, " ");
struct cmd_table_entry cmd_entry;
char *argv[MAX_CMD_ARGS];
uint8_t argc = 0;
unsigned char i;
char ret = 1;
if (!cmd)
return;
while (argc < MAX_CMD_ARGS) {
argv[argc] = strtok(NULL, " ");
if (argv[argc])
argc++;
else
break;
}
for (i = 0; i < ARRAY_SIZE(cmd_table); i++) {
memcpy_P(&cmd_entry, &cmd_table[i], sizeof(cmd_entry));
if (!strcmp_P(cmd, cmd_entry.cmd)) {
ret = cmd_entry.handler(argc, argv);
break;
}
}
if (ret)
Serial.println("ERROR");
}
#define SERIAL_BUF_LEN 60
static char serial_buf[SERIAL_BUF_LEN];
static uint8_t serial_buf_level = 0;
void processSerial() {
char c;
while (Serial.available()) {
idle_sleep_mode = SLEEP_MODE_IDLE;
// digitalWrite(LED_PIN, 1);
schedule_timer(IDLE_TIMEOUT_TIMER, IDLE_TIMEOUT_MS);
c = Serial.read();
Serial.write(c);
// digitalWrite(LED_PIN, 1);
if ((c == '\r') || (c == '\n')) {
if (c == '\r')
Serial.write('\n');
serial_buf[serial_buf_level] = '\0';
serial_buf_level++;
parse_cmdline(serial_buf, serial_buf_level);
serial_buf_level = 0;
Serial.write("> ");
} else {
if (serial_buf_level < SERIAL_BUF_LEN - 1) {
serial_buf[serial_buf_level] = c;
serial_buf_level++;
}
}
}
// digitalWrite(LED_PIN, 0);
}