From 5503560775389d3ccb73a7f89c2ccb2088a58d9b Mon Sep 17 00:00:00 2001 From: Adam Honse Date: Thu, 14 Jan 2021 17:00:07 -0600 Subject: [PATCH] Add TPM2 protocol support for LED Strip controller --- .../LEDStripController/LEDStripController.cpp | 72 ++++++++++++++++++- .../LEDStripController/LEDStripController.h | 17 ++++- .../LEDStripControllerDetect.cpp | 25 ++++++- 3 files changed, 111 insertions(+), 3 deletions(-) diff --git a/Controllers/LEDStripController/LEDStripController.cpp b/Controllers/LEDStripController/LEDStripController.cpp index 2f0fa17e3..5f4da1f93 100644 --- a/Controllers/LEDStripController/LEDStripController.cpp +++ b/Controllers/LEDStripController/LEDStripController.cpp @@ -20,13 +20,16 @@ LEDStripController::~LEDStripController() { } -void LEDStripController::Initialize(char* ledstring) +void LEDStripController::Initialize(char* ledstring, led_protocol proto) { LPSTR numleds = NULL; LPSTR source = NULL; LPSTR udpport_baud = NULL; LPSTR next = NULL; + //Set the protocol + protocol = proto; + //Assume serial device unless a different protocol is specified bool serial = TRUE; @@ -123,6 +126,23 @@ char* LEDStripController::GetLEDString() } void LEDStripController::SetLEDs(std::vector colors) +{ + switch(protocol) + { + case LED_PROTOCOL_KEYBOARD_VISUALIZER: + SetLEDsKeyboardVisualizer(colors); + break; + + case LED_PROTOCOL_ADALIGHT: + break; + + case LED_PROTOCOL_TPM2: + SetLEDsTPM2(colors); + break; + } +} + +void LEDStripController::SetLEDsKeyboardVisualizer(std::vector colors) { unsigned char *serial_buf; @@ -160,3 +180,53 @@ void LEDStripController::SetLEDs(std::vector colors) delete[] serial_buf; } + +void LEDStripController::SetLEDsTPM2(std::vector colors) +{ + unsigned char *serial_buf; + + /*-------------------------------------------------------------*\ + | TPM2 Protocol | + | | + | Packet size: Number of data bytes + 5 | + | | + | 0: Packet Start Byte (0xC9) | + | 1: Packet Type (0xDA - Data, 0xC0 - Command, 0xAA - Read) | + | 2: Payload Size MSB | + | 3: Payload Size LSB | + | 4-n: Data Bytes | + | n+1: Packet End Byte (0x36) | + \*-------------------------------------------------------------*/ + unsigned int payload_size = (colors.size() * 3); + unsigned int packet_size = payload_size + 5; + + serial_buf = new unsigned char[packet_size]; + + /*-------------------------------------------------------------*\ + | Set up header and end byte | + \*-------------------------------------------------------------*/ + serial_buf[0x00] = 0xC9; + serial_buf[0x01] = 0xDA; + serial_buf[0x02] = (payload_size >> 8); + serial_buf[0x03] = (payload_size & 0xFF); + serial_buf[packet_size - 1] = 0x36; + + /*-------------------------------------------------------------*\ + | Copy in color data in RGB order | + \*-------------------------------------------------------------*/ + for(unsigned int color_idx = 0; color_idx < colors.size(); color_idx++) + { + unsigned int color_offset = color_idx * 3; + + serial_buf[0x04 + color_offset] = RGBGetRValue(colors[color_idx]); + serial_buf[0x05 + color_offset] = RGBGetGValue(colors[color_idx]); + serial_buf[0x06 + color_offset] = RGBGetBValue(colors[color_idx]); + } + + if (serialport != NULL) + { + serialport->serial_write((char *)serial_buf, packet_size); + } + + delete[] serial_buf; +} \ No newline at end of file diff --git a/Controllers/LEDStripController/LEDStripController.h b/Controllers/LEDStripController/LEDStripController.h index 691a6bd6b..36db51672 100644 --- a/Controllers/LEDStripController/LEDStripController.h +++ b/Controllers/LEDStripController/LEDStripController.h @@ -23,11 +23,21 @@ #define strtok_s strtok_r #endif +typedef unsigned int led_protocol; + +enum +{ + LED_PROTOCOL_KEYBOARD_VISUALIZER, + LED_PROTOCOL_ADALIGHT, + LED_PROTOCOL_TPM2 +}; + struct LEDStripDevice { std::string port; unsigned int baud; unsigned int num_leds; + led_protocol protocol; }; class LEDStripController @@ -36,7 +46,8 @@ public: LEDStripController(); ~LEDStripController(); - void Initialize(char* ledstring); + void Initialize(char* ledstring, led_protocol proto); + void InitializeSerial(char* portname, int baud); void InitializeUDP(char* clientname, char* port); @@ -45,6 +56,9 @@ public: void SetLEDs(std::vector colors); + void SetLEDsKeyboardVisualizer(std::vector colors); + void SetLEDsTPM2(std::vector colors); + int num_leds; private: @@ -55,6 +69,7 @@ private: std::string client_name; serial_port *serialport; net_port *udpport; + led_protocol protocol; }; #endif diff --git a/Controllers/LEDStripController/LEDStripControllerDetect.cpp b/Controllers/LEDStripController/LEDStripControllerDetect.cpp index 88db6d1e4..436fc171b 100644 --- a/Controllers/LEDStripController/LEDStripControllerDetect.cpp +++ b/Controllers/LEDStripController/LEDStripControllerDetect.cpp @@ -34,6 +34,11 @@ void DetectLEDStripControllers(std::vector &rgb_controllers) { for(unsigned int device_idx = 0; device_idx < ledstrip_settings["devices"].size(); device_idx++) { + /*-------------------------------------------------*\ + | Default to the Keyboard Visualizer protocol | + \*-------------------------------------------------*/ + dev.protocol = LED_PROTOCOL_KEYBOARD_VISUALIZER; + if(ledstrip_settings["devices"][device_idx].contains("port")) { dev.port = ledstrip_settings["devices"][device_idx]["port"]; @@ -49,10 +54,28 @@ void DetectLEDStripControllers(std::vector &rgb_controllers) dev.num_leds = ledstrip_settings["devices"][device_idx]["num_leds"]; } + if(ledstrip_settings["devices"][device_idx].contains("protocol")) + { + std::string protocol_string = ledstrip_settings["devices"][device_idx]["protocol"]; + + if(protocol_string == "keyboard_visualizer") + { + dev.protocol = LED_PROTOCOL_KEYBOARD_VISUALIZER; + } + else if(protocol_string == "adalight") + { + dev.protocol = LED_PROTOCOL_ADALIGHT; + } + else if(protocol_string == "tpm2") + { + dev.protocol = LED_PROTOCOL_TPM2; + } + } + std::string value = dev.port + "," + std::to_string(dev.baud) + "," + std::to_string(dev.num_leds); new_ledstrip = new LEDStripController(); - new_ledstrip->Initialize((char *)value.c_str()); + new_ledstrip->Initialize((char *)value.c_str(), dev.protocol); new_controller = new RGBController_LEDStrip(new_ledstrip); rgb_controllers.push_back(new_controller);