OSDN Git Service

libsensors: workaround sensors never detected
[android-x86/hardware-intel-libsensors.git] / HidSensor_Compass3D.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_Compass3D.h"
28
29 #define CHANNEL_X 0
30 #define CHANNEL_Y 1
31 #define CHANNEL_Z 2
32
33 struct compass_3d_sample{
34     unsigned int compass_x;
35     unsigned int compass_y;
36     unsigned int compass_z;
37 } __packed;
38
39 const struct sensor_t CompassSensor::sSensorInfo_compass3D = {
40     "HID_SENSOR Compass 3D", "Intel", 1, SENSORS_MAGNETIC_FIELD_HANDLE,
41     SENSOR_TYPE_MAGNETIC_FIELD, RANGE_M, RESOLUTION_M, 0.1f, 0, 0, 0,
42     SENSOR_STRING_TYPE_MAGNETIC_FIELD, "", 0 , SENSOR_FLAG_CONTINUOUS_MODE, {}
43 };
44 const int retry_cnt = 10;
45
46 CompassSensor::CompassSensor(): SensorIIODev("magn_3d", "in_magn_scale", "in_magn_offset", "in_magn_", retry_cnt){
47     ALOGV(">>ComassSensor 3D: constructor!");
48     mPendingEvent.version = sizeof(sensors_event_t);
49     mPendingEvent.sensor = ID_M;
50     mPendingEvent.type = SENSOR_TYPE_MAGNETIC_FIELD;
51     memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));
52
53     // CDD 4.2 requires 10Hz.  20Hz is the maximum for HSB.
54     sample_delay_min_ms = 50;
55
56     ALOGV("<<ComassSensor 3D: constructor!");
57 }
58
59 int CompassSensor::processEvent(unsigned char *raw_data, size_t raw_data_len){
60     struct compass_3d_sample *sample;
61
62     ALOGV(">>%s", __func__);
63     if (IsDeviceInitialized() == false){
64         ALOGE("Device was not initialized \n");
65         return  - 1;
66     } if (raw_data_len < sizeof(struct compass_3d_sample)){
67         ALOGE("Insufficient length \n");
68         return  - 1;
69     }
70     sample = (struct compass_3d_sample*)raw_data;
71
72     float sc = GetScaleValue();
73     mPendingEvent.data[0] = mPendingEvent.magnetic.x = CONVERT_M_MG_VTF16E14_X
74         (GetChannelBytesUsedSize(CHANNEL_X), sc, sample->compass_x);
75     mPendingEvent.data[1] = mPendingEvent.magnetic.y = CONVERT_M_MG_VTF16E14_Y
76         (GetChannelBytesUsedSize(CHANNEL_Y), sc, sample->compass_y);
77     mPendingEvent.data[2] = mPendingEvent.magnetic.z = CONVERT_M_MG_VTF16E14_Z
78         (GetChannelBytesUsedSize(CHANNEL_Z), sc, sample->compass_z);
79
80     ALOGV("COMPASS 3D Sample %fuT %fuT %fuT\n", mPendingEvent.magnetic.x,
81         mPendingEvent.magnetic.y, mPendingEvent.magnetic.z);
82     ALOGV("<<%s", __func__);
83     return 0;
84 }