OSDN Git Service

Allow for a slight difference when parsing available frequencies
authorPatrick Porlan <patrick.porlan@intel.com>
Wed, 17 Dec 2014 14:42:16 +0000 (15:42 +0100)
committerGerrit Code Review <gerrit2@irsgerrit001.ir.intel.com>
Thu, 18 Dec 2014 08:35:29 +0000 (08:35 +0000)
I'm seeing missed matches due to rounding errors (accelerometer
set at 31.26 Hz because 15.62 is lower than the minimum 15.6201).

Change-Id: I16593fc51e2b88bc140d0b6f264fe833b3ec1c8d
Signed-off-by: Patrick Porlan <patrick.porlan@intel.com>
control.c

index e1268a8..14f5570 100644 (file)
--- a/control.c
+++ b/control.c
@@ -7,6 +7,7 @@
 #include <fcntl.h>
 #include <pthread.h>
 #include <time.h>
+#include <math.h>
 #include <sys/epoll.h>
 #include <sys/socket.h>
 #include <utils/Log.h>
@@ -816,9 +817,15 @@ static int setup_delay_sysfs (int s, float requested_rate)
                        /* Decode a single value */
                        sr = strtod(cursor, NULL);
 
-                       /* If this matches the selected rate, we're happy */
-                       if (arb_sampling_rate == sr)
+                       /*
+                        * If this matches the selected rate, we're happy.
+                        * Have some tolerance to counter rounding errors and
+                        * avoid needless jumps to higher rates.
+                        */
+                       if (fabs(arb_sampling_rate - sr) <= 0.001) {
+                               arb_sampling_rate = sr;
                                break;
+                       }
 
                        /*
                         * If we reached a higher value than the desired rate,
@@ -1540,7 +1547,7 @@ int sensor_set_delay (int s, int64_t ns)
                return -EINVAL;
        }
 
-       requested_sampling_rate = 1000000000LL/ns;
+       requested_sampling_rate = 1000000000.0/ns;
 
        ALOGV("Entering set delay S%d (%s): current rate: %f, requested: %f\n",
                s, sensor[s].friendly_name, sensor[s].sampling_rate,