OSDN Git Service

Adding TYPE_GYROSCOPE_UNCALIBRATED
[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 "common.h"
11 #include "transform.h"
12 #include "utils.h"
13 #include "calibration.h"
14
15 /*----------------------------------------------------------------------------*/
16
17 /* Macros related to Intel Sensor Hub */
18
19 #define GRAVITY 9.80665f
20
21 /* 720 LSG = 1G */
22 #define LSG                         (1024.0f)
23 #define NUMOFACCDATA                (8.0f)
24
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))
30
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)
36
37 #define CONVERT_GAUSS_TO_MICROTESLA(x)        ( (x) * 100 )
38
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)
44
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)
50
51 #define BIT(x) (1 << (x))
52
53 inline unsigned int set_bit_range(int start, int end)
54 {
55     int i;
56     unsigned int value = 0;
57
58     for (i = start; i < end; ++i)
59         value |= BIT(i);
60     return value;
61 }
62
63 inline float convert_from_vtf_format(int size, int exponent, unsigned int value)
64 {
65     int divider=1;
66     int i;
67     float sample;
68     int mul = 1.0;
69
70     value = value & set_bit_range(0, size*8);
71     if (value & BIT(size*8-1)) {
72         value =  ((1LL << (size*8)) - value);
73         mul = -1.0;
74     }
75     sample = value * 1.0;
76     if (exponent < 0) {
77         exponent = abs(exponent);
78         for (i = 0; i < exponent; ++i) {
79             divider = divider*10;
80         }
81         return mul * sample/divider;
82     } else {
83         return mul * sample * pow(10.0, exponent);
84     }
85 }
86
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
91
92 #define DEF_ORIENT_GYRO_X                   1
93 #define DEF_ORIENT_GYRO_Y                   1
94 #define DEF_ORIENT_GYRO_Z                   1
95
96 // G to m/s2
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)
104
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))
115
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)
120
121
122 /*----------------------------------------------------------------------------*/
123
124 static int64_t sample_as_int64(unsigned char* sample, struct datum_info_t* type)
125 {
126         uint16_t u16;
127         uint32_t u32;
128         uint64_t u64;
129         int i;
130         int zeroed_bits = type->storagebits - type->realbits;
131
132         u64 = 0;
133
134         if (type->endianness == 'b')
135                 for (i=0; i<type->storagebits/8; i++)
136                         u64 = (u64 << 8) | sample[i];
137         else
138                 for (i=type->storagebits/8 - 1; i>=0; i--)
139                         u64 = (u64 << 8) | sample[i];
140
141         u64 = (u64 >> type->shift) & (~0ULL >> zeroed_bits);
142
143         if (type->sign == 'u')
144                 return (int64_t) u64; /* We don't handle unsigned 64 bits int */
145
146         switch (type->realbits) {
147                 case 8:
148                         return (int64_t) (int8_t) u64;
149
150                 case 12:
151                         return (int64_t)  (u64 >>  11) ?
152                                         (((int64_t)-1) ^ 0xfff) | u64 : u64;
153
154                 case 16:
155                         return (int64_t) (int16_t) u64;
156
157                 case 32:
158                         return (int64_t) (int32_t) u64;
159
160                 case 64:
161                         return (int64_t) u64;
162         }
163
164         ALOGE("Unhandled sample storage size\n");
165         return 0;
166 }
167
168
169 static void reorder_fields(float* data, unsigned char map[MAX_CHANNELS])
170 {
171         int i;
172         float temp[MAX_CHANNELS];
173
174         for (i=0; i<MAX_CHANNELS; i++)
175                 temp[i] = data[map[i]];
176
177         for (i=0; i<MAX_CHANNELS; i++)
178                 data[i] = temp[i];
179 }
180
181
182 static int finalize_sample_default(int s, struct sensors_event_t* data)
183 {
184         int i           = sensor_info[s].catalog_index;
185         int sensor_type = sensor_catalog[i].type;
186
187         /* Swap fields if we have a custom channel ordering on this sensor */
188         if (sensor_info[s].flags & FLAG_FIELD_ORDERING)
189                 reorder_fields(data->data, sensor_info[s].order);
190
191         switch (sensor_type) {
192                 case SENSOR_TYPE_ACCELEROMETER:
193                         break;
194
195                 case SENSOR_TYPE_MAGNETIC_FIELD:
196                         calibrate_compass (data, &sensor_info[s], get_timestamp());
197                         break;
198
199                 case SENSOR_TYPE_GYROSCOPE:
200                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
201                         calibrate_gyro(data, &sensor_info[s]);
202                         break;
203
204                 case SENSOR_TYPE_LIGHT:
205                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
206                 case SENSOR_TYPE_TEMPERATURE:
207                         /* Only keep two decimals for these readings */
208                         data->data[0] = 0.01 * ((int) (data->data[0] * 100));
209
210                         /*
211                          * These are on change sensors ; drop the sample if it
212                          * has the same value as the previously reported one.
213                          */
214                         if (data->data[0] == sensor_info[s].prev_val)
215                                 return 0;
216
217                         sensor_info[s].prev_val = data->data[0];
218                         break;
219         }
220
221         return 1; /* Return sample to Android */
222 }
223
224
225 static float transform_sample_default(int s, int c, unsigned char* sample_data)
226 {
227         struct datum_info_t* sample_type = &sensor_info[s].channel[c].type_info;
228         int64_t              s64 = sample_as_int64(sample_data, sample_type);
229         float scale = sensor_info[s].scale ?
230                         sensor_info[s].scale : sensor_info[s].channel[c].scale;
231
232         /* In case correction has been requested using properties, apply it */
233         scale *= sensor_info[s].channel[c].opt_scale;
234
235         /* Apply default scaling rules */
236         return (sensor_info[s].offset + s64) * scale;
237 }
238
239
240 static int finalize_sample_ISH(int s, struct sensors_event_t* data)
241 {
242         int i           = sensor_info[s].catalog_index;
243         int sensor_type = sensor_catalog[i].type;
244         float pitch, roll, yaw;
245
246         /* Swap fields if we have a custom channel ordering on this sensor */
247         if (sensor_info[s].flags & FLAG_FIELD_ORDERING)
248                 reorder_fields(data->data, sensor_info[s].order);
249
250         if (sensor_type == SENSOR_TYPE_ORIENTATION) {
251
252                 pitch = data->data[0];
253                 roll = data->data[1];
254                 yaw = data->data[2];
255
256                 data->data[0] = 360.0 - yaw;
257                 data->data[1] = -pitch;
258                 data->data[2] = -roll;
259         }
260
261         return 1; /* Return sample to Android */
262 }
263
264
265 static float transform_sample_ISH(int s, int c, unsigned char* sample_data)
266 {
267         struct datum_info_t* sample_type = &sensor_info[s].channel[c].type_info;
268         int val         = (int) sample_as_int64(sample_data, sample_type);
269         int i           = sensor_info[s].catalog_index;
270         int sensor_type = sensor_catalog[i].type;
271         float correction;
272         int data_bytes  = (sample_type->realbits)/8;
273         int exponent    = sensor_info[s].offset;
274
275         /* In case correction has been requested using properties, apply it */
276         correction = sensor_info[s].channel[c].opt_scale;
277
278         switch (sensor_type) {
279                 case SENSOR_TYPE_ACCELEROMETER:
280                         switch (c) {
281                                 case 0:
282                                         return  correction *
283                                                 CONVERT_A_G_VTF16E14_X(
284                                                 data_bytes, exponent, val);
285
286                                 case 1:
287                                         return  correction *
288                                                 CONVERT_A_G_VTF16E14_Y(
289                                                 data_bytes, exponent, val);
290
291                                 case 2:
292                                         return  correction *
293                                                 CONVERT_A_G_VTF16E14_Z(
294                                                 data_bytes, exponent, val);
295                         }
296                         break;
297
298
299                 case SENSOR_TYPE_GYROSCOPE:
300                         switch (c) {
301                                 case 0:
302                                         return  correction *
303                                                 CONVERT_G_D_VTF16E14_X(
304                                                 data_bytes, exponent, val);
305
306                                 case 1:
307                                         return  correction *
308                                                 CONVERT_G_D_VTF16E14_Y(
309                                                 data_bytes, exponent, val);
310
311                                 case 2:
312                                         return  correction *
313                                                 CONVERT_G_D_VTF16E14_Z(
314                                                 data_bytes, exponent, val);
315                         }
316                         break;
317
318                 case SENSOR_TYPE_MAGNETIC_FIELD:
319                         switch (c) {
320                                 case 0:
321                                         return  correction *
322                                                 CONVERT_M_MG_VTF16E14_X(
323                                                 data_bytes, exponent, val);
324
325                                 case 1:
326                                         return  correction *
327                                                 CONVERT_M_MG_VTF16E14_Y(
328                                                 data_bytes, exponent, val);
329
330                                 case 2:
331                                         return  correction *
332                                                 CONVERT_M_MG_VTF16E14_Z(
333                                                 data_bytes, exponent, val);
334                         }
335                         break;
336
337                 case SENSOR_TYPE_LIGHT:
338                                 return (float) val;
339
340                 case SENSOR_TYPE_ORIENTATION:
341                         return  correction * convert_from_vtf_format(
342                                                 data_bytes, exponent, val);
343
344                 case SENSOR_TYPE_ROTATION_VECTOR:
345                         return  correction * convert_from_vtf_format(
346                                                 data_bytes, exponent, val);
347         }
348
349         return 0;
350 }
351
352
353 void select_transform (int s)
354 {
355         char prop_name[PROP_NAME_MAX];
356         char prop_val[PROP_VALUE_MAX];
357         int i                   = sensor_info[s].catalog_index;
358         const char *prefix      = sensor_catalog[i].tag;
359
360         sprintf(prop_name, PROP_BASE, prefix, "transform");
361
362         if (property_get(prop_name, prop_val, "")) {
363                 if (!strcmp(prop_val, "ISH")) {
364                         ALOGI(  "Using Intel Sensor Hub semantics on %s\n",
365                                 sensor_info[s].friendly_name);
366
367                         sensor_info[s].ops.transform = transform_sample_ISH;
368                         sensor_info[s].ops.finalize = finalize_sample_ISH;
369                         return;
370                 }
371         }
372
373         sensor_info[s].ops.transform = transform_sample_default;
374         sensor_info[s].ops.finalize = finalize_sample_default;
375 }
376
377
378 float acquire_immediate_value(int s, int c)
379 {
380         char sysfs_path[PATH_MAX];
381         float val;
382         int ret;
383         int dev_num = sensor_info[s].dev_num;
384         int i = sensor_info[s].catalog_index;
385         const char* raw_path = sensor_catalog[i].channel[c].raw_path;
386         const char* input_path = sensor_catalog[i].channel[c].input_path;
387         float scale = sensor_info[s].scale ?
388                         sensor_info[s].scale : sensor_info[s].channel[c].scale;
389         float offset = sensor_info[s].offset;
390         int sensor_type = sensor_catalog[i].type;
391         float correction;
392
393         /* In case correction has been requested using properties, apply it */
394         correction = sensor_info[s].channel[c].opt_scale;
395
396         /* Acquire a sample value for sensor s / channel c through sysfs */
397
398         if (input_path[0]) {
399                 sprintf(sysfs_path, BASE_PATH "%s", dev_num, input_path);
400                 ret = sysfs_read_float(sysfs_path, &val);
401
402                 if (!ret) {
403                         return val * correction;
404                 }
405         };
406
407         if (!raw_path[0])
408                 return 0;
409
410         sprintf(sysfs_path, BASE_PATH "%s", dev_num, raw_path);
411         ret = sysfs_read_float(sysfs_path, &val);
412
413         if (ret == -1)
414                 return 0;
415
416         /*
417         There is no transform ops defined yet for Raw sysfs values
418         Use this function to perform transformation as well.
419         */
420         if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD)
421                 return  CONVERT_GAUSS_TO_MICROTESLA ((val + offset) * scale) *
422                         correction;
423
424         return (val + offset) * scale * correction;
425 }