-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapplication.c
372 lines (321 loc) · 8.57 KB
/
application.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
#include "application.h"
#include "application-temperature.h"
#include "application-twi.h"
#include "application-pwm.h"
#include "application-light.h"
#include "stats.h"
#include "util.h"
#include "stackmonitor.h"
#include "eeprom.h"
#include <avr/pgmspace.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
typedef uint8_t (*application_function_t)(uint8_t nargs, uint8_t args[application_num_args][application_length_args], uint16_t size, uint8_t *dst);
typedef struct
{
const char command[7];
uint8_t required_args;
application_function_t function;
const char description[37];
} application_function_table_t;
static uint8_t cmd_led_timeout = 0;
static uint8_t heartbeat_led_timeout = 0;
static uint8_t application_function_dump(uint8_t nargs, uint8_t args[application_num_args][application_length_args], uint16_t size, uint8_t *dst);
static uint8_t application_function_help(uint8_t nargs, uint8_t args[application_num_args][application_length_args], uint16_t size, uint8_t *dst);
static uint8_t application_function_quit(uint8_t nargs, uint8_t args[application_num_args][application_length_args], uint16_t size, uint8_t *dst);
static uint8_t application_function_reset(uint8_t nargs, uint8_t args[application_num_args][application_length_args], uint16_t size, uint8_t *dst);
static uint8_t application_function_stack(uint8_t nargs, uint8_t args[application_num_args][application_length_args], uint16_t size, uint8_t *dst);
static uint8_t application_function_stats(uint8_t nargs, uint8_t args[application_num_args][application_length_args], uint16_t size, uint8_t *dst);
static const __flash application_function_table_t application_function_table[] =
{
{
"bgw",
1,
application_function_bg_write,
"write bandgap caliburation (V)",
},
{
"dump",
0,
application_function_dump,
"dump eeprom contents",
},
{
"help",
0,
application_function_help,
"help (command)",
},
{
"lightr",
1,
application_function_light_read,
"read light sensor (0)",
},
{
"lightw",
3,
application_function_light_write,
"write light cal. (0)/factor/offset",
},
{
"?",
0,
application_function_help,
"help (command)",
},
{
"q",
0,
application_function_quit,
"quit",
},
{
"quit",
0,
application_function_quit,
"quit",
},
{
"reset",
0,
application_function_reset,
"reset using watchdog timeout",
},
{
"S",
0,
application_function_stack,
"free memory (stack usage)",
},
{
"stack",
0,
application_function_stack,
"free memory (stack usage)",
},
{
"s",
0,
application_function_stats,
"statistics",
},
{
"stats",
0,
application_function_stats,
"statistics",
},
{
"tempr",
1,
application_function_temp_read,
"read temperature sensor (0/1)",
},
{
"tempw",
3,
application_function_temp_write,
"write temp cal. (0/1)/factor/offset",
},
{
"twia",
1,
application_function_twiaddress,
"set twi slave address",
},
{
"twir",
1,
application_function_twiread,
"read bytes from twi slave",
},
{
"twirst",
0,
application_function_twireset,
"twi interface reset",
},
{
"twiw",
1,
application_function_twiwrite,
"write bytes to twi slave",
},
{
"",
0,
(application_function_t)0,
"",
},
};
void application_init(void)
{
application_init_temp();
application_init_light();
application_init_pwm();
}
void application_periodic(void)
{
uint16_t missed_ticks;
if((missed_ticks = t1_unhandled) == 0)
return;
t1_unhandled = 0;
if(missed_ticks > t1_unhandled_max)
t1_unhandled_max = missed_ticks;
if(heartbeat_led_timeout > missed_ticks)
heartbeat_led_timeout -= missed_ticks;
else
{
heartbeat_led_timeout = 122;
PORTD ^= _BV(0);
}
if(cmd_led_timeout > missed_ticks)
cmd_led_timeout -= missed_ticks;
else
{
cmd_led_timeout = 0;
PORTD &= ~_BV(1);
}
application_periodic_pwm(missed_ticks);
}
int16_t application_content(uint16_t src_length, const uint8_t *src, uint16_t size, uint8_t *dst)
{
static const __flash char error_fmt_unknown[] = "Command \"%s\" unknown\n";
static const __flash char error_fmt_args[] = "Insufficient arguments: %d (%d required)\n";
uint8_t args[application_num_args][application_length_args];
uint8_t args_count, arg_current;
uint8_t src_current = 0;
uint8_t ws_skipped;
const application_function_table_t __flash *tableptr;
if((src_length == 0) || (src[0] == 0x0ff)) // telnet options
return(0);
for(args_count = 0; (src_length > 0) && (args_count < application_num_args);)
{
ws_skipped = 0;
for(arg_current = 0;
(src_length > 0) && (arg_current < (application_length_args - 1));
src_current++, src_length--)
{
if((src[src_current] <= ' ') || (src[src_current] > '~'))
{
if(!ws_skipped)
continue;
else
break;
}
ws_skipped = 1;
args[args_count][arg_current++] = src[src_current];
}
args[args_count][arg_current] = '\0';
if(arg_current)
args_count++;
while((src_length > 0) && (src[src_current] > ' ') && (src[src_current] <= '~'))
{
src_length--;
src_current++;
}
}
*dst = '\0';
cmd_led_timeout = 10;
PORTD |= _BV(1);
if(args_count == 0)
return(0);
for(tableptr = application_function_table; tableptr->function; tableptr++)
if(!strcmp_P((const char *)args[0], tableptr->command))
break;
if(tableptr->function)
{
if(args_count < (tableptr->required_args + 1))
{
snprintf_P((char *)dst, size, error_fmt_args, args_count - 1, tableptr->required_args);
return(strlen((char *)dst));
}
if(tableptr->function(args_count, args, size, dst))
return(strlen((const char *)dst));
else
return(-1);
}
snprintf_P((char *)dst, size, error_fmt_unknown, args[0]);
return(strlen((char *)dst));
}
static uint8_t application_function_dump(uint8_t nargs, uint8_t args[application_num_args][application_length_args], uint16_t size, uint8_t *dst)
{
static const __flash char format1[] = "> bandgap: %.5f\n";
static const __flash char format2[] = "> temp cal[%d]: *=%.5f / +=%.5f\n";
static const __flash char format3[] = "> light cal[%d]: *=%.5f / +=%.5f\n";
uint8_t index, offset;
offset = snprintf_P((char *)dst, size, format1, eeprom_read_bandgap());
dst += offset;
size -= offset;
for(index = 0; index < temp_cal_size; index++)
{
offset = snprintf_P((char *)dst, size, format2,
index, eeprom_read_temp_cal_factor(index), eeprom_read_temp_cal_offset(index));
dst += offset;
size -= offset;
}
for(index = 0; index < light_cal_size; index++)
{
offset = snprintf_P((char *)dst, size, format3,
index, eeprom_read_light_cal_factor(index), eeprom_read_light_cal_offset(index));
dst += offset;
size -= offset;
}
return(1);
}
static uint8_t application_function_help(uint8_t nargs, uint8_t args[application_num_args][application_length_args], uint16_t size, uint8_t *dst)
{
static const __flash char list_header[] = "> %S: %d args\n";
static const __flash char detail_header[] = "> %S[%d]: ";
static const __flash char detail_footer[] = "\n";
static const __flash char detail_error[] = "> no help for \"%s\"\n";
const application_function_table_t __flash *tableptr;
uint8_t offset;
if(nargs > 1)
{
for(tableptr = application_function_table; tableptr->function; tableptr++)
if(!strcmp_P((const char *)args[1], tableptr->command))
break;
if(tableptr->function)
{
snprintf_P((char *)dst, size, detail_header, tableptr->command, tableptr->required_args);
strlcat_P((char *)dst, tableptr->description, size);
strlcat_P((char *)dst, detail_footer, size);
}
else
snprintf_P((char *)dst, size, detail_error, (const char *)args[1]);
}
else
{
for(tableptr = application_function_table; tableptr->function; tableptr++)
{
offset = snprintf_P((char *)dst, size, list_header, tableptr->command, tableptr->required_args);
dst += offset;
size -= offset;
}
}
return(1);
}
static uint8_t application_function_quit(uint8_t nargs, uint8_t args[application_num_args][application_length_args], uint16_t size, uint8_t *dst)
{
return(0);
}
static uint8_t application_function_reset(uint8_t nargs, uint8_t args[application_num_args][application_length_args], uint16_t size, uint8_t *dst)
{
reset();
return(0);
}
static uint8_t application_function_stats(uint8_t nargs, uint8_t args[application_num_args][application_length_args], uint16_t size, uint8_t *dst)
{
stats_generate(size, dst);
return(1);
}
static uint8_t application_function_stack(uint8_t nargs, uint8_t args[application_num_args][application_length_args], uint16_t size, uint8_t *dst)
{
static const __flash char stackfree_fmt[] = "Stackmonitor: %d bytes free\n";
snprintf_P((char *)dst, (size_t)size, stackfree_fmt, stackmonitor_free());
return(1);
}