diff options
Diffstat (limited to 'libsensors/bh1721.c')
-rw-r--r-- | libsensors/bh1721.c | 105 |
1 files changed, 47 insertions, 58 deletions
diff --git a/libsensors/bh1721.c b/libsensors/bh1721.c index 1486ef9..34d4594 100644 --- a/libsensors/bh1721.c +++ b/libsensors/bh1721.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 Paul Kocialkowski + * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,6 +20,8 @@ #include <stdint.h> #include <fcntl.h> #include <errno.h> +#include <math.h> +#include <sys/types.h> #include <linux/ioctl.h> #include <linux/input.h> @@ -36,7 +38,8 @@ struct bh1721_data { char path_delay[PATH_MAX]; }; -int bh1721_init(struct piranha_sensors_handlers *handlers, struct piranha_sensors_device *device) +int bh1721_init(struct piranha_sensors_handlers *handlers, + struct piranha_sensors_device *device) { struct bh1721_data *data = NULL; char path[PATH_MAX] = { 0 }; @@ -48,6 +51,8 @@ int bh1721_init(struct piranha_sensors_handlers *handlers, struct piranha_sensor if (handlers == NULL) return -EINVAL; + data = (struct bh1721_data *) calloc(1, sizeof(struct bh1721_data)); + input_fd = input_open("light_sensor"); if (input_fd < 0) { ALOGE("%s: Unable to open input", __func__); @@ -60,8 +65,6 @@ int bh1721_init(struct piranha_sensors_handlers *handlers, struct piranha_sensor goto error; } - data = (struct bh1721_data *) calloc(1, sizeof(struct bh1721_data)); - snprintf(data->path_enable, PATH_MAX, "%s/enable", path); snprintf(data->path_delay, PATH_MAX, "%s/poll_delay", path); @@ -71,12 +74,12 @@ int bh1721_init(struct piranha_sensors_handlers *handlers, struct piranha_sensor return 0; error: - if (input_fd >= 0) - close(input_fd); - if (data != NULL) free(data); + if (input_fd >= 0) + close(input_fd); + handlers->poll_fd = -1; handlers->data = NULL; @@ -85,22 +88,17 @@ error: int bh1721_deinit(struct piranha_sensors_handlers *handlers) { - int input_fd; - ALOGD("%s(%p)", __func__, handlers); if (handlers == NULL) return -EINVAL; - input_fd = handlers->poll_fd; - if (input_fd >= 0) - close(input_fd); - + if (handlers->poll_fd >= 0) + close(handlers->poll_fd); handlers->poll_fd = -1; if (handlers->data != NULL) free(handlers->data); - handlers->data = NULL; return 0; @@ -109,8 +107,7 @@ int bh1721_deinit(struct piranha_sensors_handlers *handlers) int bh1721_activate(struct piranha_sensors_handlers *handlers) { struct bh1721_data *data; - char enable[] = "1\n"; - int fd; + int rc; ALOGD("%s(%p)", __func__, handlers); @@ -119,15 +116,12 @@ int bh1721_activate(struct piranha_sensors_handlers *handlers) data = (struct bh1721_data *) handlers->data; - fd = open(data->path_enable, O_WRONLY); - if (fd < 0) { - ALOGE("%s: Unable to open enable path", __func__); + rc = sysfs_value_write(data->path_enable, 1); + if (rc < 0) { + ALOGE("%s: Unable to write sysfs value", __func__); return -1; } - write(fd, &enable, sizeof(enable)); - close(fd); - handlers->activated = 1; return 0; @@ -136,8 +130,7 @@ int bh1721_activate(struct piranha_sensors_handlers *handlers) int bh1721_deactivate(struct piranha_sensors_handlers *handlers) { struct bh1721_data *data; - char enable[] = "0\n"; - int fd; + int rc; ALOGD("%s(%p)", __func__, handlers); @@ -146,54 +139,41 @@ int bh1721_deactivate(struct piranha_sensors_handlers *handlers) data = (struct bh1721_data *) handlers->data; - fd = open(data->path_enable, O_WRONLY); - if (fd < 0) { - ALOGE("%s: Unable to open enable path", __func__); + rc = sysfs_value_write(data->path_enable, 0); + if (rc < 0) { + ALOGE("%s: Unable to write sysfs value", __func__); return -1; } - write(fd, &enable, sizeof(enable)); - close(fd); - - handlers->activated = 0; + handlers->activated = 1; return 0; } -int bh1721_set_delay(struct piranha_sensors_handlers *handlers, int64_t delay) +int bh1721_set_delay(struct piranha_sensors_handlers *handlers, long int delay) { struct bh1721_data *data; - char *value = NULL; - int c; - int fd; + int rc; -// ALOGD("%s(%p, %ld)", __func__, handlers, (long int) delay); + ALOGD("%s(%p, %ld)", __func__, handlers, delay); if (handlers == NULL || handlers->data == NULL) return -EINVAL; data = (struct bh1721_data *) handlers->data; - c = asprintf(&value, "%ld\n", (long int) delay); - - fd = open(data->path_delay, O_WRONLY); - if (fd < 0) { - ALOGE("%s: Unable to open delay path", __func__); + rc = sysfs_value_write(data->path_delay, (int) delay); + if (rc < 0) { + ALOGE("%s: Unable to write sysfs value", __func__); return -1; } - write(fd, value, c); - close(fd); - - if (value != NULL) - free(value); - return 0; } -float bh1721_light(int value) +float bh1721_convert(int value) { - return (float) value * 0.712f; + return value * 0.712f; } int bh1721_get_data(struct piranha_sensors_handlers *handlers, @@ -203,6 +183,8 @@ int bh1721_get_data(struct piranha_sensors_handlers *handlers, int input_fd; int rc; +// ALOGD("%s(%p, %p)", __func__, handlers, event); + if (handlers == NULL || event == NULL) return -EINVAL; @@ -210,18 +192,24 @@ int bh1721_get_data(struct piranha_sensors_handlers *handlers, if (input_fd < 0) return -EINVAL; - rc = read(input_fd, &input_event, sizeof(input_event)); - if (rc < (int) sizeof(input_event)) - return -EINVAL; - - if (input_event.type != EV_ABS || input_event.code != ABS_MISC) - return -1; - + memset(event, 0, sizeof(struct sensors_event_t)); event->version = sizeof(struct sensors_event_t); event->sensor = handlers->handle; event->type = handlers->handle; - event->timestamp = input_timestamp(&input_event); - event->light = bh1721_light(input_event.value); + + do { + rc = read(input_fd, &input_event, sizeof(input_event)); + if (rc < (int) sizeof(input_event)) + break; + + if (input_event.type == EV_REL) { + if (input_event.code == REL_MISC) + event->light = bh1721_convert(input_event.value); + } else if (input_event.type == EV_SYN) { + if (input_event.code == SYN_REPORT) + event->timestamp = input_timestamp(&input_event); + } + } while (input_event.type != EV_SYN); return 0; } @@ -236,6 +224,7 @@ struct piranha_sensors_handlers bh1721 = { .set_delay = bh1721_set_delay, .get_data = bh1721_get_data, .activated = 0, + .needed = 0, .poll_fd = -1, .data = NULL, }; |