diff options
author | Paul Kocialkowski <contact@paulk.fr> | 2012-09-03 17:20:23 +0200 |
---|---|---|
committer | Paul Kocialkowski <contact@paulk.fr> | 2012-09-03 17:20:23 +0200 |
commit | ba69038eebf24881e7eceedb5d304e931760e365 (patch) | |
tree | 70f48a0986d67a0b7d2196da1848965e41ea34f2 | |
parent | ee4174f08d2f0a116a3cad203a7423211566ef85 (diff) | |
download | device_goldelico_gta04-ba69038eebf24881e7eceedb5d304e931760e365.zip device_goldelico_gta04-ba69038eebf24881e7eceedb5d304e931760e365.tar.gz device_goldelico_gta04-ba69038eebf24881e7eceedb5d304e931760e365.tar.bz2 |
Lights: Added lights module, updated init.gta04.rc
Signed-off-by: Paul Kocialkowski <contact@paulk.fr>
-rw-r--r-- | init.gta04.rc | 14 | ||||
-rw-r--r-- | liblights/Android.mk | 31 | ||||
-rw-r--r-- | liblights/lights.c | 276 |
3 files changed, 321 insertions, 0 deletions
diff --git a/init.gta04.rc b/init.gta04.rc index 8f55798..e6ff9a7 100644 --- a/init.gta04.rc +++ b/init.gta04.rc @@ -1,4 +1,18 @@ on boot write /sys/class/usb_composite/usb_mass_storage/enable 1 + + # Modem GPIO chown radio radio /sys/class/gpio/gpio186/value chmod 660 /sys/class/gpio/gpio186/value + + # Lights + chown system system /sys/class/backlight/pwm-backlight/brightness + chown system system /sys/class/backlight/pwm-backlight/max_brightness + chown system system /sys/class/leds/gta04:red:power/brightness + chown system system /sys/class/leds/gta04:red:power/max_brightness + chown system system /sys/class/leds/gta04:green:power/brightness + chown system system /sys/class/leds/gta04:green:power/max_brightness + chown system system /sys/class/leds/gta04:red:aux/brightness + chown system system /sys/class/leds/gta04:red:aux/max_brightness + chown system system /sys/class/leds/gta04:green:aux/brightness + chown system system /sys/class/leds/gta04:green:aux/max_brightness diff --git a/liblights/Android.mk b/liblights/Android.mk new file mode 100644 index 0000000..49f1d1b --- /dev/null +++ b/liblights/Android.mk @@ -0,0 +1,31 @@ +# Copyright (C) 2012 Paul Kocialkowski <contact@paulk.fr> +# +# 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. + +ifeq ($(TARGET_DEVICE),gta04) + +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := lights.c + +LOCAL_SHARED_LIBRARIES := liblog + +LOCAL_PRELINK_MODULE := false +LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw +LOCAL_MODULE_TAGS := optional + +LOCAL_MODULE := lights.gta04 +include $(BUILD_SHARED_LIBRARY) + +endif diff --git a/liblights/lights.c b/liblights/lights.c new file mode 100644 index 0000000..2eeb171 --- /dev/null +++ b/liblights/lights.c @@ -0,0 +1,276 @@ +/* + * Copyright (C) 2012 Paul Kocialkowski <contact@paulk.fr> + * + * 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. + */ + +#define LOG_TAG "lights" +#include <cutils/log.h> +#include <stdint.h> +#include <string.h> +#include <errno.h> +#include <fcntl.h> +#include <pthread.h> +#include <sys/ioctl.h> +#include <sys/types.h> +#include <hardware/lights.h> + +/* + * Globals + */ + +pthread_mutex_t lights_mutex = PTHREAD_MUTEX_INITIALIZER; + +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"; + +/* + * Lights utils + */ + +int sysfs_write_int(char *path, int value) +{ + char buf[10]; + int length = 0; + int fd = -1; + int rc; + + if(path == NULL) + return -1; + + length = snprintf(buf, 10, "%d\n", value); + + fd = open(path, O_WRONLY); + if(fd < 0) + return -1; + + rc = write(fd, buf, length); + + close(fd); + + if(rc < length) + return -1; + + return 0; +} + +int sysfs_read_int(char *path) +{ + char buf[10]; + int val = 0; + int fd = -1; + int rc; + + if(path == NULL) + return -1; + + fd = open(path, O_RDONLY); + if(fd < 0) + return -1; + + rc = read(fd, buf, 10); + if(rc <= 0) { + close(fd); + return -1; + } + + val = atoi(buf); + + return val; +} + +/* + * Lights functions + */ + +static int set_light_notifications(struct light_device_t *dev, + const struct light_state_t *state) +{ + int red, green; + int max; + int rc; + + // GTA04 only has red and green + red = state->color & 0x00ff0000; + green = state->color & 0x0000ff00; + + // Red max + pthread_mutex_lock(&lights_mutex); + max = sysfs_read_int(notifications_red_max_brightness); + pthread_mutex_unlock(&lights_mutex); + + if(max > 0) + red = (red * max) / 0xff; + + // Green max + pthread_mutex_lock(&lights_mutex); + max = sysfs_read_int(notifications_green_max_brightness); + pthread_mutex_unlock(&lights_mutex); + + if(max > 0) + green = (green * max) / 0xff; + + pthread_mutex_lock(&lights_mutex); + rc = sysfs_write_int(notifications_red_brightness, red); + if(rc >= 0) + rc = sysfs_write_int(notifications_green_brightness, green); + pthread_mutex_unlock(&lights_mutex); + + return rc; +} + +static int set_light_battery(struct light_device_t *dev, + const struct light_state_t *state) +{ + int red, green; + int max; + int rc; + + // GTA04 only has red and green + red = state->color & 0x00ff0000; + green = state->color & 0x0000ff00; + + // Red max + pthread_mutex_lock(&lights_mutex); + max = sysfs_read_int(battery_red_max_brightness); + pthread_mutex_unlock(&lights_mutex); + + if(max > 0) + red = (red * max) / 0xff; + + // Green max + pthread_mutex_lock(&lights_mutex); + max = sysfs_read_int(battery_green_max_brightness); + pthread_mutex_unlock(&lights_mutex); + + if(max > 0) + green = (green * max) / 0xff; + + pthread_mutex_lock(&lights_mutex); + rc = sysfs_write_int(battery_red_brightness, red); + if(rc >= 0) + rc = sysfs_write_int(battery_green_brightness, green); + pthread_mutex_unlock(&lights_mutex); + + return rc; +} + +static int set_light_backlight(struct light_device_t *dev, + const struct light_state_t *state) +{ + int color; + unsigned char brightness; + int brightness_max; + int rc; + + pthread_mutex_lock(&lights_mutex); + brightness_max = + sysfs_read_int(backlight_max_brightness); + pthread_mutex_unlock(&lights_mutex); + + color = state->color & 0x00ffffff; + brightness = ((77*((color>>16) & 0x00ff)) + (150*((color>>8) & 0x00ff)) + + (29*(color & 0x00ff))) >> 8; + + if(brightness_max > 0) + brightness = (brightness * brightness_max) / 0xff; + + LOGD("Setting brightness to: %d", brightness); + + pthread_mutex_lock(&lights_mutex); + rc = sysfs_write_int(backlight_brightness, brightness); + pthread_mutex_unlock(&lights_mutex); + + return rc; +} + +/* + * Interface + */ + +static int close_lights(struct light_device_t *dev) +{ + LOGD("close_lights()"); + + if(dev != NULL) + free(dev); + + return 0; +} + +static 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); + + LOGD("open_lights(): %s", 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 = 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; +} + +static struct hw_module_methods_t lights_module_methods = { + .open = open_lights, +}; + +const 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, +}; |