diff options
author | Demon Singur <demonsingur@gmail.com> | 2020-01-08 22:35:46 +0100 |
---|---|---|
committer | Michael Bestas <mkbestas@lineageos.org> | 2020-04-30 00:48:54 +0300 |
commit | 21b901633610c76ddbf8489b0d9569833a27e219 (patch) | |
tree | 6dc48aab82c889332cfa612490fbcdf582cf96dd /light | |
parent | b6ca13ac464cdbd6f9eb593bc3f99a09d0069985 (diff) |
sdm660-common: light: Handle brightness in a less hacky way
Change-Id: I82e936742195c8248b03cea0aac44f671d10e245
Signed-off-by: Davide Garberi <dade.garberi@gmail.com>
Diffstat (limited to 'light')
-rw-r--r-- | light/Light.cpp | 53 |
1 files changed, 36 insertions, 17 deletions
diff --git a/light/Light.cpp b/light/Light.cpp index e42588d..d0ba5d6 100644 --- a/light/Light.cpp +++ b/light/Light.cpp @@ -39,6 +39,9 @@ namespace implementation { #define RAMP_STEP_MS "ramp_step_ms" #define START_IDX "start_idx" +#define MAX_LED_BRIGHTNESS 255 +#define MAX_LCD_BRIGHTNESS 4095 + /* * 8 duty percent steps. */ @@ -71,6 +74,29 @@ static void set(std::string path, int value) { set(path, std::to_string(value)); } +static uint32_t getBrightness(const LightState& state) { + uint32_t alpha, red, green, blue; + + /* + * Extract brightness from AARRGGBB. + */ + alpha = (state.color >> 24) & 0xFF; + red = (state.color >> 16) & 0xFF; + green = (state.color >> 8) & 0xFF; + blue = state.color & 0xFF; + + /* + * Scale RGB brightness if Alpha brightness is not 0xFF. + */ + if (alpha != 0xFF) { + red = red * alpha / 0xFF; + green = green * alpha / 0xFF; + blue = blue * alpha / 0xFF; + } + + return (77 * red + 150 * green + 29 * blue) >> 8; +} + /* * Scale each value of the brightness ramp according to the * brightness of the color. @@ -86,30 +112,23 @@ static std::string getScaledRamp(uint32_t brightness) { return ramp; } +static inline uint32_t scaleBrightness(uint32_t brightness, uint32_t maxBrightness) { + return brightness * maxBrightness / 0xFF; +} + +static inline uint32_t getScaledBrightness(const LightState& state, uint32_t maxBrightness) { + return scaleBrightness(getBrightness(state), maxBrightness); +} + static void handleXiaomiBacklight(Type /*type*/, const LightState& state) { - uint32_t brightness = state.color & 0xFF; - brightness *= 16; // HACK + uint32_t brightness = getScaledBrightness(state, MAX_LCD_BRIGHTNESS); set(LCD_LED BRIGHTNESS, brightness); } static void setNotification(const LightState& state) { - uint32_t whiteBrightness, brightness; - - /* - * Extract brightness from RGB. - */ - whiteBrightness = (state.color >> 16) & 0xFF; - brightness = (state.color >> 24) & 0xFF; - - /* - * Scale RGB brightness if the Alpha brightness is not 0xFF. - */ - if (brightness != 0xFF) { - whiteBrightness = (whiteBrightness * brightness) / 0xFF; - } + uint32_t whiteBrightness = getScaledBrightness(state, MAX_LED_BRIGHTNESS); /* Turn off the leds (initially) */ - set(WHITE BRIGHTNESS, 0); set(WHITE BLINK, 0); if (state.flashMode == Flash::TIMED) { |