|
| 1 | +/*---------------------------------------------------------*\ |
| 2 | +| HyperXOrigins2_65Controller.cpp | |
| 3 | +| | |
| 4 | +| Driver for HyperX Origins 2 65 keyboard | |
| 5 | +| | |
| 6 | +| Ricardo Amorim 28 Mar 2026 | |
| 7 | +| | |
| 8 | +| This file is part of the OpenRGB project | |
| 9 | +| SPDX-License-Identifier: GPL-2.0-or-later | |
| 10 | +\*---------------------------------------------------------*/ |
| 11 | + |
| 12 | +#include <cstring> |
| 13 | +#include "HyperXOrigins2_65Controller.h" |
| 14 | +#include "StringUtils.h" |
| 15 | + |
| 16 | +HyperXOrigins2_65Controller::HyperXOrigins2_65Controller(hid_device* dev_handle, const char* path, std::string dev_name) |
| 17 | +{ |
| 18 | + dev = dev_handle; |
| 19 | + location = path; |
| 20 | + name = dev_name; |
| 21 | +} |
| 22 | + |
| 23 | +HyperXOrigins2_65Controller::~HyperXOrigins2_65Controller() |
| 24 | +{ |
| 25 | + hid_close(dev); |
| 26 | +} |
| 27 | + |
| 28 | +std::string HyperXOrigins2_65Controller::GetDeviceLocation() |
| 29 | +{ |
| 30 | + return("HID " + location); |
| 31 | +} |
| 32 | + |
| 33 | +std::string HyperXOrigins2_65Controller::GetNameString() |
| 34 | +{ |
| 35 | + return(name); |
| 36 | +} |
| 37 | + |
| 38 | +std::string HyperXOrigins2_65Controller::GetSerialString() |
| 39 | +{ |
| 40 | + wchar_t serial_string[128]; |
| 41 | + int ret = hid_get_serial_number_string(dev, serial_string, 128); |
| 42 | + |
| 43 | + if(ret != 0) |
| 44 | + { |
| 45 | + return (""); |
| 46 | + } |
| 47 | + |
| 48 | + return(StringUtils::wstring_to_string(serial_string)); |
| 49 | +} |
| 50 | + |
| 51 | +void HyperXOrigins2_65Controller::SetLEDsDirect(std::vector<RGBColor> colors) |
| 52 | +{ |
| 53 | + /*-----------------------------------------------------*\ |
| 54 | + | Set up variables to track progress of color transmit | |
| 55 | + | Do this after inserting blanks | |
| 56 | + \*-----------------------------------------------------*/ |
| 57 | + int colors_to_send = (int)colors.size(); |
| 58 | + int colors_sent = 0; |
| 59 | + |
| 60 | + SendDirectInitialization(); |
| 61 | + |
| 62 | + for(int pkt_idx = 0; pkt_idx < 4; pkt_idx++) |
| 63 | + { |
| 64 | + if(colors_to_send > 20) |
| 65 | + { |
| 66 | + SendDirectColorPacket(&colors[colors_sent], 20, pkt_idx); |
| 67 | + colors_sent += 20; |
| 68 | + colors_to_send -= 20; |
| 69 | + } |
| 70 | + else if(colors_to_send > 0) |
| 71 | + { |
| 72 | + SendDirectColorPacket(&colors[colors_sent], colors_to_send, pkt_idx); |
| 73 | + colors_sent += colors_to_send; |
| 74 | + colors_to_send -= colors_to_send; |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + RGBColor temp = 0x00000000; |
| 79 | + SendDirectColorPacket(&temp, 1, pkt_idx); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +void HyperXOrigins2_65Controller::SendDirectInitialization() |
| 85 | +{ |
| 86 | + unsigned char buf[65]; |
| 87 | + |
| 88 | + /*-----------------------------------------------------*\ |
| 89 | + | Zero out buffer | |
| 90 | + \*-----------------------------------------------------*/ |
| 91 | + memset(buf, 0x00, sizeof(buf)); |
| 92 | + |
| 93 | + /*-----------------------------------------------------*\ |
| 94 | + | Set up Direct Initialization packet | |
| 95 | + \*-----------------------------------------------------*/ |
| 96 | + buf[0x00] = 0x44; |
| 97 | + buf[0x01] = 0x01; |
| 98 | + buf[0x02] = 0x04; |
| 99 | + buf[0x03] = 0x00; |
| 100 | + |
| 101 | + /*-----------------------------------------------------*\ |
| 102 | + | Send packet | |
| 103 | + \*-----------------------------------------------------*/ |
| 104 | + hid_write(dev, buf, 65); |
| 105 | +} |
| 106 | + |
| 107 | +void HyperXOrigins2_65Controller::SendDirectColorPacket |
| 108 | + ( |
| 109 | + RGBColor* color_data, |
| 110 | + unsigned int color_count, |
| 111 | + unsigned int seq |
| 112 | + ) |
| 113 | +{ |
| 114 | + unsigned char buf[65]; |
| 115 | + |
| 116 | + /*-----------------------------------------------------*\ |
| 117 | + | Zero out buffer | |
| 118 | + \*-----------------------------------------------------*/ |
| 119 | + memset(buf, 0x00, sizeof(buf)); |
| 120 | + |
| 121 | + /*-----------------------------------------------------*\ |
| 122 | + | Set up Direct Initialization packet | |
| 123 | + \*-----------------------------------------------------*/ |
| 124 | + buf[0x00] = 0x44; |
| 125 | + buf[0x01] = 0x02; |
| 126 | + buf[0x02] = (unsigned char)seq; |
| 127 | + buf[0x03] = 0x00; |
| 128 | + |
| 129 | + /*-----------------------------------------------------*\ |
| 130 | + | The maximum number of colors per packet is 20 | |
| 131 | + \*-----------------------------------------------------*/ |
| 132 | + if(color_count > 20) |
| 133 | + { |
| 134 | + color_count = 20; |
| 135 | + } |
| 136 | + |
| 137 | + /*-----------------------------------------------------*\ |
| 138 | + | Copy in color data | |
| 139 | + \*-----------------------------------------------------*/ |
| 140 | + for(unsigned int color_idx = 0; color_idx < color_count; color_idx++) |
| 141 | + { |
| 142 | + buf[4 + (color_idx * 3)] = RGBGetRValue(color_data[color_idx]); |
| 143 | + buf[4 + (color_idx * 3) + 1] = RGBGetGValue(color_data[color_idx]); |
| 144 | + buf[4 + (color_idx * 3) + 2] = RGBGetBValue(color_data[color_idx]); |
| 145 | + } |
| 146 | + |
| 147 | + /*-----------------------------------------------------*\ |
| 148 | + | Send packet | |
| 149 | + \*-----------------------------------------------------*/ |
| 150 | + hid_write(dev, buf, 65); |
| 151 | +} |
0 commit comments