2 * HDAPS accelerometer sensor
4 * Copyright (C) 2011 The Android-x86 Open Source Project
6 * by Stefan Seidel <stefans@android-x86.org>
8 * Licensed under GPLv2 or later
23 #include <linux/input.h>
25 #include <cutils/atomic.h>
26 #include <cutils/log.h>
27 #include <hardware/sensors.h>
29 //#define DEBUG_SENSOR 1
31 #define CONVERT (GRAVITY_EARTH / 156.0f)
32 #define SENSOR_NAME "hdaps"
33 #define INPUT_DIR "/dev/input"
34 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
35 #define ID_ACCELERATION (SENSORS_HANDLE_BASE + 0)
37 #define AMIN(a,b) (((a)<(fabs(b)))?(a):(b))
38 #define SQUARE(x) ((x)*(x))
39 #define COS_ASIN(m,x) (sqrt(SQUARE(m)-SQUARE(AMIN(m,x))))
40 #define COS_ASIN_2D(m,x,y) (COS_ASIN(m,x)*COS_ASIN(m,y)/(m))
42 static unsigned int forced_delay = 0;
44 struct sensors_poll_context_t {
45 struct sensors_poll_device_t device;
49 static int common__close(struct hw_device_t *dev) {
50 struct sensors_poll_context_t *ctx = (struct sensors_poll_context_t *) dev;
58 static int device__activate(struct sensors_poll_device_t *dev, int handle,
64 static int device__set_delay(struct sensors_poll_device_t *device, int handle,
66 forced_delay = ns / 1000;
71 static int device__poll(struct sensors_poll_device_t *device,
72 sensors_event_t* data, int count) {
74 struct input_event event;
76 struct sensors_poll_context_t *dev =
77 (struct sensors_poll_context_t *) device;
84 ret = read(dev->fd, &event, sizeof(event));
87 LOGD("hdaps event %d - %d - %d\n", event.type, event.code, event.value);
89 if (event.type == EV_ABS) {
91 // Even though this mapping results in wrong results with some apps,
92 // it just means that these apps are broken, i.e. they rely on the
93 // fact that phone have portrait displays. Laptops/tablets however
94 // have landscape displays, and the axes are relative to the default
95 // screen orientation, not relative to portrait orientation.
96 // See the nVidia Tegra accelerometer docs if you want to know for sure.
98 data->acceleration.x = (float) event.value * CONVERT;
101 // we invert the y-axis here because we assume the laptop is
102 // in tablet mode (and thus the display is rotated 180 degrees.
103 data->acceleration.y = -(float) event.value * CONVERT;
106 } else if (event.type == EV_SYN) {
107 data->timestamp = (int64_t) ((int64_t) event.time.tv_sec
108 * 1000000000 + (int64_t) event.time.tv_usec * 1000);
109 // hdaps doesn't have z-axis, so simulate it by rotation matrix solution
110 data ->acceleration.z = COS_ASIN_2D(GRAVITY_EARTH, data->acceleration.x, data->acceleration.y);
111 data->sensor = ID_ACCELERATION;
112 data->type = SENSOR_TYPE_ACCELEROMETER;
113 data->acceleration.status = SENSOR_STATUS_ACCURACY_HIGH;
114 // spare the CPU if desired
116 usleep(forced_delay);
124 static int open_input_device(void) {
131 dir = opendir(INPUT_DIR);
135 strcpy(devname, INPUT_DIR);
136 filename = devname + strlen(devname);
139 while ((de = readdir(dir))) {
140 if (de->d_name[0] == '.' && (de->d_name[1] == '\0' || (de->d_name[1] == '.' && de->d_name[2] == '\0')))
142 strcpy(filename, de->d_name);
143 fd = open(devname, O_RDONLY);
148 if (ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
152 if (!strcmp(name, SENSOR_NAME)) {
154 LOGI("devname is %s \n", devname);
170 static const struct sensor_t sSensorList[] = {
171 { .name = "HDAPS accelerometer",
172 .vendor = "Linux kernel",
174 .handle = ID_ACCELERATION,
175 .type = SENSOR_TYPE_ACCELEROMETER,
176 .maxRange = (GRAVITY_EARTH * 6.0f),
177 .resolution = (GRAVITY_EARTH * 6.0f) / 1024.0f,
183 static int open_sensors(const struct hw_module_t* module, const char* name,
184 struct hw_device_t** device);
186 static int sensors__get_sensors_list(struct sensors_module_t* module,
187 struct sensor_t const** list) {
190 return ARRAY_SIZE(sSensorList);
193 static struct hw_module_methods_t sensors_module_methods = {
194 .open = open_sensors };
196 const struct sensors_module_t HAL_MODULE_INFO_SYM = {
198 .tag = HARDWARE_MODULE_TAG,
201 .id = SENSORS_HARDWARE_MODULE_ID,
202 .name = "hdaps accelerometer sensor",
203 .author = "Stefan Seidel",
204 .methods = &sensors_module_methods,
208 .get_sensors_list = sensors__get_sensors_list
211 static int open_sensors(const struct hw_module_t* module, const char* name,
212 struct hw_device_t** device) {
213 int status = -EINVAL;
215 struct sensors_poll_context_t *dev = malloc(
216 sizeof(struct sensors_poll_context_t));
217 memset(&dev->device, 0, sizeof(struct sensors_poll_device_t));
219 dev->device.common.tag = HARDWARE_DEVICE_TAG;
220 dev->device.common.version = 0;
221 dev->device.common.module = (struct hw_module_t*) module;
222 dev->device.common.close = common__close;
223 dev->device.activate = device__activate;
224 dev->device.setDelay = device__set_delay;
225 dev->device.poll = device__poll;
227 if ((dev->fd = open_input_device()) < 0) {
228 LOGE("g sensor get class path error \n");
230 *device = &dev->device.common;