OSDN Git Service

Merge branch 'lineage-16.0' of https://github.com/me176c-dev/android_hardware_iio...
[android-x86/hardware-intel-libsensors.git] / transform.c
1 /*
2 // Copyright (c) 2015 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #include <stdlib.h>
18 #include <math.h>
19 #include <utils/Log.h>
20 #include <cutils/properties.h>
21 #include <hardware/sensors.h>
22 #include "calibration.h"
23 #include "common.h"
24 #include "description.h"
25 #include "transform.h"
26 #include "utils.h"
27 #include "filtering.h"
28 #include "enumeration.h"
29
30 #define GYRO_MIN_SAMPLES 5 /* Drop first few gyro samples after enable */
31
32
33 /*----------------------------------------------------------------------------*/
34
35
36 /* Macros related to Intel Sensor Hub */
37
38 #define GRAVITY         9.80665
39
40 /* 720 LSG = 1G */
41 #define LSG             1024.0
42 #define NUMOFACCDATA    8.0
43
44 /* Conversion of acceleration data to SI units (m/s^2) */
45 #define CONVERT_A       (GRAVITY_EARTH / LSG / NUMOFACCDATA)
46 #define CONVERT_A_X(x)  ((float(x) / 1000) * (GRAVITY * -1.0))
47 #define CONVERT_A_Y(x)  ((float(x) / 1000) * (GRAVITY * 1.0))
48 #define CONVERT_A_Z(x)  ((float(x) / 1000) * (GRAVITY * 1.0))
49
50 /* Conversion of magnetic data to uT units */
51 #define CONVERT_M       (1.0 / 6.6)
52 #define CONVERT_M_X     (-CONVERT_M)
53 #define CONVERT_M_Y     (-CONVERT_M)
54 #define CONVERT_M_Z     (CONVERT_M)
55
56 /* Conversion of orientation data to degree units */
57 #define CONVERT_O       (1.0 / 64)
58 #define CONVERT_O_A     (CONVERT_O)
59 #define CONVERT_O_P     (CONVERT_O)
60 #define CONVERT_O_R     (-CONVERT_O)
61
62 /* Conversion of gyro data to SI units (radian/sec) */
63 #define CONVERT_GYRO    (2000.0 / 32767 * M_PI / 180)
64 #define CONVERT_GYRO_X  (-CONVERT_GYRO)
65 #define CONVERT_GYRO_Y  (-CONVERT_GYRO)
66 #define CONVERT_GYRO_Z  (CONVERT_GYRO)
67
68 #define BIT(x) (1 << (x))
69
70 #define PROXIMITY_THRESHOLD 1
71
72 inline unsigned int set_bit_range (int start, int end)
73 {
74         int i;
75         unsigned int value = 0;
76
77         for (i = start; i < end; ++i)
78                 value |= BIT(i);
79
80         return value;
81 }
82
83 inline float convert_from_vtf_format (int size, int exponent, unsigned int value)
84 {
85         int divider = 1;
86         int i;
87         float sample;
88         float mul = 1.0;
89
90         value = value & set_bit_range(0, size * 8);
91
92         if (value & BIT(size*8-1)) {
93                 value =  ((1LL << (size * 8)) - value);
94                 mul = -1.0;
95         }
96
97         sample = value * 1.0;
98
99         if (exponent < 0) {
100                 exponent = abs(exponent);
101                 for (i = 0; i < exponent; ++i)
102                         divider = divider * 10;
103
104                 return mul * sample/divider;
105         }
106
107         return mul * sample * pow(10.0, exponent);
108 }
109
110 /* Platform sensor orientation */
111 #define DEF_ORIENT_ACCEL_X      -1
112 #define DEF_ORIENT_ACCEL_Y      -1
113 #define DEF_ORIENT_ACCEL_Z      -1
114
115 #define DEF_ORIENT_GYRO_X       1
116 #define DEF_ORIENT_GYRO_Y       1
117 #define DEF_ORIENT_GYRO_Z       1
118
119 /* G to m/s^2 */
120 #define CONVERT_FROM_VTF16(s,d,x)      convert_from_vtf_format(s,d,x)
121 #define CONVERT_A_G_VTF16E14_X(s,d,x)  (DEF_ORIENT_ACCEL_X * convert_from_vtf_format(s,d,x) * GRAVITY)
122 #define CONVERT_A_G_VTF16E14_Y(s,d,x)  (DEF_ORIENT_ACCEL_Y * convert_from_vtf_format(s,d,x) * GRAVITY)
123 #define CONVERT_A_G_VTF16E14_Z(s,d,x)  (DEF_ORIENT_ACCEL_Z * convert_from_vtf_format(s,d,x) * GRAVITY)
124
125 /* Degree/sec to radian/sec */
126 #define CONVERT_G_D_VTF16E14_X(s,d,x)  (DEF_ORIENT_GYRO_X * convert_from_vtf_format(s,d,x) * M_PI / 180)
127 #define CONVERT_G_D_VTF16E14_Y(s,d,x)  (DEF_ORIENT_GYRO_Y * convert_from_vtf_format(s,d,x) * M_PI / 180)
128 #define CONVERT_G_D_VTF16E14_Z(s,d,x)  (DEF_ORIENT_GYRO_Z * convert_from_vtf_format(s,d,x) * M_PI / 180)
129
130 /* Milli gauss to micro tesla */
131 #define CONVERT_M_MG_VTF16E14_X(s,d,x) (convert_from_vtf_format(s,d,x) / 10)
132 #define CONVERT_M_MG_VTF16E14_Y(s,d,x) (convert_from_vtf_format(s,d,x) / 10)
133 #define CONVERT_M_MG_VTF16E14_Z(s,d,x) (convert_from_vtf_format(s,d,x) / 10)
134
135
136 static int64_t sample_as_int64 (unsigned char* sample, datum_info_t* type)
137 {
138         uint64_t u64;
139         int i;
140         int zeroed_bits = type->storagebits - type->realbits;
141         uint64_t sign_mask;
142         uint64_t value_mask;
143
144         u64 = 0;
145
146         if (type->endianness == 'b')
147                 for (i=0; i<type->storagebits/8; i++)
148                         u64 = (u64 << 8) | sample[i];
149         else
150                 for (i=type->storagebits/8 - 1; i>=0; i--)
151                         u64 = (u64 << 8) | sample[i];
152
153         u64 = (u64 >> type->shift) & (~0ULL >> zeroed_bits);
154
155         if (type->sign == 'u')
156                 return (int64_t) u64; /* We don't handle unsigned 64 bits int */
157
158         /* Signed integer */
159
160         switch (type->realbits) {
161                 case 0 ... 1:
162                         return 0;
163
164                 case 8:
165                         return (int64_t) (int8_t) u64;
166
167                 case 16:
168                         return (int64_t) (int16_t) u64;
169
170                 case 32:
171                         return (int64_t) (int32_t) u64;
172
173                 case 64:
174                         return (int64_t) u64;
175
176                 default:
177                         sign_mask = 1 << (type->realbits-1);
178                         value_mask = sign_mask - 1;
179
180                         if (u64 & sign_mask)
181                                 return - ((~u64 & value_mask) + 1);     /* Negative value: return 2-complement */
182                         else
183                                 return (int64_t) u64;                   /* Positive value */
184         }
185 }
186
187
188 static void reorder_fields (float* data, unsigned char map[MAX_CHANNELS])
189 {
190         int i;
191         float temp[MAX_CHANNELS];
192
193         for (i=0; i<MAX_CHANNELS; i++)
194                 temp[i] = data[map[i]];
195
196         for (i=0; i<MAX_CHANNELS; i++)
197                 data[i] = temp[i];
198 }
199
200 static void mount_correction (float* data, float mm[9])
201 {
202         int i;
203         float temp[3];
204
205         for (i=0; i<3; i++)
206                 temp[i] = data[0] * mm[i * 3] + data[1] * mm[i * 3 + 1] + data[2] * mm[i * 3 + 2];
207
208         for (i=0; i<3; i++)
209                 data[i] = temp[i];
210 }
211
212 static void clamp_gyro_readings_to_zero (int s, sensors_event_t* data)
213 {
214         float x, y, z;
215         float near_zero;
216
217         x = data->data[0];
218         y = data->data[1];
219         z = data->data[2];
220
221         /* If we're calibrated, don't filter out as much */
222         if (sensor[s].cal_level > 0)
223                 near_zero = 0.02; /* rad/s */
224         else
225                 near_zero = 0.1;
226
227         /* If motion on all axes is small enough */
228         if (fabs(x) < near_zero && fabs(y) < near_zero && fabs(z) < near_zero) {
229
230                 /*
231                  * Report that we're not moving at all... but not exactly zero as composite sensors (orientation, rotation vector) don't
232                  * seem to react very well to it.
233                  */
234
235                 data->data[0] *= 0.000001;
236                 data->data[1] *= 0.000001;
237                 data->data[2] *= 0.000001;
238         }
239 }
240
241
242 static void process_event_gyro_uncal (int s, int i, sensors_event_t* data)
243 {
244         gyro_cal_t* gyro_data;
245
246         if (sensor[s].type == SENSOR_TYPE_GYROSCOPE) {
247                 gyro_data = (gyro_cal_t*) sensor[s].cal_data;
248
249                 memcpy(&sensor[i].sample, data, sizeof(sensors_event_t));
250
251                 sensor[i].sample.type = SENSOR_TYPE_GYROSCOPE_UNCALIBRATED;
252                 sensor[i].sample.sensor = s;
253
254                 sensor[i].sample.data[0] = data->data[0] + gyro_data->bias_x;
255                 sensor[i].sample.data[1] = data->data[1] + gyro_data->bias_y;
256                 sensor[i].sample.data[2] = data->data[2] + gyro_data->bias_z;
257
258                 sensor[i].sample.uncalibrated_gyro.bias[0] = gyro_data->bias_x;
259                 sensor[i].sample.uncalibrated_gyro.bias[1] = gyro_data->bias_y;
260                 sensor[i].sample.uncalibrated_gyro.bias[2] = gyro_data->bias_z;
261
262                 sensor[i].report_pending = 1;
263         }
264 }
265
266 static void process_event_magn_uncal (int s, int i, sensors_event_t* data)
267 {
268         compass_cal_t* magn_data;
269
270         if (sensor[s].type == SENSOR_TYPE_MAGNETIC_FIELD) {
271                 magn_data = (compass_cal_t*) sensor[s].cal_data;
272
273                 memcpy(&sensor[i].sample, data, sizeof(sensors_event_t));
274
275                 sensor[i].sample.type = SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED;
276                 sensor[i].sample.sensor = s;
277
278                 sensor[i].sample.data[0] = data->data[0] + magn_data->offset[0][0];
279                 sensor[i].sample.data[1] = data->data[1] + magn_data->offset[1][0];
280                 sensor[i].sample.data[2] = data->data[2] + magn_data->offset[2][0];
281
282                 sensor[i].sample.uncalibrated_magnetic.bias[0] = magn_data->offset[0][0];
283                 sensor[i].sample.uncalibrated_magnetic.bias[1] = magn_data->offset[1][0];
284                 sensor[i].sample.uncalibrated_magnetic.bias[2] = magn_data->offset[2][0];
285
286                 sensor[i].report_pending = 1;
287         }
288 }
289
290 static void process_event (int s, sensors_event_t* data)
291 {
292         /*
293          * This gets the real event (post process - calibration, filtering & co.) and makes it into a virtual one.
294          * The specific processing function for each sensor will populate the necessary fields and set up the report pending flag.
295          */
296
297          int i;
298
299          /* Go through out virtual sensors and check if we can use this event */
300          for (i = 0; i < sensor_count; i++)
301                 switch (sensor[i].type) {
302                         case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
303                                 process_event_gyro_uncal(s, i, data);
304                                 break;
305                         case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
306                                 process_event_magn_uncal(s, i, data);
307                                 break;
308                         default:
309                                 break;
310                 }
311 }
312
313
314 static int finalize_sample_default (int s, sensors_event_t* data)
315 {
316         /* Swap fields if we have a custom channel ordering on this sensor */
317         if (sensor[s].quirks & QUIRK_FIELD_ORDERING)
318                 reorder_fields(data->data, sensor[s].order);
319         if (sensor[s].quirks & QUIRK_MOUNTING_MATRIX)
320                 mount_correction(data->data, sensor[s].mounting_matrix);
321
322         sensor[s].event_count++;
323
324         switch (sensor[s].type) {
325                 case SENSOR_TYPE_ACCELEROMETER:
326                         /* Always consider the accelerometer accurate */
327                         data->acceleration.status = SENSOR_STATUS_ACCURACY_HIGH;
328                         if (sensor[s].quirks & QUIRK_BIASED)
329                                 calibrate_accel(s, data);
330                         denoise(s, data);
331                         break;
332
333                 case SENSOR_TYPE_MAGNETIC_FIELD:
334                         calibrate_compass (s, data);
335                         denoise(s, data);
336                         break;
337
338                 case SENSOR_TYPE_GYROSCOPE:
339
340                         /* Report medium accuracy by default ; higher accuracy levels will be reported once, and if, we achieve  calibration. */
341                         data->gyro.status = SENSOR_STATUS_ACCURACY_MEDIUM;
342
343                         /*
344                          * We're only trying to calibrate data from continuously firing gyroscope drivers, as motion based ones use
345                          * movement thresholds that may lead us to incorrectly estimate bias.
346                          */
347                         if (sensor[s].selected_trigger !=
348                                 sensor[s].motion_trigger_name)
349                                         calibrate_gyro(s, data);
350
351                         /*
352                          * For noisy sensors drop a few samples to make sure we have at least GYRO_MIN_SAMPLES events in the
353                          * filtering queue. This improves mean and std dev.
354                          */
355                         if (sensor[s].filter_type) {
356                                 if (sensor[s].selected_trigger !=
357                                     sensor[s].motion_trigger_name &&
358                                     sensor[s].event_count < GYRO_MIN_SAMPLES)
359                                                 return 0;
360
361                                 denoise(s, data);
362                         }
363
364                         /* Clamp near zero moves to (0,0,0) if appropriate */
365                         clamp_gyro_readings_to_zero(s, data);
366                         break;
367
368                 case SENSOR_TYPE_PROXIMITY:
369                         /*
370                          * See iio spec for in_proximity* - depending on the device
371                          * this value is either in meters either unit-less and cannot
372                          * be translated to SI units. Where the translation is not possible
373                          * lower values indicate something is close and higher ones indicate distance.
374                          */
375                         if (data->data[0] > PROXIMITY_THRESHOLD)
376                                 data->data[0] = PROXIMITY_THRESHOLD;
377
378                         /* ... fall through ... */
379                 case SENSOR_TYPE_LIGHT:
380                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
381                 case SENSOR_TYPE_TEMPERATURE:
382                 case SENSOR_TYPE_INTERNAL_ILLUMINANCE:
383                 case SENSOR_TYPE_INTERNAL_INTENSITY:
384                         /* Only keep two decimals for these readings */
385                         data->data[0] = 0.01 * ((int) (data->data[0] * 100));
386
387                         /* These are on change sensors ; drop the sample if it has the same value as the previously reported one. */
388                         if (data->data[0] == sensor[s].prev_val.data)
389                                 return 0;
390
391                         sensor[s].prev_val.data = data->data[0];
392                         break;
393                 case SENSOR_TYPE_STEP_COUNTER:
394                         if (data->u64.step_counter == sensor[s].prev_val.data64)
395                                 return 0;
396                         sensor[s].prev_val.data64 = data->u64.data[0];
397                         break;
398
399         }
400
401         /* If there are active virtual sensors depending on this one - process the event */
402         if (sensor[s].ref_count)
403                 process_event(s, data);
404
405
406         return 1; /* Return sample to Android */
407 }
408
409
410 static float transform_sample_default (int s, int c, unsigned char* sample_data)
411 {
412         datum_info_t* sample_type = &sensor[s].channel[c].type_info;
413         int64_t s64 = sample_as_int64(sample_data, sample_type);
414         float scale = sensor[s].scale ? sensor[s].scale : sensor[s].channel[c].scale;
415
416         /* In case correction has been requested using properties, apply it */
417         float correction = sensor[s].channel[c].opt_scale;
418
419         /* Correlated with "acquire_immediate_value" method */
420         if (sensor[s].type == SENSOR_TYPE_MAGNETIC_FIELD)
421                 return CONVERT_GAUSS_TO_MICROTESLA((sensor[s].offset + s64) * scale) * correction;
422
423         /* Apply default scaling rules */
424         return (sensor[s].offset + s64) * scale * correction;
425 }
426
427
428 static int finalize_sample_ISH (int s, sensors_event_t* data)
429 {
430         float pitch, roll, yaw;
431
432         /* Swap fields if we have a custom channel ordering on this sensor */
433         if (sensor[s].quirks & QUIRK_FIELD_ORDERING)
434                 reorder_fields(data->data, sensor[s].order);
435
436         if (sensor[s].type == SENSOR_TYPE_ORIENTATION) {
437
438                 pitch = data->data[0];
439                 roll = data->data[1];
440                 yaw = data->data[2];
441
442                 data->data[0] = 360.0 - yaw;
443                 data->data[1] = -pitch;
444                 data->data[2] = -roll;
445         }
446
447         /* Add this event to our global records, for filtering purposes */
448         record_sample(s, data);
449
450         return 1; /* Return sample to Android */
451 }
452
453
454 static float transform_sample_ISH (int s, int c, unsigned char* sample_data)
455 {
456         datum_info_t* sample_type = &sensor[s].channel[c].type_info;
457         int val         = (int) sample_as_int64(sample_data, sample_type);
458         float correction;
459         int data_bytes  = (sample_type->realbits)/8;
460         int exponent    = sensor[s].offset;
461
462         /* In case correction has been requested using properties, apply it */
463         correction = sensor[s].channel[c].opt_scale;
464
465         switch (sensor_desc[s].type) {
466                 case SENSOR_TYPE_ACCELEROMETER:
467                         switch (c) {
468                                 case 0:
469                                         return correction * CONVERT_A_G_VTF16E14_X(data_bytes, exponent, val);
470
471                                 case 1:
472                                         return correction * CONVERT_A_G_VTF16E14_Y(data_bytes, exponent, val);
473
474                                 case 2:
475                                         return  correction * CONVERT_A_G_VTF16E14_Z(data_bytes, exponent, val);
476                         }
477                         break;
478
479                 case SENSOR_TYPE_GYROSCOPE:
480                         switch (c) {
481                                 case 0:
482                                         return correction * CONVERT_G_D_VTF16E14_X(data_bytes, exponent, val);
483
484                                 case 1:
485                                         return correction * CONVERT_G_D_VTF16E14_Y(data_bytes, exponent, val);
486
487                                 case 2:
488                                         return  correction * CONVERT_G_D_VTF16E14_Z(data_bytes, exponent, val);
489                         }
490                         break;
491
492                 case SENSOR_TYPE_MAGNETIC_FIELD:
493                         switch (c) {
494                                 case 0:
495                                         return correction * CONVERT_M_MG_VTF16E14_X(data_bytes, exponent, val);
496
497                                 case 1:
498                                         return correction * CONVERT_M_MG_VTF16E14_Y(data_bytes, exponent, val);
499
500                                 case 2:
501                                         return correction * CONVERT_M_MG_VTF16E14_Z(data_bytes, exponent, val);
502                         }
503                         break;
504
505                 case SENSOR_TYPE_LIGHT:
506                         return (float) val;
507
508                 case SENSOR_TYPE_ORIENTATION:
509                         return correction * convert_from_vtf_format(data_bytes, exponent, val);
510
511                 case SENSOR_TYPE_ROTATION_VECTOR:
512                         return correction * convert_from_vtf_format(data_bytes, exponent, val);
513         }
514
515         return 0;
516 }
517
518
519 void select_transform (int s)
520 {
521         char prop_name[PROP_NAME_MAX];
522         char prop_val[PROP_VALUE_MAX];
523         int i                   = sensor[s].catalog_index;
524         const char *prefix      = sensor_catalog[i].tag;
525
526         sprintf(prop_name, PROP_BASE, prefix, "transform");
527
528         if (property_get(prop_name, prop_val, ""))
529                 if (!strcmp(prop_val, "ISH")) {
530                         ALOGI(  "Using Intel Sensor Hub semantics on %s\n", sensor[s].friendly_name);
531
532                         sensor[s].ops.transform = transform_sample_ISH;
533                         sensor[s].ops.finalize = finalize_sample_ISH;
534                         return;
535                 }
536
537         sensor[s].ops.transform = transform_sample_default;
538         sensor[s].ops.finalize = finalize_sample_default;
539 }
540
541
542 float acquire_immediate_float_value (int s, int c)
543 {
544         char sysfs_path[PATH_MAX];
545         float val;
546         int ret;
547         int dev_num = sensor[s].dev_num;
548         int i = sensor[s].catalog_index;
549         const char* raw_path = sensor_catalog[i].channel[c].raw_path;
550         const char* input_path = sensor_catalog[i].channel[c].input_path;
551         float scale = sensor[s].scale ? sensor[s].scale : sensor[s].channel[c].scale;
552         float offset = sensor[s].offset;
553         float correction;
554
555         /* In case correction has been requested using properties, apply it */
556         correction = sensor[s].channel[c].opt_scale;
557
558         /* Acquire a sample value for sensor s / channel c through sysfs */
559
560         if (sensor[s].channel[c].input_path_present) {
561                 sprintf(sysfs_path, BASE_PATH "%s", dev_num, input_path);
562                 ret = sysfs_read_float(sysfs_path, &val);
563
564                 if (!ret) {
565                         if (sensor[s].type == SENSOR_TYPE_MAGNETIC_FIELD)
566                                 return CONVERT_GAUSS_TO_MICROTESLA (val * correction);
567                         return val * correction;
568                 }
569         }
570
571         if (!sensor[s].channel[c].raw_path_present)
572                 return 0;
573
574         sprintf(sysfs_path, BASE_PATH "%s", dev_num, raw_path);
575         ret = sysfs_read_float(sysfs_path, &val);
576
577         if (ret == -1)
578                 return 0;
579
580         /*
581          * There is no transform ops defined yet for raw sysfs values.
582          * Use this function to perform transformation as well.
583          */
584         if (sensor[s].type == SENSOR_TYPE_MAGNETIC_FIELD)
585                 return CONVERT_GAUSS_TO_MICROTESLA ((val + offset) * scale) * correction;
586
587         return (val + offset) * scale * correction;
588 }
589
590 uint64_t acquire_immediate_uint64_value (int s, int c)
591 {
592         char sysfs_path[PATH_MAX];
593         uint64_t val;
594         int ret;
595         int dev_num = sensor[s].dev_num;
596         int i = sensor[s].catalog_index;
597         const char* raw_path = sensor_catalog[i].channel[c].raw_path;
598         const char* input_path = sensor_catalog[i].channel[c].input_path;
599         float scale = sensor[s].scale ? sensor[s].scale : sensor[s].channel[c].scale;
600         float offset = sensor[s].offset;
601         float correction;
602
603         /* In case correction has been requested using properties, apply it */
604         correction = sensor[s].channel[c].opt_scale;
605
606         /* Acquire a sample value for sensor s / channel c through sysfs */
607
608         if (sensor[s].channel[c].input_path_present) {
609                 sprintf(sysfs_path, BASE_PATH "%s", dev_num, input_path);
610                 ret = sysfs_read_uint64(sysfs_path, &val);
611
612                 if (!ret)
613                         return val * correction;
614         };
615
616         if (!sensor[s].channel[c].raw_path_present)
617                 return 0;
618
619         sprintf(sysfs_path, BASE_PATH "%s", dev_num, raw_path);
620         ret = sysfs_read_uint64(sysfs_path, &val);
621
622         if (ret == -1)
623                 return 0;
624
625         return (val + offset) * scale * correction;
626 }