sdm845-common: Improve LED ramp generation algorithm.

Distribute ramp duration equally to both on and off time, use more
ramp steps (16 instead of 8) and lower green brightness to better match
brightness of red and blue (green LED is much brighter than the other
two).

Change-Id: I48d94b4020515a5cbb15486f8d44dd151b475ab0
This commit is contained in:
Danny Baumann 2018-07-08 14:57:35 +02:00
parent c162e21253
commit 646a48d17f
2 changed files with 23 additions and 17 deletions

View file

@ -24,8 +24,6 @@
#include <android-base/stringprintf.h> #include <android-base/stringprintf.h>
#include <fstream> #include <fstream>
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
namespace android { namespace android {
namespace hardware { namespace hardware {
namespace light { namespace light {
@ -89,26 +87,29 @@ void Light::handleRgb(const LightState& state, size_t index) {
std::map<std::string, int> colorValues; std::map<std::string, int> colorValues;
colorValues["red"] = (stateToUse.color >> 16) & 0xff; colorValues["red"] = (stateToUse.color >> 16) & 0xff;
colorValues["green"] = (stateToUse.color >> 8) & 0xff; // lower green brightness to adjust for the (lower) brightness of red and blue
colorValues["green"] = ((stateToUse.color >> 8) & 0xff) / 2;
colorValues["blue"] = stateToUse.color & 0xff; colorValues["blue"] = stateToUse.color & 0xff;
int onMs = stateToUse.flashMode == Flash::TIMED ? stateToUse.flashOnMs : 0; int onMs = stateToUse.flashMode == Flash::TIMED ? stateToUse.flashOnMs : 0;
int offMs = stateToUse.flashMode == Flash::TIMED ? stateToUse.flashOffMs : 0; int offMs = stateToUse.flashMode == Flash::TIMED ? stateToUse.flashOffMs : 0;
static constexpr int kRampSize = 8; // LUT has 63 entries, we could theoretically use them as 3 (colors) * 21 (steps).
static constexpr int kBrightnessRamp[] = { 0, 12, 25, 37, 50, 72, 85, 100 }; // However, the last LUT entries don't seem to behave correctly for unknown
static constexpr int kRampStepDuration = 50; // reasons, so we use 17 steps for a total of 51 LUT entries only.
static constexpr int kRampSteps = 16;
static constexpr int kRampMaxStepDurationMs = 15;
auto makeLedPath = [](const std::string& led, const std::string& op) -> std::string { auto makeLedPath = [](const std::string& led, const std::string& op) -> std::string {
return "/sys/class/leds/" + led + "/" + op; return "/sys/class/leds/" + led + "/" + op;
}; };
auto getScaledDutyPercent = [](int brightness) -> std::string { auto getScaledDutyPercent = [](int brightness) -> std::string {
std::string output; std::string output;
for (size_t i = 0; i < ARRAY_SIZE(kBrightnessRamp); i++) { for (int i = 0; i <= kRampSteps; i++) {
if (i != 0) { if (i != 0) {
output += ","; output += ",";
} }
output += std::to_string(kBrightnessRamp[i] * brightness / 255); output += std::to_string(i * 512 * brightness / (255 * kRampSteps));
} }
return output; return output;
}; };
@ -119,23 +120,25 @@ void Light::handleRgb(const LightState& state, size_t index) {
} }
if (onMs > 0 && offMs > 0) { if (onMs > 0 && offMs > 0) {
int pauseHi, stepDuration; int pauseLo, pauseHi, stepDuration, index = 0;
if (kRampStepDuration * kRampSize * 2 > onMs) { if (kRampMaxStepDurationMs * kRampSteps > onMs) {
stepDuration = onMs / 2 * kRampSize; stepDuration = onMs / kRampSteps;
pauseHi = 0; pauseHi = 0;
pauseLo = offMs;
} else { } else {
stepDuration = kRampStepDuration; stepDuration = kRampMaxStepDurationMs;
pauseHi = onMs - 2 * kRampSize * stepDuration; pauseHi = onMs - kRampSteps * stepDuration;
pauseLo = offMs - kRampSteps * stepDuration;
} }
for (const auto& entry : colorValues) { for (const auto& entry : colorValues) {
set(makeLedPath(entry.first, "start_idx"), 0); set(makeLedPath(entry.first, "lut_flags"), 95);
set(makeLedPath(entry.first, "start_idx"), index);
set(makeLedPath(entry.first, "duty_pcts"), getScaledDutyPercent(entry.second)); set(makeLedPath(entry.first, "duty_pcts"), getScaledDutyPercent(entry.second));
set(makeLedPath(entry.first, "pause_lo"), offMs); set(makeLedPath(entry.first, "pause_lo"), pauseLo);
// The led driver is configured to ramp up then ramp
// down the lut. This effectively doubles the ramp duration.
set(makeLedPath(entry.first, "pause_hi"), pauseHi); set(makeLedPath(entry.first, "pause_hi"), pauseHi);
set(makeLedPath(entry.first, "ramp_step_ms"), stepDuration); set(makeLedPath(entry.first, "ramp_step_ms"), stepDuration);
index += kRampSteps + 1;
} }
// Start blinking // Start blinking

View file

@ -2,6 +2,9 @@ on boot
chown system system /sys/class/leds/red/start_idx chown system system /sys/class/leds/red/start_idx
chown system system /sys/class/leds/green/start_idx chown system system /sys/class/leds/green/start_idx
chown system system /sys/class/leds/blue/start_idx chown system system /sys/class/leds/blue/start_idx
chown system system /sys/class/leds/red/lut_flags
chown system system /sys/class/leds/green/lut_flags
chown system system /sys/class/leds/blue/lut_flags
service light-hal-2-0 /system/bin/hw/android.hardware.light@2.0-service.oneplus_sdm845 service light-hal-2-0 /system/bin/hw/android.hardware.light@2.0-service.oneplus_sdm845
class hal class hal