-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomms.c
61 lines (49 loc) · 1.05 KB
/
comms.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
#include <unistd.h>
#include <stdio.h>
#include "hidapi/hidapi.h"
#include "comms.h"
char current_filter(hid_device *dev) {
unsigned char cmd[2], res[2];
int tret;
cmd[0] = 0;
cmd[1] = 0;
hid_write(dev, cmd, 2);
tret = hid_read_timeout(dev, res, 2, 5000);
if (tret > 0) {
return res[0];
} else if (tret == 0) {
return -1;
}
}
enum Result change_filter(unsigned char filter,
hid_device *dev) {
unsigned char cmd[2];
char res, start;
cmd[0] = filter;
cmd[1] = 0;
start = current_filter(dev);
if (start == -1) {
printf("Cannot get current filter\n");
return ERR;
}
else if (start == filter)
return OK;
while (start == 0) {
usleep(100000);
start = current_filter(dev);
printf("Current Filter %d\n", start);
}
if (start == -1) {
return ERR;
}
// Wait to prevent locks maybe?
usleep(100000);
hid_write(dev, cmd, 2);
do {
usleep(100000);
res = current_filter(dev);
if (res == -1)
return ERR;
} while (res == 0 || res == start);
return OK;
}