2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <cutils/log.h>
18 #include "SensorConfig.h"
19 #include "SynthCompassSensor.h"
20 #include "OrientationSensor.h"
22 const struct sensor_t OrientationSensor::sSensorInfo_orientation = {
23 .name = "HID_SENSOR Orientation",
26 .handle = SENSORS_ORIENTATION_HANDLE,
27 .type = SENSOR_TYPE_ORIENTATION,
29 .resolution = 1./512, // Advertise as 9 bits, no idea about reality
32 .fifoReservedEventCount = 0,
33 .fifoMaxEventCount = 0,
37 OrientationSensor::OrientationSensor()
38 : SensorIIODev("incli_3d", // name
39 "in_incli_scale", // units sysfs node
40 "in_incli_offset", // exponent sysfs node
41 "in_incli_", // channel_prefix
44 mPendingEvent.version = sizeof(sensors_event_t);
45 mPendingEvent.sensor = ID_O;
46 mPendingEvent.type = SENSOR_TYPE_ORIENTATION;
47 memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));
49 sample_delay_min_ms = 50; // 20Hz default
52 int OrientationSensor::processEvent(unsigned char *data, size_t len)
54 if (IsDeviceInitialized() == false) {
55 ALOGE("Device was not initialized \n");
59 if (len < 3*sizeof(unsigned int)) {
60 ALOGE("Insufficient length \n");
65 unsigned int *sample = (unsigned int*)data;
66 float sc = GetScaleValue();
67 for (int i=0; i<3; i++) {
68 int sz = GetChannelBytesUsedSize(i);
69 vals[i] = convert_from_vtf_format(sz, sc, sample[i]);
72 // When held in the Android convention frame (X to the right, Y
73 // toward "screen up", Z out from the screen toward the user) a
74 // windows sensor hub reports pitch/roll/yaw where Android wants
75 // hdg/pitch/roll. And the sense of the rotations is reversed
76 // from what is observed on Nexus devices (which themselves seem
77 // to disagree with documentation on the sign of the output, see:
78 // http://developer.android.com/reference/android/hardware/SensorEvent.html#values).
80 // These conversions produce behavior identical to the Nexus 7,
81 // Galaxy Nexus and Google Edition GS4:
82 mPendingEvent.data[0] = 360.0 - vals[2];
83 mPendingEvent.data[1] = -vals[0];
84 mPendingEvent.data[2] = -vals[1];
86 ALOGV("orient %f %f %f\n", mPendingEvent.data[0], mPendingEvent.data[1], mPendingEvent.data[2]);