From de082b2b25d9b5a4427ee95a40bcc7fb0c3994f1 Mon Sep 17 00:00:00 2001 From: LuK1337 Date: Sat, 22 Jun 2024 16:14:02 +0200 Subject: [PATCH] aidl: light: Add RGB sync support Change-Id: I3b26dce753487c2e0a2f191a57ca7ceb4bd752e4 --- aidl/light/Devices.cpp | 8 +++---- aidl/light/LedDevice.cpp | 12 +++++++--- aidl/light/LedDevice.h | 8 +++++++ aidl/light/RgbLedDevice.cpp | 23 +++++++++++++++++-- aidl/light/RgbLedDevice.h | 11 ++++++++- .../android.hardware.light-service.xiaomi.rc | 2 ++ 6 files changed, 54 insertions(+), 10 deletions(-) diff --git a/aidl/light/Devices.cpp b/aidl/light/Devices.cpp index 150f590..6c0a25a 100644 --- a/aidl/light/Devices.cpp +++ b/aidl/light/Devices.cpp @@ -72,8 +72,8 @@ static std::vector getButtonLedDevices() { return devices; } -static const std::string kRgbLedDevices[][3] = { - {"red", "green", "blue"}, +static const std::string kRgbLedDevices[][4] = { + {"red", "green", "blue", "/sys/class/leds/rgb/rgb_blink"}, }; static std::vector getNotificationRgbLedDevices() { @@ -84,11 +84,11 @@ static std::vector getNotificationRgbLedDevices() { LedDevice green(device[1]); LedDevice blue(device[2]); - RgbLedDevice rgbLedDevice(red, green, blue); + RgbLedDevice rgbLedDevice(red, green, blue, device[3]); if (rgbLedDevice.exists()) { LOG(INFO) << "Found notification RGB LED device: " << red.getName() << ", " << green.getName() << ", " << blue.getName(); - devices.emplace_back(red, green, blue); + devices.emplace_back(red, green, blue, device[3]); } } diff --git a/aidl/light/LedDevice.cpp b/aidl/light/LedDevice.cpp index b85008d..a822432 100644 --- a/aidl/light/LedDevice.cpp +++ b/aidl/light/LedDevice.cpp @@ -39,7 +39,8 @@ static const std::string kRampStepMsNode = "ramp_step_ms"; static constexpr int kRampSteps = 8; static constexpr int kRampMaxStepDurationMs = 50; -LedDevice::LedDevice(std::string name) : mName(name), mBasePath(kBaseLedsPath + name + "/") { +LedDevice::LedDevice(std::string name) + : mName(name), mIdx(0), mBasePath(kBaseLedsPath + name + "/") { if (!readFromFile(mBasePath + kMaxBrightnessNode, mMaxBrightness)) { mMaxBrightness = kDefaultMaxBrightness; } @@ -77,7 +78,7 @@ bool LedDevice::exists() const { static std::string getScaledDutyPercent(uint8_t brightness) { std::string output; - for (int i = 0; i <= kRampSteps; i++) { + for (int i = 0; i < kRampSteps; i++) { if (i != 0) { output += ","; } @@ -108,7 +109,7 @@ bool LedDevice::setBrightness(uint8_t value, LightMode mode, uint32_t flashOnMs, pauseHi = 0; } - return writeToFile(mBasePath + kStartIdxNode, 0) && + return writeToFile(mBasePath + kStartIdxNode, mIdx * kRampSteps) && writeToFile(mBasePath + kDutyPctsNode, getScaledDutyPercent(value)) && writeToFile(mBasePath + kPauseLoNode, pauseLo) && writeToFile(mBasePath + kPauseHiNode, pauseHi) && @@ -136,8 +137,13 @@ bool LedDevice::setBrightness(uint8_t value, LightMode mode, uint32_t flashOnMs, } } +void LedDevice::setIdx(int idx) { + mIdx = idx; +} + void LedDevice::dump(int fd) const { dprintf(fd, "Name: %s", mName.c_str()); + dprintf(fd, ", index: %d", mIdx); dprintf(fd, ", exists: %d", exists()); dprintf(fd, ", base path: %s", mBasePath.c_str()); dprintf(fd, ", max brightness: %u", mMaxBrightness); diff --git a/aidl/light/LedDevice.h b/aidl/light/LedDevice.h index 8c77723..167ff9b 100644 --- a/aidl/light/LedDevice.h +++ b/aidl/light/LedDevice.h @@ -78,10 +78,18 @@ class LedDevice : public IDumpable { bool setBrightness(uint8_t value, LightMode mode = LightMode::STATIC, uint32_t flashOnMs = 0, uint32_t flashOffMs = 0); + /** + * Set the index of the LED device. + * + * @param idx The index to set + */ + void setIdx(int idx); + void dump(int fd) const override; private: std::string mName; + int mIdx; std::string mBasePath; uint32_t mMaxBrightness; std::string mBreathNode; diff --git a/aidl/light/RgbLedDevice.cpp b/aidl/light/RgbLedDevice.cpp index 9905a74..7c1230d 100644 --- a/aidl/light/RgbLedDevice.cpp +++ b/aidl/light/RgbLedDevice.cpp @@ -9,14 +9,15 @@ #define LOG_TAG "RgbLedDevice" #include +#include "Utils.h" namespace aidl { namespace android { namespace hardware { namespace light { -RgbLedDevice::RgbLedDevice(LedDevice red, LedDevice green, LedDevice blue) - : mRed(red), mGreen(green), mBlue(blue), mColors(Color::NONE) { +RgbLedDevice::RgbLedDevice(LedDevice red, LedDevice green, LedDevice blue, std::string rgbSyncNode) + : mRed(red), mGreen(green), mBlue(blue), mRgbSyncNode(rgbSyncNode), mColors(Color::NONE) { if (mRed.exists()) { mColors |= Color::RED; } @@ -26,6 +27,11 @@ RgbLedDevice::RgbLedDevice(LedDevice red, LedDevice green, LedDevice blue) if (mBlue.exists()) { mColors |= Color::BLUE; } + if (supportsRgbSync()) { + mRed.setIdx(0); + mGreen.setIdx(1); + mBlue.setIdx(2); + } } bool RgbLedDevice::exists() const { @@ -44,6 +50,10 @@ bool RgbLedDevice::supportsTimed() const { (!mBlue.exists() || mBlue.supportsTimed()); } +bool RgbLedDevice::supportsRgbSync() const { + return std::ifstream(mRgbSyncNode).good(); +} + bool RgbLedDevice::setBrightness(rgb color, LightMode mode, uint32_t flashOnMs, uint32_t flashOffMs) { bool rc = true; @@ -63,6 +73,10 @@ bool RgbLedDevice::setBrightness(rgb color, LightMode mode, uint32_t flashOnMs, mode = LightMode::STATIC; } + if (mode == LightMode::TIMED && supportsRgbSync()) { + rc &= writeToFile(mRgbSyncNode, 0); + } + if (mColors == Color::ALL) { rc &= mRed.setBrightness(color.red, mode, flashOnMs, flashOffMs); rc &= mGreen.setBrightness(color.green, mode, flashOnMs, flashOffMs); @@ -95,6 +109,10 @@ bool RgbLedDevice::setBrightness(rgb color, LightMode mode, uint32_t flashOnMs, } } + if (mode == LightMode::TIMED && supportsRgbSync()) { + rc &= writeToFile(mRgbSyncNode, 1); + } + return rc; } @@ -102,6 +120,7 @@ void RgbLedDevice::dump(int fd) const { dprintf(fd, "Exists: %d", exists()); dprintf(fd, ", supports breath: %d", supportsBreath()); dprintf(fd, ", supports timed: %d", supportsTimed()); + dprintf(fd, ", supports RGB sync: %d", supportsRgbSync()); dprintf(fd, ", colors:"); if (mColors != Color::NONE) { if (mColors & Color::RED) { diff --git a/aidl/light/RgbLedDevice.h b/aidl/light/RgbLedDevice.h index 71a784f..2523eed 100644 --- a/aidl/light/RgbLedDevice.h +++ b/aidl/light/RgbLedDevice.h @@ -30,8 +30,9 @@ class RgbLedDevice : public IDumpable { * @param red The red LED device * @param green The green LED device * @param blue The blue LED device + * @param rgbSyncNode The path to RGB sync trigger */ - RgbLedDevice(LedDevice red, LedDevice green, LedDevice blue); + RgbLedDevice(LedDevice red, LedDevice green, LedDevice blue, std::string rgbSyncNode); /** * Return whether this RGB LED device exists. @@ -61,6 +62,13 @@ class RgbLedDevice : public IDumpable { */ bool supportsTimed() const; + /** + * Return whether this RGB LED device supports RGB sync. + * + * @return bool true if the RGB LED device supports RGB sync, false otherwise + */ + bool supportsRgbSync() const; + /** * Set the brightness of this RGB LED device. * @@ -85,6 +93,7 @@ class RgbLedDevice : public IDumpable { LedDevice mRed; LedDevice mGreen; LedDevice mBlue; + std::string mRgbSyncNode; int mColors; }; diff --git a/aidl/light/android.hardware.light-service.xiaomi.rc b/aidl/light/android.hardware.light-service.xiaomi.rc index c5558a4..9aff8a4 100644 --- a/aidl/light/android.hardware.light-service.xiaomi.rc +++ b/aidl/light/android.hardware.light-service.xiaomi.rc @@ -87,6 +87,8 @@ on early-boot chown system system /sys/class/leds/red/ramp_step_ms chown system system /sys/class/leds/red/start_idx + chown system system /sys/class/leds/rgb/rgb_blink + chown system system /sys/class/leds/white/blink chown system system /sys/class/leds/white/breath chown system system /sys/class/leds/white/brightness