|
| 1 | +/*-----------------------------------------*\ |
| 2 | +| AsusAuraTUFKeyboardController.cpp | |
| 3 | +| | |
| 4 | +| Driver for ASUS Aura RGB USB | |
| 5 | +| lighting controller | |
| 6 | +| | |
| 7 | +| Mola19 03/03/2021 | |
| 8 | +\*-----------------------------------------*/ |
| 9 | + |
| 10 | +#include "AsusAuraTUFKeyboardController.h" |
| 11 | + |
| 12 | +#include <cstring> |
| 13 | +#include <string> |
| 14 | +#include <vector> |
| 15 | +#include <math.h> |
| 16 | +#include <stdio.h> |
| 17 | +#include <string.h> |
| 18 | +#include <iostream> |
| 19 | + |
| 20 | +AuraTUFKeyboardController::AuraTUFKeyboardController(hid_device* dev_handle, const char* path) |
| 21 | +{ |
| 22 | + dev = dev_handle; |
| 23 | + location = path; |
| 24 | +} |
| 25 | + |
| 26 | +AuraTUFKeyboardController::~AuraTUFKeyboardController() |
| 27 | +{ |
| 28 | + hid_close(dev); |
| 29 | +} |
| 30 | + |
| 31 | +std::string AuraTUFKeyboardController::GetDeviceLocation() |
| 32 | +{ |
| 33 | + return("HID: " + location); |
| 34 | +} |
| 35 | + |
| 36 | +std::string AuraTUFKeyboardController::GetSerialString() |
| 37 | +{ |
| 38 | + wchar_t serial_string[128]; |
| 39 | + hid_get_serial_number_string(dev, serial_string, 128); |
| 40 | + |
| 41 | + std::wstring return_wstring = serial_string; |
| 42 | + std::string return_string(return_wstring.begin(), return_wstring.end()); |
| 43 | + |
| 44 | + return(return_string); |
| 45 | +} |
| 46 | + |
| 47 | +void AuraTUFKeyboardController::UpdateSingleLed |
| 48 | + ( |
| 49 | + int led, |
| 50 | + unsigned char red, |
| 51 | + unsigned char green, |
| 52 | + unsigned char blue |
| 53 | + ) |
| 54 | +{ |
| 55 | + /*-----------------------------------------------------*\ |
| 56 | + | Set up message packet for single LED | |
| 57 | + \*-----------------------------------------------------*/ |
| 58 | + unsigned char usb_buf[65]; |
| 59 | + |
| 60 | + memset(usb_buf, 0x00, sizeof(usb_buf)); |
| 61 | + |
| 62 | + usb_buf[0] = 0x00; |
| 63 | + usb_buf[1] = 0xc0; |
| 64 | + usb_buf[2] = 0x81; |
| 65 | + usb_buf[3] = 0x01; |
| 66 | + usb_buf[4] = 0x00; |
| 67 | + |
| 68 | + /*-----------------------------------------------------*\ |
| 69 | + | Convert LED index | |
| 70 | + \*-----------------------------------------------------*/ |
| 71 | + usb_buf[5] = floor(led/6)*8 + led%6; |
| 72 | + usb_buf[6] = red; |
| 73 | + usb_buf[7] = green; |
| 74 | + usb_buf[8] = blue; |
| 75 | + |
| 76 | + hid_write(dev, usb_buf, 65); |
| 77 | +} |
| 78 | + |
| 79 | +void AuraTUFKeyboardController::UpdateLeds |
| 80 | + ( |
| 81 | + std::vector<RGBColor> colors |
| 82 | + ) |
| 83 | +{ |
| 84 | + /*-----------------------------------------------------*\ |
| 85 | + | Loop over every column (optimisation needed) | |
| 86 | + \*-----------------------------------------------------*/ |
| 87 | + for(unsigned int i = 0; i < 192; i += 16) |
| 88 | + { |
| 89 | + unsigned char usb_buf[69]; |
| 90 | + |
| 91 | + memset(usb_buf, 0x00, sizeof(usb_buf)); |
| 92 | + |
| 93 | + usb_buf[0] = 0x00; |
| 94 | + usb_buf[1] = 0xc0; |
| 95 | + usb_buf[2] = 0x81; |
| 96 | + usb_buf[3] = (138 - i / 16 * 12); |
| 97 | + usb_buf[4] = 0x00; |
| 98 | + |
| 99 | + for(unsigned int n = 0; n < 6; n++) |
| 100 | + { |
| 101 | + usb_buf[n * 4 + 5] = i + n; |
| 102 | + usb_buf[n * 4 + 6] = RGBGetRValue(colors[i/16*12+n]); |
| 103 | + usb_buf[n * 4 + 7] = RGBGetGValue(colors[i/16*12+n]); |
| 104 | + usb_buf[n * 4 + 8] = RGBGetBValue(colors[i/16*12+n]); |
| 105 | + } |
| 106 | + |
| 107 | + for(unsigned int n = 6; n < 8; n++) |
| 108 | + { |
| 109 | + usb_buf[n * 4 + 5] = i + n; |
| 110 | + usb_buf[n * 4 + 6] = 0; |
| 111 | + usb_buf[n * 4 + 7] = 0; |
| 112 | + usb_buf[n * 4 + 8] = 0; |
| 113 | + } |
| 114 | + |
| 115 | + if(i != 176) |
| 116 | + { |
| 117 | + for(unsigned int n = 8; n < 14; n++) |
| 118 | + { |
| 119 | + usb_buf[n * 4 + 5] = i + n; |
| 120 | + usb_buf[n * 4 + 6] = RGBGetRValue(colors[i/16*12+n-2]); |
| 121 | + usb_buf[n * 4 + 7] = RGBGetGValue(colors[i/16*12+n-2]); |
| 122 | + usb_buf[n * 4 + 8] = RGBGetBValue(colors[i/16*12+n-2]); |
| 123 | + } |
| 124 | + |
| 125 | + for(unsigned int n = 14; n < 16; n++) |
| 126 | + { |
| 127 | + usb_buf[n * 4 + 5] = i + n; |
| 128 | + usb_buf[n * 4 + 6] = 0; |
| 129 | + usb_buf[n * 4 + 7] = 0; |
| 130 | + usb_buf[n * 4 + 8] = 0; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + hid_write(dev, usb_buf, 69); |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +void AuraTUFKeyboardController::UpdateDevice |
| 139 | + ( |
| 140 | + unsigned char mode, |
| 141 | + std::vector<RGBColor> colors, |
| 142 | + unsigned char dir, |
| 143 | + unsigned char color_mode, |
| 144 | + unsigned char speed |
| 145 | + ) |
| 146 | +{ |
| 147 | + unsigned char usb_buf[65]; |
| 148 | + |
| 149 | + memset(usb_buf, 0x00, sizeof(usb_buf)); |
| 150 | + |
| 151 | + usb_buf[0x00] = 0x00; |
| 152 | + usb_buf[0x01] = 0x51; |
| 153 | + usb_buf[0x02] = 0x2C; |
| 154 | + usb_buf[0x03] = mode; |
| 155 | + usb_buf[0x04] = 0x00; |
| 156 | + usb_buf[0x05] = speed; |
| 157 | + usb_buf[0x06] = 0x64; |
| 158 | + usb_buf[0x07] = color_mode; |
| 159 | + usb_buf[0x08] = dir; |
| 160 | + usb_buf[0x09] = 0x02; |
| 161 | + |
| 162 | + if(mode == 4 || mode == 5) |
| 163 | + { |
| 164 | + /*-----------------------------------------------------*\ |
| 165 | + | If mode is Rainbow or Ripple | |
| 166 | + \*-----------------------------------------------------*/ |
| 167 | + usb_buf[0x0A] = colors.size(); |
| 168 | + |
| 169 | + /*-----------------------------------------------------*\ |
| 170 | + | Loop over every color given | |
| 171 | + \*-----------------------------------------------------*/ |
| 172 | + for(unsigned int i = 0; i / 4 < colors.size(); i += 4) |
| 173 | + { |
| 174 | + if(colors[i / 4]) |
| 175 | + { |
| 176 | + usb_buf[11 + i] = 100/(double)colors.size()*(i/4+1); |
| 177 | + usb_buf[12 + i] = RGBGetRValue(colors[i/4]); |
| 178 | + usb_buf[13 + i] = RGBGetGValue(colors[i/4]); |
| 179 | + usb_buf[14 + i] = RGBGetBValue(colors[i/4]); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + else |
| 184 | + { |
| 185 | + /*-----------------------------------------------------*\ |
| 186 | + | Loop over Color1, Color2 and Background if there | |
| 187 | + \*-----------------------------------------------------*/ |
| 188 | + for(unsigned int i = 0; i / 3 != colors.size(); i += 3) |
| 189 | + { |
| 190 | + if(colors[i / 3]) |
| 191 | + { |
| 192 | + usb_buf[10 + i] = RGBGetRValue(colors[i/3]); |
| 193 | + usb_buf[11 + i] = RGBGetGValue(colors[i/3]); |
| 194 | + usb_buf[12 + i] = RGBGetBValue(colors[i/3]); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + } |
| 199 | + |
| 200 | + /*-----------------------------------------------------*\ |
| 201 | + | Send packet | |
| 202 | + \*-----------------------------------------------------*/ |
| 203 | + hid_write(dev, usb_buf, 65); |
| 204 | + |
| 205 | + /*-----------------------------------------------------*\ |
| 206 | + | Set up and send packet save Packet | |
| 207 | + \*-----------------------------------------------------*/ |
| 208 | + unsigned char usb_save_buf[65]; |
| 209 | + |
| 210 | + memset(usb_save_buf, 0x00, sizeof(usb_save_buf)); |
| 211 | + |
| 212 | + usb_save_buf[0x00] = 0x00; |
| 213 | + usb_save_buf[0x01] = 0x50; |
| 214 | + usb_save_buf[0x02] = 0x55; |
| 215 | + |
| 216 | + hid_write(dev, usb_save_buf, 65); |
| 217 | +} |
| 218 | + |
0 commit comments