diff options
Diffstat (limited to 'libsensors/gp2a_proximity.c')
-rw-r--r-- | libsensors/gp2a_proximity.c | 93 |
1 files changed, 41 insertions, 52 deletions
diff --git a/libsensors/gp2a_proximity.c b/libsensors/gp2a_proximity.c index 873ad94..b2c9813 100644 --- a/libsensors/gp2a_proximity.c +++ b/libsensors/gp2a_proximity.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,7 +20,7 @@ #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 +36,8 @@ struct gp2a_proximity_data { char path_enable[PATH_MAX]; }; -int gp2a_proximity_init(struct piranha_sensors_handlers *handlers, struct piranha_sensors_device *device) +int gp2a_proximity_init(struct piranha_sensors_handlers *handlers, + struct piranha_sensors_device *device) { struct gp2a_proximity_data *data = NULL; char path[PATH_MAX] = { 0 }; @@ -48,6 +49,8 @@ int gp2a_proximity_init(struct piranha_sensors_handlers *handlers, struct piranh if (handlers == NULL) return -EINVAL; + data = (struct gp2a_proximity_data *) calloc(1, sizeof(struct gp2a_proximity_data)); + input_fd = input_open("proximity_sensor"); if (input_fd < 0) { ALOGE("%s: Unable to open input", __func__); @@ -60,8 +63,6 @@ int gp2a_proximity_init(struct piranha_sensors_handlers *handlers, struct piranh goto error; } - data = (struct gp2a_proximity_data *) calloc(1, sizeof(struct gp2a_proximity_data)); - snprintf(data->path_enable, PATH_MAX, "%s/enable", path); handlers->poll_fd = input_fd; @@ -70,12 +71,12 @@ int gp2a_proximity_init(struct piranha_sensors_handlers *handlers, struct piranh 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; @@ -84,22 +85,17 @@ error: int gp2a_proximity_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; @@ -108,8 +104,7 @@ int gp2a_proximity_deinit(struct piranha_sensors_handlers *handlers) int gp2a_proximity_activate(struct piranha_sensors_handlers *handlers) { struct gp2a_proximity_data *data; - char enable[] = "1\n"; - int fd; + int rc; ALOGD("%s(%p)", __func__, handlers); @@ -118,15 +113,12 @@ int gp2a_proximity_activate(struct piranha_sensors_handlers *handlers) data = (struct gp2a_proximity_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; @@ -135,8 +127,7 @@ int gp2a_proximity_activate(struct piranha_sensors_handlers *handlers) int gp2a_proximity_deactivate(struct piranha_sensors_handlers *handlers) { struct gp2a_proximity_data *data; - char enable[] = "0\n"; - int fd; + int rc; ALOGD("%s(%p)", __func__, handlers); @@ -145,36 +136,25 @@ int gp2a_proximity_deactivate(struct piranha_sensors_handlers *handlers) data = (struct gp2a_proximity_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 gp2a_proximity_set_delay(struct piranha_sensors_handlers *handlers, int64_t delay) +int gp2a_proximity_set_delay(struct piranha_sensors_handlers *handlers, long int delay) { - struct gp2a_proximity_data *data; - char *value = NULL; - int c; - int fd; - -// ALOGD("%s(%p, %ld)", __func__, handlers, (long int) delay); - - if (handlers == NULL || handlers->data == NULL) - return -EINVAL; + ALOGD("%s(%p, %ld)", __func__, handlers, delay); return 0; } -float gp2a_proximity_proximity(int value) +float gp2a_proximity_convert(int value) { return (float) value * 5.0f; } @@ -186,6 +166,8 @@ int gp2a_proximity_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; @@ -193,18 +175,24 @@ int gp2a_proximity_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_DISTANCE) - 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->distance = gp2a_proximity_proximity(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_ABS) { + if (input_event.code == ABS_DISTANCE) + event->distance = gp2a_proximity_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; } @@ -219,6 +207,7 @@ struct piranha_sensors_handlers gp2a_proximity = { .set_delay = gp2a_proximity_set_delay, .get_data = gp2a_proximity_get_data, .activated = 0, + .needed = 0, .poll_fd = -1, .data = NULL, }; |