diff options
author | Paul Kocialkowski <contact@paulk.fr> | 2013-12-21 17:21:26 +0100 |
---|---|---|
committer | Paul Kocialkowski <contact@paulk.fr> | 2014-01-03 23:33:43 +0100 |
commit | 8f588313abf068f0eca3819651a63ce769324ab7 (patch) | |
tree | 57d90ff22f0f65df9b0c84cbd7031741b542fde9 /sensors | |
parent | 63f28807de5f73597605fc80a0fcddae23eca833 (diff) | |
download | device_samsung_crespo-8f588313abf068f0eca3819651a63ce769324ab7.zip device_samsung_crespo-8f588313abf068f0eca3819651a63ce769324ab7.tar.gz device_samsung_crespo-8f588313abf068f0eca3819651a63ce769324ab7.tar.bz2 |
Herring Sensorsreplicant-4.2-0002replicant-4.2-0001
Signed-off-by: Paul Kocialkowski <contact@paulk.fr>
Diffstat (limited to 'sensors')
-rw-r--r-- | sensors/Android.mk | 40 | ||||
-rw-r--r-- | sensors/ak8973-reg.h | 47 | ||||
-rw-r--r-- | sensors/ak8973.h | 72 | ||||
-rw-r--r-- | sensors/akm8973.c | 960 | ||||
-rw-r--r-- | sensors/gp2a_light.c | 239 | ||||
-rw-r--r-- | sensors/gp2a_proximity.c | 213 | ||||
-rw-r--r-- | sensors/herring_sensors.c | 289 | ||||
-rw-r--r-- | sensors/herring_sensors.h | 104 | ||||
-rw-r--r-- | sensors/input.c | 335 | ||||
-rw-r--r-- | sensors/k3g.c | 254 | ||||
-rw-r--r-- | sensors/kr3dm.c | 375 | ||||
-rw-r--r-- | sensors/kr3dm.h | 46 | ||||
-rw-r--r-- | sensors/orientation.c | 444 |
13 files changed, 3418 insertions, 0 deletions
diff --git a/sensors/Android.mk b/sensors/Android.mk new file mode 100644 index 0000000..d3a5656 --- /dev/null +++ b/sensors/Android.mk @@ -0,0 +1,40 @@ +# 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 +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := \ + herring_sensors.c \ + input.c \ + orientation.c \ + akm8973.c \ + kr3dm.c \ + gp2a_light.c \ + gp2a_proximity.c \ + k3g.c + +LOCAL_C_INCLUDES := \ + $(LOCAL_PATH) + +LOCAL_SHARED_LIBRARIES := libutils libcutils liblog libhardware +LOCAL_PRELINK_MODULE := false + +LOCAL_MODULE := sensors.herring +LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw +LOCAL_MODULE_TAGS := optional + +include $(BUILD_SHARED_LIBRARY) diff --git a/sensors/ak8973-reg.h b/sensors/ak8973-reg.h new file mode 100644 index 0000000..1582076 --- /dev/null +++ b/sensors/ak8973-reg.h @@ -0,0 +1,47 @@ +/* linux/drivers/misc/ak8973-reg.h + * + * Copyright (c) 2010 Samsung Electronics Co., Ltd. + * http://www.samsung.com/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. +*/ +#ifndef __AK8973_REG__ +#define __AK8983_REG__ + +/* Compass device dependent definition */ +#define AK8973_MODE_MEASURE 0x00 /* Starts measurement. */ +#define AK8973_MODE_E2P_READ 0x02 /* E2P access mode (read). */ +#define AK8973_MODE_POWERDOWN 0x03 /* Power down mode */ + +/* Rx buffer size. i.e ST,TMPS,H1X,H1Y,H1Z*/ +#define SENSOR_DATA_SIZE 5 + +/* Read/Write buffer size.*/ +#define RWBUF_SIZE 16 + +/* AK8973 register address */ +#define AK8973_REG_ST 0xC0 +#define AK8973_REG_TMPS 0xC1 +#define AK8973_REG_H1X 0xC2 +#define AK8973_REG_H1Y 0xC3 +#define AK8973_REG_H1Z 0xC4 + +#define AK8973_REG_MS1 0xE0 +#define AK8973_REG_HXDA 0xE1 +#define AK8973_REG_HYDA 0xE2 +#define AK8973_REG_HZDA 0xE3 +#define AK8973_REG_HXGA 0xE4 +#define AK8973_REG_HYGA 0xE5 +#define AK8973_REG_HZGA 0xE6 + +#define AK8973_EEP_ETS 0x62 +#define AK8973_EEP_EVIR 0x63 +#define AK8973_EEP_EIHE 0x64 +#define AK8973_EEP_ETST 0x65 +#define AK8973_EEP_EHXGA 0x66 +#define AK8973_EEP_EHYGA 0x67 +#define AK8973_EEP_EHZGA 0x68 + +#endif /* __AK8983_REG__ */ diff --git a/sensors/ak8973.h b/sensors/ak8973.h new file mode 100644 index 0000000..3f6a28b --- /dev/null +++ b/sensors/ak8973.h @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2010 Samsung Electronics. All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA + */ +#ifndef AKM8973_H +#define AKM8973_H + +#include <linux/ioctl.h> + +#define AKM8973_I2C_NAME "ak8973" + +#define AKMIO 0xA1 + +/* IOCTLs for AKM library */ +/* WRITE and READ sizes don't include data. On WRITE, the first value is data + * size plus one and the second value is the register address. On READ + * the first value is the data size and second value is the register + * address and the data is written back into the buffer starting at + * the second byte (the length is unchanged). + */ +#define ECS_IOCTL_WRITE _IOW(AKMIO, 0x01, char[2]) +#define ECS_IOCTL_READ _IOWR(AKMIO, 0x02, char[2]) +#define ECS_IOCTL_RESET _IO(AKMIO, 0x03) +#define ECS_IOCTL_SET_MODE _IOW(AKMIO, 0x04, short) +#define ECS_IOCTL_GETDATA _IOR(AKMIO, 0x05, char[5]) +#define ECS_IOCTL_SET_YPR _IOW(AKMIO, 0x06, short[12]) +#define ECS_IOCTL_GET_OPEN_STATUS _IOR(AKMIO, 0x07, int) +#define ECS_IOCTL_GET_CLOSE_STATUS _IOR(AKMIO, 0x08, int) +#define ECS_IOCTL_GET_DELAY _IOR(AKMIO, 0x30, int64_t) +#define ECS_IOCTL_GET_PROJECT_NAME _IOR(AKMIO, 0x0D, char[64]) +#define ECS_IOCTL_GET_MATRIX _IOR(AKMIO, 0x0E, short[4][3][3]) + +/* IOCTLs for APPs */ +#define ECS_IOCTL_APP_SET_MODE _IOW(AKMIO, 0x10, short) +#define ECS_IOCTL_APP_SET_MFLAG _IOW(AKMIO, 0x11, short) +#define ECS_IOCTL_APP_GET_MFLAG _IOR(AKMIO, 0x12, short) +#define ECS_IOCTL_APP_SET_AFLAG _IOW(AKMIO, 0x13, short) +#define ECS_IOCTL_APP_GET_AFLAG _IOR(AKMIO, 0x14, short) +#define ECS_IOCTL_APP_SET_TFLAG _IOW(AKMIO, 0x15, short) +#define ECS_IOCTL_APP_GET_TFLAG _IOR(AKMIO, 0x16, short) +#define ECS_IOCTL_APP_RESET_PEDOMETER _IO(AKMIO, 0x17) +#define ECS_IOCTL_APP_SET_DELAY _IOW(AKMIO, 0x18, int64_t) +#define ECS_IOCTL_APP_GET_DELAY ECS_IOCTL_GET_DELAY + +/* Set raw magnetic vector flag */ +#define ECS_IOCTL_APP_SET_MVFLAG _IOW(AKMIO, 0x19, short) + +/* Get raw magnetic vector flag */ +#define ECS_IOCTL_APP_GET_MVFLAG _IOR(AKMIO, 0x1A, short) + +#ifdef __KERNEL__ +struct akm8973_platform_data { + int reset_line; + int reset_asserted; + int gpio_data_ready_int; +}; +#endif + +#endif diff --git a/sensors/akm8973.c b/sensors/akm8973.c new file mode 100644 index 0000000..509c365 --- /dev/null +++ b/sensors/akm8973.c @@ -0,0 +1,960 @@ +/* + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdlib.h> +#include <unistd.h> +#include <stdint.h> +#include <fcntl.h> +#include <errno.h> + +#include <hardware/sensors.h> +#include <hardware/hardware.h> + +#define LOG_TAG "herring_sensors" +#include <utils/Log.h> + +#include "herring_sensors.h" +#include "ak8973-reg.h" +#include "ak8973.h" + +#define AKM8973_CONFIG_PATH "/data/misc/akmd_set.txt" + +struct akm8973_data { + struct herring_sensors_handlers *orientation_sensor; + + sensors_vec_t magnetic; + unsigned char magnetic_data[4][3]; + int magnetic_data_count; + int magnetic_data_index; + + unsigned char magnetic_extrema[2][3]; + unsigned char gain_indexes[3]; + unsigned char hdac[3]; + int ho[3]; + + long int delay; + int device_fd; + int uinput_fd; + + pthread_t thread; + pthread_mutex_t mutex; + int thread_continue; +}; + +// This AKM8973 implementation is based on intuitive understanding of how the +// AKM8973 data is translated to SI units. +// +// Different parameters are used to properly configure and interpret the data: +// * Hardware gain, which is stored in the chip's EEPROM and is a factory +// setting that shouldn't be changed. We don't have to deal with this value at +// all for our computation. +// * Software gain, that is a factory setting we use to determine the +// coefficient to use at some point in the final equation. +// * Hardware DAC offset (HDAC) that we can tune in order to be able to see +// variations of the raw values. This value is not a linear representation +// of the offset! See page 22 of the AKM8973 datasheet for further details. +// * Software offset (HO) that we can tune in order to have the maximum final +// value at ~45uT and the minimal final value at ~-45uT for each axis. +// +// The final equation used to determine usable values from the raw input data +// was found to be: +// out = ((in - 128 - HO / 16) * k / 16 +// +// * out: Final value in uT units +// * in: Raw value as received from the sensor +// * HO: HO software offset +// * k: Coefficient that is determined from the software gain +// +// When no software calibration is used, we can translate HDAC to HO using the +// following formulas: +// if HDAC < 128: HO = HDAC * -1 * 16 * 16 +// if HDAC >= 128: HO = (HDAC - 128) * 16 * 16 + +// Constant gain-specific coefficients used in the final formula +float akm8973_gain_coefficient[] = { + 16.33f, + 16.28f, + 16.24f, + 16.18f, + 16.14f, + 16.10f, + 16.04f, + 16.00f, + 15.96f, + 15.90f, + 15.86f, + 15.82f, + 15.76f, + 15.72f, + 15.69f, + 15.65f, +}; + +int akm8973_gain_coefficient_count = sizeof(akm8973_gain_coefficient) / + sizeof(float); + +int akm8973_hdac(struct akm8973_data *data) +{ + char i2c_data[RWBUF_SIZE] = { 0 }; + int device_fd; + int rc; + + if (data == NULL) + return -EINVAL; + + device_fd = data->device_fd; + if (device_fd < 0) + return -1; + + i2c_data[0] = 4; + i2c_data[1] = AK8973_REG_HXDA; + i2c_data[2] = data->hdac[0]; + i2c_data[3] = data->hdac[1]; + i2c_data[4] = data->hdac[2]; + + rc = ioctl(device_fd, ECS_IOCTL_WRITE, &i2c_data); + if (rc < 0) { + ALOGE("%s: Unable to write akm8973 data", __func__); + return -1; + } + + return 0; +} + +int akm8973_magnetic_extrema(struct akm8973_data *data, int index) +{ + int gain_index; + + if (data == NULL || index < 0 || index >= 3) + return -EINVAL; + + gain_index = data->gain_indexes[index]; + if (gain_index < 0 || gain_index >= akm8973_gain_coefficient_count) + return -1; + + // Calculate the extrema from HO (software offset) + data->magnetic_extrema[0][index] = (unsigned char) ((int) ((16.0f * -45.0f) / akm8973_gain_coefficient[gain_index] + 128 + data->ho[index] / 16.0f + 10.0f) & 0xff); + data->magnetic_extrema[1][index] = (unsigned char) ((int) ((16.0f * 45.0f) / akm8973_gain_coefficient[gain_index] + 128 + data->ho[index] / 16.0f - 10.0f) & 0xff); + + return 0; +} + +int akm8973_config_read(struct akm8973_data *data) +{ + char buffer[256] = { 0 }; + int config_fd = -1; + int offset = 0; + int length; + int count; + int value; + char *p; + int rc; + + if (data == NULL) + return -EINVAL; + + config_fd = open(AKM8973_CONFIG_PATH, O_RDONLY); + if (config_fd < 0) { + ALOGE("%s: Unable to open akm8973 config %d %s", __func__, errno, strerror(errno)); + goto error; + } + + rc = 0; + + do { + lseek(config_fd, offset, SEEK_SET); + + length = read(config_fd, buffer, sizeof(buffer)); + if (length <= 0) + break; + + p = strchr((const char *) &buffer, '\n'); + if (p != NULL) { + offset += (int) p - (int) buffer + 1; + *p = '\0'; + } else if ((size_t) length < sizeof(buffer)) { + buffer[length] = '\0'; + } + + count = sscanf((char const *) &buffer, "HSUC_HDAC_FORM0.x = %d", &value); + if (count == 1) { + data->hdac[0] = (unsigned char) (value & 0xff); + rc |= 1; + } + + count = sscanf((char const *) &buffer, "HSUC_HDAC_FORM0.y = %d", &value); + if (count == 1) { + data->hdac[1] = (unsigned char) (value & 0xff); + rc |= 1; + } + + count = sscanf((char const *) &buffer, "HSUC_HDAC_FORM0.z = %d", &value); + if (count == 1) { + data->hdac[2] = (unsigned char) (value & 0xff); + rc |= 1; + } + + count = sscanf((char const *) &buffer, "HSUC_HO_FORM0.x = %d", &value); + if (count == 1) { + data->ho[0] = value; + rc |= akm8973_magnetic_extrema(data, 0); + } + + count = sscanf((char const *) &buffer, "HSUC_HO_FORM0.y = %d", &value); + if (count == 1) { + data->ho[1] = value; + rc |= akm8973_magnetic_extrema(data, 1); + } + + count = sscanf((char const *) &buffer, "HSUC_HO_FORM0.z = %d", &value); + if (count == 1) { + data->ho[2] = value; + rc |= akm8973_magnetic_extrema(data, 2); + } + } while (p != NULL && length > 0); + + goto complete; + +error: + rc = -1; + +complete: + if (config_fd >= 0) + close(config_fd); + + return rc; +} + +int akm8973_config_write(struct akm8973_data *data) +{ + char buffer[256] = { 0 }; + int config_fd = -1; + int length; + int value; + int rc; + + if (data == NULL) + return -EINVAL; + + config_fd = open(AKM8973_CONFIG_PATH, O_WRONLY | O_TRUNC | O_CREAT, 0664); + if (config_fd < 0) { + ALOGE("%s: Unable to open akm8973 config", __func__); + goto error; + } + + value = (int) data->hdac[0]; + length = snprintf((char *) &buffer, sizeof(buffer), "HSUC_HDAC_FORM0.x = %d\n", value); + + rc = write(config_fd, buffer, length); + if (rc < length) { + ALOGE("%s: Unable to write akm8973 config", __func__); + goto error; + } + + value = (int) data->hdac[1]; + length = snprintf((char *) &buffer, sizeof(buffer), "HSUC_HDAC_FORM0.y = %d\n", value); + + rc = write(config_fd, buffer, length); + if (rc < length) { + ALOGE("%s: Unable to write akm8973 config", __func__); + goto error; + } + + value = (int) data->hdac[2]; + length = snprintf((char *) &buffer, sizeof(buffer), "HSUC_HDAC_FORM0.z = %d\n", value); + + rc = write(config_fd, buffer, length); + if (rc < length) { + ALOGE("%s: Unable to write akm8973 config", __func__); + goto error; + } + + value = (int) data->ho[0]; + length = snprintf((char *) &buffer, sizeof(buffer), "HSUC_HO_FORM0.x = %d\n", value); + + rc = write(config_fd, buffer, length); + if (rc < length) { + ALOGE("%s: Unable to write akm8973 config", __func__); + goto error; + } + + value = (int) data->ho[1]; + length = snprintf((char *) &buffer, sizeof(buffer), "HSUC_HO_FORM0.y = %d\n", value); + + rc = write(config_fd, buffer, length); + if (rc < length) { + ALOGE("%s: Unable to write akm8973 config", __func__); + goto error; + } + + value = (int) data->ho[2]; + length = snprintf((char *) &buffer, sizeof(buffer), "HSUC_HO_FORM0.z = %d\n", value); + + rc = write(config_fd, buffer, length); + if (rc < length) { + ALOGE("%s: Unable to write akm8973 config", __func__); + goto error; + } + + rc = 0; + goto complete; + +error: + rc = -1; + +complete: + if (config_fd >= 0) + close(config_fd); + + return rc; +} + +int akm8973_hdac_calibration(struct akm8973_data *data, + unsigned char *magnetic_data, size_t magnetic_data_size) +{ + unsigned char value; + int update; + int rc; + int i; + + if (data == NULL || magnetic_data == NULL || magnetic_data_size < 3) + return -EINVAL; + + update = 0; + + for (i = 0; i < 3; i++) { + // Transform non-linear HDAC to a linear value + if (data->hdac[i] == 0) + value = 0x80; + else if (data->hdac[i] < 0x80) + value = 0x80 - data->hdac[i]; + else + value = data->hdac[i]; + + // Adjust the (linear) HDAC offset if the value is out of range. + // The correct range is [50;205] (in raw magnetic data). + + if (magnetic_data[i] < 50 ) { + if (value > (0xff - 4)) + continue; + + if (magnetic_data[i] < 10) + value += 4; + else if (magnetic_data[i] < 20) + value += 3; + else if (magnetic_data[i] < 30) + value += 2; + else + value += 1; + + update = 1; + } + + if (magnetic_data[i] > 205) { + if (value < (0x00 + 4)) + continue; + + if (magnetic_data[i] > 245) + value -= 4; + else if (magnetic_data[i] > 235) + value -= 3; + else if (magnetic_data[i] > 225) + value -= 2; + else + value -= 1; + + update = 1; + } + + if (update) { + // When the HDAC (hardware offset) value is changed, the HO value and magnetic extrema become irrelevant. + // We can calculate HO (software offset) from HDAC but it'll need to be finely tuned later on. + + data->magnetic_extrema[0][i] = 0; + data->magnetic_extrema[1][i] = 0; + + // Transform linear value to non-linear HDAC + if (value == 0x80) { + data->hdac[i] = 0; + data->ho[i] = 0; + } else if (value < 0x80) { + data->hdac[i] = 0x80 - value; + data->ho[i] = data->hdac[i] * -1 * 16 * 16; + } else { + data->hdac[i] = value; + data->ho[i] = (data->hdac[i] - 128) * 16 * 16; + } + } + } + + if (update) { + rc = akm8973_hdac(data); + if (rc < 0) { + ALOGE("%s: Unable to set akm8973 HDAC", __func__); + return -1; + } + } + + return 0; +} + +int akm8973_ho_calibration(struct akm8973_data *data, + unsigned char *magnetic_data, size_t magnetic_data_size) +{ + float ho[2]; + int gain_index; + int i; + + if (data == NULL || magnetic_data == NULL || magnetic_data_size < 3) + return -EINVAL; + + // Update the extrema from the current raw magnetic data + for (i = 0; i < 3; i++) { + if (magnetic_data[i] < data->magnetic_extrema[0][i] || data->magnetic_extrema[0][i] == 0) + data->magnetic_extrema[0][i] = magnetic_data[i]; + if (magnetic_data[i] > data->magnetic_extrema[1][i] || data->magnetic_extrema[1][i] == 0) + data->magnetic_extrema[1][i] = magnetic_data[i]; + } + + // Calculate HO (software offset) + if (data->magnetic_data_count % 10 == 0) { + for (i = 0; i < 3; i++) { + gain_index = data->gain_indexes[i]; + if (gain_index < 0 || gain_index >= akm8973_gain_coefficient_count) + continue; + + // Calculate offset for minimum to be at -45uT + ho[0] = ((float) (data->magnetic_extrema[0][i] - 0x80) + (16.0f * 45.0f) / akm8973_gain_coefficient[gain_index] ) * 16.0f; + // Calculate offset for maximum to be at +45uT + ho[1] = ((float) (data->magnetic_extrema[1][i] - 0x80) - (16.0f * 45.0f) / akm8973_gain_coefficient[gain_index] ) * 16.0f; + // Average offset to make everyone (mostly) happy + data->ho[i] = (int) (ho[0] + ho[1]) / 2.0f; + } + } + + return 0; +} + +int akm8973_magnetic_axis(struct akm8973_data *data, int index, float *axis) +{ + float value; + int count; + int gain_index; + int i; + + if (data == NULL || axis == NULL || index < 0 || index >= 3) + return -EINVAL; + + count = data->magnetic_data_count >= 4 ? 4 : data->magnetic_data_count; + value = 0; + + // Average the last 4 (or less) raw magnetic values + for (i = 0; i < count; i++) + value += (float) data->magnetic_data[i][index]; + value /= count; + + gain_index = data->gain_indexes[index]; + if (gain_index < 0 || gain_index >= akm8973_gain_coefficient_count) + return -1; + + // Formula to get the magnetic field in uT from the raw magnetic value, HO and coefficient from gain + *axis = ((value - 128 - ((float) data->ho[index] / 16.0f)) * akm8973_gain_coefficient[gain_index]) / 16.0f; + + return 0; +} + +int akm8973_magnetic(struct akm8973_data *data) +{ + int rc; + + if (data == NULL) + return -EINVAL; + + rc = 0; + rc |= akm8973_magnetic_axis(data, 0, &data->magnetic.x); + rc |= akm8973_magnetic_axis(data, 1, &data->magnetic.y); + rc |= akm8973_magnetic_axis(data, 2, &data->magnetic.z); + + return rc; +} + +void *akm8973_thread(void *thread_data) +{ + struct herring_sensors_handlers *handlers = NULL; + struct akm8973_data *data = NULL; + struct input_event event; + struct timeval time; + char i2c_data[SENSOR_DATA_SIZE] = { 0 }; + unsigned char magnetic_data[3] = { 0 }; + int index; + int value; + short mode; + long int before, after; + int diff; + int device_fd; + int uinput_fd; + int rc; + int i, j; + + if (thread_data == NULL) + return NULL; + + handlers = (struct herring_sensors_handlers *) thread_data; + if (handlers->data == NULL) + return NULL; + + data = (struct akm8973_data *) handlers->data; + + device_fd = data->device_fd; + if (device_fd < 0) + return NULL; + + uinput_fd = data->uinput_fd; + if (uinput_fd < 0) + return NULL; + + while (data->thread_continue) { + pthread_mutex_lock(&data->mutex); + if (!data->thread_continue) + break; + + while (handlers->activated) { + gettimeofday(&time, NULL); + before = timestamp(&time); + + mode = AK8973_MODE_MEASURE; + rc = ioctl(device_fd, ECS_IOCTL_SET_MODE, &mode); + if (rc < 0) { + ALOGE("%s: Unable to set akm8973 mode", __func__); + goto next; + } + + memset(&i2c_data, 0, sizeof(i2c_data)); + rc = ioctl(device_fd, ECS_IOCTL_GETDATA, &i2c_data); + if (rc < 0) { + ALOGE("%s: Unable to get akm8973 data", __func__); + goto next; + } + + if (!(i2c_data[0] & 0x01)) { + ALOGE("%s: akm8973 data is not ready", __func__); + goto next; + } + + magnetic_data[0] = (unsigned char) i2c_data[2]; + magnetic_data[1] = (unsigned char) i2c_data[3]; + magnetic_data[2] = (unsigned char) i2c_data[4]; + + rc = akm8973_hdac_calibration(data, (unsigned char *) &magnetic_data, sizeof(magnetic_data)); + if (rc < 0) { + ALOGE("%s: Unable to calibrate akm8973 HDAC", __func__); + goto next; + } + + index = data->magnetic_data_index; + + data->magnetic_data[index][0] = magnetic_data[0]; + data->magnetic_data[index][1] = magnetic_data[1]; + data->magnetic_data[index][2] = magnetic_data[2]; + + data->magnetic_data_index = (index + 1) % 4; + data->magnetic_data_count++; + + rc = akm8973_ho_calibration(data, (unsigned char *) &magnetic_data, sizeof(magnetic_data)); + if (rc < 0) { + ALOGE("%s: Unable to calibrate akm8973 HO", __func__); + goto next; + } + + rc = akm8973_magnetic(data); + if (rc < 0) { + ALOGE("%s: Unable to get akm8973 magnetic", __func__); + goto next; + } + + input_event_set(&event, EV_REL, REL_X, (int) (data->magnetic.x * 1000)); + write(uinput_fd, &event, sizeof(event)); + input_event_set(&event, EV_REL, REL_Y, (int) (data->magnetic.y * 1000)); + write(uinput_fd, &event, sizeof(event)); + input_event_set(&event, EV_REL, REL_Z, (int) (data->magnetic.z * 1000)); + write(uinput_fd, &event, sizeof(event)); + input_event_set(&event, EV_SYN, 0, 0); + write(uinput_fd, &event, sizeof(event)); + +next: + gettimeofday(&time, NULL); + after = timestamp(&time); + + diff = (int) (data->delay - (after - before)) / 1000; + if (diff <= 0) + continue; + + usleep(diff); + } + } + return NULL; +} + +int akm8973_init(struct herring_sensors_handlers *handlers, + struct herring_sensors_device *device) +{ + struct akm8973_data *data = NULL; + pthread_attr_t thread_attr; + char i2c_data[RWBUF_SIZE] = { 0 }; + char mode; + int device_fd = -1; + int uinput_fd = -1; + int input_fd = -1; + int rc; + int i; + + ALOGD("%s(%p, %p)", __func__, handlers, device); + + if (handlers == NULL || device == NULL) + return -EINVAL; + + data = (struct akm8973_data *) calloc(1, sizeof(struct akm8973_data)); + + for (i = 0; i < device->handlers_count; i++) { + if (device->handlers[i] == NULL) + continue; + + if (device->handlers[i]->handle == SENSOR_TYPE_ORIENTATION) + data->orientation_sensor = device->handlers[i]; + } + + device_fd = open("/dev/akm8973", O_RDONLY); + if (device_fd < 0) { + ALOGE("%s: Unable to open device", __func__); + goto error; + } + + rc = ioctl(device_fd, ECS_IOCTL_RESET, NULL); + if (rc < 0) { + ALOGE("%s: Unable to reset akm8973", __func__); + goto error; + } + + mode = AK8973_MODE_E2P_READ; + rc = ioctl(device_fd, ECS_IOCTL_SET_MODE, &mode); + if (rc < 0) { + ALOGE("%s: Unable to set akm8973 mode", __func__); + goto error; + } + + i2c_data[0] = 3; + i2c_data[1] = AK8973_EEP_EHXGA; + rc = ioctl(device_fd, ECS_IOCTL_READ, &i2c_data); + if (rc < 0) { + ALOGE("%s: Unable to read akm8973 EEPROM data", __func__); + goto error; + } + + data->gain_indexes[0] = (i2c_data[1] & 0xf0) >> 4; + data->gain_indexes[1] = (i2c_data[2] & 0xf0) >> 4; + data->gain_indexes[2] = (i2c_data[3] & 0xf0) >> 4; + + ALOGD("AKM8973 gain indexes are: (%d, %d, %d)", data->gain_indexes[0], + data->gain_indexes[1], data->gain_indexes[2]); + + mode = AK8973_MODE_POWERDOWN; + rc = ioctl(device_fd, ECS_IOCTL_SET_MODE, &mode); + if (rc < 0) { + ALOGE("%s: Unable to set akm8973 mode", __func__); + goto error; + } + + i2c_data[5] = i2c_data[1] & 0x0f; + i2c_data[6] = i2c_data[2] & 0x0f; + i2c_data[7] = i2c_data[3] & 0x0f; + + i2c_data[0] = 7; + i2c_data[1] = AK8973_REG_HXDA; + + i2c_data[2] = 0; + i2c_data[3] = 0; + i2c_data[4] = 0; + + rc = ioctl(device_fd, ECS_IOCTL_WRITE, &i2c_data); + if (rc < 0) { + ALOGE("%s: Unable to write akm8973 data", __func__); + goto error; + } + + uinput_fd = uinput_rel_create("magnetic"); + if (uinput_fd < 0) { + ALOGD("%s: Unable to create uinput", __func__); + goto error; + } + + input_fd = input_open("magnetic"); + if (input_fd < 0) { + ALOGE("%s: Unable to open magnetic input", __func__); + goto error; + } + + data->thread_continue = 1; + + pthread_mutex_init(&data->mutex, NULL); + pthread_mutex_lock(&data->mutex); + + pthread_attr_init(&thread_attr); + pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); + + rc = pthread_create(&data->thread, &thread_attr, akm8973_thread, (void *) handlers); + if (rc < 0) { + ALOGE("%s: Unable to create akm8973 thread", __func__); + pthread_mutex_destroy(&data->mutex); + goto error; + } + + data->device_fd = device_fd; + data->uinput_fd = uinput_fd; + handlers->poll_fd = input_fd; + handlers->data = (void *) data; + + return 0; + +error: + if (data != NULL) + free(data); + + if (uinput_fd >= 0) + close(uinput_fd); + + if (input_fd >= 0) + close(input_fd); + + if (device_fd >= 0) + close(device_fd); + + handlers->poll_fd = -1; + handlers->data = NULL; + + return -1; +} + +int akm8973_deinit(struct herring_sensors_handlers *handlers) +{ + struct akm8973_data *data = NULL; + char mode; + int rc; + + ALOGD("%s(%p)", __func__, handlers); + + if (handlers == NULL || handlers->data == NULL) + return -EINVAL; + + data = (struct akm8973_data *) handlers->data; + + handlers->activated = 0; + data->thread_continue = 0; + pthread_mutex_unlock(&data->mutex); + + pthread_mutex_destroy(&data->mutex); + + if (data->uinput_fd >= 0) { + uinput_destroy(data->uinput_fd); + close(data->uinput_fd); + } + data->uinput_fd = -1; + + if (handlers->poll_fd >= 0) + close(handlers->poll_fd); + handlers->poll_fd = -1; + + mode = AK8973_MODE_POWERDOWN; + rc = ioctl(data->device_fd, ECS_IOCTL_SET_MODE, &mode); + if (rc < 0) + ALOGE("%s: Unable to set akm8973 mode", __func__); + + if (data->device_fd >= 0) + close(data->device_fd); + data->device_fd = -1; + + free(handlers->data); + handlers->data = NULL; + + return 0; +} + +int akm8973_activate(struct herring_sensors_handlers *handlers) +{ + struct akm8973_data *data; + int rc; + + ALOGD("%s(%p)", __func__, handlers); + + if (handlers == NULL || handlers->data == NULL) + return -EINVAL; + + data = (struct akm8973_data *) handlers->data; + + rc = akm8973_config_read(data); + if (rc < 0) { + ALOGE("%s: Unable to read akm8973 config", __func__); + } else if (rc > 0) { + rc = akm8973_hdac(data); + if (rc < 0) { + ALOGE("%s: Unable to set akm8973 HDAC", __func__); + return -1; + } + } + + handlers->activated = 1; + pthread_mutex_unlock(&data->mutex); + + return 0; +} + +int akm8973_deactivate(struct herring_sensors_handlers *handlers) +{ + struct akm8973_data *data; + int device_fd; + char mode; + int empty; + int rc; + int i; + + ALOGD("%s(%p)", __func__, handlers); + + if (handlers == NULL || handlers->data == NULL) + return -EINVAL; + + data = (struct akm8973_data *) handlers->data; + + empty = 1; + + for (i = 0; i < (ssize_t) sizeof(data->magnetic_extrema) / 2; i++) { + if (data->magnetic_extrema[0][i] != 0 || data->magnetic_extrema[1][i] != 0) { + empty = 0; + break; + } + } + + if (!empty) { + rc = akm8973_config_write(data); + if (rc < 0) + ALOGE("%s: Unable to write akm8973 config", __func__); + } + + device_fd = data->device_fd; + if (device_fd < 0) + return -1; + + mode = AK8973_MODE_POWERDOWN; + rc = ioctl(data->device_fd, ECS_IOCTL_SET_MODE, &mode); + if (rc < 0) + ALOGE("%s: Unable to set akm8973 mode", __func__); + + handlers->activated = 0; + + return 0; +} + +int akm8973_set_delay(struct herring_sensors_handlers *handlers, long int delay) +{ + struct akm8973_data *data; + + ALOGD("%s(%p, %ld)", __func__, handlers, delay); + + if (handlers == NULL || handlers->data == NULL) + return -EINVAL; + + data = (struct akm8973_data *) handlers->data; + + data->delay = delay; + + return 0; +} + +float akm8973_convert(int value) +{ + return (float) value / -1000.0f; +} + +int akm8973_get_data(struct herring_sensors_handlers *handlers, + struct sensors_event_t *event) +{ + struct akm8973_data *data; + struct input_event input_event; + int input_fd; + int rc; + +// ALOGD("%s(%p, %p)", __func__, handlers, event); + + if (handlers == NULL || handlers->data == NULL || event == NULL) + return -EINVAL; + + data = (struct akm8973_data *) handlers->data; + + input_fd = handlers->poll_fd; + if (input_fd < 0) + 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->magnetic.status = SENSOR_STATUS_ACCURACY_MEDIUM; + + do { + rc = read(input_fd, &input_event, sizeof(input_event)); + if (rc < (int) sizeof(input_event)) + break; + + if (input_event.type == EV_REL) { + switch (input_event.code) { + case REL_X: + event->magnetic.y = akm8973_convert(input_event.value); + break; + case REL_Y: + event->magnetic.x = akm8973_convert(input_event.value); + break; + case REL_Z: + event->magnetic.z = akm8973_convert(input_event.value); + break; + default: + continue; + } + } 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); + + if (data->orientation_sensor != NULL) + orientation_fill(data->orientation_sensor, NULL, &event->magnetic); + + return 0; +} + +struct herring_sensors_handlers akm8973 = { + .name = "AKM8973", + .handle = SENSOR_TYPE_MAGNETIC_FIELD, + .init = akm8973_init, + .deinit = akm8973_deinit, + .activate = akm8973_activate, + .deactivate = akm8973_deactivate, + .set_delay = akm8973_set_delay, + .get_data = akm8973_get_data, + .activated = 0, + .needed = 0, + .poll_fd = -1, + .data = NULL, +}; diff --git a/sensors/gp2a_light.c b/sensors/gp2a_light.c new file mode 100644 index 0000000..10388cd --- /dev/null +++ b/sensors/gp2a_light.c @@ -0,0 +1,239 @@ +/* + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdlib.h> +#include <unistd.h> +#include <stdint.h> +#include <fcntl.h> +#include <errno.h> +#include <math.h> +#include <sys/types.h> +#include <linux/ioctl.h> +#include <linux/input.h> + +#include <hardware/sensors.h> +#include <hardware/hardware.h> + +#define LOG_TAG "herring_sensors" +#include <utils/Log.h> + +#include "herring_sensors.h" + +struct gp2a_light_data { + char path_enable[PATH_MAX]; + char path_delay[PATH_MAX]; +}; + +int gp2a_light_init(struct herring_sensors_handlers *handlers, + struct herring_sensors_device *device) +{ + struct gp2a_light_data *data = NULL; + char path[PATH_MAX] = { 0 }; + int input_fd = -1; + int rc; + + ALOGD("%s(%p, %p)", __func__, handlers, device); + + if (handlers == NULL) + return -EINVAL; + + data = (struct gp2a_light_data *) calloc(1, sizeof(struct gp2a_light_data)); + + input_fd = input_open("lightsensor-level"); + if (input_fd < 0) { + ALOGE("%s: Unable to open input", __func__); + goto error; + } + + rc = sysfs_path_prefix("lightsensor-level", (char *) &path); + if (rc < 0 || path[0] == '\0') { + ALOGE("%s: Unable to open sysfs", __func__); + goto error; + } + + snprintf(data->path_enable, PATH_MAX, "%s/enable", path); + snprintf(data->path_delay, PATH_MAX, "%s/poll_delay", path); + + handlers->poll_fd = input_fd; + handlers->data = (void *) data; + + return 0; + +error: + if (data != NULL) + free(data); + + if (input_fd >= 0) + close(input_fd); + + handlers->poll_fd = -1; + handlers->data = NULL; + + return -1; +} + +int gp2a_light_deinit(struct herring_sensors_handlers *handlers) +{ + ALOGD("%s(%p)", __func__, handlers); + + if (handlers == NULL) + return -EINVAL; + + 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; +} + +int gp2a_light_activate(struct herring_sensors_handlers *handlers) +{ + struct gp2a_light_data *data; + int rc; + + ALOGD("%s(%p)", __func__, handlers); + + if (handlers == NULL || handlers->data == NULL) + return -EINVAL; + + data = (struct gp2a_light_data *) handlers->data; + + rc = sysfs_value_write(data->path_enable, 1); + if (rc < 0) { + ALOGE("%s: Unable to write sysfs value", __func__); + return -1; + } + + handlers->activated = 1; + + return 0; +} + +int gp2a_light_deactivate(struct herring_sensors_handlers *handlers) +{ + struct gp2a_light_data *data; + int rc; + + ALOGD("%s(%p)", __func__, handlers); + + if (handlers == NULL || handlers->data == NULL) + return -EINVAL; + + data = (struct gp2a_light_data *) handlers->data; + + rc = sysfs_value_write(data->path_enable, 0); + if (rc < 0) { + ALOGE("%s: Unable to write sysfs value", __func__); + return -1; + } + + handlers->activated = 1; + + return 0; +} + +int gp2a_light_set_delay(struct herring_sensors_handlers *handlers, long int delay) +{ + struct gp2a_light_data *data; + int rc; + + ALOGD("%s(%p, %ld)", __func__, handlers, delay); + + if (handlers == NULL || handlers->data == NULL) + return -EINVAL; + + data = (struct gp2a_light_data *) handlers->data; + + rc = sysfs_value_write(data->path_delay, (int) delay); + if (rc < 0) { + ALOGE("%s: Unable to write sysfs value", __func__); + return -1; + } + + return 0; +} + +float gp2a_light_convert(int value) +{ + // Converting the raw value to lux units is done with: + // I = 10 * log(light) uA + // U = raw * 3300 / 4095 (max ADC value is 3.3V for 4095 LSB) + // R = 47kOhm + // => light = 10 ^ (I / 10) = 10 ^ (U / R / 10) + // => light = 10 ^ (raw * 330 / 4095 / 47) + // Only 1/4 of light reaches the sensor: + // => light = 4 * (10 ^ (raw * 330 / 4095 / 47)) + + return powf(10, value * (330.0f / 4095.0f / 47.0f)) * 4; +} + +int gp2a_light_get_data(struct herring_sensors_handlers *handlers, + struct sensors_event_t *event) +{ + struct input_event input_event; + int input_fd; + int rc; + +// ALOGD("%s(%p, %p)", __func__, handlers, event); + + if (handlers == NULL || event == NULL) + return -EINVAL; + + input_fd = handlers->poll_fd; + if (input_fd < 0) + return -EINVAL; + + memset(event, 0, sizeof(struct sensors_event_t)); + event->version = sizeof(struct sensors_event_t); + event->sensor = handlers->handle; + event->type = handlers->handle; + + 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_MISC) + event->light = gp2a_light_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; +} + +struct herring_sensors_handlers gp2a_light = { + .name = "GP2A Light", + .handle = SENSOR_TYPE_LIGHT, + .init = gp2a_light_init, + .deinit = gp2a_light_deinit, + .activate = gp2a_light_activate, + .deactivate = gp2a_light_deactivate, + .set_delay = gp2a_light_set_delay, + .get_data = gp2a_light_get_data, + .activated = 0, + .needed = 0, + .poll_fd = -1, + .data = NULL, +}; diff --git a/sensors/gp2a_proximity.c b/sensors/gp2a_proximity.c new file mode 100644 index 0000000..b524814 --- /dev/null +++ b/sensors/gp2a_proximity.c @@ -0,0 +1,213 @@ +/* + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdlib.h> +#include <unistd.h> +#include <stdint.h> +#include <fcntl.h> +#include <errno.h> +#include <sys/types.h> +#include <linux/ioctl.h> +#include <linux/input.h> + +#include <hardware/sensors.h> +#include <hardware/hardware.h> + +#define LOG_TAG "herring_sensors" +#include <utils/Log.h> + +#include "herring_sensors.h" + +struct gp2a_proximity_data { + char path_enable[PATH_MAX]; +}; + +int gp2a_proximity_init(struct herring_sensors_handlers *handlers, + struct herring_sensors_device *device) +{ + struct gp2a_proximity_data *data = NULL; + char path[PATH_MAX] = { 0 }; + int input_fd = -1; + int rc; + + ALOGD("%s(%p, %p)", __func__, handlers, device); + + if (handlers == NULL) + return -EINVAL; + + data = (struct gp2a_proximity_data *) calloc(1, sizeof(struct gp2a_proximity_data)); + + input_fd = input_open("proximity"); + if (input_fd < 0) { + ALOGE("%s: Unable to open input", __func__); + goto error; + } + + rc = sysfs_path_prefix("proximity", (char *) &path); + if (rc < 0 || path[0] == '\0') { + ALOGE("%s: Unable to open sysfs", __func__); + goto error; + } + + snprintf(data->path_enable, PATH_MAX, "%s/enable", path); + + handlers->poll_fd = input_fd; + handlers->data = (void *) data; + + return 0; + +error: + if (data != NULL) + free(data); + + if (input_fd >= 0) + close(input_fd); + + handlers->poll_fd = -1; + handlers->data = NULL; + + return -1; +} + +int gp2a_proximity_deinit(struct herring_sensors_handlers *handlers) +{ + ALOGD("%s(%p)", __func__, handlers); + + if (handlers == NULL) + return -EINVAL; + + 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; +} + +int gp2a_proximity_activate(struct herring_sensors_handlers *handlers) +{ + struct gp2a_proximity_data *data; + int rc; + + ALOGD("%s(%p)", __func__, handlers); + + if (handlers == NULL || handlers->data == NULL) + return -EINVAL; + + data = (struct gp2a_proximity_data *) handlers->data; + + rc = sysfs_value_write(data->path_enable, 1); + if (rc < 0) { + ALOGE("%s: Unable to write sysfs value", __func__); + return -1; + } + + handlers->activated = 1; + + return 0; +} + +int gp2a_proximity_deactivate(struct herring_sensors_handlers *handlers) +{ + struct gp2a_proximity_data *data; + int rc; + + ALOGD("%s(%p)", __func__, handlers); + + if (handlers == NULL || handlers->data == NULL) + return -EINVAL; + + data = (struct gp2a_proximity_data *) handlers->data; + + rc = sysfs_value_write(data->path_enable, 0); + if (rc < 0) { + ALOGE("%s: Unable to write sysfs value", __func__); + return -1; + } + + handlers->activated = 1; + + return 0; +} + +int gp2a_proximity_set_delay(struct herring_sensors_handlers *handlers, long int delay) +{ + ALOGD("%s(%p, %ld)", __func__, handlers, delay); + + return 0; +} + +float gp2a_proximity_convert(int value) +{ + return (float) value * 5.0f; +} + +int gp2a_proximity_get_data(struct herring_sensors_handlers *handlers, + struct sensors_event_t *event) +{ + struct input_event input_event; + int input_fd; + int rc; + +// ALOGD("%s(%p, %p)", __func__, handlers, event); + + if (handlers == NULL || event == NULL) + return -EINVAL; + + input_fd = handlers->poll_fd; + if (input_fd < 0) + return -EINVAL; + + memset(event, 0, sizeof(struct sensors_event_t)); + event->version = sizeof(struct sensors_event_t); + event->sensor = handlers->handle; + event->type = handlers->handle; + + 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; +} + +struct herring_sensors_handlers gp2a_proximity = { + .name = "GP2A Proximity", + .handle = SENSOR_TYPE_PROXIMITY, + .init = gp2a_proximity_init, + .deinit = gp2a_proximity_deinit, + .activate = gp2a_proximity_activate, + .deactivate = gp2a_proximity_deactivate, + .set_delay = gp2a_proximity_set_delay, + .get_data = gp2a_proximity_get_data, + .activated = 0, + .needed = 0, + .poll_fd = -1, + .data = NULL, +}; diff --git a/sensors/herring_sensors.c b/sensors/herring_sensors.c new file mode 100644 index 0000000..362bbe1 --- /dev/null +++ b/sensors/herring_sensors.c @@ -0,0 +1,289 @@ +/* + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdlib.h> +#include <unistd.h> +#include <stdint.h> +#include <fcntl.h> +#include <errno.h> +#include <poll.h> +#include <sys/select.h> +#include <hardware/sensors.h> +#include <hardware/hardware.h> + +#define LOG_TAG "herring_sensors" +#include <utils/Log.h> + +#include "herring_sensors.h" + +/* + * Sensors list + */ + +struct sensor_t herring_sensors[] = { + { "KR3DM Acceleration Sensor", "STMicroelectronics", 1, SENSOR_TYPE_ACCELEROMETER, + SENSOR_TYPE_ACCELEROMETER, 2 * GRAVITY_EARTH, GRAVITY_EARTH / 64.0f / 8.0f, 0.23f, 20000, {}, }, + { "AKM8973 Magnetic Sensor", "Asahi Kasei", 1, SENSOR_TYPE_MAGNETIC_FIELD, + SENSOR_TYPE_MAGNETIC_FIELD, 2000.0f, 1.0f / 16, 6.8f, 16667, {}, }, + { "Orientation Sensor", "Herring Sensors", 1, SENSOR_TYPE_ORIENTATION, + SENSOR_TYPE_ORIENTATION, 360.0f, 0.1f, 0.0f, 16667, {}, }, + { "GP2A Light Sensor", "Sharp", 1, SENSOR_TYPE_LIGHT, + SENSOR_TYPE_LIGHT, 3626657.75f, 1.0f, 0.75f, 0, {}, }, // 3626657.75 = (10 ^ (280.0f / 47.0f)) * 4 + { "GP2A Proximity Sensor", "Sharp", 1, SENSOR_TYPE_PROXIMITY, + SENSOR_TYPE_PROXIMITY, 5.0f, 5.0f, 0.75f, 0, {}, }, + { "K3G Gyroscope Sensor", "STMicroelectronics", 1, SENSOR_TYPE_GYROSCOPE, + SENSOR_TYPE_GYROSCOPE, 2000.0f * (3.1415926535f / 180.0f), (70.0f / 1000.0f) * (3.1415926535f / 180.0f), 6.1f, 1190, {}, }, +}; + +int herring_sensors_count = sizeof(herring_sensors) / sizeof(struct sensor_t); + +struct herring_sensors_handlers *herring_sensors_handlers[] = { + &kr3dm, + &akm8973, + &orientation, + &gp2a_light, + &gp2a_proximity, + &k3g, +}; + +int herring_sensors_handlers_count = sizeof(herring_sensors_handlers) / + sizeof(struct herring_sensors_handlers *); + +/* + * Herring Sensors + */ + +int herring_sensors_activate(struct sensors_poll_device_t *dev, int handle, + int enabled) +{ + struct herring_sensors_device *device; + int i; + + ALOGD("%s(%p, %d, %d)", __func__, dev, handle, enabled); + + if (dev == NULL) + return -EINVAL; + + device = (struct herring_sensors_device *) dev; + + if (device->handlers == NULL || device->handlers_count <= 0) + return -EINVAL; + + for (i = 0; i < device->handlers_count; i++) { + if (device->handlers[i] == NULL) + continue; + + if (device->handlers[i]->handle == handle) { + if (enabled && device->handlers[i]->activate != NULL) { + device->handlers[i]->needed |= HERRING_SENSORS_NEEDED_API; + if (device->handlers[i]->needed == HERRING_SENSORS_NEEDED_API) + return device->handlers[i]->activate(device->handlers[i]); + else + return 0; + } else if (!enabled && device->handlers[i]->deactivate != NULL) { + device->handlers[i]->needed &= ~HERRING_SENSORS_NEEDED_API; + if (device->handlers[i]->needed == 0) + return device->handlers[i]->deactivate(device->handlers[i]); + else + return 0; + } + } + } + + return -1; +} + +int herring_sensors_set_delay(struct sensors_poll_device_t *dev, int handle, + int64_t ns) +{ + struct herring_sensors_device *device; + int i; + + ALOGD("%s(%p, %d, %ld)", __func__, dev, handle, (long int) ns); + + if (dev == NULL) + return -EINVAL; + + device = (struct herring_sensors_device *) dev; + + if (device->handlers == NULL || device->handlers_count <= 0) + return -EINVAL; + + for (i = 0; i < device->handlers_count; i++) { + if (device->handlers[i] == NULL) + continue; + + if (device->handlers[i]->handle == handle && device->handlers[i]->set_delay != NULL) + return device->handlers[i]->set_delay(device->handlers[i], (long int) ns); + } + + return 0; +} + +int herring_sensors_poll(struct sensors_poll_device_t *dev, + struct sensors_event_t* data, int count) +{ + struct herring_sensors_device *device; + int i, j; + int c, n; + int poll_rc, rc; + +// ALOGD("%s(%p, %p, %d)", __func__, dev, data, count); + + if (dev == NULL) + return -EINVAL; + + device = (struct herring_sensors_device *) dev; + + if (device->handlers == NULL || device->handlers_count <= 0 || + device->poll_fds == NULL || device->poll_fds_count <= 0) + return -EINVAL; + + n = 0; + + do { + poll_rc = poll(device->poll_fds, device->poll_fds_count, n > 0 ? 0 : -1); + if (poll_rc < 0) + return -1; + + for (i = 0; i < device->poll_fds_count; i++) { + if (!(device->poll_fds[i].revents & POLLIN)) + continue; + + for (j = 0; j < device->handlers_count; j++) { + if (device->handlers[j] == NULL || device->handlers[j]->poll_fd != device->poll_fds[i].fd || device->handlers[j]->get_data == NULL) + continue; + + rc = device->handlers[j]->get_data(device->handlers[j], &data[n]); + if (rc < 0) { + device->poll_fds[i].revents = 0; + poll_rc = -1; + } else { + n++; + count--; + } + } + } + } while ((poll_rc > 0 || n < 1) && count > 0); + + return n; +} + +/* + * Interface + */ + +int herring_sensors_close(hw_device_t *device) +{ + struct herring_sensors_device *herring_sensors_device; + int i; + + ALOGD("%s(%p)", __func__, device); + + if (device == NULL) + return -EINVAL; + + herring_sensors_device = (struct herring_sensors_device *) device; + + if (herring_sensors_device->poll_fds != NULL) + free(herring_sensors_device->poll_fds); + + for (i = 0; i < herring_sensors_device->handlers_count; i++) { + if (herring_sensors_device->handlers[i] == NULL || herring_sensors_device->handlers[i]->deinit == NULL) + continue; + + herring_sensors_device->handlers[i]->deinit(herring_sensors_device->handlers[i]); + } + + free(device); + + return 0; +} + +int herring_sensors_open(const struct hw_module_t* module, const char *id, + struct hw_device_t** device) +{ + struct herring_sensors_device *herring_sensors_device; + int p, i; + + ALOGD("%s(%p, %s, %p)", __func__, module, id, device); + + if (module == NULL || device == NULL) + return -EINVAL; + + herring_sensors_device = (struct herring_sensors_device *) + calloc(1, sizeof(struct herring_sensors_device)); + herring_sensors_device->device.common.tag = HARDWARE_DEVICE_TAG; + herring_sensors_device->device.common.version = 0; + herring_sensors_device->device.common.module = (struct hw_module_t *) module; + herring_sensors_device->device.common.close = herring_sensors_close; + herring_sensors_device->device.activate = herring_sensors_activate; + herring_sensors_device->device.setDelay = herring_sensors_set_delay; + herring_sensors_device->device.poll = herring_sensors_poll; + herring_sensors_device->handlers = herring_sensors_handlers; + herring_sensors_device->handlers_count = herring_sensors_handlers_count; + herring_sensors_device->poll_fds = (struct pollfd *) + calloc(1, herring_sensors_handlers_count * sizeof(struct pollfd)); + + p = 0; + for (i = 0; i < herring_sensors_handlers_count; i++) { + if (herring_sensors_handlers[i] == NULL || herring_sensors_handlers[i]->init == NULL) + continue; + + herring_sensors_handlers[i]->init(herring_sensors_handlers[i], herring_sensors_device); + if (herring_sensors_handlers[i]->poll_fd >= 0) { + herring_sensors_device->poll_fds[p].fd = herring_sensors_handlers[i]->poll_fd; + herring_sensors_device->poll_fds[p].events = POLLIN; + p++; + } + } + + herring_sensors_device->poll_fds_count = p; + + *device = &(herring_sensors_device->device.common); + + return 0; +} + +int herring_sensors_get_sensors_list(struct sensors_module_t* module, + const struct sensor_t **sensors_p) +{ + ALOGD("%s(%p, %p)", __func__, module, sensors_p); + + if (sensors_p == NULL) + return -EINVAL; + + *sensors_p = herring_sensors; + return herring_sensors_count; +} + +struct hw_module_methods_t herring_sensors_module_methods = { + .open = herring_sensors_open, +}; + +struct sensors_module_t HAL_MODULE_INFO_SYM = { + .common = { + .tag = HARDWARE_MODULE_TAG, + .version_major = 1, + .version_minor = 0, + .id = SENSORS_HARDWARE_MODULE_ID, + .name = "Herring Sensors", + .author = "Paul Kocialkowski", + .methods = &herring_sensors_module_methods, + }, + .get_sensors_list = herring_sensors_get_sensors_list, +}; diff --git a/sensors/herring_sensors.h b/sensors/herring_sensors.h new file mode 100644 index 0000000..4037c23 --- /dev/null +++ b/sensors/herring_sensors.h @@ -0,0 +1,104 @@ +/* + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdint.h> +#include <poll.h> +#include <linux/input.h> + +#include <hardware/sensors.h> +#include <hardware/hardware.h> + +#ifndef _HERRING_SENSORS_H_ +#define _HERRING_SENSORS_H_ + +#define HERRING_SENSORS_NEEDED_API (1 << 0) +#define HERRING_SENSORS_NEEDED_ORIENTATION (1 << 1) + +struct herring_sensors_device; + +struct herring_sensors_handlers { + char *name; + int handle; + + int (*init)(struct herring_sensors_handlers *handlers, + struct herring_sensors_device *device); + int (*deinit)(struct herring_sensors_handlers *handlers); + int (*activate)(struct herring_sensors_handlers *handlers); + int (*deactivate)(struct herring_sensors_handlers *handlers); + int (*set_delay)(struct herring_sensors_handlers *handlers, + long int delay); + int (*get_data)(struct herring_sensors_handlers *handlers, + struct sensors_event_t *event); + + int activated; + int needed; + int poll_fd; + + void *data; +}; + +struct herring_sensors_device { + struct sensors_poll_device_t device; + + struct herring_sensors_handlers **handlers; + int handlers_count; + + struct pollfd *poll_fds; + int poll_fds_count; +}; + +extern struct herring_sensors_handlers *herring_sensors_handlers[]; +extern int herring_sensors_handlers_count; + +int herring_sensors_activate(struct sensors_poll_device_t *dev, int handle, + int enabled); +int herring_sensors_set_delay(struct sensors_poll_device_t *dev, int handle, + int64_t ns); +int herring_sensors_poll(struct sensors_poll_device_t *dev, + struct sensors_event_t* data, int count); + +/* + * Input + */ + +void input_event_set(struct input_event *event, int type, int code, int value); +long int timestamp(struct timeval *time); +long int input_timestamp(struct input_event *event); +int uinput_rel_create(const char *name); +void uinput_destroy(int uinput_fd); +int input_open(char *name); +int sysfs_path_prefix(char *name, char *path_prefix); +int sysfs_value_read(char *path); +int sysfs_value_write(char *path, int value); +int sysfs_string_read(char *path, char *buffer, size_t length); +int sysfs_string_write(char *path, char *buffer, size_t length); + +/* + * Sensors + */ + +int orientation_fill(struct herring_sensors_handlers *handlers, + sensors_vec_t *acceleration, sensors_vec_t *magnetic); + +extern struct herring_sensors_handlers kr3dm; +extern struct herring_sensors_handlers akm8973; +extern struct herring_sensors_handlers orientation; +extern struct herring_sensors_handlers gp2a_light; +extern struct herring_sensors_handlers gp2a_proximity; +extern struct herring_sensors_handlers k3g; + +#endif diff --git a/sensors/input.c b/sensors/input.c new file mode 100644 index 0000000..2022f21 --- /dev/null +++ b/sensors/input.c @@ -0,0 +1,335 @@ +/* + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdlib.h> +#include <unistd.h> +#include <stdint.h> +#include <fcntl.h> +#include <errno.h> +#include <dirent.h> +#include <linux/ioctl.h> +#include <linux/input.h> +#include <linux/uinput.h> + +#define LOG_TAG "herring_sensors" +#include <utils/Log.h> + +#include "herring_sensors.h" + +void input_event_set(struct input_event *event, int type, int code, int value) +{ + if (event == NULL) + return; + + memset(event, 0, sizeof(struct input_event)); + + event->type = type, + event->code = code; + event->value = value; + + gettimeofday(&event->time, NULL); +} + +long int timestamp(struct timeval *time) +{ + if (time == NULL) + return -1; + + return time->tv_sec * 1000000000LL + time->tv_usec * 1000; +} + +long int input_timestamp(struct input_event *event) +{ + if (event == NULL) + return -1; + + return timestamp(&event->time); +} + +int uinput_rel_create(const char *name) +{ + struct uinput_user_dev uinput_dev; + int uinput_fd; + int rc; + + if (name == NULL) + return -1; + + uinput_fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK); + if (uinput_fd < 0) { + ALOGE("%s: Unable to open uinput device", __func__); + goto error; + } + + memset(&uinput_dev, 0, sizeof(uinput_dev)); + + strncpy(uinput_dev.name, name, sizeof(uinput_dev.name)); + uinput_dev.id.bustype = BUS_I2C; + uinput_dev.id.vendor = 0; + uinput_dev.id.product = 0; + uinput_dev.id.version = 0; + + rc = 0; + rc |= ioctl(uinput_fd, UI_SET_EVBIT, EV_REL); + rc |= ioctl(uinput_fd, UI_SET_RELBIT, REL_X); + rc |= ioctl(uinput_fd, UI_SET_RELBIT, REL_Y); + rc |= ioctl(uinput_fd, UI_SET_RELBIT, REL_Z); + rc |= ioctl(uinput_fd, UI_SET_EVBIT, EV_SYN); + + if (rc < 0) { + ALOGE("%s: Unable to set uinput bits", __func__); + goto error; + } + + rc = write(uinput_fd, &uinput_dev, sizeof(uinput_dev)); + if (rc < 0) { + ALOGE("%s: Unable to write uinput device", __func__); + goto error; + } + + rc = ioctl(uinput_fd, UI_DEV_CREATE); + if (rc < 0) { + ALOGE("%s: Unable to create uinput device", __func__); + goto error; + } + + usleep(3000); + + return uinput_fd; + +error: + if (uinput_fd >= 0) + close(uinput_fd); + + return -1; +} + +void uinput_destroy(int uinput_fd) +{ + if (uinput_fd < 0) + return; + + ioctl(uinput_fd, UI_DEV_DESTROY); +} + +int input_open(char *name) +{ + DIR *d; + struct dirent *di; + + char input_name[80] = { 0 }; + char path[PATH_MAX]; + char *c; + int fd; + int rc; + + if (name == NULL) + return -EINVAL; + + d = opendir("/dev/input"); + if (d == NULL) + return -1; + + while ((di = readdir(d))) { + if (di == NULL || strcmp(di->d_name, ".") == 0 || strcmp(di->d_name, "..") == 0) + continue; + + snprintf(path, PATH_MAX, "/dev/input/%s", di->d_name); + fd = open(path, O_RDONLY | O_NONBLOCK); + if (fd < 0) + continue; + + rc = ioctl(fd, EVIOCGNAME(sizeof(input_name) - 1), &input_name); + if (rc < 0) + continue; + + c = strstr((char *) &input_name, "\n"); + if (c != NULL) + *c = '\0'; + + if (strcmp(input_name, name) == 0) + return fd; + else + close(fd); + } + + return -1; +} + +int sysfs_path_prefix(char *name, char *path_prefix) +{ + DIR *d; + struct dirent *di; + + char input_name[80] = { 0 }; + char path[PATH_MAX]; + char *c; + int fd; + + if (name == NULL || path_prefix == NULL) + return -EINVAL; + + d = opendir("/sys/class/input"); + if (d == NULL) + return -1; + + while ((di = readdir(d))) { + if (di == NULL || strcmp(di->d_name, ".") == 0 || strcmp(di->d_name, "..") == 0) + continue; + + snprintf(path, PATH_MAX, "/sys/class/input/%s/name", di->d_name); + + fd = open(path, O_RDONLY); + if (fd < 0) + continue; + + read(fd, &input_name, sizeof(input_name)); + close(fd); + + c = strstr((char *) &input_name, "\n"); + if (c != NULL) + *c = '\0'; + + if (strcmp(input_name, name) == 0) { + snprintf(path_prefix, PATH_MAX, "/sys/class/input/%s", di->d_name); + return 0; + } + } + + return -1; +} + +int sysfs_value_read(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(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; +} + +int sysfs_string_read(char *path, char *buffer, size_t length) +{ + int fd = -1; + int rc; + + if (path == NULL || buffer == NULL || length == 0) + return -1; + + fd = open(path, O_RDONLY); + if (fd < 0) + goto error; + + rc = read(fd, buffer, length); + if (rc <= 0) + goto error; + + rc = 0; + goto complete; + +error: + rc = -1; + +complete: + if (fd >= 0) + close(fd); + + return rc; +} + +int sysfs_string_write(char *path, char *buffer, size_t length) +{ + int fd = -1; + int rc; + + if (path == NULL || buffer == NULL || length == 0) + return -1; + + fd = open(path, O_WRONLY); + if (fd < 0) + goto error; + + rc = write(fd, buffer, length); + if (rc <= 0) + goto error; + + rc = 0; + goto complete; + +error: + rc = -1; + +complete: + if (fd >= 0) + close(fd); + + return rc; +} diff --git a/sensors/k3g.c b/sensors/k3g.c new file mode 100644 index 0000000..5f166f5 --- /dev/null +++ b/sensors/k3g.c @@ -0,0 +1,254 @@ +/* + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdlib.h> +#include <unistd.h> +#include <stdint.h> +#include <fcntl.h> +#include <errno.h> +#include <math.h> +#include <sys/types.h> +#include <linux/ioctl.h> +#include <linux/input.h> + +#include <hardware/sensors.h> +#include <hardware/hardware.h> + +#define LOG_TAG "herring_sensors" +#include <utils/Log.h> + +#include "herring_sensors.h" + +struct k3g_data { + char path_enable[PATH_MAX]; + char path_delay[PATH_MAX]; + + sensors_vec_t gyro; +}; + +int k3g_init(struct herring_sensors_handlers *handlers, + struct herring_sensors_device *device) +{ + struct k3g_data *data = NULL; + char path[PATH_MAX] = { 0 }; + int input_fd = -1; + int rc; + + ALOGD("%s(%p, %p)", __func__, handlers, device); + + if (handlers == NULL) + return -EINVAL; + + data = (struct k3g_data *) calloc(1, sizeof(struct k3g_data)); + + input_fd = input_open("gyro"); + if (input_fd < 0) { + ALOGE("%s: Unable to open input", __func__); + goto error; + } + + rc = sysfs_path_prefix("gyro", (char *) &path); + if (rc < 0 || path[0] == '\0') { + ALOGE("%s: Unable to open sysfs", __func__); + goto error; + } + + snprintf(data->path_enable, PATH_MAX, "%s/enable", path); + snprintf(data->path_delay, PATH_MAX, "%s/poll_delay", path); + + handlers->poll_fd = input_fd; + handlers->data = (void *) data; + + return 0; + +error: + if (data != NULL) + free(data); + + if (input_fd >= 0) + close(input_fd); + + handlers->poll_fd = -1; + handlers->data = NULL; + + return -1; +} + +int k3g_deinit(struct herring_sensors_handlers *handlers) +{ + ALOGD("%s(%p)", __func__, handlers); + + if (handlers == NULL) + return -EINVAL; + + 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; +} + +int k3g_activate(struct herring_sensors_handlers *handlers) +{ + struct k3g_data *data; + int rc; + + ALOGD("%s(%p)", __func__, handlers); + + if (handlers == NULL || handlers->data == NULL) + return -EINVAL; + + data = (struct k3g_data *) handlers->data; + + rc = sysfs_value_write(data->path_enable, 1); + if (rc < 0) { + ALOGE("%s: Unable to write sysfs value", __func__); + return -1; + } + + handlers->activated = 1; + + return 0; +} + +int k3g_deactivate(struct herring_sensors_handlers *handlers) +{ + struct k3g_data *data; + int rc; + + ALOGD("%s(%p)", __func__, handlers); + + if (handlers == NULL || handlers->data == NULL) + return -EINVAL; + + data = (struct k3g_data *) handlers->data; + + rc = sysfs_value_write(data->path_enable, 0); + if (rc < 0) { + ALOGE("%s: Unable to write sysfs value", __func__); + return -1; + } + + handlers->activated = 1; + + return 0; +} + +int k3g_set_delay(struct herring_sensors_handlers *handlers, long int delay) +{ + struct k3g_data *data; + int rc; + + ALOGD("%s(%p, %ld)", __func__, handlers, delay); + + if (handlers == NULL || handlers->data == NULL) + return -EINVAL; + + data = (struct k3g_data *) handlers->data; + + rc = sysfs_value_write(data->path_delay, (int) delay); + if (rc < 0) { + ALOGE("%s: Unable to write sysfs value", __func__); + return -1; + } + + return 0; +} + +float k3g_convert(int value) +{ + return value * (70.0f / 1000.0f) * (3.1415926535f / 180.0f); +} + +int k3g_get_data(struct herring_sensors_handlers *handlers, + struct sensors_event_t *event) +{ + struct k3g_data *data; + struct input_event input_event; + int input_fd; + int rc; + +// ALOGD("%s(%p, %p)", __func__, handlers, event); + + if (handlers == NULL || handlers->data == NULL || event == NULL) + return -EINVAL; + + data = (struct k3g_data *) handlers->data; + + input_fd = handlers->poll_fd; + if (input_fd < 0) + return -EINVAL; + + memset(event, 0, sizeof(struct sensors_event_t)); + event->version = sizeof(struct sensors_event_t); + event->sensor = handlers->handle; + event->type = handlers->handle; + + event->gyro.x = data->gyro.x; + event->gyro.y = data->gyro.y; + event->gyro.z = data->gyro.z; + + do { + rc = read(input_fd, &input_event, sizeof(input_event)); + if (rc < (int) sizeof(input_event)) + break; + + if (input_event.type == EV_REL) { + switch (input_event.code) { + case REL_RX: + event->gyro.y = k3g_convert(input_event.value * -1); + break; + case REL_RY: + event->gyro.x = k3g_convert(input_event.value); + break; + case REL_RZ: + event->gyro.z = k3g_convert(input_event.value); + break; + default: + continue; + } + } 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); + + data->gyro.x = event->gyro.x; + data->gyro.y = event->gyro.y; + data->gyro.z = event->gyro.z; + + return 0; +} + +struct herring_sensors_handlers k3g = { + .name = "K3G", + .handle = SENSOR_TYPE_GYROSCOPE, + .init = k3g_init, + .deinit = k3g_deinit, + .activate = k3g_activate, + .deactivate = k3g_deactivate, + .set_delay = k3g_set_delay, + .get_data = k3g_get_data, + .activated = 0, + .needed = 0, + .poll_fd = -1, + .data = NULL, +}; diff --git a/sensors/kr3dm.c b/sensors/kr3dm.c new file mode 100644 index 0000000..c68c77d --- /dev/null +++ b/sensors/kr3dm.c @@ -0,0 +1,375 @@ +/* + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdlib.h> +#include <unistd.h> +#include <stdint.h> +#include <fcntl.h> +#include <errno.h> + +#include <hardware/sensors.h> +#include <hardware/hardware.h> + +#define LOG_TAG "herring_sensors" +#include <utils/Log.h> + +#include "herring_sensors.h" +#include "kr3dm.h" + +struct kr3dm_data { + struct herring_sensors_handlers *orientation_sensor; + + long int delay; + int device_fd; + int uinput_fd; + + pthread_t thread; + pthread_mutex_t mutex; + int thread_continue; +}; + +void *kr3dm_thread(void *thread_data) +{ + struct herring_sensors_handlers *handlers = NULL; + struct kr3dm_data *data = NULL; + struct input_event event; + struct timeval time; + struct kr3dm_acceldata acceleration_data; + long int before, after; + int diff; + int device_fd; + int uinput_fd; + int rc; + + if (thread_data == NULL) + return NULL; + + handlers = (struct herring_sensors_handlers *) thread_data; + if (handlers->data == NULL) + return NULL; + + data = (struct kr3dm_data *) handlers->data; + + device_fd = data->device_fd; + if (device_fd < 0) + return NULL; + + uinput_fd = data->uinput_fd; + if (uinput_fd < 0) + return NULL; + + while (data->thread_continue) { + pthread_mutex_lock(&data->mutex); + if (!data->thread_continue) + break; + + while (handlers->activated) { + gettimeofday(&time, NULL); + before = timestamp(&time); + + memset(&acceleration_data, 0, sizeof(acceleration_data)); + + rc = ioctl(device_fd, KR3DM_IOCTL_READ_ACCEL_XYZ, &acceleration_data); + if (rc < 0) { + ALOGE("%s: Unable to get kr3dm data", __func__); + return NULL; + } + + input_event_set(&event, EV_REL, REL_X, (int) (acceleration_data.x * 1000)); + write(uinput_fd, &event, sizeof(event)); + input_event_set(&event, EV_REL, REL_Y, (int) (acceleration_data.y * 1000)); + write(uinput_fd, &event, sizeof(event)); + input_event_set(&event, EV_REL, REL_Z, (int) (acceleration_data.z * 1000)); + write(uinput_fd, &event, sizeof(event)); + input_event_set(&event, EV_SYN, 0, 0); + write(uinput_fd, &event, sizeof(event)); + +next: + gettimeofday(&time, NULL); + after = timestamp(&time); + + diff = (int) (data->delay - (after - before)) / 1000; + if (diff <= 0) + continue; + + usleep(diff); + } + } + return NULL; +} + +int kr3dm_init(struct herring_sensors_handlers *handlers, + struct herring_sensors_device *device) +{ + struct kr3dm_data *data = NULL; + pthread_attr_t thread_attr; + int device_fd = -1; + int uinput_fd = -1; + int input_fd = -1; + int rc; + int i; + + ALOGD("%s(%p, %p)", __func__, handlers, device); + + if (handlers == NULL || device == NULL) + return -EINVAL; + + data = (struct kr3dm_data *) calloc(1, sizeof(struct kr3dm_data)); + + for (i = 0; i < device->handlers_count; i++) { + if (device->handlers[i] == NULL) + continue; + + if (device->handlers[i]->handle == SENSOR_TYPE_ORIENTATION) + data->orientation_sensor = device->handlers[i]; + } + + device_fd = open("/dev/accelerometer", O_RDONLY); + if (device_fd < 0) { + ALOGE("%s: Unable to open device", __func__); + goto error; + } + + uinput_fd = uinput_rel_create("acceleration"); + if (uinput_fd < 0) { + ALOGD("%s: Unable to create uinput", __func__); + goto error; + } + + input_fd = input_open("acceleration"); + if (input_fd < 0) { + ALOGE("%s: Unable to open acceleration input", __func__); + goto error; + } + + data->thread_continue = 1; + + pthread_mutex_init(&data->mutex, NULL); + pthread_mutex_lock(&data->mutex); + + pthread_attr_init(&thread_attr); + pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); + + rc = pthread_create(&data->thread, &thread_attr, kr3dm_thread, (void *) handlers); + if (rc < 0) { + ALOGE("%s: Unable to create kr3dm thread", __func__); + pthread_mutex_destroy(&data->mutex); + goto error; + } + + data->device_fd = device_fd; + data->uinput_fd = uinput_fd; + handlers->poll_fd = input_fd; + handlers->data = (void *) data; + + return 0; + +error: + if (data != NULL) + free(data); + + if (uinput_fd >= 0) + close(uinput_fd); + + if (input_fd >= 0) + close(input_fd); + + if (device_fd >= 0) + close(device_fd); + + handlers->poll_fd = -1; + handlers->data = NULL; + + return -1; +} + +int kr3dm_deinit(struct herring_sensors_handlers *handlers) +{ + struct kr3dm_data *data = NULL; + + ALOGD("%s(%p)", __func__, handlers); + + if (handlers == NULL || handlers->data == NULL) + return -EINVAL; + + data = (struct kr3dm_data *) handlers->data; + + handlers->activated = 0; + data->thread_continue = 0; + pthread_mutex_unlock(&data->mutex); + + pthread_mutex_destroy(&data->mutex); + + if (data->uinput_fd >= 0) { + uinput_destroy(data->uinput_fd); + close(data->uinput_fd); + } + data->uinput_fd = -1; + + if (handlers->poll_fd >= 0) + close(handlers->poll_fd); + handlers->poll_fd = -1; + + if (data->device_fd >= 0) + close(data->device_fd); + data->device_fd = -1; + + free(handlers->data); + handlers->data = NULL; + + return 0; +} + +int kr3dm_activate(struct herring_sensors_handlers *handlers) +{ + struct kr3dm_data *data; + + ALOGD("%s(%p)", __func__, handlers); + + if (handlers == NULL || handlers->data == NULL) + return -EINVAL; + + data = (struct kr3dm_data *) handlers->data; + + handlers->activated = 1; + pthread_mutex_unlock(&data->mutex); + + return 0; +} + +int kr3dm_deactivate(struct herring_sensors_handlers *handlers) +{ + struct kr3dm_data *data; + + ALOGD("%s(%p)", __func__, handlers); + + if (handlers == NULL || handlers->data == NULL) + return -EINVAL; + + data = (struct kr3dm_data *) handlers->data; + + handlers->activated = 0; + + return 0; +} + +int kr3dm_set_delay(struct herring_sensors_handlers *handlers, long int delay) +{ + struct kr3dm_data *data; + int64_t d; + int device_fd; + int rc; + + ALOGD("%s(%p, %ld)", __func__, handlers, delay); + + if (handlers == NULL || handlers->data == NULL) + return -EINVAL; + + data = (struct kr3dm_data *) handlers->data; + + device_fd = data->device_fd; + if (device_fd < 0) + return -1; + + d = (int64_t) delay; + rc = ioctl(device_fd, KR3DM_IOCTL_SET_DELAY, &d); + if (rc < 0) { + ALOGE("%s: Unable to set kr3dm delay", __func__); + return -1; + } + + data->delay = delay; + + return 0; +} + +float kr3dm_convert(int value) +{ + return (float) (value / 1000.0f) * GRAVITY_EARTH / 64.0f / 8.0f; +} + +int kr3dm_get_data(struct herring_sensors_handlers *handlers, + struct sensors_event_t *event) +{ + struct kr3dm_data *data; + struct input_event input_event; + int input_fd; + int rc; + +// ALOGD("%s(%p, %p)", __func__, handlers, event); + + if (handlers == NULL || handlers->data == NULL || event == NULL) + return -EINVAL; + + data = (struct kr3dm_data *) handlers->data; + + input_fd = handlers->poll_fd; + if (input_fd < 0) + 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->magnetic.status = SENSOR_STATUS_ACCURACY_MEDIUM; + + do { + rc = read(input_fd, &input_event, sizeof(input_event)); + if (rc < (int) sizeof(input_event)) + break; + + if (input_event.type == EV_REL) { + switch (input_event.code) { + case REL_X: + event->acceleration.y = kr3dm_convert(input_event.value * -1); + break; + case REL_Y: + event->acceleration.x = kr3dm_convert(input_event.value); + break; + case REL_Z: + event->acceleration.z = kr3dm_convert(input_event.value); + break; + default: + continue; + } + } 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); + + if (data->orientation_sensor != NULL) + orientation_fill(data->orientation_sensor, &event->acceleration, NULL); + + return 0; +} + +struct herring_sensors_handlers kr3dm = { + .name = "KR3DM", + .handle = SENSOR_TYPE_ACCELEROMETER, + .init = kr3dm_init, + .deinit = kr3dm_deinit, + .activate = kr3dm_activate, + .deactivate = kr3dm_deactivate, + .set_delay = kr3dm_set_delay, + .get_data = kr3dm_get_data, + .activated = 0, + .needed = 0, + .poll_fd = -1, + .data = NULL, +}; diff --git a/sensors/kr3dm.h b/sensors/kr3dm.h new file mode 100644 index 0000000..d404d28 --- /dev/null +++ b/sensors/kr3dm.h @@ -0,0 +1,46 @@ +/* + * STMicroelectronics kr3dm acceleration sensor driver + * + * Copyright (C) 2010 Samsung Electronics Co.Ltd + * + * 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifndef __KR3DM_ACC_HEADER__ +#define __KR3DM__ACC_HEADER__ + +#include <linux/types.h> +#include <linux/ioctl.h> + +struct kr3dm_acceldata { + __s16 x; + __s16 y; + __s16 z; +}; + +/* dev info */ +#define ACC_DEV_NAME "accelerometer" + +/* kr3dm ioctl command label */ +#define KR3DM_IOCTL_BASE 'a' +#define KR3DM_IOCTL_SET_DELAY _IOW(KR3DM_IOCTL_BASE, 0, int64_t) +#define KR3DM_IOCTL_GET_DELAY _IOR(KR3DM_IOCTL_BASE, 1, int64_t) +#define KR3DM_IOCTL_READ_ACCEL_XYZ _IOR(KR3DM_IOCTL_BASE, 8, \ + struct kr3dm_acceldata) + +#ifdef __KERNEL__ +struct kr3dm_platform_data { + int gpio_acc_int; /* gpio for kr3dm int output */ + s8 *rotation; /* rotation matrix, if NULL assume Id */ +}; +#endif /* __KERNEL__ */ + +#endif diff --git a/sensors/orientation.c b/sensors/orientation.c new file mode 100644 index 0000000..40a7872 --- /dev/null +++ b/sensors/orientation.c @@ -0,0 +1,444 @@ +/* + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdlib.h> +#include <unistd.h> +#include <stddef.h> +#include <fcntl.h> +#include <errno.h> +#include <math.h> +#include <linux/ioctl.h> +#include <linux/uinput.h> +#include <linux/input.h> + +#include <hardware/sensors.h> +#include <hardware/hardware.h> + +#define LOG_TAG "herring_sensors" +#include <utils/Log.h> + +#include "herring_sensors.h" + +struct orientation_data { + struct herring_sensors_handlers *acceleration_sensor; + struct herring_sensors_handlers *magnetic_sensor; + + sensors_vec_t orientation; + sensors_vec_t acceleration; + sensors_vec_t magnetic; + + long int delay; + int uinput_fd; + + pthread_t thread; + pthread_mutex_t mutex; + int thread_continue; +}; + +static float rad2deg(float v) +{ + return (v * 180.0f / 3.1415926535f); +} + +static float vector_scalar(sensors_vec_t *v, sensors_vec_t *d) +{ + return v->x * d->x + v->y * d->y + v->z * d->z; +} + +static float vector_length(sensors_vec_t *v) +{ + return sqrtf(vector_scalar(v, v)); +} + +void orientation_calculate(sensors_vec_t *a, sensors_vec_t *m, sensors_vec_t *o) +{ + float azimuth, pitch, roll; + float la, sinp, cosp, sinr, cosr, x, y; + + if (a == NULL || m == NULL || o == NULL) + return; + + la = vector_length(a); + pitch = asinf(-(a->y) / la); + roll = asinf((a->x) / la); + + sinp = sinf(pitch); + cosp = cosf(pitch); + sinr = sinf(roll); + cosr = cosf(roll); + + y = -(m->x) * cosr + m->z * sinr; + x = m->x * sinp * sinr + m->y * cosp + m->z * sinp * cosr; + azimuth = atan2f(y, x); + + o->azimuth = rad2deg(azimuth); + o->pitch = rad2deg(pitch); + o->roll = rad2deg(roll); + + if (o->azimuth < 0) + o->azimuth += 360.0f; +} + +void *orientation_thread(void *thread_data) +{ + struct herring_sensors_handlers *handlers = NULL; + struct orientation_data *data = NULL; + struct input_event event; + struct timeval time; + long int before, after; + int diff; + int uinput_fd; + + if (thread_data == NULL) + return NULL; + + handlers = (struct herring_sensors_handlers *) thread_data; + if (handlers->data == NULL) + return NULL; + + data = (struct orientation_data *) handlers->data; + + uinput_fd = data->uinput_fd; + if (uinput_fd < 0) + return NULL; + + while (data->thread_continue) { + pthread_mutex_lock(&data->mutex); + if (!data->thread_continue) + break; + + while (handlers->activated) { + gettimeofday(&time, NULL); + before = timestamp(&time); + + orientation_calculate(&data->acceleration, &data->magnetic, &data->orientation); + + input_event_set(&event, EV_REL, REL_X, (int) (data->orientation.azimuth * 1000)); + write(uinput_fd, &event, sizeof(event)); + input_event_set(&event, EV_REL, REL_Y, (int) (data->orientation.pitch * 1000)); + write(uinput_fd, &event, sizeof(event)); + input_event_set(&event, EV_REL, REL_Z, (int) (data->orientation.roll * 1000)); + write(uinput_fd, &event, sizeof(event)); + input_event_set(&event, EV_SYN, 0, 0); + write(uinput_fd, &event, sizeof(event)); + + gettimeofday(&time, NULL); + after = timestamp(&time); + + diff = (int) (data->delay - (after - before)) / 1000; + if (diff <= 0) + continue; + + usleep(diff); + } + } + + return NULL; +} + +int orientation_fill(struct herring_sensors_handlers *handlers, + sensors_vec_t *acceleration, sensors_vec_t *magnetic) +{ + struct orientation_data *data; + +// ALOGD("%s(%p, %p, %p)", __func__, handlers, acceleration, magnetic); + + if (handlers == NULL || handlers->data == NULL) + return -EINVAL; + + data = (struct orientation_data *) handlers->data; + + if (acceleration != NULL) { + data->acceleration.x = acceleration->x; + data->acceleration.y = acceleration->y; + data->acceleration.z = acceleration->z; + } + + if (magnetic != NULL) { + data->magnetic.x = magnetic->x; + data->magnetic.y = magnetic->y; + data->magnetic.z = magnetic->z; + } + + return 0; +} + +int orientation_init(struct herring_sensors_handlers *handlers, + struct herring_sensors_device *device) +{ + struct orientation_data *data = NULL; + pthread_attr_t thread_attr; + int uinput_fd = -1; + int input_fd = -1; + int rc; + int i; + + ALOGD("%s(%p, %p)", __func__, handlers, device); + + if (handlers == NULL || device == NULL) + return -EINVAL; + + data = (struct orientation_data *) calloc(1, sizeof(struct orientation_data)); + + for (i = 0; i < device->handlers_count; i++) { + if (device->handlers[i] == NULL) + continue; + + if (device->handlers[i]->handle == SENSOR_TYPE_ACCELEROMETER) + data->acceleration_sensor = device->handlers[i]; + else if (device->handlers[i]->handle == SENSOR_TYPE_MAGNETIC_FIELD) + data->magnetic_sensor = device->handlers[i]; + } + + if (data->acceleration_sensor == NULL || data->magnetic_sensor == NULL) { + ALOGE("%s: Missing sensors for orientation", __func__); + goto error; + } + + uinput_fd = uinput_rel_create("orientation"); + if (uinput_fd < 0) { + ALOGD("%s: Unable to create uinput", __func__); + goto error; + } + + input_fd = input_open("orientation"); + if (input_fd < 0) { + ALOGE("%s: Unable to open orientation input", __func__); + goto error; + } + + data->thread_continue = 1; + + pthread_mutex_init(&data->mutex, NULL); + pthread_mutex_lock(&data->mutex); + + pthread_attr_init(&thread_attr); + pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); + + rc = pthread_create(&data->thread, &thread_attr, orientation_thread, (void *) handlers); + if (rc < 0) { + ALOGE("%s: Unable to create orientation thread", __func__); + pthread_mutex_destroy(&data->mutex); + goto error; + } + + data->uinput_fd = uinput_fd; + handlers->poll_fd = input_fd; + handlers->data = (void *) data; + + return 0; + +error: + if (data != NULL) + free(data); + + if (uinput_fd >= 0) + close(uinput_fd); + + if (input_fd >= 0) + close(input_fd); + + handlers->poll_fd = -1; + handlers->data = NULL; + + return -1; +} + +int orientation_deinit(struct herring_sensors_handlers *handlers) +{ + struct orientation_data *data; + + ALOGD("%s(%p)", __func__, handlers); + + if (handlers == NULL || handlers->data == NULL) + return -EINVAL; + + data = (struct orientation_data *) handlers->data; + + handlers->activated = 0; + data->thread_continue = 0; + pthread_mutex_unlock(&data->mutex); + + pthread_mutex_destroy(&data->mutex); + + if (data->uinput_fd >= 0) { + uinput_destroy(data->uinput_fd); + close(data->uinput_fd); + } + data->uinput_fd = -1; + + if (handlers->poll_fd >= 0) + close(handlers->poll_fd); + handlers->poll_fd = -1; + + free(handlers->data); + handlers->data = NULL; + + return 0; +} + +int orientation_activate(struct herring_sensors_handlers *handlers) +{ + struct orientation_data *data; + + ALOGD("%s(%p)", __func__, handlers); + + if (handlers == NULL || handlers->data == NULL) + return -EINVAL; + + data = (struct orientation_data *) handlers->data; + + if (data->acceleration_sensor == NULL || data->magnetic_sensor == NULL) + return -1; + + data->acceleration_sensor->needed |= HERRING_SENSORS_NEEDED_ORIENTATION; + if (data->acceleration_sensor->needed == HERRING_SENSORS_NEEDED_ORIENTATION) + data->acceleration_sensor->activate(data->acceleration_sensor); + + data->magnetic_sensor->needed |= HERRING_SENSORS_NEEDED_ORIENTATION; + if (data->magnetic_sensor->needed == HERRING_SENSORS_NEEDED_ORIENTATION) + data->magnetic_sensor->activate(data->magnetic_sensor); + + handlers->activated = 1; + pthread_mutex_unlock(&data->mutex); + + return 0; +} + +int orientation_deactivate(struct herring_sensors_handlers *handlers) +{ + struct orientation_data *data; + + ALOGD("%s(%p)", __func__, handlers); + + if (handlers == NULL || handlers->data == NULL) + return -EINVAL; + + data = (struct orientation_data *) handlers->data; + + if (data->acceleration_sensor == NULL || data->magnetic_sensor == NULL) + return -1; + + data->acceleration_sensor->needed &= ~(HERRING_SENSORS_NEEDED_ORIENTATION); + if (data->acceleration_sensor->needed == 0) + data->acceleration_sensor->deactivate(data->acceleration_sensor); + + data->magnetic_sensor->needed &= ~(HERRING_SENSORS_NEEDED_ORIENTATION); + if (data->magnetic_sensor->needed == 0) + data->magnetic_sensor->deactivate(data->magnetic_sensor); + + handlers->activated = 0; + + return 0; +} + +int orientation_set_delay(struct herring_sensors_handlers *handlers, + long int delay) +{ + struct orientation_data *data; + + ALOGD("%s(%p)", __func__, handlers); + + if (handlers == NULL || handlers->data == NULL) + return -EINVAL; + + data = (struct orientation_data *) handlers->data; + + if (data->acceleration_sensor == NULL || data->magnetic_sensor == NULL) + return -1; + + if (data->acceleration_sensor->needed == HERRING_SENSORS_NEEDED_ORIENTATION) + data->acceleration_sensor->set_delay(data->acceleration_sensor, delay); + + if (data->magnetic_sensor->needed == HERRING_SENSORS_NEEDED_ORIENTATION) + data->magnetic_sensor->set_delay(data->magnetic_sensor, delay); + + data->delay = delay; + + return 0; +} + +float orientation_convert(int value) +{ + return (float) value / 1000.0f; +} + +int orientation_get_data(struct herring_sensors_handlers *handlers, + struct sensors_event_t *event) +{ + struct input_event input_event; + int input_fd = -1; + int rc; + +// ALOGD("%s(%p, %p)", __func__, handlers, event); + + if (handlers == NULL || event == NULL) + return -EINVAL; + + input_fd = handlers->poll_fd; + if (input_fd < 0) + return -EINVAL; + + memset(event, 0, sizeof(struct sensors_event_t)); + event->version = sizeof(struct sensors_event_t); + event->sensor = handlers->handle; + event->type = handlers->handle; + + event->orientation.status = SENSOR_STATUS_ACCURACY_MEDIUM; + + do { + rc = read(input_fd, &input_event, sizeof(input_event)); + if (rc < (int) sizeof(input_event)) + break; + + if (input_event.type == EV_REL) { + switch (input_event.code) { + case REL_X: + event->orientation.azimuth = orientation_convert(input_event.value); + break; + case REL_Y: + event->orientation.pitch = orientation_convert(input_event.value); + break; + case REL_Z: + event->orientation.roll = orientation_convert(input_event.value); + break; + default: + continue; + } + } 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; +} + +struct herring_sensors_handlers orientation = { + .name = "Orientation", + .handle = SENSOR_TYPE_ORIENTATION, + .init = orientation_init, + .deinit = orientation_deinit, + .activate = orientation_activate, + .deactivate = orientation_deactivate, + .set_delay = orientation_set_delay, + .get_data = orientation_get_data, + .activated = 0, + .needed = 0, + .poll_fd = -1, + .data = NULL, +}; |