From 6b24284340d626496b41a9eaf5773008fe60b5d3 Mon Sep 17 00:00:00 2001 From: Adam Honse Date: Sun, 8 Nov 2026 21:46:20 -0600 Subject: [PATCH] Add controller for Retroid and AYN devices using /sys/class/leds/rgb: multi_intensity control method, tested on Retroid Pocket Nova --- .../RGBController_RetroidAYN_Linux.cpp | 133 ++++++++++ .../RGBController_RetroidAYN_Linux.h | 33 +++ .../RetroidAYNControllerDetect_Linux.cpp | 137 ++++++++++ .../RetroidAYNController_Linux.cpp | 233 ++++++++++++++++++ .../RetroidAYNController_Linux.h | 50 ++++ 5 files changed, 586 insertions(+) create mode 100644 Controllers/RetroidAYNController/RGBController_RetroidAYN_Linux.cpp create mode 100644 Controllers/RetroidAYNController/RGBController_RetroidAYN_Linux.h create mode 100644 Controllers/RetroidAYNController/RetroidAYNControllerDetect_Linux.cpp create mode 100644 Controllers/RetroidAYNController/RetroidAYNController_Linux.cpp create mode 100644 Controllers/RetroidAYNController/RetroidAYNController_Linux.h diff --git a/Controllers/RetroidAYNController/RGBController_RetroidAYN_Linux.cpp b/Controllers/RetroidAYNController/RGBController_RetroidAYN_Linux.cpp new file mode 100644 index 00000000..8723cdfb --- /dev/null +++ b/Controllers/RetroidAYNController/RGBController_RetroidAYN_Linux.cpp @@ -0,0 +1,133 @@ +/*---------------------------------------------------------*\ +| RGBController_RetroidAYN_Linux.cpp | +| | +| RGBController for Retroid and AYN handheld RGB LEDs | +| | +| Adam Honse 08 Nov 2026 | +| | +| This file is part of the OpenRGB project | +| SPDX-License-Identifier: GPL-2.0-or-later | +\*---------------------------------------------------------*/ + +#include "RGBController_RetroidAYN_Linux.h" + +/**------------------------------------------------------------------*\ + @name Retroid/AYN RGB LEDs + @category LEDStrip + @type + @save :x: + @direct :white_check_mark: + @effects :x: + @detectors DetectRetroidAYNControllers + @comment +\*-------------------------------------------------------------------*/ + +RGBController_RetroidAYN::RGBController_RetroidAYN(RetroidAYNController* controller_ptr) +{ + controller = controller_ptr; + + name = controller->GetName(); + vendor = "Retroid/AYN"; + type = DEVICE_TYPE_GAMEPAD; + description = "Retroid/AYN RGB LED Device"; + location = controller->GetLocation(); + + mode direct_mode; + direct_mode.name = "Direct"; + direct_mode.value = 0; + direct_mode.flags = MODE_FLAG_HAS_PER_LED_COLOR | MODE_FLAG_HAS_BRIGHTNESS; + direct_mode.color_mode = MODE_COLORS_PER_LED; + direct_mode.speed_min = 0; + direct_mode.speed_max = 0; + direct_mode.speed = 0; + direct_mode.brightness_min = 0; + direct_mode.brightness_max = controller->GetMaxBrightness(0); + direct_mode.brightness = controller->GetBrightness(0); + modes.push_back(direct_mode); + + SetupZones(); + + for(std::size_t led_idx = 0; led_idx < controller->GetLEDCount(); led_idx++) + { + colors[led_idx] = controller->GetLEDColor((unsigned int)led_idx); + } +} + +RGBController_RetroidAYN::~RGBController_RetroidAYN() +{ + Shutdown(); + + delete controller; +} + +void RGBController_RetroidAYN::SetupZones() +{ + int led_count = (int)controller->GetLEDCount(); + int left_count = led_count / 2; + int right_count = led_count - left_count; + + /*-----------------------------------------------------*\ + | Create Left Stick zone (l1-l4) | + \*-----------------------------------------------------*/ + zone left_zone; + left_zone.name = "Left Stick"; + left_zone.type = ZONE_TYPE_LINEAR; + left_zone.leds_min = (unsigned int)left_count; + left_zone.leds_max = (unsigned int)left_count; + left_zone.leds_count = (unsigned int)left_count; + zones.push_back(left_zone); + + /*-----------------------------------------------------*\ + | Create Right Stick zone (r1-r4) | + \*-----------------------------------------------------*/ + zone right_zone; + right_zone.name = "Right Stick"; + right_zone.type = ZONE_TYPE_LINEAR; + right_zone.leds_min = (unsigned int)right_count; + right_zone.leds_max = (unsigned int)right_count; + right_zone.leds_count = (unsigned int)right_count; + zones.push_back(right_zone); + + /*-----------------------------------------------------*\ + | Create LED entries for each LED in the controller | + | LEDs 0-3 are Left Stick, LEDs 4-7 are Right Stick | + \*-----------------------------------------------------*/ + for(std::size_t led_idx = 0; led_idx < controller->GetLEDCount(); led_idx++) + { + led new_led; + new_led.name = "LED " + std::to_string(led_idx + 1); + + leds.push_back(new_led); + } + + SetupColors(); +} + +void RGBController_RetroidAYN::DeviceUpdateLEDs() +{ + for(std::size_t led_idx = 0; led_idx < controller->GetLEDCount(); led_idx++) + { + controller->SetLEDColor((unsigned int)led_idx, colors[led_idx]); + } +} + +void RGBController_RetroidAYN::DeviceUpdateZoneLEDs(int zone) +{ + for(unsigned int led_idx = zones[zone].start_idx; led_idx < zones[zone].start_idx + zones[zone].leds_count; led_idx++) + { + DeviceUpdateSingleLED(led_idx); + } +} + +void RGBController_RetroidAYN::DeviceUpdateSingleLED(int led) +{ + controller->SetLEDColor(led, colors[led]); +} + +void RGBController_RetroidAYN::DeviceUpdateMode() +{ + for(std::size_t led_idx = 0; led_idx < controller->GetLEDCount(); led_idx++) + { + controller->SetBrightness((unsigned int)led_idx, modes[active_mode].brightness); + } +} diff --git a/Controllers/RetroidAYNController/RGBController_RetroidAYN_Linux.h b/Controllers/RetroidAYNController/RGBController_RetroidAYN_Linux.h new file mode 100644 index 00000000..584c731a --- /dev/null +++ b/Controllers/RetroidAYNController/RGBController_RetroidAYN_Linux.h @@ -0,0 +1,33 @@ +/*---------------------------------------------------------*\ +| RGBController_RetroidAYN_Linux.h | +| | +| RGBController for Retroid and AYN handheld RGB LEDs | +| | +| Adam Honse 08 Nov 2026 | +| | +| This file is part of the OpenRGB project | +| SPDX-License-Identifier: GPL-2.0-or-later | +\*---------------------------------------------------------*/ + +#pragma once + +#include "RGBController.h" +#include "RetroidAYNController_Linux.h" + +class RGBController_RetroidAYN : public RGBController +{ +public: + RGBController_RetroidAYN(RetroidAYNController* controller_ptr); + ~RGBController_RetroidAYN(); + + void SetupZones(); + + void DeviceUpdateLEDs(); + void DeviceUpdateZoneLEDs(int zone); + void DeviceUpdateSingleLED(int led); + + void DeviceUpdateMode(); + +private: + RetroidAYNController* controller; +}; diff --git a/Controllers/RetroidAYNController/RetroidAYNControllerDetect_Linux.cpp b/Controllers/RetroidAYNController/RetroidAYNControllerDetect_Linux.cpp new file mode 100644 index 00000000..2d746b66 --- /dev/null +++ b/Controllers/RetroidAYNController/RetroidAYNControllerDetect_Linux.cpp @@ -0,0 +1,137 @@ +/*---------------------------------------------------------*\ +| RetroidAYNControllerDetect_Linux.cpp | +| | +| Detector for Retroid/AYN RGB LEDs via Linux sysfs | +| | +| This file is part of the OpenRGB project | +| SPDX-License-Identifier: GPL-2.0-or-later | +\*---------------------------------------------------------*/ + +#include +#include +#include +#include +#include +#include "DetectionManager.h" +#include "StringUtils.h" +#include "filesystem.h" +#include "RGBController_RetroidAYN_Linux.h" +#include "RetroidAYNController_Linux.h" + +static std::string ReadDeviceModel() +{ + std::ifstream model_file("/sys/firmware/devicetree/base/model"); + + if(!model_file.is_open()) + { + return(""); + } + + std::string model; + std::getline(model_file, model, '\0'); + model_file.close(); + + return StringUtils::remove_null_terminating_chars(model); +} + +static bool IsRetroidOrAYNDevice(const std::string& model) +{ + if(model.empty()) + { + return(false); + } + + std::string lower = StringUtils::remove_null_terminating_chars(model); + + for(char& c : lower) + { + c = static_cast(std::tolower(static_cast(c))); + } + + return(lower.find("retroid") != std::string::npos || lower.find("ayn") != std::string::npos); +} + +void DetectRetroidAYNLEDs(std::vector& led_paths) +{ + /*-----------------------------------------------------*\ + | First verify this is a Retroid/AYN device | + \*-----------------------------------------------------*/ + std::string model = ReadDeviceModel(); + + if(!IsRetroidOrAYNDevice(model)) + { + return; + } + + const std::string leds_class_path = "/sys/class/leds"; + + if(!filesystem::exists(leds_class_path)) + { + return; + } + + for(const filesystem::directory_entry& entry : filesystem::directory_iterator(leds_class_path)) + { + std::string led_dir = entry.path().string(); + std::string led_name = filesystem::path(led_dir).filename().string(); + + /*-------------------------------------------------*\ + | Match LED directories starting with "rgb:" | + \*-------------------------------------------------*/ + if(led_name.rfind("rgb:", 0) == 0) + { + /*---------------------------------------------*\ + | Verify this LED supports multi_intensity | + \*---------------------------------------------*/ + std::string multi_intensity_path = led_dir + "/multi_intensity"; + if(filesystem::exists(multi_intensity_path)) + { + led_paths.push_back(led_dir); + } + } + } + + /*-----------------------------------------------------*\ + | Sort the LED paths alphabetically for consistent | + | ordering (l1, l2, l3, l4, r1, r2, r3, r4) | + \*-----------------------------------------------------*/ + std::sort(led_paths.begin(), led_paths.end()); +} + +DetectedControllers DetectRetroidAYNControllers() +{ + DetectedControllers detected_controllers; + + /*-----------------------------------------------------*\ + | Read the actual device model name for the controller | + | display name (e.g. "Retroid Pocket Nova") | + \*-----------------------------------------------------*/ + std::string device_model = ReadDeviceModel(); + + /*-----------------------------------------------------*\ + | Detect Retroid/AYN (rgb:*) multicolor LED controller | + \*-----------------------------------------------------*/ + std::vector retroid_ayn_led_paths; + + DetectRetroidAYNLEDs(retroid_ayn_led_paths); + + if(retroid_ayn_led_paths.size() > 0) + { + RetroidAYNController* controller = new RetroidAYNController(device_model); + + for(unsigned int led_idx = 0; led_idx < retroid_ayn_led_paths.size(); led_idx++) + { + controller->AddLED(retroid_ayn_led_paths[led_idx]); + } + + RGBController_RetroidAYN* rgb_controller = new RGBController_RetroidAYN(controller); + + detected_controllers.push_back(rgb_controller); + } + + return(detected_controllers); +} + +REGISTER_DETECTOR("Retroid/AYN RGB", DetectRetroidAYNControllers); + +REGISTER_CUSTOM_UDEV_RULE(retroid_ayn_rgb, "Retroid/AYN RGB", "SUBSYSTEM==\\\"leds\\\", KERNEL==\\\"rgb:*\\\", TAG+=\\\"uaccess\\\""); diff --git a/Controllers/RetroidAYNController/RetroidAYNController_Linux.cpp b/Controllers/RetroidAYNController/RetroidAYNController_Linux.cpp new file mode 100644 index 00000000..52ef68a1 --- /dev/null +++ b/Controllers/RetroidAYNController/RetroidAYNController_Linux.cpp @@ -0,0 +1,233 @@ +/*---------------------------------------------------------*\ +| RetroidAYNController_Linux.cpp | +| | +| Driver for Retroid and AYN handheld RGB LEDs | +| | +| Adam Honse 08 Nov 2026 | +| | +| This file is part of the OpenRGB project | +| SPDX-License-Identifier: GPL-2.0-or-later | +\*---------------------------------------------------------*/ + +#include +#include +#include "RetroidAYNController_Linux.h" + +RetroidAYNController::RetroidAYNController(std::string dev_name) +{ + name = dev_name; +} + +RetroidAYNController::~RetroidAYNController() +{ +} + +std::string RetroidAYNController::GetName() +{ + return(name); +} + +std::string RetroidAYNController::GetLocation() +{ + return(led_paths[0]); +} + +std::size_t RetroidAYNController::GetLEDCount() +{ + return(led_paths.size()); +} + +void RetroidAYNController::AddLED(std::string led_path) +{ + /*-----------------------------------------------------*\ + | Ensure the path ends with a slash | + \*-----------------------------------------------------*/ + std::string path = led_path; + + if(path.back() != '/') + { + path += '/'; + } + + led_paths.push_back(path); + + /*-----------------------------------------------------*\ + | Read the channel order from multi_index | + | (e.g. "red green blue" or "blue green red") | + \*-----------------------------------------------------*/ + ReadChannelOrder(path); + + /*-----------------------------------------------------*\ + | Open the multi_intensity file for this LED | + | This file accepts space-separated values in the | + | channel order specified by multi_index | + \*-----------------------------------------------------*/ + std::ofstream multi_intensity_file; + multi_intensity_file.open(path + "multi_intensity"); + led_multi_intensity.push_back(std::move(multi_intensity_file)); + + /*-----------------------------------------------------*\ + | Open the brightness file for this LED | + \*-----------------------------------------------------*/ + std::ofstream brightness_file; + brightness_file.open(path + "brightness"); + led_brightness.push_back(std::move(brightness_file)); +} + +unsigned int RetroidAYNController::GetBrightness(unsigned int led_idx) +{ + unsigned int brightness_value = 0; + + if(led_idx < led_paths.size()) + { + std::ifstream brightness_file(led_paths[led_idx] + "brightness"); + + if(brightness_file.is_open()) + { + std::string brightness_str; + + if(std::getline(brightness_file, brightness_str)) + { + brightness_value = std::stoi(brightness_str); + } + + brightness_file.close(); + } + } + + return(brightness_value); +} + +RGBColor RetroidAYNController::GetLEDColor(unsigned int led_idx) +{ + RGBColor return_color = 0; + std::ifstream color_file(led_paths[led_idx] + "multi_intensity"); + + if(color_file.is_open()) + { + std::string color_str; + if(std::getline(color_file, color_str)) + { + std::istringstream iss(color_str); + std::string token; + std::vector raw_color; + + while(iss >> token) + { + raw_color.push_back(static_cast(std::stoi(token))); + } + + if(raw_color.size() >= 3) + { + return_color = ToRGBColor(raw_color[led_channel_order_red[0]], raw_color[led_channel_order_green[0]], raw_color[led_channel_order_blue[0]]); + } + } + color_file.close(); + } + + return(return_color); +} + +unsigned int RetroidAYNController::GetMaxBrightness(unsigned int led_idx) +{ + unsigned int brightness_value = 0; + + if(led_idx < led_paths.size()) + { + std::ifstream brightness_file(led_paths[led_idx] + "max_brightness"); + + if(brightness_file.is_open()) + { + std::string brightness_str; + + if(std::getline(brightness_file, brightness_str)) + { + brightness_value = std::stoi(brightness_str); + } + + brightness_file.close(); + } + } + + return(brightness_value); +} + +void RetroidAYNController::SetBrightness(unsigned int led_idx, unsigned int brightness) +{ + if(led_idx < led_brightness.size() && led_brightness[led_idx].is_open()) + { + std::string brightness_str = std::to_string(brightness); + led_brightness[led_idx].write(brightness_str.c_str(), brightness_str.length()); + led_brightness[led_idx].flush(); + } +} + +void RetroidAYNController::SetLEDColor(unsigned int led_idx, RGBColor color) +{ + /*-----------------------------------------------------*\ + | Use multi_intensity to set RGB values. The values | + | are written in the channel order specified by | + | multi_index. | + \*-----------------------------------------------------*/ + if(led_idx < led_multi_intensity.size() && led_multi_intensity[led_idx].is_open()) + { + unsigned char red = RGBGetRValue(color); + unsigned char green = RGBGetGValue(color); + unsigned char blue = RGBGetBValue(color); + + unsigned char values[3] = { red, green, blue }; + std::string color_str = std::to_string(values[led_channel_order_red[led_idx]]) + " " + + std::to_string(values[led_channel_order_green[led_idx]]) + " " + + std::to_string(values[led_channel_order_blue[led_idx]]); + + led_multi_intensity[led_idx].write(color_str.c_str(), color_str.length()); + led_multi_intensity[led_idx].flush(); + } +} + +void RetroidAYNController::ReadChannelOrder(std::string led_path) +{ + /*-----------------------------------------------------*\ + | Default to red=0, green=1, blue=2 | + \*-----------------------------------------------------*/ + led_channel_order_red.push_back(0); + led_channel_order_green.push_back(1); + led_channel_order_blue.push_back(2); + + std::ifstream index_file(led_path + "multi_index"); + + if(!index_file.is_open()) + { + return; + } + + std::string index_str; + std::getline(index_file, index_str); + index_file.close(); + + /*-----------------------------------------------------*\ + | Parse the multi_index string (e.g. "red green blue") | + | Each token is a channel name, its position in the | + | string is the index in multi_intensity output | + \*-----------------------------------------------------*/ + std::istringstream iss(index_str); + std::string token; + int idx = 0; + + while(iss >> token) + { + if(token == "red") + { + led_channel_order_red.back() = idx; + } + else if(token == "green") + { + led_channel_order_green.back() = idx; + } + else if(token == "blue") + { + led_channel_order_blue.back() = idx; + } + idx++; + } +} diff --git a/Controllers/RetroidAYNController/RetroidAYNController_Linux.h b/Controllers/RetroidAYNController/RetroidAYNController_Linux.h new file mode 100644 index 00000000..aad700fc --- /dev/null +++ b/Controllers/RetroidAYNController/RetroidAYNController_Linux.h @@ -0,0 +1,50 @@ +/*---------------------------------------------------------*\ +| RetroidAYNController_Linux.h | +| | +| Driver for Retroid and AYN handheld RGB LEDs | +| | +| Adam Honse 08 Nov 2026 | +| | +| This file is part of the OpenRGB project | +| SPDX-License-Identifier: GPL-2.0-or-later | +\*---------------------------------------------------------*/ + +#pragma once + +#include +#include +#include +#include "RGBControllerInterface.h" + +class RetroidAYNController +{ +public: + RetroidAYNController(std::string dev_name); + ~RetroidAYNController(); + + std::string GetName(); + std::string GetLocation(); + + std::size_t GetLEDCount(); + + void AddLED(std::string led_path); + + unsigned int GetBrightness(unsigned int led_idx); + RGBColor GetLEDColor(unsigned int led_idx); + unsigned int GetMaxBrightness(unsigned int led_idx); + + void SetBrightness(unsigned int led_idx, unsigned int brightness); + void SetLEDColor(unsigned int led_idx, RGBColor color); + +private: + std::string name; + + std::vector led_paths; + std::vector led_multi_intensity; + std::vector led_brightness; + std::vector led_channel_order_red; + std::vector led_channel_order_green; + std::vector led_channel_order_blue; + + void ReadChannelOrder(std::string led_path); +};