forked from AVGP/panic-button
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbutton.c
More file actions
64 lines (51 loc) · 1.51 KB
/
button.c
File metadata and controls
64 lines (51 loc) · 1.51 KB
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 <stdio.h>
#include <stdlib.h>
#include "button.h"
struct libusb_device_handle *openButton()
{
libusb_context *ctx = NULL;
if (libusb_init(&ctx) != 0) {
fprintf(stderr, "Cannot init libusb!\n");
return NULL;
}
/* Find the Dream Cheeky Big Red Button */
struct libusb_device_handle *hDev = libusb_open_device_with_vid_pid(ctx, 0x1d34, 0xd);
if (hDev == NULL) {
fprintf(stderr, "Cannot access the button! Is it connected?\n");
return NULL;
}
if (libusb_kernel_driver_active(hDev, 0) == 1) {
if (libusb_detach_kernel_driver(hDev, 0) != 0) {
fprintf(stderr, "Device is busy. :(\n");
return NULL;
}
}
int result = libusb_claim_interface(hDev, 0);
if (result < 0) {
fprintf(stderr, "Cannot claim the button - Error code is %i\n", result);
return NULL;
}
return hDev;
}
unsigned char getButtonState(struct libusb_device_handle *dev) {
if (libusb_control_transfer(
dev,
CONTROL_REQUEST_TYPE_IN, HID_SET_REPORT,
(HID_REPORT_TYPE_OUTPUT<<8),
0,
HID_CTRL_DATA,
8,
500
) < 0) {
fprintf(stderr, "Cannot write to the button.\n");
return 0;
}
int bytesRead=0;
char buffer[8];
int result = libusb_interrupt_transfer(dev, 0x81, buffer, 8, &bytesRead, 500);
if (result < 0) {
fprintf(stderr, "Failed to read from button.\n");
return 0;
}
return buffer[0];
}