Skip to content

Commit f948d6e

Browse files
Clean up some stuff in the Mountain keyboard controllers that violated conventions
* Mountain60KeyboardController's detector was setting the name member directly, this is an outdated convention that breaks the RGBController rework, moved to a Controller member * Mountain60KeyboardController had some sort of HID redetect logic, this should not be part of the Controller, this will be handled in the future by HID hotplugging * Both MountainKeyboardControllers defined static variables to keep track of current mode, moved these to class members so that they won't conflict if two instances exist * Don't send any device updates as part of SetupZones
1 parent 416898b commit f948d6e

7 files changed

+59
-93
lines changed

Controllers/MountainKeyboardController/Mountain60KeyboardController.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717

1818
using namespace std::chrono_literals;
1919

20-
Mountain60KeyboardController::Mountain60KeyboardController(hid_device* dev_handle, const char* path)
20+
Mountain60KeyboardController::Mountain60KeyboardController(hid_device* dev_handle, const char* path, std::string dev_name)
2121
{
2222
dev = dev_handle;
2323
location = path;
24+
name = dev_name;
2425
}
2526

2627
Mountain60KeyboardController::~Mountain60KeyboardController()
@@ -33,9 +34,9 @@ std::string Mountain60KeyboardController::GetDeviceLocation()
3334
return("HID: " + location);
3435
}
3536

36-
const char* Mountain60KeyboardController::GetPath()
37+
std::string Mountain60KeyboardController::GetNameString()
3738
{
38-
return location.c_str();
39+
return(name);
3940
}
4041

4142
std::string Mountain60KeyboardController::GetSerialString()
@@ -51,12 +52,6 @@ std::string Mountain60KeyboardController::GetSerialString()
5152
return(StringUtils::wstring_to_string(serial_string));
5253
}
5354

54-
void Mountain60KeyboardController::SetDevice(hid_device* new_device)
55-
{
56-
hid_close(dev);
57-
dev = new_device;
58-
}
59-
6055
void Mountain60KeyboardController::UpdateData()
6156
{
6257
unsigned char usb_buf[MOUNTAIN60_KEYBOARD_USB_BUFFER_SIZE];
@@ -85,17 +80,17 @@ void Mountain60KeyboardController::SendModeDetails(const mode* current_mode)
8580
usb_buf[0x04] = 0xEA; //constant data
8681

8782
usb_buf[0x05] = current_mode->value;
88-
usb_buf[0x07] = current_mode->value == MOUNTAIN60_KEYBOARD_MODE_STATIC ? 0x32 : current_mode->speed * 25;
83+
usb_buf[0x07] = (current_mode->value == MOUNTAIN60_KEYBOARD_MODE_STATIC) ? 0x32 : current_mode->speed * 25;
8984
usb_buf[0x08] = current_mode->brightness * 25;
90-
usb_buf[0x09] = current_mode->color_mode == MODE_COLORS_RANDOM ? MOUNTAIN60_KEYBOARD_COLOR_MODE_RAINBOW : color_mode[current_mode->colors.size() - 1];
85+
usb_buf[0x09] = (current_mode->color_mode == MODE_COLORS_RANDOM) ? (unsigned char)MOUNTAIN60_KEYBOARD_COLOR_MODE_RAINBOW : color_mode[current_mode->colors.size() - 1];
9186
usb_buf[0x0A] = ConvertDirection(current_mode->direction,current_mode->value == MOUNTAIN60_KEYBOARD_MODE_TORNADO);
9287

93-
for (int idx = 0; idx < current_mode->colors.size(); ++idx)
88+
for(std::size_t idx = 0; idx < current_mode->colors.size(); idx++)
9489
{
95-
unsigned int offset = 12 + (idx * 3);
96-
usb_buf[offset] = RGBGetRValue(current_mode->colors[idx]);
97-
usb_buf[offset+1] = RGBGetGValue(current_mode->colors[idx]);
98-
usb_buf[offset+2] = RGBGetBValue(current_mode->colors[idx]);
90+
std::size_t offset = (12 + (idx * 3));
91+
usb_buf[offset] = RGBGetRValue(current_mode->colors[idx]);
92+
usb_buf[offset+1] = RGBGetGValue(current_mode->colors[idx]);
93+
usb_buf[offset+2] = RGBGetBValue(current_mode->colors[idx]);
9994
}
10095

10196
hid_send_feature_report(dev, usb_buf, MOUNTAIN60_KEYBOARD_USB_BUFFER_SIZE);

Controllers/MountainKeyboardController/Mountain60KeyboardController.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,27 +69,27 @@ enum
6969
class Mountain60KeyboardController
7070
{
7171
public:
72-
Mountain60KeyboardController(hid_device* dev_handle, const char* path);
72+
Mountain60KeyboardController(hid_device* dev_handle, const char* path, std::string dev_name);
7373
~Mountain60KeyboardController();
7474

75-
const char* GetPath();
76-
std::string GetSerialString();
7775
std::string GetDeviceLocation();
76+
std::string GetNameString();
77+
std::string GetSerialString();
7878

7979
void UpdateData();
8080
void SaveData(unsigned char mode_idx);
8181
void SelectMode(unsigned char mode_idx);
82-
void SetDevice(hid_device * new_device);
8382
void SendModeDetails(const mode* current_mode);
8483
void SendDirect(unsigned int brightness,unsigned char* color_data, unsigned int color_count);
8584

8685
private:
86+
hid_device* dev;
87+
std::string location;
88+
std::string name;
89+
8790
unsigned char ConvertDirection(unsigned int direction, bool rotation);
8891

8992
void SendDirectStartPacketCmd(unsigned int brightness);
9093
void SendDirectPacketCmd(unsigned char stream_control, unsigned char *data, unsigned int data_size);
9194
void SendDirectPacketFinishCmd();
92-
93-
hid_device* dev;
94-
std::string location;
9595
};

Controllers/MountainKeyboardController/MountainKeyboardControllerDetect.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ void DetectMountain60KeyboardControllers(hid_device_info* info, const std::strin
4444

4545
if(dev)
4646
{
47-
Mountain60KeyboardController* controller = new Mountain60KeyboardController(dev, info->path);
47+
Mountain60KeyboardController* controller = new Mountain60KeyboardController(dev, info->path, name);
4848
RGBController_Mountain60Keyboard* rgb_controller = new RGBController_Mountain60Keyboard(controller);
49-
rgb_controller->name = name;
49+
5050
ResourceManager::get()->RegisterRGBController(rgb_controller);
5151
}
5252
}

Controllers/MountainKeyboardController/RGBController_Mountain60Keyboard.cpp

Lines changed: 29 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ layout_values mountain60_layout =
3838
mountain60_keyboard_key_id_values,
3939
{
4040
/*------------------------------------------*\
41-
| No regional layout fix for the moment |
42-
\*------------------------------------------*/
41+
| No regional layout fix for the moment |
42+
\*------------------------------------------*/
4343
},
4444
};
4545

@@ -182,10 +182,10 @@ keyboard_keymap_overlay_values mountain60_keyboard_overlay_no_numpad =
182182
RGBController_Mountain60Keyboard::RGBController_Mountain60Keyboard(Mountain60KeyboardController* controller_ptr)
183183
{
184184
controller = controller_ptr;
185-
name = "Mountain Everest 60 Keyboard";
185+
name = controller->GetNameString();
186186
vendor = "Mountain";
187187
type = DEVICE_TYPE_KEYBOARD;
188-
description = "Mountain Everest Keyboard 60%";
188+
description = "Mountain Everest Keyboard 60% Device";
189189
location = controller->GetDeviceLocation();
190190
serial = controller->GetSerialString();
191191

@@ -329,22 +329,22 @@ RGBController_Mountain60Keyboard::RGBController_Mountain60Keyboard(Mountain60Key
329329
Yeti.colors.resize(2);
330330
modes.push_back(Yeti);
331331

332+
active_mode = 0;
333+
current_mode_value = -1;
334+
332335
SetupZones();
333336

334337
/*-----------------------------------------------------*\
335338
| The Mountain Everest 60 keyboard need to send a |
336339
| specific packet frequently so that leds get updated |
337340
\*-----------------------------------------------------*/
338341
mountain_thread = new std::thread(&RGBController_Mountain60Keyboard::UpdateMountain, this);
339-
mountaint_thread_running = true;
340-
update_device = true;
341-
found_device = true;
342+
mountain_thread_running = true;
342343
}
343344

344345
RGBController_Mountain60Keyboard::~RGBController_Mountain60Keyboard()
345346
{
346-
update_device = false;
347-
mountaint_thread_running = false;
347+
mountain_thread_running = false;
348348
mountain_thread->join();
349349
delete mountain_thread;
350350

@@ -391,9 +391,8 @@ void RGBController_Mountain60Keyboard::SetupZones()
391391
}
392392

393393
zones.push_back(new_zone);
394+
394395
SetupColors();
395-
DeviceUpdateMode();
396-
update_device = true;
397396
}
398397

399398
void RGBController_Mountain60Keyboard::ResizeZone(int /*zone*/, int /*new_size*/)
@@ -405,26 +404,23 @@ void RGBController_Mountain60Keyboard::ResizeZone(int /*zone*/, int /*new_size*/
405404

406405
void RGBController_Mountain60Keyboard::DeviceUpdateLEDs()
407406
{
408-
if (update_device.load())
409-
{
410-
unsigned char* color_data = new unsigned char[(leds.size()*4)];
407+
unsigned char* color_data = new unsigned char[(leds.size()*4)];
411408

412-
/*---------------------------------------------------------*\
413-
| Filling the color_data vector with progressive index |
414-
| leaving space for RGB data |
415-
\*---------------------------------------------------------*/
416-
for(unsigned int led_idx = 0; led_idx < leds.size(); led_idx++)
417-
{
418-
const unsigned int idx = led_idx * 4;
419-
color_data[idx] = leds[led_idx].value;
420-
color_data[idx + 1] = RGBGetRValue(colors[led_idx]);
421-
color_data[idx + 2] = RGBGetGValue(colors[led_idx]);
422-
color_data[idx + 3] = RGBGetBValue(colors[led_idx]);
423-
}
424-
425-
controller->SendDirect(modes[active_mode].brightness, color_data, (leds.size()*4));
426-
delete[] color_data;
409+
/*---------------------------------------------------------*\
410+
| Filling the color_data vector with progressive index |
411+
| leaving space for RGB data |
412+
\*---------------------------------------------------------*/
413+
for(unsigned int led_idx = 0; led_idx < leds.size(); led_idx++)
414+
{
415+
const unsigned int idx = led_idx * 4;
416+
color_data[idx] = leds[led_idx].value;
417+
color_data[idx + 1] = RGBGetRValue(colors[led_idx]);
418+
color_data[idx + 2] = RGBGetGValue(colors[led_idx]);
419+
color_data[idx + 3] = RGBGetBValue(colors[led_idx]);
427420
}
421+
422+
controller->SendDirect(modes[active_mode].brightness, color_data, (leds.size()*4));
423+
delete[] color_data;
428424
}
429425

430426
void RGBController_Mountain60Keyboard::UpdateZoneLEDs(int /*zone*/)
@@ -439,15 +435,13 @@ void RGBController_Mountain60Keyboard::UpdateSingleLED(int /*led*/)
439435

440436
void RGBController_Mountain60Keyboard::DeviceUpdateMode()
441437
{
442-
static mode current_mode;
443-
444-
if(modes[active_mode].value != current_mode.value && found_device.load())
438+
if(modes[active_mode].value != current_mode_value)
445439
{
446-
current_mode = modes[active_mode];
440+
current_mode_value = modes[active_mode].value;
447441
controller->SelectMode(modes[active_mode].value);
448442
}
449443

450-
if(current_mode.color_mode != MODE_FLAG_HAS_PER_LED_COLOR && update_device.load())
444+
if(modes[active_mode].color_mode != MODE_FLAG_HAS_PER_LED_COLOR)
451445
{
452446
controller->SendModeDetails(&modes[active_mode]);
453447
}
@@ -460,31 +454,10 @@ void RGBController_Mountain60Keyboard::DeviceSaveMode()
460454

461455
void RGBController_Mountain60Keyboard::UpdateMountain()
462456
{
463-
while (mountaint_thread_running.load())
457+
while(mountain_thread_running.load())
464458
{
465459
std::this_thread::sleep_for(MOUNTAIN60_KEEP_LIVE_PERIOD);
466460

467461
controller->UpdateData();
468-
469-
const char* Path = controller->GetPath();
470-
hid_device * rescan_device = hid_open_path(Path);
471-
472-
if (rescan_device)
473-
{
474-
if (!found_device.load())
475-
{
476-
controller->SetDevice(rescan_device);
477-
found_device = true;
478-
update_device = true;
479-
}
480-
}
481-
else
482-
{
483-
if (found_device.load())
484-
{
485-
update_device = false;
486-
found_device = false;
487-
}
488-
}
489462
}
490463
}

Controllers/MountainKeyboardController/RGBController_Mountain60Keyboard.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ class RGBController_Mountain60Keyboard : public RGBController
4444

4545
private:
4646
Mountain60KeyboardController* controller;
47+
int current_mode_value;
4748
std::thread* mountain_thread;
48-
std::atomic<bool> mountaint_thread_running;
49-
std::atomic<bool> update_device;
50-
std::atomic<bool> found_device;
49+
std::atomic<bool> mountain_thread_running;
5150
};

Controllers/MountainKeyboardController/RGBController_MountainKeyboard.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,9 @@ RGBController_MountainKeyboard::RGBController_MountainKeyboard(MountainKeyboardC
680680
Matrix.colors.resize(2);
681681
modes.push_back(Matrix);
682682

683+
prv_mode = -1;
684+
active_mode = 0;
685+
683686
SetupZones();
684687
}
685688

@@ -760,9 +763,8 @@ void RGBController_MountainKeyboard::SetupZones()
760763
}
761764
}
762765
}
763-
SetupColors();
764766

765-
DeviceUpdateMode();
767+
SetupColors();
766768
}
767769

768770
void RGBController_MountainKeyboard::ResizeZone(int /*zone*/, int /*new_size*/)
@@ -1008,15 +1010,12 @@ void RGBController_MountainKeyboard::DeviceUpdate(const mode& current_mode)
10081010
}
10091011
}
10101012

1011-
10121013
void RGBController_MountainKeyboard::DeviceUpdateLEDs()
10131014
{
10141015
mode current_mode = modes[active_mode];
10151016
DeviceUpdate(current_mode);
10161017
}
10171018

1018-
1019-
10201019
void RGBController_MountainKeyboard::UpdateZoneLEDs(int /*zone*/)
10211020
{
10221021
DeviceUpdateLEDs();
@@ -1029,9 +1028,8 @@ void RGBController_MountainKeyboard::UpdateSingleLED(int /*led*/)
10291028

10301029
void RGBController_MountainKeyboard::DeviceUpdateMode()
10311030
{
1032-
static int prv_mode =-1;
1033-
10341031
mode current_mode = modes[active_mode];
1032+
10351033
if(prv_mode != current_mode.value)
10361034
{
10371035
controller->SelectMode(current_mode.value);

Controllers/MountainKeyboardController/RGBController_MountainKeyboard.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ class RGBController_MountainKeyboard : public RGBController
5353

5454
private:
5555
MountainKeyboardController* controller;
56+
int prv_mode;
57+
unsigned char wheel_color[3];
58+
5659
unsigned char ConvertDirection(unsigned int direction, bool rotation);
5760
void DeviceUpdate(const mode& current_mode);
58-
59-
unsigned char wheel_color [3];
6061
};

0 commit comments

Comments
 (0)