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

@ -24,34 +24,30 @@
#define LEDS "/sys/class/leds/"
#define BUTTON1_LED LEDS "button-backlight1/"
#define BUTTON_LED LEDS "button-backlight/"
#define LCD_LED LEDS "lcd-backlight/"
#define WHITE_LED LEDS "white/"
#define WHITE_LED LEDS "red/"
#define BLINK "blink"
#define BRIGHTNESS "brightness"
#define MAX_BRIGHTNESS "max_brightness"
#define DUTY_PCTS "duty_pcts"
#define PAUSE_HI "pause_hi"
#define PAUSE_LO "pause_lo"
#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.
*/
#define RAMP_STEPS 8
#define RAMP_STEPS 15
/*
* 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.
*/
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 {
/*
@ -72,6 +68,25 @@ static void set(std::string path, int 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) {
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) {
uint32_t brightness = getScaledBrightness(state, MAX_LCD_BRIGHTNESS);
uint32_t brightness = getScaledBrightness(state, getMaxBrightness(LCD_LED MAX_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
* brightness of the color.
@ -130,7 +139,7 @@ static std::string getScaledRamp(uint32_t brightness) {
}
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 */
set(WHITE_LED BLINK, 0);
@ -146,7 +155,7 @@ static void setNotification(const LightState& state) {
int32_t pauseLo = state.flashOffMs;
if (pauseHi < 0) {
stepDuration = state.flashOnMs / (RAMP_STEPS * 2);
//stepDuration = state.flashOnMs / (RAMP_STEPS * 2);
pauseHi = 0;
}
@ -179,33 +188,28 @@ static std::vector<std::pair<Type, LightState>> notificationStates = {
};
static void handleNotification(Type type, const LightState& state) {
bool handled = false;
for (auto it : notificationStates) {
for(auto it : notificationStates) {
if (it.first == type) {
it.second = state;
}
if (!handled && isLit(it.second)) {
if (isLit(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::NOTIFICATIONS, handleNotification },
{ Type::BATTERY, handleNotification },
{ Type::BACKLIGHT, handleBacklight },
{ Type::BUTTONS, handleButtons },
};
} // anonymous namespace
} // anonymous namespace
namespace android {
namespace hardware {