2 * Copyright (C) 2014-2015 Intel Corporation.
8 #include <cutils/properties.h>
9 #include <hardware/sensors.h>
10 #include "calibration.h"
12 #include "description.h"
13 #include "transform.h"
15 #include "filtering.h"
16 #include "enumeration.h"
18 #define GYRO_MIN_SAMPLES 5 /* Drop first few gyro samples after enable */
21 /*----------------------------------------------------------------------------*/
24 /* Macros related to Intel Sensor Hub */
26 #define GRAVITY 9.80665
30 #define NUMOFACCDATA 8.0
32 /* Conversion of acceleration data to SI units (m/s^2) */
33 #define CONVERT_A (GRAVITY_EARTH / LSG / NUMOFACCDATA)
34 #define CONVERT_A_X(x) ((float(x) / 1000) * (GRAVITY * -1.0))
35 #define CONVERT_A_Y(x) ((float(x) / 1000) * (GRAVITY * 1.0))
36 #define CONVERT_A_Z(x) ((float(x) / 1000) * (GRAVITY * 1.0))
38 /* Conversion of magnetic data to uT units */
39 #define CONVERT_M (1.0 / 6.6)
40 #define CONVERT_M_X (-CONVERT_M)
41 #define CONVERT_M_Y (-CONVERT_M)
42 #define CONVERT_M_Z (CONVERT_M)
44 #define CONVERT_GAUSS_TO_MICROTESLA(x) ((x) * 100)
46 /* Conversion of orientation data to degree units */
47 #define CONVERT_O (1.0 / 64)
48 #define CONVERT_O_A (CONVERT_O)
49 #define CONVERT_O_P (CONVERT_O)
50 #define CONVERT_O_R (-CONVERT_O)
52 /* Conversion of gyro data to SI units (radian/sec) */
53 #define CONVERT_GYRO (2000.0 / 32767 * M_PI / 180)
54 #define CONVERT_GYRO_X (-CONVERT_GYRO)
55 #define CONVERT_GYRO_Y (-CONVERT_GYRO)
56 #define CONVERT_GYRO_Z (CONVERT_GYRO)
58 #define BIT(x) (1 << (x))
60 #define PROXIMITY_THRESHOLD 1
62 inline unsigned int set_bit_range (int start, int end)
65 unsigned int value = 0;
67 for (i = start; i < end; ++i)
73 inline float convert_from_vtf_format (int size, int exponent, unsigned int value)
80 value = value & set_bit_range(0, size * 8);
82 if (value & BIT(size*8-1)) {
83 value = ((1LL << (size * 8)) - value);
90 exponent = abs(exponent);
91 for (i = 0; i < exponent; ++i)
92 divider = divider * 10;
94 return mul * sample/divider;
97 return mul * sample * pow(10.0, exponent);
100 /* Platform sensor orientation */
101 #define DEF_ORIENT_ACCEL_X -1
102 #define DEF_ORIENT_ACCEL_Y -1
103 #define DEF_ORIENT_ACCEL_Z -1
105 #define DEF_ORIENT_GYRO_X 1
106 #define DEF_ORIENT_GYRO_Y 1
107 #define DEF_ORIENT_GYRO_Z 1
110 #define CONVERT_FROM_VTF16(s,d,x) convert_from_vtf_format(s,d,x)
111 #define CONVERT_A_G_VTF16E14_X(s,d,x) (DEF_ORIENT_ACCEL_X * convert_from_vtf_format(s,d,x) * GRAVITY)
112 #define CONVERT_A_G_VTF16E14_Y(s,d,x) (DEF_ORIENT_ACCEL_Y * convert_from_vtf_format(s,d,x) * GRAVITY)
113 #define CONVERT_A_G_VTF16E14_Z(s,d,x) (DEF_ORIENT_ACCEL_Z * convert_from_vtf_format(s,d,x) * GRAVITY)
115 /* Degree/sec to radian/sec */
116 #define CONVERT_G_D_VTF16E14_X(s,d,x) (DEF_ORIENT_GYRO_X * convert_from_vtf_format(s,d,x) * M_PI / 180)
117 #define CONVERT_G_D_VTF16E14_Y(s,d,x) (DEF_ORIENT_GYRO_Y * convert_from_vtf_format(s,d,x) * M_PI / 180)
118 #define CONVERT_G_D_VTF16E14_Z(s,d,x) (DEF_ORIENT_GYRO_Z * convert_from_vtf_format(s,d,x) * M_PI / 180)
120 /* Milli gauss to micro tesla */
121 #define CONVERT_M_MG_VTF16E14_X(s,d,x) (convert_from_vtf_format(s,d,x) / 10)
122 #define CONVERT_M_MG_VTF16E14_Y(s,d,x) (convert_from_vtf_format(s,d,x) / 10)
123 #define CONVERT_M_MG_VTF16E14_Z(s,d,x) (convert_from_vtf_format(s,d,x) / 10)
126 static int64_t sample_as_int64 (unsigned char* sample, datum_info_t* type)
130 int zeroed_bits = type->storagebits - type->realbits;
136 if (type->endianness == 'b')
137 for (i=0; i<type->storagebits/8; i++)
138 u64 = (u64 << 8) | sample[i];
140 for (i=type->storagebits/8 - 1; i>=0; i--)
141 u64 = (u64 << 8) | sample[i];
143 u64 = (u64 >> type->shift) & (~0ULL >> zeroed_bits);
145 if (type->sign == 'u')
146 return (int64_t) u64; /* We don't handle unsigned 64 bits int */
150 switch (type->realbits) {
155 return (int64_t) (int8_t) u64;
158 return (int64_t) (int16_t) u64;
161 return (int64_t) (int32_t) u64;
164 return (int64_t) u64;
167 sign_mask = 1 << (type->realbits-1);
168 value_mask = sign_mask - 1;
171 return - ((~u64 & value_mask) + 1); /* Negative value: return 2-complement */
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++)
190 static void mount_correction (float* data, float mm[9])
196 temp[i] = data[0] * mm[i * 3] + data[1] * mm[i * 3 + 1] + data[2] * mm[i * 3 + 2];
202 static void clamp_gyro_readings_to_zero (int s, sensors_event_t* data)
211 /* If we're calibrated, don't filter out as much */
212 if (sensor[s].cal_level > 0)
213 near_zero = 0.02; /* rad/s */
217 /* If motion on all axes is small enough */
218 if (fabs(x) < near_zero && fabs(y) < near_zero && fabs(z) < near_zero) {
221 * Report that we're not moving at all... but not exactly zero as composite sensors (orientation, rotation vector) don't
222 * seem to react very well to it.
225 data->data[0] *= 0.000001;
226 data->data[1] *= 0.000001;
227 data->data[2] *= 0.000001;
232 static void process_event_gyro_uncal (int s, int i, sensors_event_t* data)
234 gyro_cal_t* gyro_data;
236 if (sensor[s].type == SENSOR_TYPE_GYROSCOPE) {
237 gyro_data = (gyro_cal_t*) sensor[s].cal_data;
239 memcpy(&sensor[i].sample, data, sizeof(sensors_event_t));
241 sensor[i].sample.type = SENSOR_TYPE_GYROSCOPE_UNCALIBRATED;
242 sensor[i].sample.sensor = s;
244 sensor[i].sample.data[0] = data->data[0] + gyro_data->bias_x;
245 sensor[i].sample.data[1] = data->data[1] + gyro_data->bias_y;
246 sensor[i].sample.data[2] = data->data[2] + gyro_data->bias_z;
248 sensor[i].sample.uncalibrated_gyro.bias[0] = gyro_data->bias_x;
249 sensor[i].sample.uncalibrated_gyro.bias[1] = gyro_data->bias_y;
250 sensor[i].sample.uncalibrated_gyro.bias[2] = gyro_data->bias_z;
252 sensor[i].report_pending = 1;
256 static void process_event_magn_uncal (int s, int i, sensors_event_t* data)
258 compass_cal_t* magn_data;
260 if (sensor[s].type == SENSOR_TYPE_MAGNETIC_FIELD) {
261 magn_data = (compass_cal_t*) sensor[s].cal_data;
263 memcpy(&sensor[i].sample, data, sizeof(sensors_event_t));
265 sensor[i].sample.type = SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED;
266 sensor[i].sample.sensor = s;
268 sensor[i].sample.data[0] = data->data[0] + magn_data->offset[0][0];
269 sensor[i].sample.data[1] = data->data[1] + magn_data->offset[1][0];
270 sensor[i].sample.data[2] = data->data[2] + magn_data->offset[2][0];
272 sensor[i].sample.uncalibrated_magnetic.bias[0] = magn_data->offset[0][0];
273 sensor[i].sample.uncalibrated_magnetic.bias[1] = magn_data->offset[1][0];
274 sensor[i].sample.uncalibrated_magnetic.bias[2] = magn_data->offset[2][0];
276 sensor[i].report_pending = 1;
280 static void process_event (int s, sensors_event_t* data)
283 * This gets the real event (post process - calibration, filtering & co.) and makes it into a virtual one.
284 * The specific processing function for each sensor will populate the necessary fields and set up the report pending flag.
289 /* Go through out virtual sensors and check if we can use this event */
290 for (i = 0; i < sensor_count; i++)
291 switch (sensor[i].type) {
292 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
293 process_event_gyro_uncal(s, i, data);
295 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
296 process_event_magn_uncal(s, i, data);
304 static int finalize_sample_default (int s, sensors_event_t* data)
306 /* Swap fields if we have a custom channel ordering on this sensor */
307 if (sensor[s].quirks & QUIRK_FIELD_ORDERING)
308 reorder_fields(data->data, sensor[s].order);
309 if (sensor[s].quirks & QUIRK_MOUNTING_MATRIX)
310 mount_correction(data->data, sensor[s].mounting_matrix);
312 sensor[s].event_count++;
314 switch (sensor[s].type) {
315 case SENSOR_TYPE_ACCELEROMETER:
316 /* Always consider the accelerometer accurate */
317 data->acceleration.status = SENSOR_STATUS_ACCURACY_HIGH;
318 if (sensor[s].quirks & QUIRK_BIASED)
319 calibrate_accel(s, data);
323 case SENSOR_TYPE_MAGNETIC_FIELD:
324 calibrate_compass (s, data);
328 case SENSOR_TYPE_GYROSCOPE:
330 /* Report medium accuracy by default ; higher accuracy levels will be reported once, and if, we achieve calibration. */
331 data->gyro.status = SENSOR_STATUS_ACCURACY_MEDIUM;
334 * We're only trying to calibrate data from continuously firing gyroscope drivers, as motion based ones use
335 * movement thresholds that may lead us to incorrectly estimate bias.
337 if (sensor[s].selected_trigger !=
338 sensor[s].motion_trigger_name)
339 calibrate_gyro(s, data);
342 * For noisy sensors drop a few samples to make sure we have at least GYRO_MIN_SAMPLES events in the
343 * filtering queue. This improves mean and std dev.
345 if (sensor[s].filter_type) {
346 if (sensor[s].selected_trigger !=
347 sensor[s].motion_trigger_name &&
348 sensor[s].event_count < GYRO_MIN_SAMPLES)
354 /* Clamp near zero moves to (0,0,0) if appropriate */
355 clamp_gyro_readings_to_zero(s, data);
358 case SENSOR_TYPE_PROXIMITY:
360 * See iio spec for in_proximity* - depending on the device
361 * this value is either in meters either unit-less and cannot
362 * be translated to SI units. Where the translation is not possible
363 * lower values indicate something is close and higher ones indicate distance.
365 if (data->data[0] > PROXIMITY_THRESHOLD)
366 data->data[0] = PROXIMITY_THRESHOLD;
368 /* ... fall through ... */
369 case SENSOR_TYPE_LIGHT:
370 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
371 case SENSOR_TYPE_TEMPERATURE:
372 case SENSOR_TYPE_INTERNAL_ILLUMINANCE:
373 case SENSOR_TYPE_INTERNAL_INTENSITY:
374 /* Only keep two decimals for these readings */
375 data->data[0] = 0.01 * ((int) (data->data[0] * 100));
377 /* These are on change sensors ; drop the sample if it has the same value as the previously reported one. */
378 if (data->data[0] == sensor[s].prev_val.data)
381 sensor[s].prev_val.data = data->data[0];
383 case SENSOR_TYPE_STEP_COUNTER:
384 if (data->u64.step_counter == sensor[s].prev_val.data64)
386 sensor[s].prev_val.data64 = data->u64.data[0];
391 /* If there are active virtual sensors depending on this one - process the event */
392 if (sensor[s].ref_count)
393 process_event(s, data);
396 return 1; /* Return sample to Android */
400 static float transform_sample_default (int s, int c, unsigned char* sample_data)
402 datum_info_t* sample_type = &sensor[s].channel[c].type_info;
403 int64_t s64 = sample_as_int64(sample_data, sample_type);
404 float scale = sensor[s].scale ? sensor[s].scale : sensor[s].channel[c].scale;
406 /* In case correction has been requested using properties, apply it */
407 float correction = sensor[s].channel[c].opt_scale;
409 /* Correlated with "acquire_immediate_value" method */
410 if (sensor[s].type == SENSOR_TYPE_MAGNETIC_FIELD)
411 return CONVERT_GAUSS_TO_MICROTESLA((sensor[s].offset + s64) * scale) * correction;
413 /* Apply default scaling rules */
414 return (sensor[s].offset + s64) * scale * correction;
418 static int finalize_sample_ISH (int s, sensors_event_t* data)
420 float pitch, roll, yaw;
422 /* Swap fields if we have a custom channel ordering on this sensor */
423 if (sensor[s].quirks & QUIRK_FIELD_ORDERING)
424 reorder_fields(data->data, sensor[s].order);
426 if (sensor[s].type == SENSOR_TYPE_ORIENTATION) {
428 pitch = data->data[0];
429 roll = data->data[1];
432 data->data[0] = 360.0 - yaw;
433 data->data[1] = -pitch;
434 data->data[2] = -roll;
437 /* Add this event to our global records, for filtering purposes */
438 record_sample(s, data);
440 return 1; /* Return sample to Android */
444 static float transform_sample_ISH (int s, int c, unsigned char* sample_data)
446 datum_info_t* sample_type = &sensor[s].channel[c].type_info;
447 int val = (int) sample_as_int64(sample_data, sample_type);
449 int data_bytes = (sample_type->realbits)/8;
450 int exponent = sensor[s].offset;
452 /* In case correction has been requested using properties, apply it */
453 correction = sensor[s].channel[c].opt_scale;
455 switch (sensor_desc[s].type) {
456 case SENSOR_TYPE_ACCELEROMETER:
459 return correction * CONVERT_A_G_VTF16E14_X(data_bytes, exponent, val);
462 return correction * CONVERT_A_G_VTF16E14_Y(data_bytes, exponent, val);
465 return correction * CONVERT_A_G_VTF16E14_Z(data_bytes, exponent, val);
469 case SENSOR_TYPE_GYROSCOPE:
472 return correction * CONVERT_G_D_VTF16E14_X(data_bytes, exponent, val);
475 return correction * CONVERT_G_D_VTF16E14_Y(data_bytes, exponent, val);
478 return correction * CONVERT_G_D_VTF16E14_Z(data_bytes, exponent, val);
482 case SENSOR_TYPE_MAGNETIC_FIELD:
485 return correction * CONVERT_M_MG_VTF16E14_X(data_bytes, exponent, val);
488 return correction * CONVERT_M_MG_VTF16E14_Y(data_bytes, exponent, val);
491 return correction * CONVERT_M_MG_VTF16E14_Z(data_bytes, exponent, val);
495 case SENSOR_TYPE_LIGHT:
498 case SENSOR_TYPE_ORIENTATION:
499 return correction * convert_from_vtf_format(data_bytes, exponent, val);
501 case SENSOR_TYPE_ROTATION_VECTOR:
502 return correction * convert_from_vtf_format(data_bytes, exponent, val);
509 void select_transform (int s)
511 char prop_name[PROP_NAME_MAX];
512 char prop_val[PROP_VALUE_MAX];
513 int i = sensor[s].catalog_index;
514 const char *prefix = sensor_catalog[i].tag;
516 sprintf(prop_name, PROP_BASE, prefix, "transform");
518 if (property_get(prop_name, prop_val, ""))
519 if (!strcmp(prop_val, "ISH")) {
520 ALOGI( "Using Intel Sensor Hub semantics on %s\n", sensor[s].friendly_name);
522 sensor[s].ops.transform = transform_sample_ISH;
523 sensor[s].ops.finalize = finalize_sample_ISH;
527 sensor[s].ops.transform = transform_sample_default;
528 sensor[s].ops.finalize = finalize_sample_default;
532 float acquire_immediate_float_value (int s, int c)
534 char sysfs_path[PATH_MAX];
537 int dev_num = sensor[s].dev_num;
538 int i = sensor[s].catalog_index;
539 const char* raw_path = sensor_catalog[i].channel[c].raw_path;
540 const char* input_path = sensor_catalog[i].channel[c].input_path;
541 float scale = sensor[s].scale ? sensor[s].scale : sensor[s].channel[c].scale;
542 float offset = sensor[s].offset;
545 /* In case correction has been requested using properties, apply it */
546 correction = sensor[s].channel[c].opt_scale;
548 /* Acquire a sample value for sensor s / channel c through sysfs */
550 if (sensor[s].channel[c].input_path_present) {
551 sprintf(sysfs_path, BASE_PATH "%s", dev_num, input_path);
552 ret = sysfs_read_float(sysfs_path, &val);
555 if (sensor[s].type == SENSOR_TYPE_MAGNETIC_FIELD)
556 return CONVERT_GAUSS_TO_MICROTESLA (val * correction);
557 return val * correction;
561 if (!sensor[s].channel[c].raw_path_present)
564 sprintf(sysfs_path, BASE_PATH "%s", dev_num, raw_path);
565 ret = sysfs_read_float(sysfs_path, &val);
571 * There is no transform ops defined yet for raw sysfs values.
572 * Use this function to perform transformation as well.
574 if (sensor[s].type == SENSOR_TYPE_MAGNETIC_FIELD)
575 return CONVERT_GAUSS_TO_MICROTESLA ((val + offset) * scale) * correction;
577 return (val + offset) * scale * correction;
580 uint64_t acquire_immediate_uint64_value (int s, int c)
582 char sysfs_path[PATH_MAX];
585 int dev_num = sensor[s].dev_num;
586 int i = sensor[s].catalog_index;
587 const char* raw_path = sensor_catalog[i].channel[c].raw_path;
588 const char* input_path = sensor_catalog[i].channel[c].input_path;
589 float scale = sensor[s].scale ? sensor[s].scale : sensor[s].channel[c].scale;
590 float offset = sensor[s].offset;
591 int sensor_type = sensor_catalog[i].type;
594 /* In case correction has been requested using properties, apply it */
595 correction = sensor[s].channel[c].opt_scale;
597 /* Acquire a sample value for sensor s / channel c through sysfs */
599 if (sensor[s].channel[c].input_path_present) {
600 sprintf(sysfs_path, BASE_PATH "%s", dev_num, input_path);
601 ret = sysfs_read_uint64(sysfs_path, &val);
604 return val * correction;
607 if (!sensor[s].channel[c].raw_path_present)
610 sprintf(sysfs_path, BASE_PATH "%s", dev_num, raw_path);
611 ret = sysfs_read_uint64(sysfs_path, &val);
616 return (val + offset) * scale * correction;