Home | History | Annotate | Download | only in sensorservice
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <stdint.h>
     18 #include <math.h>
     19 #include <sys/types.h>
     20 
     21 #include <utils/Errors.h>
     22 
     23 #include <hardware/sensors.h>
     24 
     25 #include "OrientationSensor.h"
     26 #include "SensorDevice.h"
     27 #include "SensorFusion.h"
     28 
     29 namespace android {
     30 // ---------------------------------------------------------------------------
     31 
     32 OrientationSensor::OrientationSensor()
     33     : mSensorDevice(SensorDevice::getInstance()),
     34       mSensorFusion(SensorFusion::getInstance())
     35 {
     36 }
     37 
     38 bool OrientationSensor::process(sensors_event_t* outEvent,
     39         const sensors_event_t& event)
     40 {
     41     if (event.type == SENSOR_TYPE_ACCELEROMETER) {
     42         if (mSensorFusion.hasEstimate()) {
     43             vec3_t g;
     44             const float rad2deg = 180 / M_PI;
     45             const mat33_t R(mSensorFusion.getRotationMatrix());
     46             g[0] = atan2f(-R[1][0], R[0][0])    * rad2deg;
     47             g[1] = atan2f(-R[2][1], R[2][2])    * rad2deg;
     48             g[2] = asinf ( R[2][0])             * rad2deg;
     49             if (g[0] < 0)
     50                 g[0] += 360;
     51 
     52             *outEvent = event;
     53             outEvent->orientation.azimuth = g.x;
     54             outEvent->orientation.pitch   = g.y;
     55             outEvent->orientation.roll    = g.z;
     56             outEvent->orientation.status  = SENSOR_STATUS_ACCURACY_HIGH;
     57             outEvent->sensor = '_ypr';
     58             outEvent->type = SENSOR_TYPE_ORIENTATION;
     59             return true;
     60         }
     61     }
     62     return false;
     63 }
     64 
     65 status_t OrientationSensor::activate(void* ident, bool enabled) {
     66     return mSensorFusion.activate(this, enabled);
     67 }
     68 
     69 status_t OrientationSensor::setDelay(void* ident, int handle, int64_t ns) {
     70     return mSensorFusion.setDelay(this, ns);
     71 }
     72 
     73 Sensor OrientationSensor::getSensor() const {
     74     sensor_t hwSensor;
     75     hwSensor.name       = "Orientation Sensor";
     76     hwSensor.vendor     = "Google Inc.";
     77     hwSensor.version    = 1;
     78     hwSensor.handle     = '_ypr';
     79     hwSensor.type       = SENSOR_TYPE_ORIENTATION;
     80     hwSensor.maxRange   = 360.0f;
     81     hwSensor.resolution = 1.0f/256.0f; // FIXME: real value here
     82     hwSensor.power      = mSensorFusion.getPowerUsage();
     83     hwSensor.minDelay   = mSensorFusion.getMinDelay();
     84     Sensor sensor(&hwSensor);
     85     return sensor;
     86 }
     87 
     88 // ---------------------------------------------------------------------------
     89 }; // namespace android
     90 
     91