OSDN Git Service

4222078dd2f092a9290533a88a275f440083cef4
[android-x86/hardware-intel-libsensors.git] / HidSensor_Gyro3D.cpp
1 /*
2  * Copyright (C) 2008 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 <fcntl.h>
17 #include <errno.h>
18 #include <math.h>
19 #include <poll.h>
20 #include <unistd.h>
21 #include <dirent.h>
22 #include <sys/select.h>
23 #include <cutils/log.h>
24
25 #include "common.h"
26 #include "SensorConfig.h"
27 #include "HidSensor_Gyro3D.h"
28
29 #define CHANNEL_X 0
30 #define CHANNEL_Y 1
31 #define CHANNEL_Z 2
32
33 struct gyro_3d_sample{
34     unsigned int gyro_x;
35     unsigned int gyro_y;
36     unsigned int gyro_z;
37 } __packed;
38
39 const struct sensor_t GyroSensor::sSensorInfo_gyro3D = {
40     "HID_SENSOR Gyro 3D", "Intel", 1, SENSORS_GYROSCOPE_HANDLE,
41     SENSOR_TYPE_GYROSCOPE, RANGE_GYRO, RESOLUTION_GYRO, 6.10f, 100000, 0, 0, {}
42 };
43 const int HID_USAGE_SENSOR_UNITS_NOT_SPECIFIED      = 0x00;
44 const int HID_USAGE_SENSOR_UNITS_DEGREES_PER_SECOND = 0x15;
45 const int HID_USAGE_SENSOR_UNITS_RADIANS_PER_SECOND = 0xF012;
46 const int retry_cnt = 10;
47
48 GyroSensor::GyroSensor(): SensorIIODev("gyro_3d", "in_anglvel_scale", "in_anglvel_offset", "in_anglvel_", retry_cnt){
49     ALOGV("GyroSensor: constructor\n");
50     mPendingEvent.version = sizeof(sensors_event_t);
51     mPendingEvent.sensor = ID_GY;
52     mPendingEvent.type = SENSOR_TYPE_GYROSCOPE;
53     memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));
54
55     // CDD requires 200Hz (!), but HSB caps out at ~62Hz.  Use that as
56     // default.
57     sample_delay_min_ms = 16;
58 }
59
60 int GyroSensor::processEvent(unsigned char *raw_data, size_t raw_data_len){
61     struct gyro_3d_sample *sample;
62
63     ALOGV(">>%s", __func__);
64     if (IsDeviceInitialized() == false){
65         ALOGE("Device was not initialized \n");
66         return  - 1;
67     } if (raw_data_len < sizeof(struct gyro_3d_sample)){
68         ALOGE("Insufficient length \n");
69         return  - 1;
70     }
71     sample = (struct gyro_3d_sample*)raw_data;
72     switch (GetUnitValue()) {
73     case HID_USAGE_SENSOR_UNITS_NOT_SPECIFIED:
74     case HID_USAGE_SENSOR_UNITS_DEGREES_PER_SECOND:
75         mPendingEvent.data[0] = mPendingEvent.gyro.x = CONVERT_G_D_VTF16E14_X
76             (GetChannelBytesUsedSize(CHANNEL_X), GetExponentValue(), sample->gyro_x)
77             ;
78         mPendingEvent.data[1] = mPendingEvent.gyro.y = CONVERT_G_D_VTF16E14_Y
79             (GetChannelBytesUsedSize(CHANNEL_Y), GetExponentValue(), sample->gyro_y)
80             ;
81         mPendingEvent.data[2] = mPendingEvent.gyro.z = CONVERT_G_D_VTF16E14_Z
82             (GetChannelBytesUsedSize(CHANNEL_Z), GetExponentValue(), sample->gyro_z)
83             ;
84         break;
85     case HID_USAGE_SENSOR_UNITS_RADIANS_PER_SECOND:
86         mPendingEvent.data[0] = mPendingEvent.gyro.x = CONVERT_FROM_VTF16
87             (GetChannelBytesUsedSize(CHANNEL_X), GetExponentValue(), sample->gyro_x)
88             ;
89         mPendingEvent.data[1] = mPendingEvent.gyro.y = CONVERT_FROM_VTF16
90             (GetChannelBytesUsedSize(CHANNEL_Y), GetExponentValue(), sample->gyro_y)
91             ;
92         mPendingEvent.data[2] = mPendingEvent.gyro.z = CONVERT_FROM_VTF16
93             (GetChannelBytesUsedSize(CHANNEL_Z), GetExponentValue(), sample->gyro_z)
94             ;
95         break;
96     default:
97         ALOGE("Gyro Unit is not supported");
98         break;
99     }
100
101     ALOGV("GYRO 3D Sample %fm/s2 %fm/s2 %fm/s2\n", mPendingEvent.gyro.x,
102         mPendingEvent.gyro.y, mPendingEvent.gyro.z);
103     ALOGV("<<%s", __func__);
104     return 0;
105 }