-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcdout.c
216 lines (154 loc) · 3.25 KB
/
lcdout.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
#include <termios.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "lcdout.h"
static inline int add_presence(unsigned char presence)
{
int res;
int i;
res = 0;
for(i=0;i<8;i++)
{
res += 1&(presence>>i);
}
return res;
}
lcd_device *lcd_init()
{
int res;
lcd_device *lcdfd;
if(!(lcdfd = malloc(sizeof(lcdfd)))) {
perror("Can't allocate memory for lcd device");
return NULL;
}
res = hid_init();
if(res != 0) {
perror("hid_init");
return NULL;
}
lcdfd->dev = hid_open(LCD_VID,LCD_PID,NULL);
if(!(lcdfd->dev)) {
perror("open hid system");
return NULL;
}
lcdfd->buttonstate = 0;
return lcdfd;
}
void lcd_close(lcd_device *lcdfd)
{
hid_close(lcdfd->dev);
hid_exit();
free(lcdfd);
}
int lcd_clear(lcd_device *lcdfd)
{
int res;
static unsigned char outbuf[] = {0x02,0x00};
res = hid_write(lcdfd->dev,outbuf,2);
if(res == -1) {
perror("clear LCD screen");
return 1;
}
return res;
}
int lcd_puts(lcd_device *lcdfd,char* message)
{
int res;
int size;
int messoffset;
static unsigned char outbuf[8] = {0x01,0x00,0x00,0x00,
0x00,0x00,0x00,0x00};;
size = strlen(message);
messoffset = 0;
while(size>7) {
memcpy(outbuf+1,message+messoffset,7);
res = hid_write(lcdfd->dev,outbuf,8);
if(res == -1) {
perror("lcd_puts can't send characters");
return 1;
}
size -= 7;
messoffset += 7;
}
if(size>0) {
memcpy(outbuf+1,message+messoffset,size);
res = hid_write(lcdfd->dev,outbuf,size+1);
if(res == -1) {
perror("lcd_puts can't send characters");
return 1;
}
}
return 0;
}
int lcd_gotoxy(lcd_device *lcdfd,unsigned char x, unsigned char y)
{
static unsigned char outbuf[3] = {0x03,0x00,0x00};
int res;
x %= 40;
y %= 2;
outbuf[1] = x;
outbuf[2] = y;
res = hid_write(lcdfd->dev,outbuf,3);
if(res == -1) {
perror("gotoxy: can't move");
}
return res;
}
int lcd_backlight(lcd_device *lcdfd,unsigned char x)
{
int res;
static unsigned char outbuf[2] = {0x04,0x00};
outbuf[1] = x;
res = hid_write(lcdfd->dev,outbuf,2);
if(res == -1) {
perror("backlight: can't change");
}
return res;
}
int lcd_buttonstate(lcd_device *lcdfd)
{
unsigned char inbuf[3];
int res;
res = hid_read_timeout(lcdfd->dev,inbuf,3,100);
if(res < 0) {
perror("Can't read packet from usb");
return res;
}
if(res == 3) {
lcdfd->buttonstate = inbuf[1] << 8;
lcdfd->buttonstate += inbuf[2];
}
return 0;
}
int lcd_check_button_event(lcd_device *lcdfd)
{
unsigned char inbuf[3];
int res;
res = hid_read_timeout(lcdfd->dev,inbuf,3,100);
if(res < 0) {
perror("Can't read packet from usb");
return res;
}
if(res == 3) {
lcdfd->buttonstate = inbuf[2] << 8;
lcdfd->buttonstate += inbuf[1];
lcdfd->presence = add_presence(inbuf[2]);
return 0;
}
return 1;
}
int lcd_force_button_update(lcd_device *lcdfd)
{
int res;
static unsigned char outbuf[] = {0x05,0x00};
res = hid_write(lcdfd->dev,outbuf,2);
if(res == -1) {
perror("lcd_force_button_update");
return -1;
}
return lcd_check_button_event(lcdfd);
}