|
| 1 | +/*---------------------------------------------------------*\ |
| 2 | +| RGBController_SinowealthKeyboard10c.cpp | |
| 3 | +| | |
| 4 | +| RGBController for Sinowealth Keyboards with PID 010C | |
| 5 | +| | |
| 6 | +| Rodrigo Tavares 27 Nov 2025 | |
| 7 | +| | |
| 8 | +| This file is part of the OpenRGB project | |
| 9 | +| SPDX-License-Identifier: GPL-2.0-or-later | |
| 10 | +\*---------------------------------------------------------*/ |
| 11 | + |
| 12 | +#include "KeyboardLayoutManager.h" |
| 13 | +#include "RGBControllerKeyNames.h" |
| 14 | +#include "SinowealthKeyboard10cController.h" |
| 15 | +#include "SinowealthKeyboard10cDevices.h" |
| 16 | +#include "RGBController_SinowealthKeyboard10c.h" |
| 17 | + |
| 18 | +using namespace kbd10c; |
| 19 | +using namespace std::chrono_literals; |
| 20 | + |
| 21 | +/**------------------------------------------------------------------*\ |
| 22 | + @name Sinowealth Keyboard |
| 23 | + @category Keyboard |
| 24 | + @type USB |
| 25 | + @save :x: |
| 26 | + @direct :white_check_mark: |
| 27 | + @effects :x: |
| 28 | + @detectors DetectSinowealthKeyboard10c |
| 29 | + @comment |
| 30 | +\*-------------------------------------------------------------------*/ |
| 31 | + |
| 32 | +RGBController_SinowealthKeyboard10c::RGBController_SinowealthKeyboard10c( |
| 33 | + SinowealthKeyboard10cController* controller_ptr, unsigned char model_id) |
| 34 | + : model_id(model_id) |
| 35 | +{ |
| 36 | + controller = controller_ptr; |
| 37 | + |
| 38 | + name = controller->GetName(); |
| 39 | + type = DEVICE_TYPE_KEYBOARD; |
| 40 | + vendor = "Sinowealth"; |
| 41 | + description = "Sinowealth Keyboard Device"; |
| 42 | + location = controller->GetLocation(); |
| 43 | + serial = controller->GetSerialString(); |
| 44 | + |
| 45 | + mode Off; |
| 46 | + Off.name = "Off"; |
| 47 | + Off.flags = 0; |
| 48 | + Off.color_mode = MODE_COLORS_NONE; |
| 49 | + Off.value = MODE_OFF; |
| 50 | + modes.push_back(Off); |
| 51 | + |
| 52 | + mode Direct; |
| 53 | + Direct.name = "Direct"; |
| 54 | + Direct.flags = MODE_FLAG_HAS_PER_LED_COLOR; |
| 55 | + Direct.color_mode = MODE_COLORS_PER_LED; |
| 56 | + Direct.value = MODE_DIRECT; |
| 57 | + modes.push_back(Direct); |
| 58 | + |
| 59 | + active_mode = MODE_DIRECT; |
| 60 | + |
| 61 | + SetupZones(); |
| 62 | + |
| 63 | + /*---------------------------------------------------------*\ |
| 64 | + | The Sinowealth 010C Keyboard requires a steady stream | |
| 65 | + | of packets in order to not revert out of direct mode. | |
| 66 | + | Start a thread to continuously refresh the device | |
| 67 | + \*---------------------------------------------------------*/ |
| 68 | + keepalive_thread_run = true; |
| 69 | + keepalive_thread = new std::thread(&RGBController_SinowealthKeyboard10c::KeepaliveThreadFunction, this); |
| 70 | +} |
| 71 | + |
| 72 | +RGBController_SinowealthKeyboard10c::~RGBController_SinowealthKeyboard10c() |
| 73 | +{ |
| 74 | + keepalive_thread_run = false; |
| 75 | + keepalive_thread->join(); |
| 76 | + delete keepalive_thread; |
| 77 | + delete controller; |
| 78 | +} |
| 79 | + |
| 80 | +void RGBController_SinowealthKeyboard10c::SetupZones() |
| 81 | +{ |
| 82 | + /*---------------------------------------------------------*\ |
| 83 | + | Create the keyboard zone usiung Keyboard Layout Manager | |
| 84 | + \*---------------------------------------------------------*/ |
| 85 | + zone new_zone; |
| 86 | + new_zone.name = ZONE_EN_KEYBOARD; |
| 87 | + new_zone.type = ZONE_TYPE_MATRIX; |
| 88 | + |
| 89 | + sinowealth_device device = sinowealth_10c_keyboards.at(model_id); |
| 90 | + |
| 91 | + KeyboardLayoutManager new_kb(KEYBOARD_LAYOUT_ANSI_QWERTY, device.keyboard_layout.base_size, |
| 92 | + device.keyboard_layout.key_values); |
| 93 | + new_kb.ChangeKeys(device.keyboard_layout.edit_keys); |
| 94 | + |
| 95 | + matrix_map_type* new_map = new matrix_map_type; |
| 96 | + new_zone.matrix_map = new_map; |
| 97 | + new_zone.matrix_map->height = new_kb.GetRowCount(); |
| 98 | + new_zone.matrix_map->width = new_kb.GetColumnCount(); |
| 99 | + |
| 100 | + new_zone.matrix_map->map = new unsigned int[new_map->height * new_map->width]; |
| 101 | + new_zone.leds_count = new_kb.GetRowCount() * new_kb.GetColumnCount(); |
| 102 | + new_zone.leds_min = new_zone.leds_count; |
| 103 | + new_zone.leds_max = new_zone.leds_count; |
| 104 | + |
| 105 | + /*---------------------------------------------------------*\ |
| 106 | + | These keyboards use sparse LED indexes — for example, a | |
| 107 | + | 99-key board might use LED indexes 0–112, leaving some | |
| 108 | + | numbers unused. Empty positions are marked 0xFFFFFFFF. | |
| 109 | + | | |
| 110 | + | We map each key to its actual LED index, filling the | |
| 111 | + | `leds` vector by those indexes and leaving gaps where no | |
| 112 | + | LED exists. | |
| 113 | + \*---------------------------------------------------------*/ |
| 114 | + |
| 115 | + new_kb.GetKeyMap(new_map->map, KEYBOARD_MAP_FILL_TYPE_VALUE, new_map->height, new_map->width); |
| 116 | + |
| 117 | + leds.resize(new_zone.leds_count); |
| 118 | + |
| 119 | + for(unsigned int i = 0, j = 0; i < new_zone.leds_count; i++) |
| 120 | + { |
| 121 | + if(new_map->map[i] == 0xFFFFFFFF) |
| 122 | + { |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + led new_led; |
| 127 | + |
| 128 | + new_led.name = new_kb.GetKeyNameAt(j); |
| 129 | + new_led.value = new_kb.GetKeyValueAt(j); |
| 130 | + |
| 131 | + leds[new_map->map[i]] = new_led; |
| 132 | + |
| 133 | + j++; |
| 134 | + } |
| 135 | + |
| 136 | + zones.push_back(new_zone); |
| 137 | + |
| 138 | + SetupColors(); |
| 139 | +} |
| 140 | + |
| 141 | +void RGBController_SinowealthKeyboard10c::ResizeZone(int /*zone*/, int /*new_size*/) |
| 142 | +{ |
| 143 | + /*---------------------------------------------------------*\ |
| 144 | + | This device does not support resizing zones | |
| 145 | + \*---------------------------------------------------------*/ |
| 146 | +} |
| 147 | + |
| 148 | +void RGBController_SinowealthKeyboard10c::DeviceUpdateLEDs() |
| 149 | +{ |
| 150 | + last_update_time = std::chrono::steady_clock::now(); |
| 151 | + controller->SetLEDsDirect(colors); |
| 152 | +} |
| 153 | + |
| 154 | +void RGBController_SinowealthKeyboard10c::UpdateZoneLEDs(int /*zone*/) |
| 155 | +{ |
| 156 | + DeviceUpdateLEDs(); |
| 157 | +} |
| 158 | + |
| 159 | +void RGBController_SinowealthKeyboard10c::UpdateSingleLED(int /*led*/) |
| 160 | +{ |
| 161 | + DeviceUpdateLEDs(); |
| 162 | +} |
| 163 | + |
| 164 | +void RGBController_SinowealthKeyboard10c::DeviceUpdateMode() |
| 165 | +{ |
| 166 | +} |
| 167 | + |
| 168 | +void RGBController_SinowealthKeyboard10c::KeepaliveThreadFunction() |
| 169 | +{ |
| 170 | + while(keepalive_thread_run.load()) |
| 171 | + { |
| 172 | + if(active_mode == MODE_DIRECT && (std::chrono::steady_clock::now() - last_update_time) > 1s) |
| 173 | + { |
| 174 | + UpdateLEDs(); |
| 175 | + } |
| 176 | + std::this_thread::sleep_for(500ms); |
| 177 | + } |
| 178 | +} |
0 commit comments