OSDN Git Service

Reverse the default orientation of accelerometer
[android-x86/hardware-intel-libsensors.git] / compass-calibration.c
index 72fb066..602f4e2 100644 (file)
@@ -1,6 +1,18 @@
 /*
- * Copyright (C) 2014-2015 Intel Corporation.
- */
+// Copyright (c) 2015 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
 
 #include <math.h>
 #include <hardware/sensors.h>
@@ -17,6 +29,7 @@
 
 #define MAGNETIC_LOW                960 /* 31 micro tesla squared */
 #define CAL_STEPS                   5
+#define CAL_VERSION 1.0
 
 /* We'll have multiple calibration levels so that we can provide an estimation as fast as possible */
 static const float          min_diffs       [CAL_STEPS] = {0.2,  0.25, 0.4, 0.6, 1.0};
@@ -265,21 +278,24 @@ static void compass_cal_init (FILE* data_file, sensor_info_t* info)
 {
     compass_cal_t* cal_data = (compass_cal_t*) info->cal_data;
     int cal_steps = (info->max_cal_level && info->max_cal_level <= CAL_STEPS) ? info->max_cal_level : CAL_STEPS;
+    float version;
+
     if (cal_data == NULL)
         return;
 
-    int data_count = 14;
+    int data_count = 15;
     reset_sample(cal_data);
 
     if (!info->cal_level && data_file != NULL) {
-       int ret = fscanf(data_file, "%d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
-            &info->cal_level, &cal_data->offset[0][0], &cal_data->offset[1][0], &cal_data->offset[2][0],
+       int ret = fscanf(data_file, "%f %d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
+            &version, &info->cal_level,
+            &cal_data->offset[0][0], &cal_data->offset[1][0], &cal_data->offset[2][0],
             &cal_data->w_invert[0][0], &cal_data->w_invert[0][1], &cal_data->w_invert[0][2],
             &cal_data->w_invert[1][0], &cal_data->w_invert[1][1], &cal_data->w_invert[1][2],
             &cal_data->w_invert[2][0], &cal_data->w_invert[2][1], &cal_data->w_invert[2][2],
             &cal_data->bfield);
 
-        if (ret != data_count || info->cal_level >= cal_steps)
+        if (ret != data_count || info->cal_level >= cal_steps || version != CAL_VERSION)
             info->cal_level = 0;
     }
 
@@ -316,8 +332,9 @@ static void compass_store_result (FILE* data_file, sensor_info_t* info)
     if (data_file == NULL || cal_data == NULL)
         return;
 
-    int ret = fprintf(data_file, "%d %f %f %f %f %f %f %f %f %f %f %f %f %f\n",
-        info->cal_level, cal_data->offset[0][0], cal_data->offset[1][0], cal_data->offset[2][0],
+    int ret = fprintf(data_file, "%f %d %f %f %f %f %f %f %f %f %f %f %f %f %f\n",
+        CAL_VERSION, info->cal_level,
+        cal_data->offset[0][0], cal_data->offset[1][0], cal_data->offset[2][0],
         cal_data->w_invert[0][0], cal_data->w_invert[0][1], cal_data->w_invert[0][2],
         cal_data->w_invert[1][0], cal_data->w_invert[1][1], cal_data->w_invert[1][2],
         cal_data->w_invert[2][0], cal_data->w_invert[2][1], cal_data->w_invert[2][2],
@@ -477,14 +494,14 @@ static int compass_ready (sensor_info_t* info)
 }
 
 
-void calibrate_compass (sensors_event_t* event, sensor_info_t* info)
+void calibrate_compass (int s, sensors_event_t* event)
 {
     int cal_level;
 
     /* Calibration is continuous */
-    compass_collect (event, info);
+    compass_collect (event, &sensor[s]);
 
-    cal_level = compass_ready(info);
+    cal_level = compass_ready(&sensor[s]);
 
     switch (cal_level) {
         case 0:
@@ -493,38 +510,38 @@ void calibrate_compass (sensors_event_t* event, sensor_info_t* info)
             break;
 
         case 1:
-            compass_compute_cal (event, info);
+            compass_compute_cal (event, &sensor[s]);
             event->magnetic.status = SENSOR_STATUS_ACCURACY_LOW;
             break;
 
         case 2:
-            compass_compute_cal (event, info);
+            compass_compute_cal (event, &sensor[s]);
             event->magnetic.status = SENSOR_STATUS_ACCURACY_MEDIUM;
             break;
 
         default:
-            compass_compute_cal (event, info);
+            compass_compute_cal (event, &sensor[s]);
             event->magnetic.status = SENSOR_STATUS_ACCURACY_HIGH;
             break;
     }
 }
 
-void compass_read_data (sensor_info_t* info)
+void compass_read_data (int s)
 {
     FILE* data_file = fopen (COMPASS_CALIBRATION_PATH, "r");
 
-    compass_cal_init(data_file, info);
+    compass_cal_init(data_file, &sensor[s]);
 
     if (data_file)
         fclose(data_file);
 }
 
 
-void compass_store_data (sensor_info_t* info)
+void compass_store_data (int s)
 {
     FILE* data_file = fopen (COMPASS_CALIBRATION_PATH, "w");
 
-    compass_store_result(data_file, info);
+    compass_store_result(data_file, &sensor[s]);
 
     if (data_file)
         fclose(data_file);