2 * Copyright (C) 2014 Intel Corporation.
8 #include <cutils/properties.h>
9 #include <hardware/sensors.h>
10 #include "calibration.h"
12 #include "description.h"
13 #include "transform.h"
16 /*----------------------------------------------------------------------------*/
18 /* Macros related to Intel Sensor Hub */
20 #define GRAVITY 9.80665f
24 #define NUMOFACCDATA (8.0f)
26 /* conversion of acceleration data to SI units (m/s^2) */
27 #define CONVERT_A (GRAVITY_EARTH / LSG / NUMOFACCDATA)
28 #define CONVERT_A_X(x) ((float(x)/1000) * (GRAVITY * -1.0))
29 #define CONVERT_A_Y(x) ((float(x)/1000) * (GRAVITY * 1.0))
30 #define CONVERT_A_Z(x) ((float(x)/1000) * (GRAVITY * 1.0))
32 /* conversion of magnetic data to uT units */
33 #define CONVERT_M (1.0f/6.6f)
34 #define CONVERT_M_X (-CONVERT_M)
35 #define CONVERT_M_Y (-CONVERT_M)
36 #define CONVERT_M_Z (CONVERT_M)
38 #define CONVERT_GAUSS_TO_MICROTESLA(x) ( (x) * 100 )
40 /* conversion of orientation data to degree units */
41 #define CONVERT_O (1.0f/64.0f)
42 #define CONVERT_O_A (CONVERT_O)
43 #define CONVERT_O_P (CONVERT_O)
44 #define CONVERT_O_R (-CONVERT_O)
46 /*conversion of gyro data to SI units (radian/sec) */
47 #define CONVERT_GYRO ((2000.0f/32767.0f)*((float)M_PI / 180.0f))
48 #define CONVERT_GYRO_X (-CONVERT_GYRO)
49 #define CONVERT_GYRO_Y (-CONVERT_GYRO)
50 #define CONVERT_GYRO_Z (CONVERT_GYRO)
52 #define BIT(x) (1 << (x))
54 inline unsigned int set_bit_range(int start, int end)
57 unsigned int value = 0;
59 for (i = start; i < end; ++i)
64 inline float convert_from_vtf_format(int size, int exponent, unsigned int value)
71 value = value & set_bit_range(0, size*8);
72 if (value & BIT(size*8-1)) {
73 value = ((1LL << (size*8)) - value);
78 exponent = abs(exponent);
79 for (i = 0; i < exponent; ++i) {
82 return mul * sample/divider;
84 return mul * sample * pow(10.0, exponent);
88 // Platform sensor orientation
89 #define DEF_ORIENT_ACCEL_X -1
90 #define DEF_ORIENT_ACCEL_Y -1
91 #define DEF_ORIENT_ACCEL_Z -1
93 #define DEF_ORIENT_GYRO_X 1
94 #define DEF_ORIENT_GYRO_Y 1
95 #define DEF_ORIENT_GYRO_Z 1
98 #define CONVERT_FROM_VTF16(s,d,x) (convert_from_vtf_format(s,d,x))
99 #define CONVERT_A_G_VTF16E14_X(s,d,x) (DEF_ORIENT_ACCEL_X *\
100 convert_from_vtf_format(s,d,x)*GRAVITY)
101 #define CONVERT_A_G_VTF16E14_Y(s,d,x) (DEF_ORIENT_ACCEL_Y *\
102 convert_from_vtf_format(s,d,x)*GRAVITY)
103 #define CONVERT_A_G_VTF16E14_Z(s,d,x) (DEF_ORIENT_ACCEL_Z *\
104 convert_from_vtf_format(s,d,x)*GRAVITY)
106 // Degree/sec to radian/sec
107 #define CONVERT_G_D_VTF16E14_X(s,d,x) (DEF_ORIENT_GYRO_X *\
108 convert_from_vtf_format(s,d,x) * \
109 ((float)M_PI/180.0f))
110 #define CONVERT_G_D_VTF16E14_Y(s,d,x) (DEF_ORIENT_GYRO_Y *\
111 convert_from_vtf_format(s,d,x) * \
112 ((float)M_PI/180.0f))
113 #define CONVERT_G_D_VTF16E14_Z(s,d,x) (DEF_ORIENT_GYRO_Z *\
114 convert_from_vtf_format(s,d,x) * \
115 ((float)M_PI/180.0f))
117 // Milli gauss to micro tesla
118 #define CONVERT_M_MG_VTF16E14_X(s,d,x) (convert_from_vtf_format(s,d,x)/10)
119 #define CONVERT_M_MG_VTF16E14_Y(s,d,x) (convert_from_vtf_format(s,d,x)/10)
120 #define CONVERT_M_MG_VTF16E14_Z(s,d,x) (convert_from_vtf_format(s,d,x)/10)
123 /*----------------------------------------------------------------------------*/
125 static int64_t sample_as_int64(unsigned char* sample, struct datum_info_t* type)
129 int zeroed_bits = type->storagebits - type->realbits;
135 if (type->endianness == 'b')
136 for (i=0; i<type->storagebits/8; i++)
137 u64 = (u64 << 8) | sample[i];
139 for (i=type->storagebits/8 - 1; i>=0; i--)
140 u64 = (u64 << 8) | sample[i];
142 u64 = (u64 >> type->shift) & (~0ULL >> zeroed_bits);
144 if (type->sign == 'u')
145 return (int64_t) u64; /* We don't handle unsigned 64 bits int */
149 switch (type->realbits) {
154 return (int64_t) (int8_t) u64;
157 return (int64_t) (int16_t) u64;
160 return (int64_t) (int32_t) u64;
163 return (int64_t) u64;
166 sign_mask = 1 << (type->realbits-1);
167 value_mask = sign_mask - 1;
170 /* Negative value: return 2-complement */
171 return - ((~u64 & value_mask) + 1);
173 return (int64_t) u64; /* Positive value */
178 static void reorder_fields(float* data, unsigned char map[MAX_CHANNELS])
181 float temp[MAX_CHANNELS];
183 for (i=0; i<MAX_CHANNELS; i++)
184 temp[i] = data[map[i]];
186 for (i=0; i<MAX_CHANNELS; i++)
191 static void denoise (struct sensor_info_t* si, struct sensors_event_t* data,
197 int sampling_rate = (int) si->sampling_rate;
200 /* Don't denoise anything if we have less than two samples per second */
201 if (sampling_rate < 2)
204 /* Restrict window size in case of a very high sampling rate */
205 if (sampling_rate > 100)
208 history_size = sampling_rate;
210 /* Reset history if we're operating on an incorrect window size */
211 if (si->history_size != history_size) {
212 si->history_size = history_size;
213 si->history_entries = 0;
214 si->history_index = 0;
215 si->history = (float*) realloc(si->history,
216 si->history_size * num_fields * sizeof(float));
220 return; /* Unlikely, but still... */
222 /* Update initialized samples count */
223 if (si->history_entries < si->history_size)
224 si->history_entries++;
226 /* Record new sample */
227 for (f=0; f < num_fields; f++)
228 si->history[si->history_index * num_fields + f] = data->data[f];
230 /* Update our rolling index (next evicted cell) */
231 si->history_index = (si->history_index + 1) % si->history_size;
233 /* For now simply compute a mobile mean for each field */
234 for (f=0; f < num_fields; f++) {
237 for (i=0; i < si->history_entries; i++)
238 total += si->history[i * num_fields + f];
240 /* Output filtered data */
241 data->data[f] = total / si->history_entries;
246 static int finalize_sample_default(int s, struct sensors_event_t* data)
248 int i = sensor_info[s].catalog_index;
249 int sensor_type = sensor_catalog[i].type;
251 /* Swap fields if we have a custom channel ordering on this sensor */
252 if (sensor_info[s].quirks & QUIRK_FIELD_ORDERING)
253 reorder_fields(data->data, sensor_info[s].order);
255 switch (sensor_type) {
256 case SENSOR_TYPE_ACCELEROMETER:
257 if (sensor_info[s].quirks & QUIRK_NOISY)
258 denoise(&sensor_info[s], data, 3);
261 case SENSOR_TYPE_MAGNETIC_FIELD:
262 calibrate_compass (data, &sensor_info[s], get_timestamp());
263 if (sensor_info[s].quirks & QUIRK_NOISY)
264 denoise(&sensor_info[s], data, 3);
267 case SENSOR_TYPE_GYROSCOPE:
268 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
269 calibrate_gyro(data, &sensor_info[s]);
270 if (sensor_info[s].quirks & QUIRK_NOISY)
271 denoise(&sensor_info[s], data, 3);
274 case SENSOR_TYPE_LIGHT:
275 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
276 case SENSOR_TYPE_TEMPERATURE:
277 /* Only keep two decimals for these readings */
278 data->data[0] = 0.01 * ((int) (data->data[0] * 100));
280 /* ... fall through ... */
282 case SENSOR_TYPE_PROXIMITY:
284 * These are on change sensors ; drop the sample if it
285 * has the same value as the previously reported one.
287 if (data->data[0] == sensor_info[s].prev_val)
290 sensor_info[s].prev_val = data->data[0];
294 return 1; /* Return sample to Android */
298 static float transform_sample_default(int s, int c, unsigned char* sample_data)
300 struct datum_info_t* sample_type = &sensor_info[s].channel[c].type_info;
301 int64_t s64 = sample_as_int64(sample_data, sample_type);
302 float scale = sensor_info[s].scale ?
303 sensor_info[s].scale : sensor_info[s].channel[c].scale;
305 /* In case correction has been requested using properties, apply it */
306 scale *= sensor_info[s].channel[c].opt_scale;
308 /* Apply default scaling rules */
309 return (sensor_info[s].offset + s64) * scale;
313 static int finalize_sample_ISH(int s, struct sensors_event_t* data)
315 int i = sensor_info[s].catalog_index;
316 int sensor_type = sensor_catalog[i].type;
317 float pitch, roll, yaw;
319 /* Swap fields if we have a custom channel ordering on this sensor */
320 if (sensor_info[s].quirks & QUIRK_FIELD_ORDERING)
321 reorder_fields(data->data, sensor_info[s].order);
323 if (sensor_type == SENSOR_TYPE_ORIENTATION) {
325 pitch = data->data[0];
326 roll = data->data[1];
329 data->data[0] = 360.0 - yaw;
330 data->data[1] = -pitch;
331 data->data[2] = -roll;
334 return 1; /* Return sample to Android */
338 static float transform_sample_ISH(int s, int c, unsigned char* sample_data)
340 struct datum_info_t* sample_type = &sensor_info[s].channel[c].type_info;
341 int val = (int) sample_as_int64(sample_data, sample_type);
342 int i = sensor_info[s].catalog_index;
343 int sensor_type = sensor_catalog[i].type;
345 int data_bytes = (sample_type->realbits)/8;
346 int exponent = sensor_info[s].offset;
348 /* In case correction has been requested using properties, apply it */
349 correction = sensor_info[s].channel[c].opt_scale;
351 switch (sensor_type) {
352 case SENSOR_TYPE_ACCELEROMETER:
356 CONVERT_A_G_VTF16E14_X(
357 data_bytes, exponent, val);
361 CONVERT_A_G_VTF16E14_Y(
362 data_bytes, exponent, val);
366 CONVERT_A_G_VTF16E14_Z(
367 data_bytes, exponent, val);
372 case SENSOR_TYPE_GYROSCOPE:
376 CONVERT_G_D_VTF16E14_X(
377 data_bytes, exponent, val);
381 CONVERT_G_D_VTF16E14_Y(
382 data_bytes, exponent, val);
386 CONVERT_G_D_VTF16E14_Z(
387 data_bytes, exponent, val);
391 case SENSOR_TYPE_MAGNETIC_FIELD:
395 CONVERT_M_MG_VTF16E14_X(
396 data_bytes, exponent, val);
400 CONVERT_M_MG_VTF16E14_Y(
401 data_bytes, exponent, val);
405 CONVERT_M_MG_VTF16E14_Z(
406 data_bytes, exponent, val);
410 case SENSOR_TYPE_LIGHT:
413 case SENSOR_TYPE_ORIENTATION:
414 return correction * convert_from_vtf_format(
415 data_bytes, exponent, val);
417 case SENSOR_TYPE_ROTATION_VECTOR:
418 return correction * convert_from_vtf_format(
419 data_bytes, exponent, val);
426 void select_transform (int s)
428 char prop_name[PROP_NAME_MAX];
429 char prop_val[PROP_VALUE_MAX];
430 int i = sensor_info[s].catalog_index;
431 const char *prefix = sensor_catalog[i].tag;
433 sprintf(prop_name, PROP_BASE, prefix, "transform");
435 if (property_get(prop_name, prop_val, "")) {
436 if (!strcmp(prop_val, "ISH")) {
437 ALOGI( "Using Intel Sensor Hub semantics on %s\n",
438 sensor_info[s].friendly_name);
440 sensor_info[s].ops.transform = transform_sample_ISH;
441 sensor_info[s].ops.finalize = finalize_sample_ISH;
446 sensor_info[s].ops.transform = transform_sample_default;
447 sensor_info[s].ops.finalize = finalize_sample_default;
451 float acquire_immediate_value(int s, int c)
453 char sysfs_path[PATH_MAX];
456 int dev_num = sensor_info[s].dev_num;
457 int i = sensor_info[s].catalog_index;
458 const char* raw_path = sensor_catalog[i].channel[c].raw_path;
459 const char* input_path = sensor_catalog[i].channel[c].input_path;
460 float scale = sensor_info[s].scale ?
461 sensor_info[s].scale : sensor_info[s].channel[c].scale;
462 float offset = sensor_info[s].offset;
463 int sensor_type = sensor_catalog[i].type;
466 /* In case correction has been requested using properties, apply it */
467 correction = sensor_info[s].channel[c].opt_scale;
469 /* Acquire a sample value for sensor s / channel c through sysfs */
472 sprintf(sysfs_path, BASE_PATH "%s", dev_num, input_path);
473 ret = sysfs_read_float(sysfs_path, &val);
476 return val * correction;
483 sprintf(sysfs_path, BASE_PATH "%s", dev_num, raw_path);
484 ret = sysfs_read_float(sysfs_path, &val);
490 There is no transform ops defined yet for Raw sysfs values
491 Use this function to perform transformation as well.
493 if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD)
494 return CONVERT_GAUSS_TO_MICROTESLA ((val + offset) * scale) *
497 return (val + offset) * scale * correction;