-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_buff.c
66 lines (54 loc) · 1.38 KB
/
cmd_buff.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
#include "cmd_buff.h"
void cmd_buf_init(CMD_BUF_t *buf,
signed char (*ser_dequeue)(void),
void (*process)(unsigned char *))
{
buf->status = OUT_PACKET;
buf->data[0] = 0x00;
buf->index = 0;
buf->ser_dequeue = ser_dequeue;
buf->process = process;
}
signed char cmd_buf_tick(CMD_BUF_t *buf)
{
signed char recv;
recv = buf->ser_dequeue();
if (recv == -1)
{
return -1;
}
if (buf->status == OUT_PACKET)
{
if (recv == STX)
{
buf->status = IN_PACKET;
buf->index = 0;
buf->data[0] = 0x00;
}
else
{
/* ignor */
}
}
else
{ /* packet_status == IN_PACK */
if (recv == ETX)
{ /* end of a packet */
buf->status = OUT_PACKET;
buf->data[buf->index] = 0x00; /* padding a terminal char */
buf->process(buf->data);
}
else
{ /* normally */
buf->data[buf->index] = recv;
buf->index += 1;
if (buf->index >= CMD_BUF_MAX-1)
{ /* a huge packet ? */
buf->status = OUT_PACKET;
buf->index = 0;
buf->data[0] = 0x00;
}
}
}
return 0;
}