1 /*
2 * Copyright (C) 2015 Intel Corporation
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 <cutils/log.h>
18 #include "LSM303dOrientation.hpp"
19 #include "SensorsHAL.hpp"
20 #include "SensorUtils.hpp"
21
22 struct sensor_t LSM303dOrientation::sensorDescription = {
23 .name = "LSM303d Orientation",
24 .vendor = "Unknown",
25 .version = 1,
26 .handle = -1,
27 .type = SENSOR_TYPE_ORIENTATION,
28 .maxRange = 12.0f,
29 .resolution = .00003f,
30 .power = 0.0003f,
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
createSensor(int pollFd)41 Sensor * LSM303dOrientation::createSensor(int pollFd) {
42 return new LSM303dOrientation(pollFd, SensorUtils::getI2cBusNumber());
43 }
44
initModule()45 void LSM303dOrientation::initModule() {
46 SensorContext::addSensorModule(&sensorDescription, createSensor);
47 }
48
LSM303dOrientation(int pollFd,int bus,int address)49 LSM303dOrientation::LSM303dOrientation(int pollFd,
50 int bus, int address)
51 : LSM303d(bus, address), pollFd(pollFd) {
52 this->type = SENSOR_TYPE_ORIENTATION;
53 this->handle = sensorDescription.handle;
54 }
55
~LSM303dOrientation()56 LSM303dOrientation::~LSM303dOrientation() {}
57
pollEvents(sensors_event_t * data,int count)58 int LSM303dOrientation::pollEvents(sensors_event_t* data, int count) {
59 getCoordinates();
60 int16_t *rawdatap = getRawCoorData();
61 data->orientation.x = (double)rawdatap[0];
62 data->orientation.y = (double)rawdatap[1];
63 data->orientation.z = (double)rawdatap[2];
64 return 1;
65 }
66
activate(int handle,int enabled)67 int LSM303dOrientation::activate(int handle, int enabled) {
68 /* start or stop the acquisition thread */
69 return activateAcquisitionThread(pollFd, handle, enabled);
70 }
71