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 "MMA7660Accelerometer.hpp"
19 #include "SensorsHAL.hpp"
20 #include "SensorUtils.hpp"
21
22 struct sensor_t MMA7660Accelerometer::sensorDescription = {
23 .name = "MMA7660 Accelerometer",
24 .vendor = "Freescale Semiconductor",
25 .version = 1,
26 .handle = -1,
27 .type = SENSOR_TYPE_ACCELEROMETER,
28 /* maxRange = 1.5g */
29 .maxRange = 14.72f,
30 .resolution = 0.459915612f,
31 .power = 0.047f,
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 * MMA7660Accelerometer::createSensor(int pollFd) {
43 return new MMA7660Accelerometer(pollFd,
44 SensorUtils::getI2cBusNumber(),
45 MMA7660_DEFAULT_I2C_ADDR);
46 }
47
initModule()48 void MMA7660Accelerometer::initModule() {
49 SensorContext::addSensorModule(&sensorDescription, createSensor);
50 }
51
MMA7660Accelerometer(int pollFd,int bus,uint8_t address)52 MMA7660Accelerometer::MMA7660Accelerometer(int pollFd, int bus, uint8_t address)
53 : MMA7660 (bus, address), pollFd(pollFd) {
54 handle = sensorDescription.handle;
55 type = SENSOR_TYPE_ACCELEROMETER;
56 }
57
~MMA7660Accelerometer()58 MMA7660Accelerometer::~MMA7660Accelerometer() {}
59
pollEvents(sensors_event_t * data,int count)60 int MMA7660Accelerometer::pollEvents(sensors_event_t* data, int count) {
61 getAcceleration(&data->acceleration.x, &data->acceleration.y, &data->acceleration.z);
62 data->acceleration.x *= Sensor::kGravitationalAcceleration;
63 data->acceleration.y *= Sensor::kGravitationalAcceleration;
64 data->acceleration.z *= Sensor::kGravitationalAcceleration;
65 return 1;
66 }
67
68 // MMA7660 accelerometer sensor implementation
activate(int handle,int enabled)69 int MMA7660Accelerometer::activate(int handle, int enabled) {
70 setModeStandby();
71 if (enabled) {
72 if (!setSampleRate(upm::MMA7660::AUTOSLEEP_64)) {
73 ALOGE("%s: Failed to set sensor SampleRate", __func__);
74 return -1;
75 }
76
77 setModeActive();
78 usleep(kActivationPeriod);
79 }
80
81 /* start or stop the acquisition thread */
82 return activateAcquisitionThread(pollFd, handle, enabled);
83 }
84