From 8827d0e0c223905b3e8d9da63038bb7d059dc649 Mon Sep 17 00:00:00 2001 From: Adam Honse Date: Fri, 29 May 2020 13:19:16 -0500 Subject: [PATCH] Convert Hue 2 driver to use hidapi instead of libusb --- Controllers/Hue2Controller/Hue2Controller.cpp | 22 +++--- Controllers/Hue2Controller/Hue2Controller.h | 6 +- .../Hue2Controller/Hue2ControllerDetect.cpp | 79 ++++++++++++------- 3 files changed, 63 insertions(+), 44 deletions(-) diff --git a/Controllers/Hue2Controller/Hue2Controller.cpp b/Controllers/Hue2Controller/Hue2Controller.cpp index f1bfb8d52..1d2a08eb0 100644 --- a/Controllers/Hue2Controller/Hue2Controller.cpp +++ b/Controllers/Hue2Controller/Hue2Controller.cpp @@ -11,7 +11,7 @@ #include #include -Hue2Controller::Hue2Controller(libusb_device_handle* dev_handle) +Hue2Controller::Hue2Controller(hid_device* dev_handle) { dev = dev_handle; @@ -46,10 +46,9 @@ unsigned int Hue2Controller::GetStripsOnChannel(unsigned int /*channel*/) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; - int actual = 0; - libusb_interrupt_transfer(dev, 0x01, usb_buf, 64, &actual, 0); - libusb_interrupt_transfer(dev, 0x81, usb_buf, 64, &actual, 0); + hid_write(dev, usb_buf, 64); + hid_read(dev, usb_buf, 64); for(int chan = 0; chan < 4; chan++) { @@ -167,7 +166,6 @@ void Hue2Controller::SendApply unsigned char channel ) { - int actual; unsigned char usb_buf[64]; /*-----------------------------------------------------*\ @@ -190,8 +188,8 @@ void Hue2Controller::SendApply /*-----------------------------------------------------*\ | Send packet | \*-----------------------------------------------------*/ - libusb_interrupt_transfer(dev, 0x01, usb_buf, 64, &actual, 0); - libusb_interrupt_transfer(dev, 0x81, usb_buf, 64, &actual, 0); + hid_write(dev, usb_buf, 64); + hid_read(dev, usb_buf, 64); } void Hue2Controller::SendDirect @@ -202,7 +200,6 @@ void Hue2Controller::SendDirect unsigned char* color_data ) { - int actual; unsigned char usb_buf[64]; /*-----------------------------------------------------*\ @@ -226,8 +223,8 @@ void Hue2Controller::SendDirect /*-----------------------------------------------------*\ | Send packet | \*-----------------------------------------------------*/ - libusb_interrupt_transfer(dev, 0x01, usb_buf, 64, &actual, 0); - libusb_interrupt_transfer(dev, 0x81, usb_buf, 64, &actual, 0); + hid_write(dev, usb_buf, 64); + hid_read(dev, usb_buf, 64); } void Hue2Controller::SendEffect @@ -240,7 +237,6 @@ void Hue2Controller::SendEffect unsigned char* color_data ) { - int actual; unsigned char usb_buf[64]; /*-----------------------------------------------------*\ @@ -281,6 +277,6 @@ void Hue2Controller::SendEffect \*-----------------------------------------------------*/ memcpy(&usb_buf[0x0A], color_data, color_count * 3); - libusb_interrupt_transfer(dev, 0x01, usb_buf, 64, &actual, 0); - libusb_interrupt_transfer(dev, 0x81, usb_buf, 64, &actual, 0); + hid_write(dev, usb_buf, 64); + hid_read(dev, usb_buf, 64); } diff --git a/Controllers/Hue2Controller/Hue2Controller.h b/Controllers/Hue2Controller/Hue2Controller.h index 08d488157..ea20f0d90 100644 --- a/Controllers/Hue2Controller/Hue2Controller.h +++ b/Controllers/Hue2Controller/Hue2Controller.h @@ -6,7 +6,7 @@ #include "RGBController.h" #include -#include +#include #pragma once @@ -53,7 +53,7 @@ enum class Hue2Controller { public: - Hue2Controller(libusb_device_handle* dev_handle); + Hue2Controller(hid_device* dev_handle); ~Hue2Controller(); unsigned int GetStripsOnChannel @@ -81,7 +81,7 @@ public: unsigned int channel_leds[HUE_2_NUM_CHANNELS]; private: - libusb_device_handle* dev; + hid_device* dev; void SendApply ( diff --git a/Controllers/Hue2Controller/Hue2ControllerDetect.cpp b/Controllers/Hue2Controller/Hue2ControllerDetect.cpp index b5d8f4ae2..88c7a12e8 100644 --- a/Controllers/Hue2Controller/Hue2ControllerDetect.cpp +++ b/Controllers/Hue2Controller/Hue2ControllerDetect.cpp @@ -2,52 +2,75 @@ #include "RGBController.h" #include "RGBController_Hue2.h" #include -#include +#include -#define NZXT_HUE_2_VID 0x1E71 -#define NZXT_HUE_2_PID 0x2001 -#define NZXT_SMART_DEVICE_V2_PID 0x2006 +#define NZXT_VID 0x1E71 +#define NZXT_HUE_2_PID 0x2001 +#define NZXT_SMART_DEVICE_V2_PID 0x2006 + +typedef struct +{ + unsigned short usb_vid; + unsigned short usb_pid; + const char * name; +} nzxt_hue_2_device; + +#define NZXT_HUE_2_NUM_DEVICES (sizeof(device_list) / sizeof(device_list[ 0 ])) + +static const nzxt_hue_2_device device_list[] = +{ + /*-----------------------------------------------------------------------------------------------------*\ + | NZXT Hue 2 devices | + \*-----------------------------------------------------------------------------------------------------*/ + { NZXT_VID, NZXT_HUE_2_PID, "NZXT Hue 2" }, + { NZXT_VID, NZXT_SMART_DEVICE_V2_PID, "NZXT Smart Device V2" }, +}; /******************************************************************************************\ * * * DetectHue2Controllers * * * * Detect devices supported by the Hue2 driver * -* * * +* * \******************************************************************************************/ void DetectHue2Controllers(std::vector &rgb_controllers) { - libusb_context * ctx; - libusb_init(&ctx); + hid_device_info* info; + hid_device* dev; - //Look for NZXT Hue 2 - libusb_device_handle * hue_2_dev = libusb_open_device_with_vid_pid(ctx, NZXT_HUE_2_VID, NZXT_HUE_2_PID); + hid_init(); - if( hue_2_dev ) + for(std::size_t device_idx = 0; device_idx < NZXT_HUE_2_NUM_DEVICES; device_idx++) { - libusb_detach_kernel_driver(hue_2_dev, 0); - libusb_claim_interface(hue_2_dev, 0); + dev = NULL; - Hue2Controller* controller = new Hue2Controller(hue_2_dev); + info = hid_enumerate(device_list[device_idx].usb_vid, device_list[device_idx].usb_pid); - RGBController_Hue2* rgb_controller = new RGBController_Hue2(controller); + //Look for NZXT Hue 2 devices + while(info) + { + if((info->vendor_id == device_list[device_idx].usb_vid) + &&(info->product_id == device_list[device_idx].usb_pid)) + { + dev = hid_open_path(info->path); + break; + } + else + { + info = info->next; + } + } - rgb_controllers.push_back(rgb_controller); - } + if( dev ) + { + Hue2Controller* controller = new Hue2Controller(dev); - //Look for NZXT Smart Device V2 - libusb_device_handle * smart_dev_2_dev = libusb_open_device_with_vid_pid(ctx, NZXT_HUE_2_VID, NZXT_SMART_DEVICE_V2_PID); + RGBController_Hue2* rgb_controller = new RGBController_Hue2(controller); - if( smart_dev_2_dev ) - { - libusb_detach_kernel_driver(smart_dev_2_dev, 0); - libusb_claim_interface(smart_dev_2_dev, 0); - - Hue2Controller* controller = new Hue2Controller(smart_dev_2_dev); - - RGBController_Hue2* rgb_controller = new RGBController_Hue2(controller); - - rgb_controllers.push_back(rgb_controller); + rgb_controller->name = device_list[device_idx].name; + + rgb_controllers.push_back(rgb_controller); + } } } /* DetectHuePlusControllers() */