From dfd8f7142fd290bfd1160c5b5cea755ddc32b70a Mon Sep 17 00:00:00 2001 From: Paul Kocialkowski Date: Tue, 22 Apr 2014 14:18:06 +0200 Subject: lights: Cleanup implementation Signed-off-by: Paul Kocialkowski --- lights/Android.mk | 2 +- lights/gta04_lights.c | 292 ++++++++++++++++++++++++++++++++++++++++++++++++++ lights/lights.c | 276 ----------------------------------------------- 3 files changed, 293 insertions(+), 277 deletions(-) create mode 100644 lights/gta04_lights.c delete mode 100644 lights/lights.c diff --git a/lights/Android.mk b/lights/Android.mk index 9dc66f6..ac90572 100644 --- a/lights/Android.mk +++ b/lights/Android.mk @@ -15,7 +15,7 @@ LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) -LOCAL_SRC_FILES := lights.c +LOCAL_SRC_FILES := gta04_lights.c LOCAL_SHARED_LIBRARIES := liblog diff --git a/lights/gta04_lights.c b/lights/gta04_lights.c new file mode 100644 index 0000000..b0f4194 --- /dev/null +++ b/lights/gta04_lights.c @@ -0,0 +1,292 @@ +/* + * Copyright (C) 2012-2014 Paul Kocialkowski + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define LOG_TAG "gta04_lights" +#include + +#include + +/* + * Globals + */ + +const char backlight_brightness[] = + "/sys/class/backlight/pwm-backlight/brightness"; +const char backlight_max_brightness[] = + "/sys/class/backlight/pwm-backlight/max_brightness"; + +const char battery_red_brightness[] = + "/sys/class/leds/gta04:red:power/brightness"; +const char battery_red_max_brightness[] = + "/sys/class/leds/gta04:red:power/max_brightness"; +const char battery_green_brightness[] = + "/sys/class/leds/gta04:green:power/brightness"; +const char battery_green_max_brightness[] = + "/sys/class/leds/gta04:green:power/max_brightness"; + +const char notifications_red_brightness[] = + "/sys/class/leds/gta04:red:aux/brightness"; +const char notifications_red_max_brightness[] = + "/sys/class/leds/gta04:red:aux/max_brightness"; +const char notifications_green_brightness[] = + "/sys/class/leds/gta04:green:aux/brightness"; +const char notifications_green_max_brightness[] = + "/sys/class/leds/gta04:green:aux/max_brightness"; + +pthread_mutex_t lights_mutex = PTHREAD_MUTEX_INITIALIZER; + +/* + * Sysfs + */ + +int sysfs_value_read(const char *path) +{ + char buffer[100]; + int value; + int fd = -1; + int rc; + + if (path == NULL) + return -1; + + fd = open(path, O_RDONLY); + if (fd < 0) + goto error; + + rc = read(fd, &buffer, sizeof(buffer)); + if (rc <= 0) + goto error; + + value = atoi(buffer); + goto complete; + +error: + value = -1; + +complete: + if (fd >= 0) + close(fd); + + return value; +} + +int sysfs_value_write(const char *path, int value) +{ + char buffer[100]; + int fd = -1; + int rc; + + if (path == NULL) + return -1; + + fd = open(path, O_WRONLY); + if (fd < 0) + goto error; + + snprintf((char *) &buffer, sizeof(buffer), "%d\n", value); + + rc = write(fd, buffer, strlen(buffer)); + if (rc < (int) strlen(buffer)) + goto error; + + rc = 0; + goto complete; + +error: + rc = -1; + +complete: + if (fd >= 0) + close(fd); + + return rc; +} + +/* + * GTA04 Lights + */ + +int gta04_lights_set_light_notifications(struct light_device_t *device, + const struct light_state_t *state) +{ + int red, green; + int max; + int rc = 0; + + if (state == NULL) + return -EINVAL; + + // GTA04 only has red and green + red = state->color & 0x00ff0000; + green = state->color & 0x0000ff00; + + pthread_mutex_lock(&lights_mutex); + + // Red max + max = sysfs_value_read(notifications_red_max_brightness); + if (max > 0) + red = (red * max) / 0xff; + + // Green max + max = sysfs_value_read(notifications_green_max_brightness); + if (max > 0) + green = (green * max) / 0xff; + + rc |= sysfs_value_write(notifications_red_brightness, red); + rc |= sysfs_value_write(notifications_green_brightness, green); + + pthread_mutex_unlock(&lights_mutex); + + return rc; +} + +int gta04_lights_set_light_battery(struct light_device_t *device, + const struct light_state_t *state) +{ + int red, green; + int max; + int rc = 0; + + if (state == NULL) + return -EINVAL; + + // GTA04 only has red and green + red = state->color & 0x00ff0000; + green = state->color & 0x0000ff00; + + pthread_mutex_lock(&lights_mutex); + + // Red max + max = sysfs_value_read(battery_red_max_brightness); + if (max > 0) + red = (red * max) / 0xff; + + // Green max + max = sysfs_value_read(battery_green_max_brightness); + if (max > 0) + green = (green * max) / 0xff; + + rc |= sysfs_value_write(battery_red_brightness, red); + rc |= sysfs_value_write(battery_green_brightness, green); + + pthread_mutex_unlock(&lights_mutex); + + return rc; +} + +int gta04_lights_set_light_backlight(struct light_device_t *device, + const struct light_state_t *state) +{ + int brightness; + int color; + int max; + int rc; + + if (state == NULL) + return -EINVAL; + + color = state->color & 0x00ffffff; + brightness = ((77 * ((color >> 16) & 0xff)) + (150 * ((color >> 8) & 0xff)) + (29 * (color & 0xff))) >> 8; + + pthread_mutex_lock(&lights_mutex); + + // Brightness max + max = sysfs_value_read(backlight_max_brightness); + if (max > 0) + brightness = (brightness * max) / 0xff; + + rc = sysfs_value_write(backlight_brightness, brightness); + + pthread_mutex_unlock(&lights_mutex); + + return rc; +} + +/* + * Interface + */ + +int gta04_lights_close(struct light_device_t *device) +{ + ALOGD("%s(%p)", __func__, device); + + if (device == NULL) + return -EINVAL; + + pthread_mutex_destroy(&lights_mutex); + + free(device); + + return 0; +} + +int gta04_lights_open(const struct hw_module_t *module, char const *light_id, + struct hw_device_t **device) +{ + struct light_device_t *light_device; + int (*set_light)(struct light_device_t *light_device, + const struct light_state_t *state); + + ALOGD("%s(%p, %s, %p)", __func__, module, light_id, device); + + if (module == NULL || light_id == NULL || device == NULL) + return -EINVAL; + + if (strcmp(LIGHT_ID_BACKLIGHT, light_id) == 0) + set_light = gta04_lights_set_light_backlight; + else if (strcmp(LIGHT_ID_BATTERY, light_id) == 0) + set_light = gta04_lights_set_light_battery; + else if (strcmp(LIGHT_ID_NOTIFICATIONS, light_id) == 0) + set_light = gta04_lights_set_light_notifications; + else + return -EINVAL; + + pthread_mutex_init(&lights_mutex, NULL); + + light_device = (struct light_device_t *) calloc(1, sizeof(struct light_device_t)); + light_device->common.tag = HARDWARE_DEVICE_TAG; + light_device->common.version = 0; + light_device->common.module = (struct hw_module_t *) module; + light_device->common.close = (int (*)(struct hw_device_t *)) gta04_lights_close; + light_device->set_light = set_light; + + *device = (struct hw_device_t *) light_device; + + return 0; +} + +struct hw_module_methods_t gta04_lights_module_methods = { + .open = gta04_lights_open, +}; + +struct hw_module_t HAL_MODULE_INFO_SYM = { + .tag = HARDWARE_MODULE_TAG, + .version_major = 1, + .version_minor = 0, + .id = LIGHTS_HARDWARE_MODULE_ID, + .name = "GTA04 Lights", + .author = "Paul Kocialkowski", + .methods = >a04_lights_module_methods, +}; diff --git a/lights/lights.c b/lights/lights.c deleted file mode 100644 index 418c5ef..0000000 --- a/lights/lights.c +++ /dev/null @@ -1,276 +0,0 @@ -/* - * Copyright (C) 2012-2014 Paul Kocialkowski - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#define LOG_TAG "lights" -#include -#include - -/* - * Globals - */ - -const char backlight_brightness[] = - "/sys/class/backlight/pwm-backlight/brightness"; -const char backlight_max_brightness[] = - "/sys/class/backlight/pwm-backlight/max_brightness"; - -const char battery_red_brightness[] = - "/sys/class/leds/gta04:red:power/brightness"; -const char battery_red_max_brightness[] = - "/sys/class/leds/gta04:red:power/max_brightness"; -const char battery_green_brightness[] = - "/sys/class/leds/gta04:green:power/brightness"; -const char battery_green_max_brightness[] = - "/sys/class/leds/gta04:green:power/max_brightness"; - -const char notifications_red_brightness[] = - "/sys/class/leds/gta04:red:aux/brightness"; -const char notifications_red_max_brightness[] = - "/sys/class/leds/gta04:red:aux/max_brightness"; -const char notifications_green_brightness[] = - "/sys/class/leds/gta04:green:aux/brightness"; -const char notifications_green_max_brightness[] = - "/sys/class/leds/gta04:green:aux/max_brightness"; - -pthread_mutex_t lights_mutex = PTHREAD_MUTEX_INITIALIZER; - -/* - * Sysfs - */ - -int sysfs_value_read(const char *path) -{ - char buffer[100]; - int value; - int fd = -1; - int rc; - - if (path == NULL) - return -1; - - fd = open(path, O_RDONLY); - if (fd < 0) - goto error; - - rc = read(fd, &buffer, sizeof(buffer)); - if (rc <= 0) - goto error; - - value = atoi(buffer); - goto complete; - -error: - value = -1; - -complete: - if (fd >= 0) - close(fd); - - return value; -} - -int sysfs_value_write(const char *path, int value) -{ - char buffer[100]; - int fd = -1; - int rc; - - if (path == NULL) - return -1; - - fd = open(path, O_WRONLY); - if (fd < 0) - goto error; - - snprintf((char *) &buffer, sizeof(buffer), "%d\n", value); - - rc = write(fd, buffer, strlen(buffer)); - if (rc < (int) strlen(buffer)) - goto error; - - rc = 0; - goto complete; - -error: - rc = -1; - -complete: - if (fd >= 0) - close(fd); - - return rc; -} - -/* - * Lights - */ - -int set_light_notifications(struct light_device_t *dev, - const struct light_state_t *state) -{ - int red, green; - int max; - int rc = 0; - - // GTA04 only has red and green - red = state->color & 0x00ff0000; - green = state->color & 0x0000ff00; - - pthread_mutex_lock(&lights_mutex); - - // Red max - max = sysfs_value_read(notifications_red_max_brightness); - if(max > 0) - red = (red * max) / 0xff; - - // Green max - max = sysfs_value_read(notifications_green_max_brightness); - if(max > 0) - green = (green * max) / 0xff; - - rc |= sysfs_value_write(notifications_red_brightness, red); - rc |= sysfs_value_write(notifications_green_brightness, green); - - pthread_mutex_unlock(&lights_mutex); - - return rc; -} - -int set_light_battery(struct light_device_t *dev, - const struct light_state_t *state) -{ - int red, green; - int max; - int rc = 0; - - // GTA04 only has red and green - red = state->color & 0x00ff0000; - green = state->color & 0x0000ff00; - - pthread_mutex_lock(&lights_mutex); - - // Red max - max = sysfs_value_read(battery_red_max_brightness); - if(max > 0) - red = (red * max) / 0xff; - - // Green max - max = sysfs_value_read(battery_green_max_brightness); - if(max > 0) - green = (green * max) / 0xff; - - rc |= sysfs_value_write(battery_red_brightness, red); - rc |= sysfs_value_write(battery_green_brightness, green); - - pthread_mutex_unlock(&lights_mutex); - - return rc; -} - -int set_light_backlight(struct light_device_t *dev, - const struct light_state_t *state) -{ - int brightness; - int color; - int max; - int rc; - - pthread_mutex_lock(&lights_mutex); - - color = state->color & 0x00ffffff; - brightness = ((77 * ((color >> 16) & 0xff)) + (150 * ((color >> 8) & 0xff)) + (29 * (color & 0xff))) >> 8; - - // Brightness max - max = sysfs_value_read(backlight_max_brightness); - if(max > 0) - brightness = (brightness * max) / 0xff; - - rc = sysfs_value_write(backlight_brightness, brightness); - - pthread_mutex_unlock(&lights_mutex); - - return rc; -} - -/* - * Interface - */ - -int close_lights(struct light_device_t *dev) -{ - ALOGD("%s()", __func__); - - if(dev != NULL) - free(dev); - - return 0; -} - -int open_lights(const struct hw_module_t *module, char const *name, - struct hw_device_t **device) -{ - struct light_device_t *dev = NULL; - int (*set_light)(struct light_device_t *dev, - const struct light_state_t *state); - - ALOGD("%s(%s)", __func__, name); - - if(strcmp(LIGHT_ID_BACKLIGHT, name) == 0) { - set_light = set_light_backlight; - } else if(strcmp(LIGHT_ID_BATTERY, name) == 0) { - set_light = set_light_battery; - } else if(strcmp(LIGHT_ID_NOTIFICATIONS, name) == 0) { - set_light = set_light_notifications; - } else { - return -1; - } - - pthread_mutex_init(&lights_mutex, NULL); - - dev = (struct light_device_t *) calloc(1, sizeof(struct light_device_t)); - dev->common.tag = HARDWARE_DEVICE_TAG; - dev->common.version = 0; - dev->common.module = (struct hw_module_t *) module; - dev->common.close = (int (*)(struct hw_device_t *)) close_lights; - dev->set_light = set_light; - - *device = (struct hw_device_t *) dev; - - return 0; -} - -struct hw_module_methods_t lights_module_methods = { - .open = open_lights, -}; - -struct hw_module_t HAL_MODULE_INFO_SYM = { - .tag = HARDWARE_MODULE_TAG, - .version_major = 1, - .version_minor = 0, - .id = LIGHTS_HARDWARE_MODULE_ID, - .name = "Goldelico GTA04 lights", - .author = "Paul Kocialkowski", - .methods = &lights_module_methods, -}; -- cgit v1.1