|
| 1 | +/*---------------------------------------------------------*\ |
| 2 | +| LenovoK510Controller.cpp | |
| 3 | +| | |
| 4 | +| Driver for Lenovo Legion K510 keyboard | |
| 5 | +| | |
| 6 | +| Bnyro 27 Oct 2025 | |
| 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 <vector> |
| 14 | +#include "LenovoK510Controller.h" |
| 15 | + |
| 16 | +using namespace std; |
| 17 | + |
| 18 | +LenovoK510Controller::LenovoK510Controller(hid_device* dev_handle, const hid_device_info& info, std::string dev_name) |
| 19 | +{ |
| 20 | + device = dev_handle; |
| 21 | + location = info.path; |
| 22 | + name = dev_name; |
| 23 | +} |
| 24 | + |
| 25 | +LenovoK510Controller::~LenovoK510Controller() |
| 26 | +{ |
| 27 | + hid_close(device); |
| 28 | +} |
| 29 | + |
| 30 | +std::string LenovoK510Controller::GetDeviceLocation() |
| 31 | +{ |
| 32 | + return("HID: " + location); |
| 33 | +} |
| 34 | + |
| 35 | +std::string LenovoK510Controller::GetDeviceName() |
| 36 | +{ |
| 37 | + return(name); |
| 38 | +} |
| 39 | + |
| 40 | +void LenovoK510Controller::SetMode(unsigned int color_mode, RGBColor color, unsigned char mode_value, unsigned int brigthness, unsigned int speed, unsigned int direction) |
| 41 | +{ |
| 42 | + unsigned char usb_buf[K510_DATA_SIZE]; |
| 43 | + memset(usb_buf, 0x00, K510_DATA_SIZE); |
| 44 | + usb_buf[0x00] = 0x04; // ReportID |
| 45 | + |
| 46 | + // magic bytes to trigger an LED update |
| 47 | + usb_buf[0x03] = 0x06; |
| 48 | + usb_buf[0x04] = 0x38; |
| 49 | + |
| 50 | + usb_buf[0x09] = mode_value; |
| 51 | + usb_buf[0x0A] = static_cast<unsigned char>(brigthness); |
| 52 | + // speed behaves contrary to normal expectations: the lower the value, the higher the speed |
| 53 | + usb_buf[0x0B] = static_cast<unsigned char>(speed); |
| 54 | + |
| 55 | + if(direction == MODE_DIRECTION_UP || direction == MODE_DIRECTION_LEFT) |
| 56 | + { |
| 57 | + // 0x01 reverses the direction of the animation |
| 58 | + usb_buf[0x0C] = 0x01; |
| 59 | + } |
| 60 | + |
| 61 | + if(color_mode == MODE_COLORS_MODE_SPECIFIC) |
| 62 | + { |
| 63 | + usb_buf[0x0D] = 0x00; |
| 64 | + usb_buf[0x0E] = static_cast<unsigned char>(RGBGetRValue(color)); |
| 65 | + usb_buf[0x0F] = static_cast<unsigned char>(RGBGetGValue(color)); |
| 66 | + usb_buf[0x10] = static_cast<unsigned char>(RGBGetBValue(color)); |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + // sets color to automatic |
| 71 | + usb_buf[0x0D] = 0x01; |
| 72 | + } |
| 73 | + |
| 74 | + hid_write(device, usb_buf, K510_DATA_SIZE); |
| 75 | +} |
| 76 | + |
| 77 | +mode LenovoK510Controller::GetCurrentState() |
| 78 | +{ |
| 79 | + unsigned char usb_buf[K510_DATA_SIZE]; |
| 80 | + memset(usb_buf, 0x00, K510_DATA_SIZE); |
| 81 | + usb_buf[0x00] = 0x04; // ReportID |
| 82 | + |
| 83 | + // magic bytes to get a response containing the current configuration |
| 84 | + usb_buf[0x03] = 0x05; |
| 85 | + usb_buf[0x04] = 0x38; |
| 86 | + |
| 87 | + hid_write(device, usb_buf, K510_DATA_SIZE); |
| 88 | + |
| 89 | + unsigned char res_buf[K510_DATA_SIZE]; |
| 90 | + hid_read_timeout(device, res_buf, K510_DATA_SIZE, 50); |
| 91 | + |
| 92 | + mode current_mode; |
| 93 | + current_mode.value = res_buf[0x09]; |
| 94 | + current_mode.brightness = res_buf[0x0A]; |
| 95 | + current_mode.speed = res_buf[0x0B]; |
| 96 | + current_mode.direction = res_buf[0x0C]; |
| 97 | + current_mode.color_mode = res_buf[0x0D] ? MODE_COLORS_RANDOM : MODE_COLORS_MODE_SPECIFIC; |
| 98 | + current_mode.colors.push_back(ToRGBColor(res_buf[0x0E], res_buf[0x0F], res_buf[0x10])); |
| 99 | + |
| 100 | + return(current_mode); |
| 101 | +} |
0 commit comments