2 * Copyright (C) 2014 Intel Corporation.
8 #include <cutils/properties.h>
9 #include <hardware/sensors.h>
11 #include "transform.h"
13 #include "calibration.h"
15 /*----------------------------------------------------------------------------*/
17 /* Macros related to Intel Sensor Hub */
19 #define GRAVITY 9.80665f
23 #define NUMOFACCDATA (8.0f)
25 /* conversion of acceleration data to SI units (m/s^2) */
26 #define CONVERT_A (GRAVITY_EARTH / LSG / NUMOFACCDATA)
27 #define CONVERT_A_X(x) ((float(x)/1000) * (GRAVITY * -1.0))
28 #define CONVERT_A_Y(x) ((float(x)/1000) * (GRAVITY * 1.0))
29 #define CONVERT_A_Z(x) ((float(x)/1000) * (GRAVITY * 1.0))
31 /* conversion of magnetic data to uT units */
32 #define CONVERT_M (1.0f/6.6f)
33 #define CONVERT_M_X (-CONVERT_M)
34 #define CONVERT_M_Y (-CONVERT_M)
35 #define CONVERT_M_Z (CONVERT_M)
37 #define CONVERT_GAUSS_TO_MICROTESLA(x) ( (x) * 100 )
39 /* conversion of orientation data to degree units */
40 #define CONVERT_O (1.0f/64.0f)
41 #define CONVERT_O_A (CONVERT_O)
42 #define CONVERT_O_P (CONVERT_O)
43 #define CONVERT_O_R (-CONVERT_O)
45 /*conversion of gyro data to SI units (radian/sec) */
46 #define CONVERT_GYRO ((2000.0f/32767.0f)*((float)M_PI / 180.0f))
47 #define CONVERT_GYRO_X (-CONVERT_GYRO)
48 #define CONVERT_GYRO_Y (-CONVERT_GYRO)
49 #define CONVERT_GYRO_Z (CONVERT_GYRO)
51 #define BIT(x) (1 << (x))
53 inline unsigned int set_bit_range(int start, int end)
56 unsigned int value = 0;
58 for (i = start; i < end; ++i)
63 inline float convert_from_vtf_format(int size, int exponent, unsigned int value)
70 value = value & set_bit_range(0, size*8);
71 if (value & BIT(size*8-1)) {
72 value = ((1LL << (size*8)) - value);
77 exponent = abs(exponent);
78 for (i = 0; i < exponent; ++i) {
81 return mul * sample/divider;
83 return mul * sample * pow(10.0, exponent);
87 // Platform sensor orientation
88 #define DEF_ORIENT_ACCEL_X -1
89 #define DEF_ORIENT_ACCEL_Y -1
90 #define DEF_ORIENT_ACCEL_Z -1
92 #define DEF_ORIENT_GYRO_X 1
93 #define DEF_ORIENT_GYRO_Y 1
94 #define DEF_ORIENT_GYRO_Z 1
97 #define CONVERT_FROM_VTF16(s,d,x) (convert_from_vtf_format(s,d,x))
98 #define CONVERT_A_G_VTF16E14_X(s,d,x) (DEF_ORIENT_ACCEL_X *\
99 convert_from_vtf_format(s,d,x)*GRAVITY)
100 #define CONVERT_A_G_VTF16E14_Y(s,d,x) (DEF_ORIENT_ACCEL_Y *\
101 convert_from_vtf_format(s,d,x)*GRAVITY)
102 #define CONVERT_A_G_VTF16E14_Z(s,d,x) (DEF_ORIENT_ACCEL_Z *\
103 convert_from_vtf_format(s,d,x)*GRAVITY)
105 // Degree/sec to radian/sec
106 #define CONVERT_G_D_VTF16E14_X(s,d,x) (DEF_ORIENT_GYRO_X *\
107 convert_from_vtf_format(s,d,x) * \
108 ((float)M_PI/180.0f))
109 #define CONVERT_G_D_VTF16E14_Y(s,d,x) (DEF_ORIENT_GYRO_Y *\
110 convert_from_vtf_format(s,d,x) * \
111 ((float)M_PI/180.0f))
112 #define CONVERT_G_D_VTF16E14_Z(s,d,x) (DEF_ORIENT_GYRO_Z *\
113 convert_from_vtf_format(s,d,x) * \
114 ((float)M_PI/180.0f))
116 // Milli gauss to micro tesla
117 #define CONVERT_M_MG_VTF16E14_X(s,d,x) (convert_from_vtf_format(s,d,x)/10)
118 #define CONVERT_M_MG_VTF16E14_Y(s,d,x) (convert_from_vtf_format(s,d,x)/10)
119 #define CONVERT_M_MG_VTF16E14_Z(s,d,x) (convert_from_vtf_format(s,d,x)/10)
122 #define ACC_EXPONENT -2
123 #define GYRO_EXPONENT -1
124 #define MAGN_EXPONENT 0
125 #define INC_EXPONENT -1
126 #define ROT_EXPONENT -8
128 /*----------------------------------------------------------------------------*/
130 static int64_t sample_as_int64(unsigned char* sample, struct datum_info_t* type)
136 int zeroed_bits = type->storagebits - type->realbits;
140 if (type->endianness == 'b')
141 for (i=0; i<type->storagebits/8; i++)
142 u64 = (u64 << 8) | sample[i];
144 for (i=type->storagebits/8 - 1; i>=0; i--)
145 u64 = (u64 << 8) | sample[i];
147 u64 = (u64 >> type->shift) & (~0ULL >> zeroed_bits);
149 if (type->sign == 'u')
150 return (int64_t) u64; /* We don't handle unsigned 64 bits int */
152 switch (type->realbits) {
154 return (int64_t) (int8_t) u64;
157 return (int64_t) (u64 >> 11) ?
158 (((int64_t)-1) ^ 0xfff) | u64 : u64;
161 return (int64_t) (int16_t) u64;
164 return (int64_t) (int32_t) u64;
167 return (int64_t) u64;
170 ALOGE("Unhandled sample storage size\n");
175 static void finalize_sample_default(int s, struct sensors_event_t* data)
177 int i = sensor_info[s].catalog_index;
178 int sensor_type = sensor_catalog[i].type;
181 switch (sensor_type) {
182 case SENSOR_TYPE_ACCELEROMETER:
184 * Invert x and z axes orientation from SI units - see
185 * /hardware/libhardware/include/hardware/sensors.h
186 * for a discussion of what Android expects
197 case SENSOR_TYPE_MAGNETIC_FIELD:
206 /* Calibrate compass */
207 calibrate_compass (data, get_timestamp());
210 case SENSOR_TYPE_GYROSCOPE:
216 if (fabs(x) < 0.1 && fabs(y) < 0.1 && fabs(z) < 0.1)
224 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
225 case SENSOR_TYPE_TEMPERATURE:
226 /* Only keep two decimals for temperature readings */
227 data->data[0] = 0.01 * ((int) (data->data[0] * 100));
234 static float transform_sample_default(int s, int c, unsigned char* sample_data)
236 struct datum_info_t* sample_type = &sensor_info[s].channel[c].type_info;
237 int64_t s64 = sample_as_int64(sample_data, sample_type);
238 float scale = sensor_info[s].scale ?
239 sensor_info[s].scale : sensor_info[s].channel[c].scale;
241 /* In case correction has been requested using properties, apply it */
242 scale *= sensor_info[s].channel[c].opt_scale;
244 /* Apply default scaling rules */
245 return (sensor_info[s].offset + s64) * scale;
249 static void finalize_sample_ISH(int s, struct sensors_event_t* data)
251 int i = sensor_info[s].catalog_index;
252 int sensor_type = sensor_catalog[i].type;
253 float pitch, roll, yaw;
255 if (sensor_type == SENSOR_TYPE_ORIENTATION) {
257 pitch = data->data[0];
258 roll = data->data[1];
261 data->data[0] = 360.0 - yaw;
262 data->data[1] = -pitch;
263 data->data[2] = -roll;
268 static float transform_sample_ISH(int s, int c, unsigned char* sample_data)
270 struct datum_info_t* sample_type = &sensor_info[s].channel[c].type_info;
271 int val = (int) sample_as_int64(sample_data, sample_type);
272 int i = sensor_info[s].catalog_index;
273 int sensor_type = sensor_catalog[i].type;
276 /* In case correction has been requested using properties, apply it */
277 correction = sensor_info[s].channel[c].opt_scale;
279 switch (sensor_type) {
280 case SENSOR_TYPE_ACCELEROMETER:
284 CONVERT_A_G_VTF16E14_X(
285 DATA_BYTES, ACC_EXPONENT, val);
289 CONVERT_A_G_VTF16E14_Y(
290 DATA_BYTES, ACC_EXPONENT, val);
294 CONVERT_A_G_VTF16E14_Z(
295 DATA_BYTES, ACC_EXPONENT, val);
300 case SENSOR_TYPE_GYROSCOPE:
304 CONVERT_G_D_VTF16E14_X(
305 DATA_BYTES, GYRO_EXPONENT, val);
309 CONVERT_G_D_VTF16E14_Y(
310 DATA_BYTES, GYRO_EXPONENT, val);
314 CONVERT_G_D_VTF16E14_Z(
315 DATA_BYTES, GYRO_EXPONENT, val);
319 case SENSOR_TYPE_MAGNETIC_FIELD:
323 CONVERT_M_MG_VTF16E14_X(
324 DATA_BYTES, MAGN_EXPONENT, val);
328 CONVERT_M_MG_VTF16E14_Y(
329 DATA_BYTES, MAGN_EXPONENT, val);
333 CONVERT_M_MG_VTF16E14_Z(
334 DATA_BYTES, MAGN_EXPONENT, val);
338 case SENSOR_TYPE_ORIENTATION:
339 return correction * convert_from_vtf_format(
340 DATA_BYTES, INC_EXPONENT, val);
342 case SENSOR_TYPE_ROTATION_VECTOR:
343 return correction * convert_from_vtf_format(
344 DATA_BYTES, ROT_EXPONENT, val);
351 void select_transform (int s)
353 char prop_name[PROP_NAME_MAX];
354 char prop_val[PROP_VALUE_MAX];
355 int i = sensor_info[s].catalog_index;
356 const char *prefix = sensor_catalog[i].tag;
358 sprintf(prop_name, PROP_BASE, prefix, "transform");
360 if (property_get(prop_name, prop_val, "")) {
361 if (!strcmp(prop_val, "ISH")) {
362 ALOGI( "Using Intel Sensor Hub semantics on %s\n",
363 sensor_info[s].friendly_name);
365 sensor_info[s].ops.transform = transform_sample_ISH;
366 sensor_info[s].ops.finalize = finalize_sample_ISH;
371 sensor_info[s].ops.transform = transform_sample_default;
372 sensor_info[s].ops.finalize = finalize_sample_default;
376 float acquire_immediate_value(int s, int c)
378 char sysfs_path[PATH_MAX];
381 int dev_num = sensor_info[s].dev_num;
382 int i = sensor_info[s].catalog_index;
383 const char* raw_path = sensor_catalog[i].channel[c].raw_path;
384 const char* input_path = sensor_catalog[i].channel[c].input_path;
385 float scale = sensor_info[s].scale ?
386 sensor_info[s].scale : sensor_info[s].channel[c].scale;
387 float offset = sensor_info[s].offset;
388 int sensor_type = sensor_catalog[i].type;
391 /* In case correction has been requested using properties, apply it */
392 correction = sensor_info[s].channel[c].opt_scale;
394 /* Acquire a sample value for sensor s / channel c through sysfs */
397 sprintf(sysfs_path, BASE_PATH "%s", dev_num, input_path);
398 ret = sysfs_read_float(sysfs_path, &val);
401 return val * correction;
408 sprintf(sysfs_path, BASE_PATH "%s", dev_num, raw_path);
409 ret = sysfs_read_float(sysfs_path, &val);
415 There is no transform ops defined yet for Raw sysfs values
416 Use this function to perform transformation as well.
418 if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD)
419 return CONVERT_GAUSS_TO_MICROTESLA ((val + offset) * scale) *
422 return (val + offset) * scale * correction;