lights: We have only single color led.

Also add additional steps to duty cycle to make notif light fade
smoother.
Also detect maximum brightness dynamically
This commit is contained in:
Vasishath Kaushal 2018-06-06 21:35:05 +05:30
parent b9eb4bc24c
commit abcab1e2cd
5 changed files with 51 additions and 50 deletions

View file

@ -1,3 +1,4 @@
//
// Copyright (C) 2018 The LineageOS Project // Copyright (C) 2018 The LineageOS Project
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");

View file

@ -24,34 +24,30 @@
#define LEDS "/sys/class/leds/" #define LEDS "/sys/class/leds/"
#define BUTTON1_LED LEDS "button-backlight1/"
#define BUTTON_LED LEDS "button-backlight/"
#define LCD_LED LEDS "lcd-backlight/" #define LCD_LED LEDS "lcd-backlight/"
#define WHITE_LED LEDS "white/" #define WHITE_LED LEDS "red/"
#define BLINK "blink" #define BLINK "blink"
#define BRIGHTNESS "brightness" #define BRIGHTNESS "brightness"
#define MAX_BRIGHTNESS "max_brightness"
#define DUTY_PCTS "duty_pcts" #define DUTY_PCTS "duty_pcts"
#define PAUSE_HI "pause_hi" #define PAUSE_HI "pause_hi"
#define PAUSE_LO "pause_lo" #define PAUSE_LO "pause_lo"
#define RAMP_STEP_MS "ramp_step_ms" #define RAMP_STEP_MS "ramp_step_ms"
#define START_IDX "start_idx" #define START_IDX "start_idx"
#define MAX_LED_BRIGHTNESS 255
#define MAX_LCD_BRIGHTNESS 4095
/* /*
* 8 duty percent steps. * 8 duty percent steps.
*/ */
#define RAMP_STEPS 8 #define RAMP_STEPS 15
/* /*
* Each step will stay on for 50ms by default. * Each step will stay on for 50ms by default.
*/ */
#define RAMP_STEP_DURATION 50 #define RAMP_STEP_DURATION 150
/* /*
* Each value represents a duty percent (0 - 100) for the led pwm. * Each value represents a duty percent (0 - 100) for the led pwm.
*/ */
static int32_t BRIGHTNESS_RAMP[RAMP_STEPS] = {0, 12, 25, 37, 50, 72, 85, 100}; static int32_t BRIGHTNESS_RAMP[RAMP_STEPS] = {0, 12, 25, 37, 50, 72, 85, 100, 85, 72, 50, 37, 25, 12, 0};
namespace { namespace {
/* /*
@ -72,6 +68,25 @@ static void set(std::string path, int value) {
set(path, std::to_string(value)); set(path, std::to_string(value));
} }
static int get(std::string path) {
std::ifstream file(path);
int value;
if (!file.is_open()) {
ALOGW("failed to read from %s", path.c_str());
return 0;
}
file >> value;
return value;
}
static int getMaxBrightness(std::string path) {
int value = get(path);
ALOGW("Got max brightness %d", value);
return value;
}
static uint32_t getBrightness(const LightState& state) { static uint32_t getBrightness(const LightState& state) {
uint32_t alpha, red, green, blue; uint32_t alpha, red, green, blue;
@ -104,16 +119,10 @@ static inline uint32_t getScaledBrightness(const LightState& state, uint32_t max
} }
static void handleBacklight(Type /* type */, const LightState& state) { static void handleBacklight(Type /* type */, const LightState& state) {
uint32_t brightness = getScaledBrightness(state, MAX_LCD_BRIGHTNESS); uint32_t brightness = getScaledBrightness(state, getMaxBrightness(LCD_LED MAX_BRIGHTNESS));
set(LCD_LED BRIGHTNESS, brightness); set(LCD_LED BRIGHTNESS, brightness);
} }
static void handleButtons(Type /* type */, const LightState& state) {
uint32_t brightness = getScaledBrightness(state, MAX_LED_BRIGHTNESS);
set(BUTTON_LED BRIGHTNESS, brightness);
set(BUTTON1_LED BRIGHTNESS, brightness);
}
/* /*
* Scale each value of the brightness ramp according to the * Scale each value of the brightness ramp according to the
* brightness of the color. * brightness of the color.
@ -130,7 +139,7 @@ static std::string getScaledRamp(uint32_t brightness) {
} }
static void setNotification(const LightState& state) { static void setNotification(const LightState& state) {
uint32_t whiteBrightness = getScaledBrightness(state, MAX_LED_BRIGHTNESS); uint32_t whiteBrightness = getScaledBrightness(state, getMaxBrightness(WHITE_LED MAX_BRIGHTNESS));
/* Disable blinking */ /* Disable blinking */
set(WHITE_LED BLINK, 0); set(WHITE_LED BLINK, 0);
@ -146,7 +155,7 @@ static void setNotification(const LightState& state) {
int32_t pauseLo = state.flashOffMs; int32_t pauseLo = state.flashOffMs;
if (pauseHi < 0) { if (pauseHi < 0) {
stepDuration = state.flashOnMs / (RAMP_STEPS * 2); //stepDuration = state.flashOnMs / (RAMP_STEPS * 2);
pauseHi = 0; pauseHi = 0;
} }
@ -179,30 +188,25 @@ static std::vector<std::pair<Type, LightState>> notificationStates = {
}; };
static void handleNotification(Type type, const LightState& state) { static void handleNotification(Type type, const LightState& state) {
bool handled = false;
for(auto it : notificationStates) { for(auto it : notificationStates) {
if (it.first == type) { if (it.first == type) {
it.second = state; it.second = state;
} }
if (!handled && isLit(it.second)) { if (isLit(it.second)) {
setNotification(it.second); setNotification(it.second);
handled = true; return;
} }
} }
if (!handled) {
setNotification(offState); setNotification(offState);
} }
}
static std::map<Type, std::function<void(Type, const LightState&)>> lights = { static std::map<Type, std::function<void(Type type, const LightState&)>> lights = {
{ Type::ATTENTION, handleNotification }, { Type::ATTENTION, handleNotification },
{ Type::NOTIFICATIONS, handleNotification }, { Type::NOTIFICATIONS, handleNotification },
{ Type::BATTERY, handleNotification }, { Type::BATTERY, handleNotification },
{ Type::BACKLIGHT, handleBacklight }, { Type::BACKLIGHT, handleBacklight },
{ Type::BUTTONS, handleButtons },
}; };
} // anonymous namespace } // anonymous namespace

View file

@ -21,11 +21,12 @@
#include <hardware/lights.h> #include <hardware/lights.h>
#include <hidl/Status.h> #include <hidl/Status.h>
#include <map> #include <map>
#include <mutex>
#include <vector> #include <vector>
#include <mutex>
using ::android::hardware::Return; using ::android::hardware::Return;
using ::android::hardware::Void; using ::android::hardware::Void;
using ::android::hardware::hidl_vec;
using ::android::hardware::light::V2_0::ILight; using ::android::hardware::light::V2_0::ILight;
using ::android::hardware::light::V2_0::Flash; using ::android::hardware::light::V2_0::Flash;
using ::android::hardware::light::V2_0::LightState; using ::android::hardware::light::V2_0::LightState;

View file

@ -1,23 +1,18 @@
on boot on boot
# White light chown system system /sys/class/leds/red/brightness
chown system system /sys/class/leds/white/brightness chown system system /sys/class/leds/red/blink
chown system system /sys/class/leds/red/duty_pcts
chown system system /sys/class/leds/red/pause_hi
chown system system /sys/class/leds/red/pause_lo
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/white/blink chmod 660 /sys/class/leds/red/blink
chown system system /sys/class/leds/white/duty_pcts chmod 660 /sys/class/leds/red/duty_pcts
chown system system /sys/class/leds/white/pause_hi chmod 660 /sys/class/leds/red/pause_hi
chown system system /sys/class/leds/white/pause_lo chmod 660 /sys/class/leds/red/pause_lo
chown system system /sys/class/leds/white/ramp_step_ms chmod 660 /sys/class/leds/red/ramp_step_ms
chown system system /sys/class/leds/white/start_idx chmod 660 /sys/class/leds/red/start_idx
chmod 660 /sys/class/leds/white/blink
chmod 660 /sys/class/leds/white/duty_pcts
chmod 660 /sys/class/leds/white/pause_hi
chmod 660 /sys/class/leds/white/pause_lo
chmod 660 /sys/class/leds/white/ramp_step_ms
chmod 660 /sys/class/leds/white/start_idx
# Button backlight
chown system system /sys/class/leds/button-backlight1/brightness
service light-hal-2-0 /vendor/bin/hw/android.hardware.light@2.0-service.xiaomi_sdm660 service light-hal-2-0 /vendor/bin/hw/android.hardware.light@2.0-service.xiaomi_sdm660
class hal class hal

View file

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
#define LOG_TAG "android.hardware.light@2.0-service.xiaomi_msm8998" #define LOG_TAG "android.hardware.light@2.0-service.xiaomi_sdm660"
#include <hidl/HidlTransportSupport.h> #include <hidl/HidlTransportSupport.h>
@ -31,7 +31,7 @@ using android::sp;
using android::status_t; using android::status_t;
int main() { int main() {
sp<ILight> service = new Light(); android::sp<ILight> service = new Light();
configureRpcThreadpool(1, true); configureRpcThreadpool(1, true);