1 /* Testing Neopixel Rainbow */
10 #include <libusb-1.0/libusb.h>
12 #include "../firmware/interface.h"
14 struct libusb_context *usb_ctxt;
15 struct libusb_device_handle *devh;
17 static libusb_device *find_device(void)
19 libusb_device **devlist;
20 ssize_t devn = libusb_get_device_list(usb_ctxt, &devlist);
23 fprintf(stderr, "Cannot enumerate USB devices: error %d\n", (int) devn);
27 for (ssize_t i=0; i<devn; i++)
29 struct libusb_device_descriptor desc;
30 libusb_device *dev = devlist[i];
31 if (!libusb_get_device_descriptor(dev, &desc))
33 if (desc.idVendor == NPIX_USB_VENDOR && desc.idProduct == NPIX_USB_PRODUCT)
35 printf("Found device at usb%d.%d\n", libusb_get_bus_number(dev), libusb_get_device_address(dev));
36 // FIXME: Free device list
42 libusb_free_device_list(devlist, 1);
43 fprintf(stderr, "Device not found\n");
47 int main(int argc, char **argv)
49 byte packet[NPIX_NUM_LEDS*3] = { };
52 if (argc > NPIX_NUM_LEDS*3 + 1)
53 die("Too many arguments!");
55 for (int i=1; i < argc; i++)
56 packet[len++] = atoi(argv[i]);
59 if (err = libusb_init(&usb_ctxt))
60 die("Cannot initialize libusb: error %d", err);
62 libusb_device *dev = find_device();
64 if (err = libusb_open(dev, &devh))
65 die("Cannot open device: error %d", err);
66 // libusb_reset_device(devh);
67 if (err = libusb_claim_interface(devh, 0))
68 die("Cannot claim interface: error %d", err);
71 if (err = libusb_bulk_transfer(devh, 0x01, packet, len, &transferred, 1000))
72 die("Transfer failed: error %d\n", err);
73 if (transferred != len)
74 die("Short transfer: %d out of %d bytes", transferred, len);