forked from Nazrax/DreamMaskV4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.c
204 lines (180 loc) · 5.82 KB
/
commands.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
#include "globals.h"
#include "commands.h"
#include "serial.h"
#include "flash.h"
#include "alarm.h"
#include "morse.h"
#include "adc.h"
//#include "sensor.h"
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/wdt.h>
#include <stdio.h>
#include <string.h>
const char on[] PROGMEM = ": on";
const char off[] PROGMEM = ": off";
const char prompt[] PROGMEM = "\r\n> ";
const char badinput[] PROGMEM = "\r\nBad input";
const char executed[] PROGMEM = "\r\nCommand executed: ";
const char cmdresult[] PROGMEM = "\r\nResult:";
const char youentered[] PROGMEM = "\r\nYou entered:";
const char timeisnow[] PROGMEM = "\r\nThe time is now: ";
const char timepattern[] PROGMEM = "%02d:%02d:%02d";
static char* p;
static void handle_time(void);
static void handle_flash_verb(void);
static void handle_adc_verb(void);
static void handle_adc(void);
static void handle_spi_verb(void);
static void handle_spi(void);
static void handle_buf(void);
void handle_command() {
serial_in[serial_in_ctr] = '\0';
p = serial_out;
if (!strncmp_P((char*)serial_in, PSTR("TIME"), 4)) {
handle_time();
} else if (!strncmp_P((char*)serial_in, PSTR("ADCVERB"), 7)) {
handle_adc_verb();
} else if (!strncmp_P((char*)serial_in, PSTR("SPIVERB"), 7)) {
handle_spi_verb();
} else if (!strncmp_P((char*)serial_in, PSTR("FLASHVERB"), 9)) {
handle_flash_verb();
} else if (!strncmp_P((char*)serial_in, PSTR("ADC"), 3)) {
handle_adc();
} else if (!strncmp_P((char*)serial_in, PSTR("ALARM"), 4)) {
p += strlcpy_P(p, cmdresult, 64);
p += sprintf(p, " %d", alarm_time);
} else if (!strncmp_P((char*)serial_in, PSTR("SCAN"), 4)) {
flash_scan();
p += strlcpy_P(p, cmdresult, 64);
p += sprintf(p, " %03d %03d", flash_addr >> 8, flash_addr & 0xFF);
} else if (!strncmp_P((char*)serial_in, PSTR("ADDR"), 4)) {
p += strlcpy_P(p, cmdresult, 64);
p += sprintf(p, " %03d %03d", flash_addr >> 8, flash_addr & 0xFF);
} else if (!strncmp_P((char*)serial_in, PSTR("ERASE"), 5)) {
flash_erase();
p += strlcpy_P(p, executed, 64);
p += strlcpy_P(p, PSTR(" Erase"), 64);
} else if (!strncmp_P((char*)serial_in, PSTR("SPI"), 3)) {
handle_spi();
} else if (!strncmp_P((char*)serial_in, PSTR("SELF"), 4)) {
flash_select();
p += strlcpy_P(p, cmdresult, 64);
p += strlcpy_P(p, PSTR("Flash Selected"), 64);
} else if (!strncmp_P((char*)serial_in, PSTR("DSELF"), 5)) {
flash_deselect();
p += strlcpy_P(p, cmdresult, 64);
p += strlcpy_P(p, PSTR("Flash Deselected"), 64);
} else if (!strncmp_P((char*)serial_in, PSTR("VOLT"), 4)) {
//double v = adc_voltage();
uint16_t v = adc_voltage();
p += sprintf(p, "\r\nVoltage: %d", v);
} else if (!strncmp_P((char*)serial_in, PSTR("SUM"), 4)) {
uint32_t sum;
handle_time();
usart_send();
while (flag_serial_sending);
sum = flash_adler32();
sprintf(serial_out, "\r\nAdler32: %lu", sum);
usart_send();
while (flag_serial_sending);
p = serial_out;
handle_time();
} else if (!strncmp_P((char*)serial_in, PSTR("MORSE"), 5)) {
//strcpy_P(morse_out, PSTR("Dream"));
int k=0;
while (serial_in[k+5] != '\0') {
morse_out[k] = serial_in[k+5];
k++;
}
morse_out[k] = '\0';
morse_send();
p += strlcpy_P(p, PSTR("\n\rMorsing"), 64);
} else if (!strncmp_P((char*)serial_in, PSTR("BUF"), 3)) {
handle_buf();
} else if (!strncmp_P((char*)serial_in, PSTR("RESET"), 5)) {
// Use the WDT to reboot
wdt_enable(WDTO_15MS);
while(1) {}
} else if (strlen((char*)serial_in) == 0) {
// Do nothing
} else {
p += strlcpy_P(p, PSTR("\r\nUnknown: "), 64);
strncpy(p, (char*)serial_in, serial_in_ctr);
p += serial_in_ctr;
}
strcpy_P(p, prompt);
usart_send();
while (flag_serial_sending);
serial_in_ctr = 0;
flag_command_ready = false;
}
static void handle_time() {
if (serial_in_ctr == 10) {
clock.hours = (serial_in[4] - '0') * 10 + (serial_in[5] - '0');
clock.minutes = (serial_in[6] - '0') * 10 + (serial_in[7] - '0');
clock.seconds = (serial_in[8] - '0') * 10 + (serial_in[9] - '0');
p += strlcpy_P(p, executed, 64);
p += strlcpy_P(p, PSTR("TIME"), 64);
} else {
uint8_t timepatlen = strlen_P(timepattern);
char pattern[timepatlen+1];
strcpy_P(pattern, timepattern);
p += strlcpy_P(p, timeisnow, 64);
p += sprintf(p, pattern, clock.hours, clock.minutes, clock.seconds);
}
}
static void handle_spi() {
int i;
strlcpy_P(p, cmdresult, 64);
for(i=1; (i*3)+2<serial_in_ctr; i++) {
uint8_t j = i * 3;
uint8_t x = (serial_in[j] - '0') * 100 + (serial_in[j+1] - '0') * 10 + (serial_in[j+2]-'0');
uint8_t result = spi_send(x);
p += sprintf(p, "{%03d : %03d} ", x, result);
}
}
static void handle_buf() {
int i;
strlcpy_P(p, cmdresult, 64);
for(i=0; i<15; i++) {
p += sprintf(p, "%03u ", (int8_t)(flash_buf[i]) & 0xff);
}
}
static void handle_adc_verb() {
flag_adc_verbose = !flag_adc_verbose;
p += strlcpy_P(p, executed, 64);
p += strlcpy_P(p, PSTR("ADC verbose"), 64);
if (flag_adc_verbose)
p += strlcpy_P(p, on, 64);
else
p += strlcpy_P(p, off, 64);
}
static void handle_spi_verb() {
flag_spi_verbose = !flag_spi_verbose;
p += strlcpy_P(p, executed, 64);
p += strlcpy_P(p, PSTR("SPI verbose"), 64);
if (flag_spi_verbose)
p += strlcpy_P(p, on, 64);
else
p += strlcpy_P(p, off, 64);
}
static void handle_flash_verb() {
flag_flash_verbose = !flag_flash_verbose;
p += strlcpy_P(p, executed, 64);
p += strlcpy_P(p, PSTR("FLASH verbose"), 64);
if (flag_flash_verbose)
p += strlcpy_P(p, on, 64);
else
p += strlcpy_P(p, off, 64);
}
static void handle_adc() {
flag_want_adc = !flag_want_adc;
flag_want_header = true;
p += strlcpy_P(p, executed, 64);
p += strlcpy_P(p, PSTR("ADC"), 64);
if (flag_want_adc)
p += strlcpy_P(p, on, 64);
else
p += strlcpy_P(p, off, 64);
}