aidl: light: Add RGB sync support

Change-Id: I3b26dce753487c2e0a2f191a57ca7ceb4bd752e4
This commit is contained in:
LuK1337 2024-06-22 16:14:02 +02:00 committed by Sebastiano Barezzi
parent 842e692081
commit de082b2b25
No known key found for this signature in database
GPG Key ID: 763BD3AE91A7A13F
6 changed files with 54 additions and 10 deletions

View File

@ -72,8 +72,8 @@ static std::vector<LedDevice> 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<RgbLedDevice> getNotificationRgbLedDevices() {
@ -84,11 +84,11 @@ static std::vector<RgbLedDevice> 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]);
}
}

View File

@ -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);

View File

@ -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;

View File

@ -9,14 +9,15 @@
#define LOG_TAG "RgbLedDevice"
#include <android-base/logging.h>
#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) {

View File

@ -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;
};

View File

@ -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