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 #ifndef MMA7660_ACCELEROMETER_HPP
18 #define MMA7660_ACCELEROMETER_HPP
19 
20 #include <hardware/sensors.h>
21 #include "Sensor.hpp"
22 #include "mma7660.h"
23 
24 struct sensors_event_t;
25 
26 /**
27  * MMA7660Accelerometer exposes the MMA7660 accelerometer sensor
28  *
29  * Overrides the pollEvents & activate Sensor methods.
30  */
31 class MMA7660Accelerometer : public Sensor, public upm::MMA7660 {
32   public:
33     static const int kMaxRange = 1000;
34     /**
35      * Time period in microseconds (1/64 * 10^6 = 15625) to wait before
36      * requesting events for the default activation sampling rate (64 Hz)
37      */
38     static const int kActivationPeriod = 15625;
39 
40     /**
41      * MMA7660Accelerometer constructor
42      * @param pollFd poll file descriptor
43      * @param bus number of the bus
44      * @param address device address
45      */
46     MMA7660Accelerometer(int pollFd, int bus, uint8_t address);
47 
48     /**
49      * MMA7660Accelerometer destructor
50      */
51     ~MMA7660Accelerometer() override;
52 
53     /**
54      * Poll for events
55      * @param data where to store the events
56      * @param count the number of events returned must be <= to the count
57      * @return number of events returned in data on success and a negative error number otherwise
58      */
59     int pollEvents(sensors_event_t* data, int count) override;
60 
61     /**
62      * Activate the sensor
63      * @param handle sensor identifier
64      * @param enabled 1 for enabling and 0 for disabling
65      * @return 0 on success and a negative error number otherwise
66      */
67     int activate(int handle, int enabled);
68 
69 private:
70     static Sensor * createSensor(int pollFd);
71     static void initModule() __attribute__((constructor
72         (DEFAULT_SENSOR_CONSTRUCTOR_PRIORITY)));
73 
74     int pollFd;
75     static struct sensor_t sensorDescription;
76 };
77 
78 #endif  // MMA7660_ACCELEROMETER_HPP
79