diff options
| author | Luca Stefani <luca020400@lineageos.org> | 2017-03-13 14:42:45 +0100 |
|---|---|---|
| committer | Rygebin <kaankulahli2@gmail.com> | 2017-05-25 18:36:29 +0000 |
| commit | e7a37a900b6de2fea54acd7ac23d2feafff5a366 (patch) | |
| tree | 90bb92a3c5fa4a83b046da1b859ad3020b217fbe | |
| parent | 32dfa06664aa96b6f782f857516b27c101fbf845 (diff) | |
msm8916-common: lights: Improve code style
Change-Id: Ibeee1aa6e124d5a186dfc596780f59c41a069ceb
| -rw-r--r-- | liblight/lights.c | 125 |
1 files changed, 53 insertions, 72 deletions
diff --git a/liblight/lights.c b/liblight/lights.c index 2035e95..7f9a64a 100644 --- a/liblight/lights.c +++ b/liblight/lights.c @@ -41,20 +41,13 @@ static pthread_once_t g_init = PTHREAD_ONCE_INIT; static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER; static struct light_state_t g_lights[LIGHT_MAX]; -char const*const LCD_FILE - = "/sys/class/leds/lcd-backlight/brightness"; +#define LCD_BRIGHTNESS_FILE "/sys/class/leds/lcd-backlight/brightness" -char const*const GREEN_LED_FILE - = "/sys/class/leds/green/brightness"; +#define GREEN_LED_BRIGHTNESS_FILE "/sys/class/leds/green/brightness" +#define RED_LED_BRIGHTNESS_FILE "/sys/class/leds/red/brightness" -char const*const RED_LED_FILE - = "/sys/class/leds/red/brightness"; - -char const*const GREEN_PWM_FILE - = "/sys/class/leds/green/pwm_us"; - -char const*const RED_PWM_FILE - = "/sys/class/leds/red/pwm_us"; +#define GREEN_LED_PWM_FILE "/sys/class/leds/green/pwm_us" +#define RED_LED_PWM_FILE "/sys/class/leds/red/pwm_us" /** * device methods */ @@ -65,8 +58,7 @@ void init_globals(void) pthread_mutex_init(&g_lock, NULL); } -static int -write_int(char const* path, int value) +static int write_int(char const* path, int value) { int fd; static int already_warned = 0; @@ -80,15 +72,14 @@ write_int(char const* path, int value) return amt == -1 ? -errno : 0; } else { if (already_warned == 0) { - ALOGE("write_int failed to open %s\n", path); + ALOGE("%s: failed to open %s\n", __func__, path); already_warned = 1; } return -errno; } } -static int -write_str(char const* path, char *value) +static int write_str(char const* path, char *value) { int fd; static int already_warned = 0; @@ -102,57 +93,53 @@ write_str(char const* path, char *value) return amt == -1 ? -errno : 0; } else { if (already_warned == 0) { - ALOGE("write_str failed to open %s\n", path); + ALOGE("%s: failed to open %s\n", __func__, path); already_warned = 1; } return -errno; } } -static int -is_lit(struct light_state_t const* state) +static int is_lit(struct light_state_t const* state) { return state->color & 0x00ffffff; } -static int -rgb_to_brightness(struct light_state_t const* state) +static int rgb_to_brightness(struct light_state_t const* state) { int color = state->color & 0x00ffffff; - return ((77*((color>>16)&0x00ff)) - + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8; + return ((77 * ((color >> 16) & 0x00ff)) + + (150 * ((color >> 8) & 0x00ff)) + (29 * (color & 0x00ff))) >> 8; } -static int -set_light_backlight(__attribute__((unused)) struct light_device_t* dev, +static int set_light_backlight(__attribute__((unused)) struct light_device_t* dev, struct light_state_t const* state) { int err = 0; int brightness = rgb_to_brightness(state); pthread_mutex_lock(&g_lock); - err = write_int(LCD_FILE, brightness); + err = write_int(LCD_BRIGHTNESS_FILE, brightness); pthread_mutex_unlock(&g_lock); return err; } -static int -set_light_locked(struct light_state_t const* state) +static int set_light_locked(struct light_state_t const* state) { int onMS, offMS; int blink, fake_pwm, pwm; int brightness_level; switch (state->flashMode) { - case LIGHT_FLASH_TIMED: - case LIGHT_FLASH_HARDWARE: - onMS = state->flashOnMS; - offMS = state->flashOffMS; - break; - case LIGHT_FLASH_NONE: - default: - onMS = 0; - offMS = 0; - break; + case LIGHT_FLASH_TIMED: + case LIGHT_FLASH_HARDWARE: + onMS = state->flashOnMS; + offMS = state->flashOffMS; + break; + case LIGHT_FLASH_NONE: + default: + onMS = 0; + offMS = 0; + break; } if (onMS > 0 && offMS > 0) { @@ -176,41 +163,38 @@ set_light_locked(struct light_state_t const* state) if (is_lit(state)) brightness_level = (state->color & 0xff000000) ? - (state->color & 0xff000000) >> 24 : LED_LIGHT_ON; + (state->color & 0xff000000) >> 24 : LED_LIGHT_ON; else brightness_level = LED_LIGHT_OFF; // turn led off - write_int(GREEN_LED_FILE, LED_LIGHT_OFF); - write_int(RED_LED_FILE, LED_LIGHT_OFF); + write_int(GREEN_LED_BRIGHTNESS_FILE, LED_LIGHT_OFF); + write_int(RED_LED_BRIGHTNESS_FILE, LED_LIGHT_OFF); if (blink) { // brightness equals to led on in ms - write_int(GREEN_LED_FILE, fake_pwm); + write_int(GREEN_LED_BRIGHTNESS_FILE, fake_pwm); // pwn uquals to led off in us - write_int(GREEN_PWM_FILE, pwm); + write_int(GREEN_LED_PWM_FILE, pwm); } else { - write_int(GREEN_LED_FILE, brightness_level); - write_int(GREEN_PWM_FILE, 100); + write_int(GREEN_LED_BRIGHTNESS_FILE, brightness_level); + write_int(GREEN_LED_PWM_FILE, 100); } return 0; } -static int -handle_led_prioritized_locked(struct light_state_t const* state) +static int handle_led_prioritized_locked(struct light_state_t const* state) { - if (is_lit(&g_lights[ATTENTION])) { + if (is_lit(&g_lights[ATTENTION])) return set_light_locked(&g_lights[ATTENTION]); - } else if (is_lit(&g_lights[NOTIFICATION])) { + else if (is_lit(&g_lights[NOTIFICATION])) return set_light_locked(&g_lights[NOTIFICATION]); - } else { + else return set_light_locked(state); - } } -static int -set_light_notifications(__attribute__((unused)) struct light_device_t* dev, +static int set_light_notifications(__attribute__((unused)) struct light_device_t* dev, struct light_state_t const* state) { int err = 0; @@ -221,8 +205,7 @@ set_light_notifications(__attribute__((unused)) struct light_device_t* dev, return err; } -static int -set_light_attention(__attribute__((unused)) struct light_device_t* dev, +static int set_light_attention(__attribute__((unused)) struct light_device_t* dev, struct light_state_t const* state) { int err = 0; @@ -233,8 +216,7 @@ set_light_attention(__attribute__((unused)) struct light_device_t* dev, return err; } -static int -set_light_battery(__attribute__((unused)) struct light_device_t* dev, +static int set_light_battery(__attribute__((unused)) struct light_device_t* dev, struct light_state_t const* state) { int err = 0; @@ -254,21 +236,21 @@ set_light_battery(__attribute__((unused)) struct light_device_t* dev, level = 100; // turn led off - write_int(GREEN_LED_FILE, LED_LIGHT_OFF); - write_int(RED_LED_FILE, LED_LIGHT_OFF); + write_int(GREEN_LED_BRIGHTNESS_FILE, LED_LIGHT_OFF); + write_int(RED_LED_BRIGHTNESS_FILE, LED_LIGHT_OFF); if (is_lit(state)) { if (level <= 15) { - write_int(RED_LED_FILE, 255); - write_int(RED_PWM_FILE, 100); + write_int(RED_LED_BRIGHTNESS_FILE, 255); + write_int(RED_LED_PWM_FILE, 100); } else if (level <= 99) { - write_int(GREEN_LED_FILE, 255); - write_int(GREEN_PWM_FILE, 100); - write_int(RED_LED_FILE, 255); - write_int(RED_PWM_FILE, 100); + write_int(GREEN_LED_BRIGHTNESS_FILE, 255); + write_int(GREEN_LED_PWM_FILE, 100); + write_int(RED_LED_BRIGHTNESS_FILE, 255); + write_int(RED_LED_PWM_FILE, 100); } else { - write_int(GREEN_LED_FILE, 255); - write_int(GREEN_PWM_FILE, 100); + write_int(GREEN_LED_BRIGHTNESS_FILE, 255); + write_int(GREEN_LED_PWM_FILE, 100); } } @@ -278,12 +260,11 @@ set_light_battery(__attribute__((unused)) struct light_device_t* dev, /** Close the lights device */ -static int -close_lights(struct light_device_t *dev) +static int close_lights(struct light_device_t *dev) { - if (dev) { + if (dev) free(dev); - } + return 0; } |
