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 impliedt data output.
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 "LSM9DS0Accelerometer.hpp"
19 #include "SensorsHAL.hpp"
20 #include "SensorUtils.hpp"
21 
22 struct sensor_t LSM9DS0Accelerometer::sensorDescription = {
23   .name = "LSM9DS0 Accelerometer",
24   .vendor = "Unknown",
25   .version = 1,
26   .handle = -1,
27   .type = SENSOR_TYPE_ACCELEROMETER,
28   .maxRange = 16.0f,
29   .resolution = 0.061,
30   .power = 0.000350,
31   .minDelay = 0,
32   .fifoReservedEventCount = 0,
33   .fifoMaxEventCount = 0,
34   .stringType = SENSOR_STRING_TYPE_ACCELEROMETER,
35   .requiredPermission = "",
36   .maxDelay = 1000,
37   .flags = SENSOR_FLAG_CONTINUOUS_MODE,
38   .reserved = {},
39 };
40 
createSensor(int pollFd)41 Sensor * LSM9DS0Accelerometer::createSensor(int pollFd) {
42   return new LSM9DS0Accelerometer(pollFd,
43       SensorUtils::getI2cBusNumber(),
44       LSM9DS0_DEFAULT_GYRO_ADDR, LSM9DS0_DEFAULT_XM_ADDR);
45 
46 }
47 
initModule()48 void LSM9DS0Accelerometer::initModule() {
49   SensorContext::addSensorModule(&sensorDescription, createSensor);
50 }
51 
LSM9DS0Accelerometer(int pollFd,int bus,uint8_t gAddress,uint8_t xmAddress)52 LSM9DS0Accelerometer::LSM9DS0Accelerometer(int pollFd,
53     int bus, uint8_t gAddress, uint8_t xmAddress)
54     : LSM9DS0 (bus, gAddress, xmAddress), pollFd(pollFd) {
55   handle = sensorDescription.handle;
56   type = SENSOR_TYPE_ACCELEROMETER;
57 }
58 
~LSM9DS0Accelerometer()59 LSM9DS0Accelerometer::~LSM9DS0Accelerometer() {}
60 
pollEvents(sensors_event_t * data,int count)61 int LSM9DS0Accelerometer::pollEvents(sensors_event_t* data, int count) {
62   updateAccelerometer();
63   getAccelerometer(&data->acceleration.x, &data->acceleration.y, &data->acceleration.z);
64   data->acceleration.x *= Sensor::kGravitationalAcceleration;
65   data->acceleration.y *= Sensor::kGravitationalAcceleration;
66   data->acceleration.z *= Sensor::kGravitationalAcceleration;
67   return 1;
68 }
69 
70 // LSM9DS0 accelerometer sensor implementation
activate(int handle,int enabled)71 int LSM9DS0Accelerometer::activate(int handle, int enabled) {
72   init();
73 
74   /* start or stop the acquisition thread */
75   return activateAcquisitionThread(pollFd, handle, enabled);
76 }
77