forked from HetGroteBos/fourlights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb.c
117 lines (90 loc) · 2.43 KB
/
usb.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
/* Compile with -lusb -lm -std=gnu99 usb.c -o usb */
#include <libusb-1.0/libusb.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
/* Following two values taken from ola (opendmx.net) */
#define URB_TIMEOUT_MS 500
#define UDMX_SET_CHANNEL_RANGE 0x0002
int write_buf(libusb_device_handle *h, unsigned char* dat, int size) {
long r = libusb_control_transfer(h,
LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE |
LIBUSB_ENDPOINT_OUT, UDMX_SET_CHANNEL_RANGE,
size, 0, dat, size, URB_TIMEOUT_MS);
return r >= 0;
}
libusb_device_handle * open_dmx(void) {
libusb_device_handle *h = NULL;
libusb_device **dl;
size_t dc;
int f = 0;
unsigned int i = 0;
if (libusb_init(NULL)) {
perror("libusb_init");
goto aexit;
}
dc = libusb_get_device_list(NULL, &dl);
for (i = 0; i < dc; i++) {
struct libusb_device_descriptor dd;
libusb_get_device_descriptor(dl[i], &dd);
printf("Vendor: 0x%4x, Product: 0x%4x\n", dd.idVendor,
dd.idProduct);
if ((dd.idVendor == 0x3eb) && (dd.idProduct = 0x8888)) {
if(libusb_open(dl[i], &h)) {
perror("libusb_open");
goto aexit;
}
f = 1;
printf("Found!\n");
break;
}
}
if (!f) {
printf("Not found!\n");
goto aexit;
}
if (libusb_claim_interface(h, 0)) {
perror("libusb_claim_interface");
goto aexit;
}
return h;
aexit:
if (h)
libusb_close(h);
libusb_exit(NULL);
}
void close_dmx(libusb_device_handle *h) {
if (h) {
libusb_release_interface(h, 0);
libusb_close(h);
}
libusb_exit(NULL);
return;
}
void sin_demo(libusb_device_handle *h, unsigned char *buf, int bufsize) {
int foo = 0;
for (int i = 0; i < 10000; i++) {
for (int c = 0; c < bufsize; c+=3) {
buf[c + 0] = 255 * !foo;
buf[c + 1] = 255 * !foo;
buf[c + 2] = 255 * !foo;
}
foo = (foo + 1) % 10;
write_buf(h, buf, bufsize);
usleep(10000);
}
}
void demo(libusb_device_handle *h) {
int bufsize = 12;
unsigned char *buf = calloc(bufsize, sizeof(unsigned char));
sin_demo(h, buf, bufsize);
free(buf);
return;
}
int main() {
libusb_device_handle *h = NULL;
h = open_dmx();
demo(h);
close_dmx(h);
}