/
first.c
173 lines (141 loc) · 3.46 KB
/
first.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
#include "ncursesdisplay.h"
#include "first.h"
unsigned char buf[64] = {0};
hid_device *handle;
// Device state
uint16_t pot1;
void toggle_led1()
{
static unsigned char CMD = 0x80;
int response;
// Toggle LED (cmd 0x80). The first byte is the report number (0x0).
buf[0] = 0x0;
buf[1] = CMD;
response = hid_write(handle, buf, 64);
if(response < 0)
{
printf("Error sending command %x", CMD);
return;
}
}
bool get_push1_state()
{
static unsigned char CMD = 0x81;
int response;
// Request state (cmd 0x81). The first byte is the report number (0x0).
buf[0] = 0x0;
buf[1] = CMD;
response = hid_write(handle, buf, 64);
if(response < 0)
{
printf("Error sending command %x", CMD);
return false;
}
// Read requested state
response = hid_read(handle, buf, 64);
if(response < 0)
{
printf("Error reading response for command %x", CMD);
return false;
}
// Print out the returned buffer.
// for (i = 0; i < 2; i++)
// printf("buf[%d]: %d\n", i, buf[i]);
return !buf[1];
}
uint16_t get_pot1_val()
{
static unsigned char CMD = 0x37;
int response;
// Request state (cmd 0x37). The first byte is the report number (0x0).
buf[0] = 0x0;
buf[1] = CMD;
response = hid_write(handle, buf, 64);
if(response < 0)
{
printf("Error sending command %x", CMD);
return -1;
}
// Read in the requested state
response = hid_read(handle, buf, 64);
if(response < 0)
{
printf("Error reading response for command %x", CMD);
return -1;
}
return merge_uint8(buf[1], buf[2]);
// Print out the returned buffer.
// for (i = 0; i < 3; i++)
// printf("buf[%d]: %d\n", i, buf[i]);
}
void read_device_info()
{
int res;
wchar_t wstr[MAX_STR];
// Read the Manufacturer String
printf("Trying to read manufacturer string...\n");
res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
wprintf(L"The manufacturer string is: %ls\n", wstr);
// Read the Product String
res = hid_get_product_string(handle, wstr, MAX_STR);
wprintf(L"Product String: %ls\n", wstr);
// Read the Serial Number String
res = hid_get_serial_number_string(handle, wstr, MAX_STR);
wprintf(L"Serial Number String: (%d) %ls\n", wstr[0], wstr);
// Read Indexed String 1
res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
wprintf(L"Indexed String 1: %ls\n", wstr);
}
int main(int argc, char* argv[])
{
int res;
int i;
// Initialize the hidapi library
printf("Starting to initialize HID library...\n");
res = hid_init();
printf("Initialized HID library.\n");
// Open the device using the VID, PID,
// and optionally the Serial number.
printf("Trying to open device...\n");
handle = hid_open(0x04d8, 0x003f, NULL);
if(handle != NULL) {
printf("Opened device successfully.\n");
} else {
printf("Could not open device.\n");
exit(EXIT_FAILURE);
}
read_device_info();
toggle_led1();
printf("Button is %spressed.\n", get_push1_state() ? "" : "not ");
printf("The decimal result is %d\n", get_pot1_val());
// Register signal handler
if (signal(SIGINT, sig_handler) == SIG_ERR) {
printf("\nCan't catch SIGINT\n");
exit(EXIT_FAILURE);
}
// Enter ncurses mode
initialize_display();
// Main loop
while(1) {
int a;
a = (int)((100 / 1023.0) * get_pot1_val());
print_percent_bar(2, a);
refresh();
sleep(0.1);
}
// Leave ncurses mode
end_display();
// Unregister signal handler
signal(SIGINT, SIG_DFL);
// Finalize the hidapi library
res = hid_exit();
return 0;
}
void sig_handler(int signo)
{
if (signo == SIGINT)
{
end_display();
exit(EXIT_SUCCESS);
}
}