OSDN Git Service

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