Skip to content

Commit c1781bc

Browse files
macromorganCalcProgrammer1
authored andcommitted
Add support for led_classdev_mc via sysfs
Update the LinuxLEDController to support multicolor LEDs defined by the Linux kernel. For multicolor LEDs Linux exposes a single brightness which allows a range of 0 (off) to 255 (max) and a multi_intensity value that accepts multiple channels of colors. This current implementation assumes that there are 3 multi_intensity channels between 0 and 255. https://docs.kernel.org/leds/leds-class-multicolor.html Note that for now in Linux the brightness value doesn't so much affect the brightness of the LED as it does apply a coefficient to the individual channel intensities. For now hard code the brightness to 255 and simply adjust the intensities to alter the color. This change should keep the existing sysfs code path as-is and only attempt to use this new method when a led_rgb_path is present in the config file. For reference, my OpenRGB.json has the following lines: ` "LinuxLEDDevices" : { "devices": [ { "name": "Fan LED 1", "rgb_path": "/sys/class/leds/rgb:programming-0/" } ] }` Signed-off-by: Chris Morgan <macromorgan@hotmail.com>
1 parent 5966632 commit c1781bc

4 files changed

Lines changed: 65 additions & 23 deletions

File tree

Controllers/LinuxLEDController/LinuxLEDControllerDetect_Linux.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ void DetectLinuxLEDControllers()
4242
std::string red_path;
4343
std::string green_path;
4444
std::string blue_path;
45+
std::string rgb_path;
4546

4647
if(linux_led_settings["devices"][device_idx].contains("name"))
4748
{
@@ -63,10 +64,16 @@ void DetectLinuxLEDControllers()
6364
blue_path = linux_led_settings["devices"][device_idx]["blue_path"];
6465
}
6566

67+
if(linux_led_settings["devices"][device_idx].contains("rgb_path"))
68+
{
69+
rgb_path = linux_led_settings["devices"][device_idx]["rgb_path"];
70+
}
71+
6672
LinuxLEDController* controller = new LinuxLEDController(name);
6773
controller->OpenRedPath(red_path);
6874
controller->OpenGreenPath(green_path);
6975
controller->OpenBluePath(blue_path);
76+
controller->OpenRgbPath(rgb_path);
7077

7178
RGBController_LinuxLED* rgb_controller = new RGBController_LinuxLED(controller);
7279

Controllers/LinuxLEDController/LinuxLEDController_Linux.cpp

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ std::string LinuxLEDController::GetBluePath()
4141
return(led_b_path);
4242
}
4343

44+
std::string LinuxLEDController::GetRgbPath()
45+
{
46+
return(led_rgb_path);
47+
}
48+
4449
void LinuxLEDController::OpenRedPath(std::string red_path)
4550
{
4651
led_r_path = red_path;
@@ -59,29 +64,54 @@ void LinuxLEDController::OpenBluePath(std::string blue_path)
5964
led_b_brightness.open(led_b_path + "brightness");
6065
}
6166

67+
void LinuxLEDController::OpenRgbPath(std::string rgb_path)
68+
{
69+
led_rgb_path = rgb_path;
70+
led_rgb_brightness.open(led_rgb_path + "brightness");
71+
led_rgb_color.open(led_rgb_path + "multi_intensity");
72+
}
73+
6274
void LinuxLEDController::SetRGB(unsigned char red, unsigned char grn, unsigned char blu)
6375
{
6476
std::string brightness_str;
6577

66-
/*-------------------------------------------------------------*\
67-
| My phone LED that I tested this on shuts down if you set zero |
68-
\*-------------------------------------------------------------*/
69-
if(red == 0) red = 1;
70-
if(grn == 0) grn = 1;
71-
if(blu == 0) blu = 1;
72-
73-
brightness_str = std::to_string((unsigned int)red);
74-
75-
led_r_brightness.write(brightness_str.c_str(), brightness_str.length());
76-
led_r_brightness.flush();
77-
78-
brightness_str = std::to_string((unsigned int)grn);
79-
80-
led_g_brightness.write(brightness_str.c_str(), brightness_str.length());
81-
led_g_brightness.flush();
82-
83-
brightness_str = std::to_string((unsigned int)blu);
84-
85-
led_b_brightness.write(brightness_str.c_str(), brightness_str.length());
86-
led_b_brightness.flush();
78+
if (led_rgb_path.empty()) {
79+
/*-------------------------------------------------------------*\
80+
| My phone LED that I tested this on shuts down if you set zero |
81+
\*-------------------------------------------------------------*/
82+
if(red == 0) red = 1;
83+
if(grn == 0) grn = 1;
84+
if(blu == 0) blu = 1;
85+
86+
brightness_str = std::to_string((unsigned int)red);
87+
88+
led_r_brightness.write(brightness_str.c_str(), brightness_str.length());
89+
led_r_brightness.flush();
90+
91+
brightness_str = std::to_string((unsigned int)grn);
92+
93+
led_g_brightness.write(brightness_str.c_str(), brightness_str.length());
94+
led_g_brightness.flush();
95+
96+
brightness_str = std::to_string((unsigned int)blu);
97+
98+
led_b_brightness.write(brightness_str.c_str(), brightness_str.length());
99+
led_b_brightness.flush();
100+
}
101+
102+
else {
103+
/*
104+
* For the led_classdev_mc brightness just aplies a coefficient to the
105+
* multi_intensity. Set brightness to maximum and use the RGB values
106+
* directly instead.
107+
*/
108+
brightness_str = std::to_string((unsigned int)255);
109+
led_rgb_brightness.write(brightness_str.c_str(), brightness_str.length());
110+
led_rgb_brightness.flush();
111+
brightness_str = std::to_string((unsigned int)red) + " " \
112+
+ std::to_string((unsigned int)grn) + " " \
113+
+ std::to_string((unsigned int)blu);
114+
led_rgb_color.write(brightness_str.c_str(), brightness_str.length());
115+
led_rgb_color.flush();
116+
}
87117
}

Controllers/LinuxLEDController/LinuxLEDController_Linux.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,24 @@ class LinuxLEDController
2424
std::string GetRedPath();
2525
std::string GetBluePath();
2626
std::string GetGreenPath();
27+
std::string GetRgbPath();
2728

2829
void OpenRedPath(std::string red_path);
2930
void OpenGreenPath(std::string green_path);
3031
void OpenBluePath(std::string blue_path);
32+
void OpenRgbPath(std::string rgb_path);
3133

3234
void SetRGB(unsigned char red, unsigned char grn, unsigned char blu);
3335

3436
private:
3537
std::string led_r_path;
3638
std::string led_g_path;
3739
std::string led_b_path;
40+
std::string led_rgb_path;
3841
std::ofstream led_r_brightness;
3942
std::ofstream led_g_brightness;
4043
std::ofstream led_b_brightness;
44+
std::ofstream led_rgb_brightness;
45+
std::ofstream led_rgb_color;
4146
std::string name;
4247
};

Controllers/LinuxLEDController/RGBController_LinuxLED_Linux.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ RGBController_LinuxLED::RGBController_LinuxLED(LinuxLEDController* controller_pt
3232

3333
location = "R: " + controller->GetRedPath() + "\r\n" +
3434
"G: " + controller->GetGreenPath() + "\r\n" +
35-
"B: " + controller->GetBluePath();
36-
35+
"B: " + controller->GetBluePath() + "\r\n" +
36+
"M: " + controller->GetRgbPath();
3737
mode Direct;
3838
Direct.name = "Direct";
3939
Direct.value = 0;

0 commit comments

Comments
 (0)