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 "MPU9150Accelerometer.hpp"
19 #include "SensorsHAL.hpp"
20 #include "SensorUtils.hpp"
21
22 struct sensor_t MPU9150Accelerometer::sensorDescription = {
23 .name = "MPU9150/9250 Accelerometer",
24 .vendor = "InvenSense",
25 .version = 1,
26 .handle = -1,
27 .type = SENSOR_TYPE_ACCELEROMETER,
28 /* maxRange = 2g */
29 .maxRange = 19.62f,
30 .resolution = 0.000061035f,
31 .power = 0.0198f,
32 .minDelay = 10,
33 .fifoReservedEventCount = 0,
34 .fifoMaxEventCount = 0,
35 .stringType = SENSOR_STRING_TYPE_ACCELEROMETER,
36 .requiredPermission = "",
37 .maxDelay = 1000,
38 .flags = SENSOR_FLAG_CONTINUOUS_MODE,
39 .reserved = {},
40 };
41
createSensor(int pollFd)42 Sensor * MPU9150Accelerometer::createSensor(int pollFd) {
43 return new MPU9150Accelerometer(pollFd,
44 SensorUtils::getI2cBusNumber(),
45 MPU9150_DEFAULT_I2C_ADDR, AK8975_DEFAULT_I2C_ADDR, false);
46 }
47
initModule()48 void MPU9150Accelerometer::initModule() {
49 SensorContext::addSensorModule(&sensorDescription, createSensor);
50 }
51
MPU9150Accelerometer(int pollFd,int bus,int address,int magAddress,bool enableAk8975)52 MPU9150Accelerometer::MPU9150Accelerometer(int pollFd, int bus, int address,
53 int magAddress, bool enableAk8975)
54 : MPU9150(bus, address, magAddress, enableAk8975), pollFd(pollFd) {
55 handle = sensorDescription.handle;
56 type = SENSOR_TYPE_ACCELEROMETER;
57 }
58
~MPU9150Accelerometer()59 MPU9150Accelerometer::~MPU9150Accelerometer() {}
60
pollEvents(sensors_event_t * data,int count)61 int MPU9150Accelerometer::pollEvents(sensors_event_t* data, int count) {
62 update();
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
activate(int handle,int enabled)70 int MPU9150Accelerometer::activate(int handle, int enabled) {
71 if (enabled) {
72 if (init() != true) {
73 ALOGE("%s: Failed to initialize sensor error", __func__);
74 return -1;
75 }
76 }
77
78 /* start or stop the acquisition thread */
79 return activateAcquisitionThread(pollFd, handle, enabled);
80 }
81