OSDN Git Service

Moping the floor: remove superfluous f type specifiers
[android-x86/hardware-intel-libsensors.git] / transform.c
index aaf33c3..e5842ba 100644 (file)
@@ -7,10 +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 "calibration.h"
+#include "filtering.h"
 
 /*----------------------------------------------------------------------------*/
 
@@ -19,8 +21,8 @@
 #define GRAVITY 9.80665f
 
 /* 720 LSG = 1G */
-#define LSG                         (1024.0f)
-#define NUMOFACCDATA                (8.0f)
+#define LSG                         (1024.0)
+#define NUMOFACCDATA                (8.0)
 
 /* conversion of acceleration data to SI units (m/s^2) */
 #define CONVERT_A                   (GRAVITY_EARTH / LSG / NUMOFACCDATA)
 #define CONVERT_A_Z(x)              ((float(x)/1000) * (GRAVITY * 1.0))
 
 /* conversion of magnetic data to uT units */
-#define CONVERT_M                   (1.0f/6.6f)
+#define CONVERT_M                   (1.0/6.6)
 #define CONVERT_M_X                 (-CONVERT_M)
 #define CONVERT_M_Y                 (-CONVERT_M)
 #define CONVERT_M_Z                 (CONVERT_M)
 
-#define CONVERT_GAUSS_TO_MICROTESLA(x)        ( (x) * 100 )
+#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                   (1.0/64)
 #define CONVERT_O_A                 (CONVERT_O)
 #define CONVERT_O_P                 (CONVERT_O)
 #define CONVERT_O_R                 (-CONVERT_O)
 
 /*conversion of gyro data to SI units (radian/sec) */
-#define CONVERT_GYRO                ((2000.0f/32767.0f)*((float)M_PI / 180.0f))
+#define CONVERT_GYRO                (2000.0/32767*M_PI/180)
 #define CONVERT_GYRO_X              (-CONVERT_GYRO)
 #define CONVERT_GYRO_Y              (-CONVERT_GYRO)
 #define CONVERT_GYRO_Z              (CONVERT_GYRO)
@@ -105,13 +107,13 @@ inline float convert_from_vtf_format(int size, int exponent, unsigned int value)
 // Degree/sec to radian/sec
 #define CONVERT_G_D_VTF16E14_X(s,d,x)  (DEF_ORIENT_GYRO_X *\
                                         convert_from_vtf_format(s,d,x) * \
-                                        ((float)M_PI/180.0f))
+                                        M_PI/180)
 #define CONVERT_G_D_VTF16E14_Y(s,d,x)  (DEF_ORIENT_GYRO_Y *\
                                         convert_from_vtf_format(s,d,x) * \
-                                        ((float)M_PI/180.0f))
+                                        M_PI/180)
 #define CONVERT_G_D_VTF16E14_Z(s,d,x)  (DEF_ORIENT_GYRO_Z *\
                                         convert_from_vtf_format(s,d,x) * \
-                                        ((float)M_PI/180.0f))
+                                        M_PI/180)
 
 // Milli gauss to micro tesla
 #define CONVERT_M_MG_VTF16E14_X(s,d,x) (convert_from_vtf_format(s,d,x)/10)
@@ -187,26 +189,122 @@ static void reorder_fields(float* data,  unsigned char map[MAX_CHANNELS])
 }
 
 
-static int finalize_sample_default(int s, struct sensors_event_t* data)
+static void clamp_gyro_readings_to_zero (int s, struct sensors_event_t* data)
 {
-       int i           = sensor_info[s].catalog_index;
-       int sensor_type = sensor_catalog[i].type;
+       float x, y, z;
+       float near_zero;
+
+       switch (sensor_info[s].type) {
+               case SENSOR_TYPE_GYROSCOPE:
+                       x = data->data[0];
+                       y = data->data[1];
+                       z = data->data[2];
+                       break;
+
+               case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
+                       x = data->data[0] - data->uncalibrated_gyro.bias[0];
+                       y = data->data[1] - data->uncalibrated_gyro.bias[1];
+                       z = data->data[2] - data->uncalibrated_gyro.bias[2];
+                       break;
+
+               default:
+                       return;
+       }
+
+       /* If we're calibrated, don't filter out as much */
+       if (sensor_info[s].cal_level > 0)
+               near_zero = 0.02; /* rad/s */
+       else
+               near_zero = 0.1;
+
+       /* If motion on all axes is small enough */
+       if (fabs(x) < near_zero && fabs(y) < near_zero && fabs(z) < near_zero) {
+
+               /*
+                * Report that we're not moving at all... but not exactly zero
+                * as composite sensors (orientation, rotation vector) don't
+                * seem to react very well to it.
+                */
+               switch (sensor_info[s].type) {
+                       case SENSOR_TYPE_GYROSCOPE:
+                               data->data[0] *= 0.000001;
+                               data->data[1] *= 0.000001;
+                               data->data[2] *= 0.000001;
+                               break;
+
+                       case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
+                               data->data[0]= data->uncalibrated_gyro.bias[0]
+                                               + 0.000001 * x;
+                               data->data[1]= data->uncalibrated_gyro.bias[1]
+                                               + 0.000001 * y;
+                               data->data[2]= data->uncalibrated_gyro.bias[2]
+                                               + 0.000001 * z;
+                               break;
+               }
+       }
+}
+
 
+static int finalize_sample_default (int s, struct sensors_event_t* data)
+{
        /* Swap fields if we have a custom channel ordering on this sensor */
-       if (sensor_info[s].flags & FLAG_FIELD_ORDERING)
+       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(s, data);
                        break;
 
                case SENSOR_TYPE_MAGNETIC_FIELD:
-                       calibrate_compass (data, &sensor_info[s], get_timestamp());
+                       calibrate_compass (data, &sensor_info[s]);
+                       if (sensor_info[s].quirks & QUIRK_NOISY)
+                               denoise(s, data);
                        break;
 
                case SENSOR_TYPE_GYROSCOPE:
+
+                       /*
+                        * Report medium accuracy by default ; higher accuracy
+                        * levels will be reported once, and if, we achieve
+                        * calibration.
+                        */
+                       data->gyro.status = SENSOR_STATUS_ACCURACY_MEDIUM;
+
+                       /* ... fall through */
+
                case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
-                       calibrate_gyro(data, &sensor_info[s]);
+
+                       /*
+                        * 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.
+                        */
+                       if (sensor_info[s].selected_trigger !=
+                               sensor_info[s].motion_trigger_name)
+                                       calibrate_gyro(data, &sensor_info[s]);
+
+                       /*
+                        * For noisy sensors drop a few samples to make sure we
+                        * have at least GYRO_MIN_SAMPLES events in the
+                        * filtering queue. This improves mean and std dev.
+                        */
+                       if (sensor_info[s].quirks & QUIRK_NOISY) {
+                               if (sensor_info[s].selected_trigger !=
+                                   sensor_info[s].motion_trigger_name &&
+                                   sensor_info[s].event_count<GYRO_MIN_SAMPLES)
+                                               return 0;
+
+                               denoise(s, data);
+                       }
+
+                       /* Clamp near zero moves to (0,0,0) if appropriate */
+                       clamp_gyro_readings_to_zero(s, data);
                        break;
 
                case SENSOR_TYPE_LIGHT:
@@ -215,6 +313,9 @@ static int finalize_sample_default(int s, struct sensors_event_t* data)
                        /* 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.
@@ -245,17 +346,15 @@ static float transform_sample_default(int s, int c, unsigned char* sample_data)
 }
 
 
-static int 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;
 
        /* Swap fields if we have a custom channel ordering on this sensor */
-       if (sensor_info[s].flags & FLAG_FIELD_ORDERING)
+       if (sensor_info[s].quirks & QUIRK_FIELD_ORDERING)
                reorder_fields(data->data, sensor_info[s].order);
 
-       if (sensor_type == SENSOR_TYPE_ORIENTATION) {
+       if (sensor_info[s].type == SENSOR_TYPE_ORIENTATION) {
 
                pitch = data->data[0];
                roll = data->data[1];
@@ -266,16 +365,17 @@ static int finalize_sample_ISH(int s, struct sensors_event_t* data)
                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;
@@ -283,7 +383,7 @@ static float transform_sample_ISH(int s, int c, unsigned char* sample_data)
        /* 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: