Android-x86
Fork
Donation

  • R/O
  • HTTP
  • SSH
  • HTTPS

hardware-libsensors: Commit

hardware/libsensors


Commit MetaInfo

Revision76cd154fcd4ccde8d211183120668d9480b0bfb2 (tree)
Time2014-07-02 17:14:56
AuthorTanguy Pruvot <tanguy.pruvot@gmai...>
CommiterChih-Wei Huang

Log Message

hdaps: add support for ST LIS302DL (Acer 1425P/1825PTZ)

Allow also to tune axis signs and to swap axis like X<>Y

setprop hal.sensors.axis.revert "1,-1,1" # to revert Y axis
setprop hal.sensors.axis.order "1,0,2" # for Y,X,Z

This could be required for tablet pc with Accelerometers in Base
and not in Screen part (Acer 1425P has a rotative screen)

Signed-off-by: Tanguy Pruvot <tanguy.pruvot@gmail.com>

Change Summary

Incremental Difference

--- a/hdaps.c
+++ b/hdaps.c
@@ -4,11 +4,13 @@
44 * Copyright (C) 2011 The Android-x86 Open Source Project
55 *
66 * by Stefan Seidel <stefans@android-x86.org>
7+ * Adaptation by Tanguy Pruvot <tpruvot@github>
78 *
89 * Licensed under GPLv2 or later
910 *
1011 **/
1112
13+/* #define LOG_NDEBUG 0 */
1214 #define LOG_TAG "HdapsSensors"
1315
1416 #include <stdint.h>
@@ -26,12 +28,9 @@
2628
2729 #include <cutils/atomic.h>
2830 #include <cutils/log.h>
31+#include <cutils/properties.h>
2932 #include <hardware/sensors.h>
3033
31-//#define DEBUG_SENSOR 1
32-
33-#define CONVERT (GRAVITY_EARTH / 156.0f)
34-#define CONVERT_PEGA (GRAVITY_EARTH / 256.0f)
3534 #define INPUT_DIR "/dev/input"
3635 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
3736 #define ID_ACCELERATION (SENSORS_HANDLE_BASE + 0)
@@ -44,12 +43,25 @@
4443 typedef struct {
4544 const char *name;
4645 float conv[3];
46+ int swap[3];
4747 int avg_cnt;
4848 } accel_params;
4949
50+/* precision, result data should give GRAVITY_EARTH ~9.8 */
51+#define CONVERT (GRAVITY_EARTH / 156.0f)
52+#define CONVERT_PEGA (GRAVITY_EARTH / 256.0f)
53+#define CONVERT_LIS (GRAVITY_EARTH / 1024.0f)
54+
55+/* axis swap for tablet pcs, X=0, Y=1, Z=2 */
56+#define NO_SWAP { 0, 1, 2 }
57+#define SWAP_YXZ { 1, 0, 2 }
58+#define SWAP_ZXY { 2, 0, 1 }
59+
5060 static accel_params accel_list[] = {
51- { "hdaps", { CONVERT, -CONVERT, 0 }, 1 },
52- { "Pegatron Lucid Tablet Accelerometer", { CONVERT_PEGA, CONVERT_PEGA, CONVERT_PEGA }, 4 },
61+ { "hdaps", { CONVERT, -CONVERT, 0 }, NO_SWAP, 1 },
62+ { "Pegatron Lucid Tablet Accelerometer", { CONVERT_PEGA, CONVERT_PEGA, CONVERT_PEGA }, NO_SWAP, 4 },
63+// { "ST LIS3LV02DL Accelerometer", { -CONVERT_LIS, CONVERT_LIS, CONVERT_LIS }, SWAP_YXZ, 2 }, /* tablet mode */
64+ { "ST LIS3LV02DL Accelerometer", { CONVERT_LIS, -CONVERT_LIS, CONVERT_LIS }, SWAP_ZXY, 2 }, /* pc mode */
5365 };
5466
5567 static accel_params accelerometer;
@@ -88,20 +100,46 @@ static int device__poll(struct sensors_poll_device_t *device,
88100 sensors_event_t *data, int count) {
89101
90102 struct input_event event;
91- int ret;
92103 struct sensors_poll_context_t *dev =
93104 (struct sensors_poll_context_t *) device;
94105
106+ accel_params signs;
107+ char prop[PROPERTY_VALUE_MAX] = "";
108+ float val;
109+ int x, y, z;
110+
95111 if (dev->fd < 0)
96112 return 0;
97113
98- while (1) {
114+ // dynamic axis tuning, expect "1,-1,-1" format
115+ if (property_get("hal.sensors.axis.revert", prop, 0)) {
116+ sscanf(prop, "%d,%d,%d", &x, &y, &z);
117+ ALOGD("axis signs set to %d %d %d", x, y, z);
118+ } else {
119+ x = y = z = 1;
120+ }
121+ signs.conv[ABS_X] = accelerometer.conv[ABS_X] * x;
122+ signs.conv[ABS_Y] = accelerometer.conv[ABS_Y] * y;
123+ signs.conv[ABS_Z] = accelerometer.conv[ABS_Z] * z;
124+
125+ ALOGV("axis convert set to %.6f %.6f %.6f",
126+ signs.conv[ABS_X], signs.conv[ABS_Y] ,signs.conv[ABS_Z]);
127+
128+ // dynamic axis swap, expect "0,1,2" format
129+ if (property_get("hal.sensors.axis.order", prop, 0)) {
130+ sscanf(prop, "%d,%d,%d", &x, &y, &z);
131+ ALOGD("axis order set to %c %c %c", 'x'+x, 'x'+y, 'x'+z);
132+ } else {
133+ // use default values (accel_params)
134+ x = accelerometer.swap[0];
135+ y = accelerometer.swap[1];
136+ z = accelerometer.swap[2];
137+ }
138+
139+ while (read(dev->fd, &event, sizeof(event)) > 0) {
99140
100- ret = read(dev->fd, &event, sizeof(event));
141+ ALOGV("gsensor event %d - %d - %d", event.type, event.code, event.value);
101142
102-#ifdef DEBUG_SENSOR
103- ALOGD("hdaps event %d - %d - %d\n", event.type, event.code, event.value);
104-#endif
105143 if (event.type == EV_ABS) {
106144 switch (event.code) {
107145 // Even though this mapping results in wrong results with some apps,
@@ -113,7 +151,13 @@ static int device__poll(struct sensors_poll_device_t *device,
113151 case ABS_X: // 0x00
114152 case ABS_Y: // 0x01
115153 case ABS_Z: // 0x02
116- events_q[event_cnt].v[event.code] = accelerometer.conv[event.code] * event.value;
154+ val = signs.conv[event.code] * event.value;
155+ if (event.code == ABS_X)
156+ events_q[event_cnt].v[x] = val;
157+ else if (event.code == ABS_Y)
158+ events_q[event_cnt].v[y] = val;
159+ else if (event.code == ABS_Z)
160+ events_q[event_cnt].v[z] = val;
117161 break;
118162 }
119163 } else if (event.type == EV_SYN) {
@@ -121,7 +165,7 @@ static int device__poll(struct sensors_poll_device_t *device,
121165 data->timestamp = (int64_t) ((int64_t) event.time.tv_sec
122166 * 1000000000 + (int64_t) event.time.tv_usec * 1000);
123167 // hdaps doesn't have z-axis, so simulate it by rotation matrix solution
124- if (accelerometer.conv[2] == 0)
168+ if (signs.conv[2] == 0)
125169 events_q[event_cnt].z = COS_ASIN_2D(GRAVITY_EARTH, events_q[event_cnt].x, events_q[event_cnt].y);
126170 memset(&data->acceleration, 0, sizeof(sensors_vec_t));
127171 for (i = 0; i < accelerometer.avg_cnt; ++i) {
@@ -134,6 +178,7 @@ static int device__poll(struct sensors_poll_device_t *device,
134178 data->acceleration.status = SENSOR_STATUS_ACCURACY_HIGH;
135179 if (++event_cnt >= accelerometer.avg_cnt)
136180 event_cnt = 0;
181+
137182 // spare the CPU if desired
138183 if (forced_delay)
139184 usleep(forced_delay);
@@ -174,13 +219,12 @@ static int open_input_device(void) {
174219 name[0] = '\0';
175220 }
176221
177-#ifdef DEBUG_SENSOR
178- ALOGD("%s name is %s", devname, name);
179-#endif
222+ ALOGV("%s name is %s", devname, name);
223+
180224 for (i = 0; i < ARRAY_SIZE(accel_list); ++i) {
181225 if (!strcmp(name, accel_list[i].name)) {
182226 int c = accel_list[i].avg_cnt;
183- accelerometer.avg_cnt = c;
227+ memcpy(&accelerometer, &accel_list[i], sizeof(accel_params));
184228 accelerometer.conv[0] = accel_list[i].conv[0] / c;
185229 accelerometer.conv[1] = accel_list[i].conv[1] / c;
186230 accelerometer.conv[2] = accel_list[i].conv[2] / c;
@@ -258,7 +302,7 @@ static int open_sensors(const struct hw_module_t* module, const char* name,
258302 dev->device.poll = device__poll;
259303
260304 if ((dev->fd = open_input_device()) < 0) {
261- ALOGE("g sensor get class path error \n");
305+ ALOGE("GSensor get class path error");
262306 } else {
263307 *device = &dev->device.common;
264308 status = 0;
Show on old repository browser