|
| 1 | +/*-----------------------------------------*\ |
| 2 | +| OKSKeyboardController.cpp | |
| 3 | +| | |
| 4 | +| Driver for OKS RGB keyboardlighting | |
| 5 | +| controller | |
| 6 | +| | |
| 7 | +| Merafour (OKS) 2/24/2023 | |
| 8 | +\*-----------------------------------------*/ |
| 9 | + |
| 10 | +#include <cstring> |
| 11 | +#include "OKSKeyboardController.h" |
| 12 | + |
| 13 | +OKSKeyboardController::OKSKeyboardController(hid_device* dev_handle, const char* path, const unsigned short pid) |
| 14 | +{ |
| 15 | + dev = dev_handle; |
| 16 | + location = path; |
| 17 | + usb_pid = pid; |
| 18 | + |
| 19 | + SendInitialize(); |
| 20 | +} |
| 21 | + |
| 22 | +OKSKeyboardController::~OKSKeyboardController() |
| 23 | +{ |
| 24 | + hid_close(dev); |
| 25 | +} |
| 26 | + |
| 27 | +std::string OKSKeyboardController::GetDeviceLocation() |
| 28 | +{ |
| 29 | + return("HID: " + location); |
| 30 | +} |
| 31 | + |
| 32 | +std::string OKSKeyboardController::GetSerialString() |
| 33 | +{ |
| 34 | + wchar_t serial_string[128]; |
| 35 | + int ret = hid_get_serial_number_string(dev, serial_string, 128); |
| 36 | + |
| 37 | + if(ret != 0) |
| 38 | + { |
| 39 | + return(""); |
| 40 | + } |
| 41 | + |
| 42 | + std::wstring return_wstring = serial_string; |
| 43 | + std::string return_string(return_wstring.begin(), return_wstring.end()); |
| 44 | + |
| 45 | + return(return_string); |
| 46 | +} |
| 47 | + |
| 48 | +unsigned short OKSKeyboardController::GetUSBPID() |
| 49 | +{ |
| 50 | + return(usb_pid); |
| 51 | +} |
| 52 | + |
| 53 | +void OKSKeyboardController::SendColors(unsigned char* color_data, unsigned int color_data_size) |
| 54 | +{ |
| 55 | + char usb_buf[65]; |
| 56 | + union kb2_port_t Pack; |
| 57 | + uint8_t cnt; |
| 58 | + uint8_t red,green,blue; |
| 59 | + uint16_t color_idx; |
| 60 | + uint32_t irgb[14]; |
| 61 | + uint16_t pos=0; |
| 62 | + for(color_idx=0; color_idx<(6*21); color_idx += 14) |
| 63 | + { |
| 64 | + for(cnt=0; cnt<14; cnt++) |
| 65 | + { |
| 66 | + pos = color_idx+cnt; |
| 67 | + red = color_data[pos*3+0]; |
| 68 | + green = color_data[pos*3+1]; |
| 69 | + blue = color_data[pos*3+2]; |
| 70 | + irgb[cnt] = blue&0xFF; |
| 71 | + irgb[cnt] <<= 8; |
| 72 | + irgb[cnt] |= green&0xFF; |
| 73 | + irgb[cnt] <<= 8; |
| 74 | + irgb[cnt] |= red&0xFF; |
| 75 | + irgb[cnt] <<= 8; |
| 76 | + irgb[cnt] |= pos&0xFF; |
| 77 | + } |
| 78 | + kb2M_wled(&Pack, irgb); |
| 79 | + usb_buf[0] = 0x04; |
| 80 | + for(uint8_t i=0; i<64; i++) usb_buf[i+1] = Pack.bin[i]; |
| 81 | + /*-----------------------------------------------------*\ |
| 82 | + | Send packet | |
| 83 | + \*-----------------------------------------------------*/ |
| 84 | + hid_write(dev, (unsigned char *)usb_buf, 65); |
| 85 | + } |
| 86 | + std::this_thread::sleep_for(std::chrono::milliseconds(5)); |
| 87 | +} |
| 88 | + |
| 89 | +void OKSKeyboardController::SendKeyboardModeEx(const mode &m, unsigned char red, unsigned char green, unsigned char blue) |
| 90 | +{ |
| 91 | + union kb2_port_t Pack; |
| 92 | + kb2M_wrgb(&Pack, m.brightness, m.value, m.speed, m.direction); |
| 93 | + Send(Pack.bin, Pack.length+KB2_HEAD_SIZE); |
| 94 | +} |
| 95 | + |
| 96 | +void OKSKeyboardController::Send(const uint8_t bin[], const uint16_t len) |
| 97 | +{ |
| 98 | + char usb_buf[65]; |
| 99 | + uint16_t Len; |
| 100 | + uint16_t pos; |
| 101 | + pos=0; |
| 102 | + while(pos<len) |
| 103 | + { |
| 104 | + /*-----------------------------------------------------*\ |
| 105 | + | Zero out buffer | |
| 106 | + \*-----------------------------------------------------*/ |
| 107 | + memset(usb_buf, 0x00, sizeof(usb_buf)); |
| 108 | + /*-----------------------------------------------------*\ |
| 109 | + | Set send data packet | |
| 110 | + \*-----------------------------------------------------*/ |
| 111 | + usb_buf[0] = 0x04; |
| 112 | + Len = len-pos; |
| 113 | + if(Len>64) Len=64; |
| 114 | + for(uint8_t i=0; i<Len; i++) usb_buf[i+1] = bin[i+pos]; |
| 115 | + pos += Len; |
| 116 | + /*-----------------------------------------------------*\ |
| 117 | + | Send packet | |
| 118 | + \*-----------------------------------------------------*/ |
| 119 | + hid_write(dev, (unsigned char *)usb_buf, 65); |
| 120 | + std::this_thread::sleep_for(std::chrono::milliseconds(2)); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +void OKSKeyboardController::SendInitialize() |
| 125 | +{ |
| 126 | + union kb2_port_t Pack; |
| 127 | + kb2M_wrgb(&Pack, 0, 0, 2, 1); |
| 128 | + Send(Pack.bin, Pack.length+KB2_HEAD_SIZE); |
| 129 | +} |
| 130 | + |
| 131 | +uint8_t OKSKeyboardController::kb2_ComputeChecksum(const union kb2_port_t* const Pack) |
| 132 | +{ |
| 133 | + uint8_t checksum = 0x35; |
| 134 | + const uint8_t len = Pack->length; |
| 135 | + |
| 136 | + checksum += Pack->head; |
| 137 | + checksum += len; |
| 138 | + checksum += Pack->cmd; |
| 139 | + if((len>0) && (len<=KB2_DATA_SIZE)) checksum += Pack->data[len-1]; |
| 140 | + return checksum; |
| 141 | +} |
| 142 | +int OKSKeyboardController::kb2_add_32b(union kb2_port_t* const Pack, const uint32_t value) |
| 143 | +{ |
| 144 | + union uint32_kb2 Value; |
| 145 | + if((Pack->length+4)>KB2_DATA_SIZE) return -1; |
| 146 | + Value.data = value; |
| 147 | + Pack->data[Pack->length++] = Value.Byte0; |
| 148 | + Pack->data[Pack->length++] = Value.Byte1; |
| 149 | + Pack->data[Pack->length++] = Value.Byte2; |
| 150 | + Pack->data[Pack->length++] = Value.Byte3; |
| 151 | + return 4; |
| 152 | +} |
| 153 | +void OKSKeyboardController::kb2M_init(union kb2_port_t* const Pack, const enum kb2_cmd cmd) |
| 154 | +{ |
| 155 | + Pack->head = KB2_PACK_HEAD; |
| 156 | + Pack->length = 0; |
| 157 | + Pack->cmd = cmd; |
| 158 | + Pack->checksum = 0; |
| 159 | +} |
| 160 | +void OKSKeyboardController::kb2M_wrgb(union kb2_port_t* const Pack, const uint8_t bright, const uint8_t mode, const uint8_t speed, const uint8_t dir) |
| 161 | +{ |
| 162 | + kb2M_init(Pack, KB2_CMD_WRGB); |
| 163 | + kb2_add_32b(Pack, bright); |
| 164 | + Pack->data[Pack->length++] = mode; |
| 165 | + Pack->data[Pack->length++] = speed; |
| 166 | + Pack->data[Pack->length++] = dir; |
| 167 | + Pack->data[Pack->length++] = 0xFF; |
| 168 | + Pack->data[Pack->length++] = 0xFF; |
| 169 | + Pack->checksum = kb2_ComputeChecksum(Pack); |
| 170 | +} |
| 171 | +void OKSKeyboardController::kb2M_wled(union kb2_port_t* const Pack, const uint32_t irgb[14]) |
| 172 | +{ |
| 173 | + kb2M_init(Pack, KB2_CMD_WLED); |
| 174 | + for(uint8_t i=0; i<14; i++) kb2_add_32b(Pack, irgb[i]); |
| 175 | + Pack->data[Pack->length++] = 0xFF; |
| 176 | + Pack->data[Pack->length++] = 0xFF; |
| 177 | + Pack->checksum = kb2_ComputeChecksum(Pack); |
| 178 | +} |
0 commit comments