OSDN Git Service

Add support for signed 13 and 15 bits samples
[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         uint64_t u64;
127         int i;
128         int zeroed_bits = type->storagebits - type->realbits;
129         uint64_t sign_mask;
130         uint64_t value_mask;
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         /* Signed integer */
147
148         switch (type->realbits) {
149                 case 0 ... 1:
150                         return 0;
151
152                 case 8:
153                         return (int64_t) (int8_t) u64;
154
155                 case 16:
156                         return (int64_t) (int16_t) u64;
157
158                 case 32:
159                         return (int64_t) (int32_t) u64;
160
161                 case 64:
162                         return (int64_t) u64;
163
164                 default:
165                         sign_mask = 1 << (type->realbits-1);
166                         value_mask = sign_mask - 1;
167
168                         if (u64 & sign_mask)
169                                 /* Negative value: return 2-complement */
170                                 return - ((~u64 & value_mask) + 1);
171                         else
172                                 return (int64_t) u64; /* Positive value */
173         }
174 }
175
176
177 static void reorder_fields(float* data, unsigned char map[MAX_CHANNELS])
178 {
179         int i;
180         float temp[MAX_CHANNELS];
181
182         for (i=0; i<MAX_CHANNELS; i++)
183                 temp[i] = data[map[i]];
184
185         for (i=0; i<MAX_CHANNELS; i++)
186                 data[i] = temp[i];
187 }
188
189
190 static int finalize_sample_default(int s, struct sensors_event_t* data)
191 {
192         int i           = sensor_info[s].catalog_index;
193         int sensor_type = sensor_catalog[i].type;
194
195         /* Swap fields if we have a custom channel ordering on this sensor */
196         if (sensor_info[s].flags & FLAG_FIELD_ORDERING)
197                 reorder_fields(data->data, sensor_info[s].order);
198
199         switch (sensor_type) {
200                 case SENSOR_TYPE_ACCELEROMETER:
201                         break;
202
203                 case SENSOR_TYPE_MAGNETIC_FIELD:
204                         calibrate_compass (data, &sensor_info[s], get_timestamp());
205                         break;
206
207                 case SENSOR_TYPE_GYROSCOPE:
208                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
209                         calibrate_gyro(data, &sensor_info[s]);
210                         break;
211
212                 case SENSOR_TYPE_LIGHT:
213                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
214                 case SENSOR_TYPE_TEMPERATURE:
215                         /* Only keep two decimals for these readings */
216                         data->data[0] = 0.01 * ((int) (data->data[0] * 100));
217
218                         /*
219                          * These are on change sensors ; drop the sample if it
220                          * has the same value as the previously reported one.
221                          */
222                         if (data->data[0] == sensor_info[s].prev_val)
223                                 return 0;
224
225                         sensor_info[s].prev_val = data->data[0];
226                         break;
227         }
228
229         return 1; /* Return sample to Android */
230 }
231
232
233 static float transform_sample_default(int s, int c, unsigned char* sample_data)
234 {
235         struct datum_info_t* sample_type = &sensor_info[s].channel[c].type_info;
236         int64_t              s64 = sample_as_int64(sample_data, sample_type);
237         float scale = sensor_info[s].scale ?
238                         sensor_info[s].scale : sensor_info[s].channel[c].scale;
239
240         /* In case correction has been requested using properties, apply it */
241         scale *= sensor_info[s].channel[c].opt_scale;
242
243         /* Apply default scaling rules */
244         return (sensor_info[s].offset + s64) * scale;
245 }
246
247
248 static int finalize_sample_ISH(int s, struct sensors_event_t* data)
249 {
250         int i           = sensor_info[s].catalog_index;
251         int sensor_type = sensor_catalog[i].type;
252         float pitch, roll, yaw;
253
254         /* Swap fields if we have a custom channel ordering on this sensor */
255         if (sensor_info[s].flags & FLAG_FIELD_ORDERING)
256                 reorder_fields(data->data, sensor_info[s].order);
257
258         if (sensor_type == SENSOR_TYPE_ORIENTATION) {
259
260                 pitch = data->data[0];
261                 roll = data->data[1];
262                 yaw = data->data[2];
263
264                 data->data[0] = 360.0 - yaw;
265                 data->data[1] = -pitch;
266                 data->data[2] = -roll;
267         }
268
269         return 1; /* Return sample to Android */
270 }
271
272
273 static float transform_sample_ISH(int s, int c, unsigned char* sample_data)
274 {
275         struct datum_info_t* sample_type = &sensor_info[s].channel[c].type_info;
276         int val         = (int) sample_as_int64(sample_data, sample_type);
277         int i           = sensor_info[s].catalog_index;
278         int sensor_type = sensor_catalog[i].type;
279         float correction;
280         int data_bytes  = (sample_type->realbits)/8;
281         int exponent    = sensor_info[s].offset;
282
283         /* In case correction has been requested using properties, apply it */
284         correction = sensor_info[s].channel[c].opt_scale;
285
286         switch (sensor_type) {
287                 case SENSOR_TYPE_ACCELEROMETER:
288                         switch (c) {
289                                 case 0:
290                                         return  correction *
291                                                 CONVERT_A_G_VTF16E14_X(
292                                                 data_bytes, exponent, val);
293
294                                 case 1:
295                                         return  correction *
296                                                 CONVERT_A_G_VTF16E14_Y(
297                                                 data_bytes, exponent, val);
298
299                                 case 2:
300                                         return  correction *
301                                                 CONVERT_A_G_VTF16E14_Z(
302                                                 data_bytes, exponent, val);
303                         }
304                         break;
305
306
307                 case SENSOR_TYPE_GYROSCOPE:
308                         switch (c) {
309                                 case 0:
310                                         return  correction *
311                                                 CONVERT_G_D_VTF16E14_X(
312                                                 data_bytes, exponent, val);
313
314                                 case 1:
315                                         return  correction *
316                                                 CONVERT_G_D_VTF16E14_Y(
317                                                 data_bytes, exponent, val);
318
319                                 case 2:
320                                         return  correction *
321                                                 CONVERT_G_D_VTF16E14_Z(
322                                                 data_bytes, exponent, val);
323                         }
324                         break;
325
326                 case SENSOR_TYPE_MAGNETIC_FIELD:
327                         switch (c) {
328                                 case 0:
329                                         return  correction *
330                                                 CONVERT_M_MG_VTF16E14_X(
331                                                 data_bytes, exponent, val);
332
333                                 case 1:
334                                         return  correction *
335                                                 CONVERT_M_MG_VTF16E14_Y(
336                                                 data_bytes, exponent, val);
337
338                                 case 2:
339                                         return  correction *
340                                                 CONVERT_M_MG_VTF16E14_Z(
341                                                 data_bytes, exponent, val);
342                         }
343                         break;
344
345                 case SENSOR_TYPE_LIGHT:
346                                 return (float) val;
347
348                 case SENSOR_TYPE_ORIENTATION:
349                         return  correction * convert_from_vtf_format(
350                                                 data_bytes, exponent, val);
351
352                 case SENSOR_TYPE_ROTATION_VECTOR:
353                         return  correction * convert_from_vtf_format(
354                                                 data_bytes, exponent, val);
355         }
356
357         return 0;
358 }
359
360
361 void select_transform (int s)
362 {
363         char prop_name[PROP_NAME_MAX];
364         char prop_val[PROP_VALUE_MAX];
365         int i                   = sensor_info[s].catalog_index;
366         const char *prefix      = sensor_catalog[i].tag;
367
368         sprintf(prop_name, PROP_BASE, prefix, "transform");
369
370         if (property_get(prop_name, prop_val, "")) {
371                 if (!strcmp(prop_val, "ISH")) {
372                         ALOGI(  "Using Intel Sensor Hub semantics on %s\n",
373                                 sensor_info[s].friendly_name);
374
375                         sensor_info[s].ops.transform = transform_sample_ISH;
376                         sensor_info[s].ops.finalize = finalize_sample_ISH;
377                         return;
378                 }
379         }
380
381         sensor_info[s].ops.transform = transform_sample_default;
382         sensor_info[s].ops.finalize = finalize_sample_default;
383 }
384
385
386 float acquire_immediate_value(int s, int c)
387 {
388         char sysfs_path[PATH_MAX];
389         float val;
390         int ret;
391         int dev_num = sensor_info[s].dev_num;
392         int i = sensor_info[s].catalog_index;
393         const char* raw_path = sensor_catalog[i].channel[c].raw_path;
394         const char* input_path = sensor_catalog[i].channel[c].input_path;
395         float scale = sensor_info[s].scale ?
396                         sensor_info[s].scale : sensor_info[s].channel[c].scale;
397         float offset = sensor_info[s].offset;
398         int sensor_type = sensor_catalog[i].type;
399         float correction;
400
401         /* In case correction has been requested using properties, apply it */
402         correction = sensor_info[s].channel[c].opt_scale;
403
404         /* Acquire a sample value for sensor s / channel c through sysfs */
405
406         if (input_path[0]) {
407                 sprintf(sysfs_path, BASE_PATH "%s", dev_num, input_path);
408                 ret = sysfs_read_float(sysfs_path, &val);
409
410                 if (!ret) {
411                         return val * correction;
412                 }
413         };
414
415         if (!raw_path[0])
416                 return 0;
417
418         sprintf(sysfs_path, BASE_PATH "%s", dev_num, raw_path);
419         ret = sysfs_read_float(sysfs_path, &val);
420
421         if (ret == -1)
422                 return 0;
423
424         /*
425         There is no transform ops defined yet for Raw sysfs values
426         Use this function to perform transformation as well.
427         */
428         if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD)
429                 return  CONVERT_GAUSS_TO_MICROTESLA ((val + offset) * scale) *
430                         correction;
431
432         return (val + offset) * scale * correction;
433 }