From 7878aa1d455ab24d0336fc68eb09f726bcba10b2 Mon Sep 17 00:00:00 2001 From: jaseg Date: Tue, 4 Mar 2014 01:07:22 +0100 Subject: Now faster. And with more Jenny Holzer. --- host/usb.c | 156 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 host/usb.c (limited to 'host/usb.c') diff --git a/host/usb.c b/host/usb.c new file mode 100644 index 0000000..6ed0d1e --- /dev/null +++ b/host/usb.c @@ -0,0 +1,156 @@ + +#include +#include +#include +#include +#include +#include + +#include "color.h" +#include "usb.h" + +int matelight_usb_init(){ + int rc = 0; + rc = libusb_init(NULL); + if(rc){ + fprintf(stderr, "LIBML: Cannot initialize libusb\n"); + return 1; + } + return 0; +} + +void matelight_usb_destroy(){ + libusb_exit(NULL); +} + +static int matelight_cmp_str_desc(libusb_device_handle *dev, uint8_t index, char *value){ + char data[256]; + if(libusb_get_string_descriptor_ascii(dev, index, (unsigned char*)data, sizeof(data)) < 0){ + fprintf(stderr, "LIBML: Cannot read device string descriptor\n"); + return 0; + } + return strcmp(data, value) == 0; +} + +matelight_handle *matelight_open(){ + matelight_handle *out = NULL; + libusb_device** device_list; + struct libusb_device_descriptor desc; + ssize_t res = libusb_get_device_list(NULL, &device_list); + if(res == 0){ + fprintf(stderr, "LIBML: Cannot find any connected matelight\n"); + goto error; + }else if(res < 0){ + fprintf(stderr, "LIBML: Error enumerating connected USB devices\n"); + goto error; + }else{ + out = calloc(res+1, sizeof(matelight_handle)); + if(!out){ + fprintf(stderr, "LIBML: Cannot allocate memory\n"); + goto error; + } + memset(out, 0, (res+1)*sizeof(matelight_handle)); + unsigned int found = 0; + for(ssize_t i=0; i0 && out){ + for(ssize_t i=0; ir = GAMMA_APPLY(src->r); + dst->g = GAMMA_APPLY(src->g); + dst->b = GAMMA_APPLY(src->b); +// fprintf(stderr, "%c", ((dst->r > 1) ? '#' : '.')); +#undef GAMMA_APPLY + } +// fprintf(stderr, "\n"); + } + int transferred = 0; + int rc = libusb_bulk_transfer(ml->handle, MATELIGHT_FRAMEDATA_ENDPOINT, (unsigned char*)&frame, sizeof(frame), &transferred, MATELIGHT_TIMEOUT); + if(rc){ + fprintf(stderr, "LIBML: Cannot write frame data\n"); + return 1; + } + if(transferred != sizeof(frame)){ + fprintf(stderr, "LIBML: Could not transfer all frame data. Only transferred %d out of %d bytes.\n", transferred, sizeof(frame)); + return 1; + } + } + } + uint8_t payload = 0x01; + int transferred = 0; + int rc = libusb_bulk_transfer(ml->handle, MATELIGHT_FRAMEDATA_ENDPOINT, &payload, sizeof(uint8_t), &transferred, MATELIGHT_TIMEOUT); + if(rc){ + fprintf(stderr, "LIBML: Cannot write frame data\n"); + return 1; + } + if(transferred != sizeof(uint8_t)){ + fprintf(stderr, "LIBML: Could not transfer all frame data. Only transferred %d out of %d bytes.\n", transferred, sizeof(uint8_t)); + return 1; + } + return 0; +} + -- cgit