From f6bf044ba059360d973d814dc5e67bbee75b4d9d Mon Sep 17 00:00:00 2001 From: Adam Honse Date: Sun, 5 Jan 2020 17:22:43 -0600 Subject: [PATCH] Add functions for setting Hue+ and Hue 2 effect modes --- Controllers/Hue2Controller/Hue2Controller.cpp | 54 +++++++++++++++ Controllers/Hue2Controller/Hue2Controller.h | 3 +- .../HuePlusController/HuePlusController.cpp | 65 +++++++++++++++++++ .../HuePlusController/HuePlusController.h | 1 + 4 files changed, 122 insertions(+), 1 deletion(-) diff --git a/Controllers/Hue2Controller/Hue2Controller.cpp b/Controllers/Hue2Controller/Hue2Controller.cpp index 36916bca..c100f2cd 100644 --- a/Controllers/Hue2Controller/Hue2Controller.cpp +++ b/Controllers/Hue2Controller/Hue2Controller.cpp @@ -78,6 +78,60 @@ unsigned int Hue2Controller::GetStripsOnChannel(unsigned int /*channel*/) return(ret_val); } +void Hue2Controller::SetChannelEffect(unsigned int channel, unsigned int mode, std::vector colors) +{ + unsigned char usb_buf[] = + { + 0x28, 0x03, 0x00, 0x28, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00 + }; + int actual = 0; + + /*-----------------------------------------------------*\ + | Set channel in USB packet | + \*-----------------------------------------------------*/ + usb_buf[0x02] = channel; + + /*-----------------------------------------------------*\ + | Set mode in USB packet | + \*-----------------------------------------------------*/ + usb_buf[0x04] = mode; + + /*-----------------------------------------------------*\ + | Set color count in USB packet | + \*-----------------------------------------------------*/ + usb_buf[0x08] = colors.size(); + + /*-----------------------------------------------------*\ + | Fill in color data | + \*-----------------------------------------------------*/ + for (std::size_t idx = 0; idx < colors.size(); idx++) + { + int pixel_idx = idx * 3; + RGBColor color = colors[idx]; + usb_buf[pixel_idx + 0x0A] = RGBGetGValue(color); + usb_buf[pixel_idx + 0x0B] = RGBGetRValue(color); + usb_buf[pixel_idx + 0x0C] = RGBGetBValue(color); + } + + libusb_interrupt_transfer(dev, 0x01, usb_buf, 64, &actual, 0); + libusb_interrupt_transfer(dev, 0x81, usb_buf, 64, &actual, 0); +} + void Hue2Controller::SetChannelLEDs(unsigned int channel, std::vector colors) { unsigned char usb_buf[] = diff --git a/Controllers/Hue2Controller/Hue2Controller.h b/Controllers/Hue2Controller/Hue2Controller.h index 22161733..7e274c14 100644 --- a/Controllers/Hue2Controller/Hue2Controller.h +++ b/Controllers/Hue2Controller/Hue2Controller.h @@ -49,8 +49,9 @@ public: char* GetLEDString(); unsigned int GetStripsOnChannel(unsigned int channel); + void SetChannelEffect(unsigned int channel, unsigned int mode, std::vector colors); void SetChannelLEDs(unsigned int channel, std::vector colors); - + unsigned int channel_leds[HUE_2_NUM_CHANNELS]; private: diff --git a/Controllers/HuePlusController/HuePlusController.cpp b/Controllers/HuePlusController/HuePlusController.cpp index 7766b53d..91531a00 100644 --- a/Controllers/HuePlusController/HuePlusController.cpp +++ b/Controllers/HuePlusController/HuePlusController.cpp @@ -77,6 +77,71 @@ unsigned int HuePlusController::GetStripsOnChannel(unsigned int channel) return(ret_val); } +void HuePlusController::SetChannelEffect(unsigned int channel, unsigned int mode, std::vector colors) +{ + unsigned char serial_buf[] = + { + 0x4B, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00 + }; + int actual = 0; + + /*-----------------------------------------------------*\ + | Set channel in serial packet | + \*-----------------------------------------------------*/ + serial_buf[0x01] = channel; + + /*-----------------------------------------------------*\ + | Set mode in serial packet | + \*-----------------------------------------------------*/ + serial_buf[0x02] = mode; + + /*-----------------------------------------------------*\ + | Fill in color data | + \*-----------------------------------------------------*/ + for (std::size_t idx = 0; idx < colors.size(); idx++) + { + int pixel_idx = idx * 3; + RGBColor color = colors[idx]; + serial_buf[pixel_idx + 0x05] = RGBGetGValue(color); + serial_buf[pixel_idx + 0x06] = RGBGetRValue(color); + serial_buf[pixel_idx + 0x07] = RGBGetBValue(color); + } + + serialport->serial_write((char *)serial_buf, HUE_PLUS_PACKET_SIZE); + serialport->serial_flush_tx(); +} + void HuePlusController::SetChannelLEDs(unsigned int channel, std::vector colors) { if (serialport != NULL) diff --git a/Controllers/HuePlusController/HuePlusController.h b/Controllers/HuePlusController/HuePlusController.h index 1c133680..33076d62 100644 --- a/Controllers/HuePlusController/HuePlusController.h +++ b/Controllers/HuePlusController/HuePlusController.h @@ -56,6 +56,7 @@ public: void Initialize(char* port_name); char* GetLEDString(); unsigned int GetStripsOnChannel(unsigned int channel); + void SetChannelEffect(unsigned int channel, unsigned int mode, std::vector colors); void SetChannelLEDs(unsigned int channel, std::vector colors); unsigned int channel_leds[HUE_PLUS_NUM_CHANNELS];