From 7f0af2eccfd94f0ed1700bf296f7d6d401e3496e Mon Sep 17 00:00:00 2001 From: jhenrique09 Date: Sat, 18 Nov 2017 11:33:50 -0200 Subject: [PATCH] sanders: liblight: Fix blinking support, and cleanup code Signed-off-by: ronaxdevil --- liblight/lights.c | 65 ++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 34 deletions(-) diff --git a/liblight/lights.c b/liblight/lights.c index df32aa5..31e26da 100644 --- a/liblight/lights.c +++ b/liblight/lights.c @@ -32,16 +32,15 @@ /******************************************************************************/ -#define LED_LIGHT_OFF 0 -#define LED_LIGHT_ON 10 +#define LED_LIGHT_OFF 0 +#define LED_LIGHT_BLINK_FAST 1 +#define LED_LIGHT_BLINK_SLOW 2 +#define LED_LIGHT_SOLID_ON 3 static pthread_once_t g_init = PTHREAD_ONCE_INIT; static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER; -static struct light_state_t g_battery; -static struct light_state_t g_notification; - -char const*const CHARGING_LED_FILE +char const*const LED_FILE = "/sys/class/leds/charging/brightness"; char const*const LCD_FILE @@ -105,38 +104,25 @@ set_light_backlight(struct light_device_t* dev, return err; } -static int -set_speaker_light_locked(struct light_device_t* dev, - struct light_state_t const* state) -{ - int brightness_level; - - if (is_lit(state)) - brightness_level = LED_LIGHT_ON; - else - brightness_level = LED_LIGHT_OFF; - - return write_int(CHARGING_LED_FILE, brightness_level); -} - -static void -handle_speaker_battery_locked(struct light_device_t* dev) -{ - if (is_lit(&g_notification)) { - set_speaker_light_locked(dev, &g_notification); - } else { - set_speaker_light_locked(dev, &g_battery); - } -} - static int set_light_battery(struct light_device_t* dev, struct light_state_t const* state) { + int brightness_level; int err = 0; + + if (!dev) + return -1; + pthread_mutex_lock(&g_lock); - g_battery = *state; - handle_speaker_battery_locked(dev); + + if (is_lit(state)) + brightness_level = LED_LIGHT_SOLID_ON; + else + brightness_level = LED_LIGHT_OFF; + + err = write_int(LED_FILE, brightness_level); + pthread_mutex_unlock(&g_lock); return err; } @@ -145,10 +131,21 @@ static int set_light_notifications(struct light_device_t* dev, struct light_state_t const* state) { + int brightness_level; int err = 0; + + if (!dev) + return -1; + pthread_mutex_lock(&g_lock); - g_notification = *state; - handle_speaker_battery_locked(dev); + + if (is_lit(state)) + brightness_level = LED_LIGHT_BLINK_FAST; + else + brightness_level = LED_LIGHT_OFF; + + err = write_int(LED_FILE, brightness_level); + pthread_mutex_unlock(&g_lock); return err; }