-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.c
49 lines (41 loc) · 1.01 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
#include "cmd.h"
#include "usart.h"
static volatile uint8_t buf[CMD_BUF_SIZE];
static volatile struct command_t *commands[N_CMDS];
static uint8_t n_commands;
static struct command_t *current;
static uint8_t pos;
void cmd_register_handler(struct command_t *cmd) {
if(n_commands < N_CMDS)
commands[n_commands++] = cmd;
}
void cmd_reset(void) {
pos = 0;
current = 0;
}
void cmd_receive(uint8_t val) {
buf[pos++] = val;
if(pos == 1) {
current = 0;
for(uint8_t i = 0; i < n_commands; i++) {
if(commands[i]->id == (val & commands[i]->mask)) {
current = commands[i];
break;
}
}
if(!current) {
led2_set(1);
pos = 0;
}
}
if(current && pos == current->buflen) {
led1_tgl();
current->exec(buf);
usart_write('K');
pos = 0;
}
}
void cmd_receive_buf(uint8_t *buf, uint8_t len) {
for(uint8_t i = 0; i < len; i++)
receive(*(buf++));
}