forked from vogelchr/avr_cobalt_panel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.c
130 lines (112 loc) · 2.5 KB
/
protocol.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
#include <stdio.h>
#include "protocol.h"
#include "cobalt_leds.h"
#include "hd44780.h"
/* serial protocol:
any character except ESC -> send to LCD
* ESC 'c' -> _C_lear and home LCD
* ESC 'h' -> _H_ome LCD
* p -> cursor _P_osition
ESC 'p' x y c b -> goto : x={'0'..'9','a'..'f'}, y={'0','1'} -> goto x y
c = show cursor ('0' or '1'), b= blink ('0' or '1')
* l -> _L_EDS
ESC 'l' x y -> set LEDs, x+y = hexadecimal LED mask
* u -> _U_ser defined character
ESC 'u' x a b c d e f g h -> define user character at position
u (hex nibble 0..7)
with binary characters a .. h
* ? -> Inquire
ESC '?' --> Returns 'avr-cobalt-panel\r\n'
*/
const prog_char inquire_ret[]="avr-cobalt-panel\r\n";
static uint8_t hex_nibble(char c){
if(c >= '0' && c <= '9')
return c - '0';
if(c >= 'A' && c <= 'F')
return c - 'A' + 10;
if(c >= 'a' && c <= 'f')
return c - 'a' + 10;
return 0;
}
static uint8_t hex_byte(char a, char b){
return (hex_nibble(a)<<4)|hex_nibble(b);
}
enum parser_state {
parser_idle,
parser_esc,
parser_feed
};
int parser_buf[9];
int parser_eat_n;
char parser_esc_char;
enum parser_state parser_state;
void
protocol_eat_char(char c)
{
switch(parser_state){
case parser_idle:
if(c == '\033'){
parser_state = parser_esc;
} else {
hd44780_data(c); /* write out character to LCD */
}
break;
case parser_esc:
parser_esc_char = c;
switch(c){
case 'c':
hd44780_clear(c);
parser_state = parser_idle;
break;
case 'h':
hd44780_home(c);
parser_state = parser_idle;
break;
case '?' :
puts_P(inquire_ret);
parser_state = parser_idle;
case 'p':
parser_eat_n=4;
parser_state = parser_feed;
break;
case 'l':
parser_eat_n=2;
parser_state = parser_feed;
break;
case 'u':
parser_eat_n=9;
parser_state = parser_feed;
break;
}
break;
case parser_feed:
if(parser_eat_n){
parser_buf[--parser_eat_n]=c;
if(parser_eat_n)
break;
}
parser_state = parser_idle;
switch(parser_esc_char){
case 'p':
hd44780_cursor(
hex_nibble(parser_buf[3]),
hex_nibble(parser_buf[2]),
hex_nibble(parser_buf[1]),
hex_nibble(parser_buf[0]));
break;
case 'l':
cobalt_leds_set(
hex_byte(parser_buf[1],
parser_buf[0]));
break;
case 'u':
c = hex_nibble(parser_buf[8]);
hd44780_cgram(c*8);
for(c=0;c<8;c++)
hd44780_data(parser_buf[7-c]);
hd44780_cursor(0,0,0,0);
break;
}
break;
}
}