OSDN Git Service

libsensors: fix building issues on Android 5.0
[android-x86/hardware-intel-libsensors.git] / OrientationSensor.cpp
1 /*
2  * Copyright (C) 2013 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 #include <cutils/log.h>
17 #include "common.h"
18 #include "SensorConfig.h"
19 #include "SynthCompassSensor.h"
20 #include "OrientationSensor.h"
21
22 const struct sensor_t OrientationSensor::sSensorInfo_orientation = {
23     .name       = "HID_SENSOR Orientation",
24     .vendor     = "Intel",
25     .version    = 1,
26     .handle     = SENSORS_ORIENTATION_HANDLE,
27     .type       = SENSOR_TYPE_ORIENTATION,
28     .maxRange   = 360.0,
29     .resolution = 1./512, // Advertise as 9 bits, no idea about reality
30     .power      = 0.1f,
31     .minDelay   = 0,
32     .fifoReservedEventCount = 0,
33     .fifoMaxEventCount      = 0,
34     .stringType = SENSOR_STRING_TYPE_ORIENTATION,
35     .requiredPermission     = "",
36     .maxDelay   = 0,
37     .flags      = SENSOR_FLAG_CONTINUOUS_MODE,
38     .reserved   = {},
39 };
40
41 OrientationSensor::OrientationSensor()
42     : SensorIIODev("incli_3d",  // name
43                    "in_incli_scale",  // units sysfs node
44                    "in_incli_offset", // exponent sysfs node
45                    "in_incli_",       // channel_prefix
46                    10)                // retry count
47 {
48     mPendingEvent.version = sizeof(sensors_event_t);
49     mPendingEvent.sensor = ID_O;
50     mPendingEvent.type = SENSOR_TYPE_ORIENTATION;
51     memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));
52
53     sample_delay_min_ms = 50; // 20Hz default
54 }
55
56 int OrientationSensor::processEvent(unsigned char *data, size_t len)
57 {
58     if (IsDeviceInitialized() == false) {
59         ALOGE("Device was not initialized \n");
60         return -1;
61     }
62
63     if (len < 3*sizeof(unsigned int)) {
64         ALOGE("Insufficient length \n");
65         return -1;
66     }
67
68     float vals[3];
69     unsigned int *sample = (unsigned int*)data;
70     long ex = GetExponentValue();
71     for (int i=0; i<3; i++) {
72         int sz = GetChannelBytesUsedSize(i);
73         vals[i] = convert_from_vtf_format(sz, ex, sample[i]);
74     }
75
76     // When held in the Android convention frame (X to the right, Y
77     // toward "screen up", Z out from the screen toward the user) a
78     // windows sensor hub reports pitch/roll/yaw where Android wants
79     // hdg/pitch/roll.  And the sense of the rotations is reversed
80     // from what is observed on Nexus devices (which themselves seem
81     // to disagree with documentation on the sign of the output, see:
82     // http://developer.android.com/reference/android/hardware/SensorEvent.html#values).
83     //
84     // These conversions produce behavior identical to the Nexus 7,
85     // Galaxy Nexus and Google Edition GS4:
86     mPendingEvent.data[0] = 360.0 - vals[2];
87     mPendingEvent.data[1] = -vals[0];
88     mPendingEvent.data[2] = -vals[1];
89
90     ALOGV("orient %f %f %f\n", mPendingEvent.data[0], mPendingEvent.data[1], mPendingEvent.data[2]);
91
92     return 0;
93 }