OSDN Git Service

Determine maxRange and resolution from IIO attributes
authorAdriana Reus <adriana.reus@intel.com>
Tue, 21 Jul 2015 10:19:20 +0000 (13:19 +0300)
committerbuildslave <sys_buildbot@intel.com>
Wed, 22 Jul 2015 13:10:44 +0000 (13:10 +0000)
This patch determines the Android maxRange and resolution sensor
attributes based on the IIO attributes:

resolution = scale (or 1 if no scale is defined)

maxRange = 2^realbits * resolution + offset

If scale or offset are not defined at the sensor level we pick them
from the first channel.

This should avoid the need to define Android properties in most
cases. However, one can still use Android properties to override the
automatically computed values.

Change-Id: I04536be1fe741d887103ab07fda7daf4b4408ddc
Tracked-On: https://jira01.devtools.intel.com/browse/GMINL-14246
Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
Signed-off-by: Adriana Reus <adriana.reus@intel.com>
Reviewed-on: https://android.intel.com:443/392075

control.c
description.c
description.h
transform.c
transform.h

index 09209bd..7dc1bfb 100644 (file)
--- a/control.c
+++ b/control.c
@@ -300,6 +300,8 @@ void build_sensor_report_maps (int dev_num)
                        known_channels++;
                }
 
+               sensor_update_max_range(s);
+
                /* Stop sampling - if we are recovering from hal restart */
                 enable_buffer(dev_num, 0);
                 setup_trigger(s, "\n");
index 7e899b3..b5a72f0 100644 (file)
@@ -11,6 +11,7 @@
 #include "enumeration.h"
 #include "description.h"
 #include "utils.h"
+#include "transform.h"
 
 #define IIO_SENSOR_HAL_VERSION 1
 
@@ -218,6 +219,60 @@ int sensor_get_version (__attribute__((unused)) int s)
        return IIO_SENSOR_HAL_VERSION;
 }
 
+void sensor_update_max_range(int s)
+{
+       if (sensor[s].max_range)
+               return;
+
+       if (sensor[s].num_channels && sensor[s].channel[0].type_info.realbits) {
+               switch (sensor[s].type) {
+               case SENSOR_TYPE_MAGNETIC_FIELD:
+                       sensor[s].max_range = (1ULL << sensor[s].channel[0].type_info.realbits) *
+                                       CONVERT_MICROTESLA_TO_GAUSS(sensor[s].resolution) +
+                                       (sensor[s].offset || sensor[s].channel[0].offset);
+                       sensor[s].max_range = CONVERT_GAUSS_TO_MICROTESLA(sensor[s].max_range);
+                       break;
+               case SENSOR_TYPE_PROXIMITY:
+                       break;
+               default:
+                       sensor[s].max_range =  (1ULL << sensor[s].channel[0].type_info.realbits) *
+                               sensor[s].resolution + (sensor[s].offset || sensor[s].channel[0].offset);
+                       break;
+               }
+       }
+
+       if (!sensor[s].max_range) {
+               /* Try returning a sensible value given the sensor type */
+               /* We should cap returned samples accordingly... */
+               switch (sensor[s].type) {
+               case SENSOR_TYPE_ACCELEROMETER:         /* m/s^2        */
+                       sensor[s].max_range = 50;
+                       break;
+               case SENSOR_TYPE_MAGNETIC_FIELD:        /* micro-tesla  */
+                       sensor[s].max_range = 500;
+                       break;
+               case SENSOR_TYPE_ORIENTATION:           /* degrees      */
+                       sensor[s].max_range = 360;
+                       break;
+               case SENSOR_TYPE_GYROSCOPE:             /* radians/s    */
+                       sensor[s].max_range = 10;
+                       break;
+               case SENSOR_TYPE_LIGHT:                 /* SI lux units */
+                       sensor[s].max_range = 50000;
+                       break;
+               case SENSOR_TYPE_AMBIENT_TEMPERATURE:   /* °C          */
+               case SENSOR_TYPE_TEMPERATURE:           /* °C          */
+               case SENSOR_TYPE_PROXIMITY:             /* centimeters  */
+               case SENSOR_TYPE_PRESSURE:              /* hecto-pascal */
+               case SENSOR_TYPE_RELATIVE_HUMIDITY:     /* percent */
+                       sensor[s].max_range = 100;
+                       break;
+               }
+       }
+
+       if (sensor[s].max_range)
+               sensor_desc[s].maxRange = sensor[s].max_range;
+}
 
 float sensor_get_max_range (int s)
 {
@@ -236,36 +291,7 @@ float sensor_get_max_range (int s)
                !sensor_get_fl_prop(s, "max_range", &sensor[s].max_range))
                        return sensor[s].max_range;
 
-       /* Try returning a sensible value given the sensor type */
-
-       /* We should cap returned samples accordingly... */
-
-       switch (sensor_desc[s].type) {
-               case SENSOR_TYPE_ACCELEROMETER:         /* m/s^2        */
-                       return 50;
-
-               case SENSOR_TYPE_MAGNETIC_FIELD:        /* micro-tesla  */
-                       return 500;
-
-               case SENSOR_TYPE_ORIENTATION:           /* degrees      */
-                       return 360;
-
-               case SENSOR_TYPE_GYROSCOPE:             /* radians/s    */
-                       return 10;
-
-               case SENSOR_TYPE_LIGHT:                 /* SI lux units */
-                       return 50000;
-
-               case SENSOR_TYPE_AMBIENT_TEMPERATURE:   /* °C          */
-               case SENSOR_TYPE_TEMPERATURE:           /* °C          */
-               case SENSOR_TYPE_PROXIMITY:             /* centimeters  */
-               case SENSOR_TYPE_PRESSURE:              /* hecto-pascal */
-               case SENSOR_TYPE_RELATIVE_HUMIDITY:     /* percent */
-                       return 100;
-
-               default:
-                       return 0;
-               }
+       return 0;
 }
 
 static float sensor_get_min_freq (int s)
@@ -319,10 +345,18 @@ float sensor_get_resolution (int s)
        }
 
        if (sensor[s].resolution != 0.0 ||
-               !sensor_get_fl_prop(s, "resolution", &sensor[s].resolution))
-                       return sensor[s].resolution;
+           !sensor_get_fl_prop(s, "resolution", &sensor[s].resolution)) {
+               return sensor[s].resolution;
+       }
 
-       return 0;
+       sensor[s].resolution = sensor[s].scale;
+       if (!sensor[s].resolution && sensor[s].num_channels)
+               sensor[s].resolution = sensor[s].channel[0].scale;
+
+       if (sensor[s].type == SENSOR_TYPE_MAGNETIC_FIELD)
+               sensor[s].resolution = CONVERT_GAUSS_TO_MICROTESLA(sensor[s].resolution);
+
+       return sensor[s].resolution ? : 1;
 }
 
 
index 34b79d4..dd6e794 100644 (file)
@@ -34,6 +34,7 @@ char*         sensor_get_name         (int s);
 char*          sensor_get_vendor       (int s);
 int            sensor_get_version      (int s);
 float          sensor_get_max_range    (int s);
+void           sensor_update_max_range (int s);
 float          sensor_get_resolution   (int s);
 float          sensor_get_power        (int s);
 flag_t         sensor_get_flags        (int s);
index 9738f71..3c975f5 100644 (file)
@@ -41,8 +41,6 @@
 #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.0 / 64)
 #define CONVERT_O_A    (CONVERT_O)
index 687c1dd..a4b97df 100644 (file)
@@ -7,6 +7,9 @@
 
 #include "common.h"
 
+#define CONVERT_GAUSS_TO_MICROTESLA(x) ((x) * 100)
+#define CONVERT_MICROTESLA_TO_GAUSS(x) ((x) / 100)
+
 void   select_transform        (int s);
 float  acquire_immediate_float_value   (int s, int c);
 uint64_t acquire_immediate_uint64_value        (int s, int c);