OSDN Git Service

GMINL-2659: Keep recent events history for fusion-like processing
[android-x86/hardware-intel-libsensors.git] / transform.c
index ba4e85f..a49300d 100644 (file)
@@ -7,9 +7,12 @@
 #include <utils/Log.h>
 #include <cutils/properties.h>
 #include <hardware/sensors.h>
+#include "calibration.h"
 #include "common.h"
+#include "description.h"
 #include "transform.h"
 #include "utils.h"
+#include "filtering.h"
 
 /*----------------------------------------------------------------------------*/
 
@@ -33,6 +36,8 @@
 #define CONVERT_M_Y                 (-CONVERT_M)
 #define CONVERT_M_Z                 (CONVERT_M)
 
+#define CONVERT_GAUSS_TO_MICROTESLA(x)        ( (x) * 100 )
+
 /* conversion of orientation data to degree units */
 #define CONVERT_O                   (1.0f/64.0f)
 #define CONVERT_O_A                 (CONVERT_O)
@@ -115,94 +120,225 @@ inline float convert_from_vtf_format(int size, int exponent, unsigned int value)
 #define CONVERT_M_MG_VTF16E14_Y(s,d,x) (convert_from_vtf_format(s,d,x)/10)
 #define CONVERT_M_MG_VTF16E14_Z(s,d,x) (convert_from_vtf_format(s,d,x)/10)
 
-#define DATA_BYTES     2
-#define ACC_EXPONENT   -2
-#define GYRO_EXPONENT  -1
-#define MAGN_EXPONENT  0
-#define INC_EXPONENT   -1
-#define ROT_EXPONENT   -8
 
 /*----------------------------------------------------------------------------*/
 
 static int64_t sample_as_int64(unsigned char* sample, struct datum_info_t* type)
 {
-       uint16_t u16;
-       uint32_t u32;
        uint64_t u64;
        int i;
+       int zeroed_bits = type->storagebits - type->realbits;
+       uint64_t sign_mask;
+       uint64_t value_mask;
 
-       switch (type->storagebits) {
-               case 64:
-                       u64 = 0;
+       u64 = 0;
 
-                       if (type->endianness == 'b')
-                               for (i=0; i<8; i++)
-                                       u64 = (u64 << 8) | sample[i];
-                       else
-                               for (i=7; i>=0; i--)
-                                       u64 = (u64 << 8) | sample[i];
+       if (type->endianness == 'b')
+               for (i=0; i<type->storagebits/8; i++)
+                       u64 = (u64 << 8) | sample[i];
+       else
+               for (i=type->storagebits/8 - 1; i>=0; i--)
+                       u64 = (u64 << 8) | sample[i];
 
-                       if (type->sign == 'u')
-                               return (int64_t) (u64 >> type->shift);
+       u64 = (u64 >> type->shift) & (~0ULL >> zeroed_bits);
 
-                       return ((int64_t) u64) >> type->shift;
+       if (type->sign == 'u')
+               return (int64_t) u64; /* We don't handle unsigned 64 bits int */
 
-               case 32:
-                       if (type->endianness == 'b')
-                               u32 = (sample[0] << 24) | (sample[1] << 16) |
-                                       (sample[2] << 8) | sample[3];
-                       else
-                               u32 = (sample[3] << 24) | (sample[2] << 16) |
-                                       (sample[1] << 8) | sample[0];
+       /* Signed integer */
 
-                       if (type->sign == 'u')
-                               return u32 >> type->shift;
+       switch (type->realbits) {
+               case 0 ... 1:
+                       return 0;
 
-                       return ((int32_t) u32) >> type->shift;
+               case 8:
+                       return (int64_t) (int8_t) u64;
 
                case 16:
-                       if (type->endianness == 'b')
-                               u16 = (sample[0] << 8) | sample[1];
+                       return (int64_t) (int16_t) u64;
+
+               case 32:
+                       return (int64_t) (int32_t) u64;
+
+               case 64:
+                       return (int64_t) u64;
+
+               default:
+                       sign_mask = 1 << (type->realbits-1);
+                       value_mask = sign_mask - 1;
+
+                       if (u64 & sign_mask)
+                               /* Negative value: return 2-complement */
+                               return - ((~u64 & value_mask) + 1);
                        else
-                               u16 = (sample[1] << 8) | sample[0];
+                               return (int64_t) u64; /* Positive value */
+       }
+}
 
-                       if (type->sign == 'u')
-                               return u16 >> type->shift;
 
-                       return  ((int16_t) u16) >> type->shift;
+static void reorder_fields(float* data,        unsigned char map[MAX_CHANNELS])
+{
+       int i;
+       float temp[MAX_CHANNELS];
+
+       for (i=0; i<MAX_CHANNELS; i++)
+               temp[i] = data[map[i]];
+
+       for (i=0; i<MAX_CHANNELS; i++)
+               data[i] = temp[i];
+}
+
+
+static void denoise (struct sensor_info_t* si, struct sensors_event_t* data,
+                    int num_fields, int max_samples)
+{
+       /*
+        * Smooth out incoming data using a moving average over a number of
+        * samples. We accumulate one second worth of samples, or max_samples,
+        * depending on which is lower.
+        */
+
+       int i;
+       int f;
+       int sampling_rate = (int) si->sampling_rate;
+       int history_size;
+       int history_full = 0;
+
+       /* Don't denoise anything if we have less than two samples per second */
+       if (sampling_rate < 2)
+               return;
+
+       /* Restrict window size to the min of sampling_rate and max_samples */
+       if (sampling_rate > max_samples)
+               history_size = max_samples;
+       else
+               history_size = sampling_rate;
+
+       /* Reset history if we're operating on an incorrect window size */
+       if (si->history_size != history_size) {
+               si->history_size = history_size;
+               si->history_entries = 0;
+               si->history_index = 0;
+               si->history = (float*) realloc(si->history,
+                               si->history_size * num_fields * sizeof(float));
+               if (si->history) {
+                       si->history_sum = (float*) realloc(si->history_sum,
+                               num_fields * sizeof(float));
+                       if (si->history_sum)
+                               memset(si->history_sum, 0, num_fields * sizeof(float));
+               }
        }
 
-       ALOGE("Unhandled sample storage size\n");
-       return 0;
+       if (!si->history || !si->history_sum)
+               return; /* Unlikely, but still... */
+
+       /* Update initialized samples count */
+       if (si->history_entries < si->history_size)
+               si->history_entries++;
+       else
+               history_full = 1;
+
+       /* Record new sample and calculate the moving sum */
+       for (f=0; f < num_fields; f++) {
+               /**
+                * A field is going to be overwritten if
+                * history is full, so decrease the history sum
+                */
+               if (history_full)
+                       si->history_sum[f] -=
+                               si->history[si->history_index * num_fields + f];
+
+               si->history[si->history_index * num_fields + f] = data->data[f];
+               si->history_sum[f] += data->data[f];
+
+               /* For now simply compute a mobile mean for each field */
+               /* and output filtered data */
+               data->data[f] = si->history_sum[f] / si->history_entries;
+       }
+
+       /* Update our rolling index (next evicted cell) */
+       si->history_index = (si->history_index + 1) % si->history_size;
 }
 
 
-static void finalize_sample_default(int s, struct sensors_event_t* data)
+static int finalize_sample_default (int s, struct sensors_event_t* data)
 {
-       int i           = sensor_info[s].catalog_index;
-       int sensor_type = sensor_catalog[i].type;
+       /* Swap fields if we have a custom channel ordering on this sensor */
+       if (sensor_info[s].quirks & QUIRK_FIELD_ORDERING)
+               reorder_fields(data->data, sensor_info[s].order);
 
-       switch (sensor_type) {
+       sensor_info[s].event_count++;
+       switch (sensor_info[s].type) {
                case SENSOR_TYPE_ACCELEROMETER:
+                       /* Always consider the accelerometer accurate */
+                       data->acceleration.status = SENSOR_STATUS_ACCURACY_HIGH;
+                       if (sensor_info[s].quirks & QUIRK_NOISY)
+                               denoise(&sensor_info[s], data, 3, 20);
+                       break;
+
+               case SENSOR_TYPE_MAGNETIC_FIELD:
+                       calibrate_compass (data, &sensor_info[s], get_timestamp());
+                       if (sensor_info[s].quirks & QUIRK_NOISY)
+                               denoise(&sensor_info[s], data, 3, 100);
+                       break;
+
+               case SENSOR_TYPE_GYROSCOPE:
+               case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
+                       /*
+                        * Report medium accuracy by default ; higher accuracy
+                        * levels will be reported once, and if, we achieve
+                        * calibration.
+                        */
+                       data->gyro.status = SENSOR_STATUS_ACCURACY_MEDIUM;
+
                        /*
-                        * Invert x axis orientation from SI units - see
-                        * /hardware/libhardware/include/hardware/sensors.h
-                        * for a discussion of what Android expects
+                        * We're only trying to calibrate data from continuously
+                        * firing gyroscope drivers, as motion based ones use
+                        * movement thresholds that may lead us to incorrectly
+                        * estimate bias.
                         */
-                       data->data[0] = -data->data[0];
+                       if (sensor_info[s].selected_trigger !=
+                               sensor_info[s].motion_trigger_name)
+                                       calibrate_gyro(data, &sensor_info[s]);
+
+                       /* For noisy sensors we'll drop a very few number
+                        * of samples to make sure we have at least MIN_SAMPLES events
+                        * in the filtering queue. This is to make sure we are not sending
+                        * events that can disturb our mean or stddev.
+                        */
+                       if (sensor_info[s].quirks & QUIRK_NOISY) {
+                               denoise_median(&sensor_info[s], data, 3);
+                               if((sensor_info[s].selected_trigger !=
+                                       sensor_info[s].motion_trigger_name) &&
+                                       sensor_info[s].event_count < MIN_SAMPLES)
+                                               return 0;
+                       }
                        break;
 
-               case SENSOR_TYPE_GYROSCOPE:
-                       /* Limit drift */
-                       if (    fabs(data->data[0]) < 0.1 &&
-                               fabs(data->data[1]) < 0.1 &&
-                               fabs(data->data[2]) < 0.1) {
-                                       data->data[0] = 0;
-                                       data->data[1] = 0;
-                                       data->data[2] = 0;
-                               }
+               case SENSOR_TYPE_LIGHT:
+               case SENSOR_TYPE_AMBIENT_TEMPERATURE:
+               case SENSOR_TYPE_TEMPERATURE:
+                       /* Only keep two decimals for these readings */
+                       data->data[0] = 0.01 * ((int) (data->data[0] * 100));
+
+                       /* ... fall through ... */
+
+               case SENSOR_TYPE_PROXIMITY:
+                       /*
+                        * These are on change sensors ; drop the sample if it
+                        * has the same value as the previously reported one.
+                        */
+                       if (data->data[0] == sensor_info[s].prev_val)
+                               return 0;
+
+                       sensor_info[s].prev_val = data->data[0];
                        break;
        }
+
+       /* Add this event to our global records, for filtering purposes */
+       record_sample(s, data);
+
+       return 1; /* Return sample to Android */
 }
 
 
@@ -210,19 +346,26 @@ static float transform_sample_default(int s, int c, unsigned char* sample_data)
 {
        struct datum_info_t* sample_type = &sensor_info[s].channel[c].type_info;
        int64_t              s64 = sample_as_int64(sample_data, sample_type);
+       float scale = sensor_info[s].scale ?
+                       sensor_info[s].scale : sensor_info[s].channel[c].scale;
+
+       /* In case correction has been requested using properties, apply it */
+       scale *= sensor_info[s].channel[c].opt_scale;
 
        /* Apply default scaling rules */
-       return (sensor_info[s].offset + s64) * sensor_info[s].scale;
+       return (sensor_info[s].offset + s64) * scale;
 }
 
 
-static void finalize_sample_ISH(int s, struct sensors_event_t* data)
+static int finalize_sample_ISH (int s, struct sensors_event_t* data)
 {
-       int i           = sensor_info[s].catalog_index;
-       int sensor_type = sensor_catalog[i].type;
        float pitch, roll, yaw;
 
-       if (sensor_type == SENSOR_TYPE_ORIENTATION) {
+       /* Swap fields if we have a custom channel ordering on this sensor */
+       if (sensor_info[s].quirks & QUIRK_FIELD_ORDERING)
+               reorder_fields(data->data, sensor_info[s].order);
+
+       if (sensor_info[s].type == SENSOR_TYPE_ORIENTATION) {
 
                pitch = data->data[0];
                roll = data->data[1];
@@ -232,30 +375,42 @@ static void finalize_sample_ISH(int s, struct sensors_event_t* data)
                data->data[1] = -pitch;
                data->data[2] = -roll;
        }
+
+       /* Add this event to our global records, for filtering purposes */
+       record_sample(s, data);
+
+       return 1; /* Return sample to Android */
 }
 
 
-static float transform_sample_ISH(int s, int c, unsigned char* sample_data)
+static float transform_sample_ISH (int s, int c, unsigned char* sample_data)
 {
        struct datum_info_t* sample_type = &sensor_info[s].channel[c].type_info;
        int val         = (int) sample_as_int64(sample_data, sample_type);
-       int i           = sensor_info[s].catalog_index;
-       int sensor_type = sensor_catalog[i].type;
+       float correction;
+       int data_bytes  = (sample_type->realbits)/8;
+       int exponent    = sensor_info[s].offset;
+
+       /* In case correction has been requested using properties, apply it */
+       correction = sensor_info[s].channel[c].opt_scale;
 
-       switch (sensor_type) {
+       switch (sensor_info[s].type) {
                case SENSOR_TYPE_ACCELEROMETER:
                        switch (c) {
                                case 0:
-                                       return CONVERT_A_G_VTF16E14_X(
-                                               DATA_BYTES, ACC_EXPONENT, val);
+                                       return  correction *
+                                               CONVERT_A_G_VTF16E14_X(
+                                               data_bytes, exponent, val);
 
                                case 1:
-                                       return CONVERT_A_G_VTF16E14_Y(
-                                               DATA_BYTES, ACC_EXPONENT, val);
+                                       return  correction *
+                                               CONVERT_A_G_VTF16E14_Y(
+                                               data_bytes, exponent, val);
 
                                case 2:
-                                       return CONVERT_A_G_VTF16E14_Z(
-                                               DATA_BYTES, ACC_EXPONENT, val);
+                                       return  correction *
+                                               CONVERT_A_G_VTF16E14_Z(
+                                               data_bytes, exponent, val);
                        }
                        break;
 
@@ -263,42 +418,51 @@ static float transform_sample_ISH(int s, int c, unsigned char* sample_data)
                case SENSOR_TYPE_GYROSCOPE:
                        switch (c) {
                                case 0:
-                                       return CONVERT_G_D_VTF16E14_X(
-                                               DATA_BYTES, GYRO_EXPONENT, val);
+                                       return  correction *
+                                               CONVERT_G_D_VTF16E14_X(
+                                               data_bytes, exponent, val);
 
                                case 1:
-                                       return CONVERT_G_D_VTF16E14_Y(
-                                               DATA_BYTES, GYRO_EXPONENT, val);
+                                       return  correction *
+                                               CONVERT_G_D_VTF16E14_Y(
+                                               data_bytes, exponent, val);
 
                                case 2:
-                                       return CONVERT_G_D_VTF16E14_Z(
-                                               DATA_BYTES, GYRO_EXPONENT, val);
+                                       return  correction *
+                                               CONVERT_G_D_VTF16E14_Z(
+                                               data_bytes, exponent, val);
                        }
                        break;
 
                case SENSOR_TYPE_MAGNETIC_FIELD:
                        switch (c) {
                                case 0:
-                                       return CONVERT_M_MG_VTF16E14_X(
-                                               DATA_BYTES, MAGN_EXPONENT, val);
+                                       return  correction *
+                                               CONVERT_M_MG_VTF16E14_X(
+                                               data_bytes, exponent, val);
 
                                case 1:
-                                       return CONVERT_M_MG_VTF16E14_Y(
-                                               DATA_BYTES, MAGN_EXPONENT, val);
+                                       return  correction *
+                                               CONVERT_M_MG_VTF16E14_Y(
+                                               data_bytes, exponent, val);
 
                                case 2:
-                                       return CONVERT_M_MG_VTF16E14_Z(
-                                               DATA_BYTES, MAGN_EXPONENT, val);
+                                       return  correction *
+                                               CONVERT_M_MG_VTF16E14_Z(
+                                               data_bytes, exponent, val);
                        }
                        break;
 
+               case SENSOR_TYPE_LIGHT:
+                               return (float) val;
+
                case SENSOR_TYPE_ORIENTATION:
-                       return convert_from_vtf_format(DATA_BYTES, INC_EXPONENT,
-                               val);
+                       return  correction * convert_from_vtf_format(
+                                               data_bytes, exponent, val);
 
                case SENSOR_TYPE_ROTATION_VECTOR:
-                       return convert_from_vtf_format(DATA_BYTES, ROT_EXPONENT,
-                               val);
+                       return  correction * convert_from_vtf_format(
+                                               data_bytes, exponent, val);
        }
 
        return 0;
@@ -339,8 +503,14 @@ float acquire_immediate_value(int s, int c)
        int i = sensor_info[s].catalog_index;
        const char* raw_path = sensor_catalog[i].channel[c].raw_path;
        const char* input_path = sensor_catalog[i].channel[c].input_path;
-       float scale = sensor_info[s].scale;
+       float scale = sensor_info[s].scale ?
+                       sensor_info[s].scale : sensor_info[s].channel[c].scale;
        float offset = sensor_info[s].offset;
+       int sensor_type = sensor_catalog[i].type;
+       float correction;
+
+       /* In case correction has been requested using properties, apply it */
+       correction = sensor_info[s].channel[c].opt_scale;
 
        /* Acquire a sample value for sensor s / channel c through sysfs */
 
@@ -349,7 +519,7 @@ float acquire_immediate_value(int s, int c)
                ret = sysfs_read_float(sysfs_path, &val);
 
                if (!ret) {
-                       return val;
+                       return val * correction;
                }
        };
 
@@ -362,5 +532,13 @@ float acquire_immediate_value(int s, int c)
        if (ret == -1)
                return 0;
 
-       return (val + offset) * scale;
+       /*
+       There is no transform ops defined yet for Raw sysfs values
+        Use this function to perform transformation as well.
+       */
+       if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD)
+                return  CONVERT_GAUSS_TO_MICROTESLA ((val + offset) * scale) *
+                       correction;
+
+       return (val + offset) * scale * correction;
 }