OSDN Git Service

STPK-1429 Support "on change" event firing
authorPatrick Porlan <patrick.porlan@intel.com>
Wed, 25 Jun 2014 12:34:43 +0000 (14:34 +0200)
committerPatrick Porlan <patrick.porlan@intel.com>
Thu, 26 Jun 2014 13:30:56 +0000 (15:30 +0200)
The Android documentation states that ALS and temperature
sensors are "on change" rather than continuously firing
sensors. So, for these types, make note of the last returned
lux or temperature reading, and drop the event that we're
about to generate if it is identical to the last returned one.

Issue: STPK-1429

Change-Id: I4c07da6555727f905e25e2e3bb7cef975af4e4c7
Signed-off-by: Patrick Porlan <patrick.porlan@intel.com>
common.h
control.c
transform.c

index 012d4a2..0bf4c90 100644 (file)
--- a/common.h
+++ b/common.h
@@ -88,7 +88,7 @@ struct sample_ops_t
        float (*transform)(int s, int c, unsigned char* sample_data);
 
        /* Function called once per sample */
-       void (*finalize)(int s, struct sensors_event_t* data);
+       int (*finalize)(int s, struct sensors_event_t* data);
 };
 
 struct sensor_info_t
@@ -147,6 +147,9 @@ struct sensor_info_t
 
        int calibrated;
        void* cal_data;
+
+       float prev_val; /* Previously reported value, for on-change sensors */
+
        /* Note: we may have to explicitely serialize access to some fields */
 };
 
index d196fee..dfa4322 100644 (file)
--- a/control.c
+++ b/control.c
@@ -431,7 +431,7 @@ static int integrate_device_report(int dev_num)
 }
 
 
-static void propagate_sensor_report(int s, struct sensors_event_t* data)
+static int propagate_sensor_report(int s, struct sensors_event_t* data)
 {
        /* There's a sensor report pending for this sensor ; transmit it */
 
@@ -500,8 +500,7 @@ static void propagate_sensor_report(int s, struct sensors_event_t* data)
                        ALOGV("\tfield %d: %f\n", c, data->data[c]);
                }
 
-               sensor_info[s].ops.finalize(s, data);
-               return;
+               return sensor_info[s].ops.finalize(s, data);
        }
 
        /* Convert the data into the expected Android-level format */
@@ -517,7 +516,7 @@ static void propagate_sensor_report(int s, struct sensors_event_t* data)
                current_sample += sensor_info[s].channel[c].size;
        }
 
-       sensor_info[s].ops.finalize(s, data);
+       return sensor_info[s].ops.finalize(s, data);
 }
 
 
@@ -583,11 +582,20 @@ return_first_available_sensor_report:
        for (s=0; s<sensor_count; s++)
                if (sensor_info[s].report_pending) {
 
-                       /* Return that up */
-                       propagate_sensor_report(s, data);
+                       /* Lower flag */
                        sensor_info[s].report_pending = 0;
-                       ALOGV("Report on sensor %d\n", s);
-                       return 1;
+
+                       if (propagate_sensor_report(s, data)) {
+                               /* Return that up */
+                               ALOGV("Report on sensor %d\n", s);
+                               return 1;
+                       }
+
+                       /*
+                        * If the sample was deemed invalid or unreportable,
+                        * e.g. had the same value as the previously reported
+                        * value for a 'on change' sensor, silently drop it
+                        */
                }
 await_event:
 
index c306558..55ca267 100644 (file)
@@ -166,7 +166,7 @@ static int64_t sample_as_int64(unsigned char* sample, struct datum_info_t* type)
 }
 
 
-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;
@@ -183,13 +183,24 @@ static void finalize_sample_default(int s, struct sensors_event_t* data)
                        calibrate_gyro(data, &sensor_info[s]);
                        break;
 
+               case SENSOR_TYPE_LIGHT:
                case SENSOR_TYPE_AMBIENT_TEMPERATURE:
                case SENSOR_TYPE_TEMPERATURE:
-                       /* Only keep two decimals for temperature readings */
+                       /* Only keep two decimals for these readings */
                        data->data[0] = 0.01 * ((int) (data->data[0] * 100));
-                       break;
 
+                       /*
+                        * 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;
        }
+
+       return 1; /* Return sample to Android */
 }
 
 
@@ -208,7 +219,7 @@ static float transform_sample_default(int s, int c, unsigned char* sample_data)
 }
 
 
-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;
@@ -224,6 +235,8 @@ static void finalize_sample_ISH(int s, struct sensors_event_t* data)
                data->data[1] = -pitch;
                data->data[2] = -roll;
        }
+
+       return 1; /* Return sample to Android */
 }