OSDN Git Service

IRDA-3484: Add support for filter property
[android-x86/hardware-intel-libsensors.git] / filtering.c
index 97dbd25..510737c 100644 (file)
@@ -1,11 +1,12 @@
-#include <stdlib.h>
+/*
+ * Copyright (C) 2014 Intel Corporation.
+ */
+
 #include <hardware/sensors.h>
-#include <math.h>
-#include <pthread.h>
 #include <utils/Log.h>
 #include "common.h"
 #include "filtering.h"
-
+#include "description.h"
 
 typedef struct
 {
@@ -17,8 +18,7 @@ typedef struct
 filter_median_t;
 
 
-static unsigned int partition (        float* list, unsigned int left,
-                               unsigned int right, unsigned int pivot_index)
+static unsigned int partition (float* list, unsigned int left, unsigned int right, unsigned int pivot_index)
 {
        unsigned int i;
        unsigned int store_index = left;
@@ -81,11 +81,9 @@ static float median (float* queue, unsigned int size)
 }
 
 
-static void denoise_median_init(int s, unsigned int num_fields,
-                               unsigned int max_samples)
+static void denoise_median_init (int s, unsigned int num_fields, unsigned int max_samples)
 {
-       filter_median_t* f_data = (filter_median_t*)
-                                        malloc(sizeof(filter_median_t));
+       filter_median_t* f_data = (filter_median_t*) malloc(sizeof(filter_median_t));
 
        f_data->buff = (float*) calloc(max_samples, sizeof(float) * num_fields);
        f_data->sample_size = max_samples;
@@ -107,9 +105,7 @@ static void denoise_median_reset (sensor_info_t* info)
 }
 
 
-static void denoise_median (   sensor_info_t* info,
-                               sensors_event_t* data,
-                               unsigned int num_fields)
+static void denoise_median (sensor_info_t* info, sensors_event_t* data, unsigned int num_fields)
 {
        float x, y, z;
        float scale;
@@ -137,9 +133,7 @@ static void denoise_median (        sensor_info_t* info,
 }
 
 
-static void denoise_average (  sensor_info_t* si,
-                               sensors_event_t* data,
-                               int num_fields, int max_samples)
+static void denoise_average (sensor_info_t* si, sensors_event_t* data, int num_fields, int max_samples)
 {
        /*
         * Smooth out incoming data using a moving average over a number of
@@ -168,11 +162,9 @@ static void denoise_average (      sensor_info_t* si,
                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));
+               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));
+                       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));
                }
@@ -189,10 +181,7 @@ static void denoise_average (      sensor_info_t* si,
 
        /* 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
-                */
+               /** 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];
@@ -200,8 +189,7 @@ static void denoise_average (       sensor_info_t* si,
                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 */
+               /* For now simply compute a mobile mean for each field and output filtered data */
                data->data[f] = si->history_sum[f] / si->history_entries;
        }
 
@@ -212,8 +200,36 @@ static void denoise_average (      sensor_info_t* si,
 
 void setup_noise_filtering (int s)
 {
-       switch (sensor[s].type) {
-               case SENSOR_TYPE_GYROSCOPE:
+       char filter_buf[MAX_NAME_SIZE];
+
+       /* By default, don't apply filtering */
+       sensor[s].filter_type = FILTER_TYPE_NONE;
+
+       /* If noisy, start with default filter for sensor type */
+       if (sensor[s].quirks & QUIRK_NOISY)
+               switch (sensor[s].type) {
+                       case SENSOR_TYPE_GYROSCOPE:
+                               sensor[s].filter_type = FILTER_TYPE_MEDIAN;
+                               break;
+
+                       case SENSOR_TYPE_MAGNETIC_FIELD:
+                               sensor[s].filter_type = FILTER_TYPE_MOVING_AVERAGE;
+                               break;
+               }
+
+       /* Use whatever was specified if there's an explicit configuration choice for this sensor */
+
+       filter_buf[0] = '\0';
+       sensor_get_st_prop(s, "filter", filter_buf);
+
+       if (strstr(filter_buf, "median"))
+               sensor[s].filter_type = FILTER_TYPE_MEDIAN;
+
+       if (strstr(filter_buf, "average"))
+               sensor[s].filter_type = FILTER_TYPE_MOVING_AVERAGE;
+
+       switch (sensor[s].filter_type) {
+               case FILTER_TYPE_MEDIAN:
                        denoise_median_init(s, 3, 5);
                        break;
        }
@@ -222,12 +238,12 @@ void setup_noise_filtering (int s)
 
 void denoise (int s, sensors_event_t* data)
 {
-       switch (sensor[s].type) {
-               case SENSOR_TYPE_GYROSCOPE:
+       switch (sensor[s].filter_type) {
+               case FILTER_TYPE_MEDIAN:
                        denoise_median(&sensor[s], data, 3);
                        break;
 
-               case SENSOR_TYPE_MAGNETIC_FIELD:
+               case FILTER_TYPE_MOVING_AVERAGE:
                        denoise_average(&sensor[s], data, 3 , 20);
                        break;
        }
@@ -273,13 +289,10 @@ typedef struct
 recorded_sample_t;
 
 /*
- * This is a circular buffer holding the last GLOBAL_HISTORY_SIZE events,
- * covering the entire sensor collection. It is intended as a way to correlate
- * data coming from active sensors, no matter the sensor type, over a recent
- * window of time. The array is not sorted ; we simply evict the oldest cell
- * (by insertion time) and replace its contents. Timestamps don't necessarily
- * grow monotonically as they tell the data acquisition type, and that there can
- * be a delay between acquisition and insertion into this table.
+ * This is a circular buffer holding the last GLOBAL_HISTORY_SIZE events, covering the entire sensor collection. It is intended as a way to correlate
+ * data coming from active sensors, no matter the sensor type, over a recent window of time. The array is not sorted ; we simply evict the oldest cell
+ * (by insertion time) and replace its contents. Timestamps don't necessarily grow monotonically as they tell the data acquisition type, and that there
+ * can be a delay between acquisition and insertion into this table.
  */
 
 static recorded_sample_t global_history[GLOBAL_HISTORY_SIZE];
@@ -309,8 +322,7 @@ void record_sample (int s, const sensors_event_t* event)
 
        cell->sensor = s;
 
-       cell->motion_trigger = (sensor[s].selected_trigger ==
-                               sensor[s].motion_trigger_name);
+       cell->motion_trigger = (sensor[s].selected_trigger == sensor[s].motion_trigger_name);
 
        memcpy(&cell->data, event, sizeof(sensors_event_t));
 }