upm  0.2.0
Sensor/Actuator repository for libmraa (v0.6.1)
mma7660.h
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2015 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #pragma once
25 
26 #include <string>
27 #include <mraa/i2c.h>
28 #include <mraa/gpio.h>
29 
30 #define MMA7660_I2C_BUS 0
31 #define MMA7660_DEFAULT_I2C_ADDR 0x4c
32 
33 namespace upm {
34 
61  class MMA7660 {
62  public:
63 
64  // MMA7660 registers
65  typedef enum { REG_XOUT = 0x00,
66  REG_YOUT = 0x01,
67  REG_ZOUT = 0x02,
68  REG_TILT = 0x03,
69  REG_SRST = 0x04, // Sampling Rate Status
70  REG_SPCNT = 0x05, // sleep count
71  REG_INTSU = 0x06, // Interrupt setup
72  REG_MODE = 0x07, // operating mode
73  REG_SR = 0x08, // auto wake/sleep, SPS and debounce
74  REG_PDET = 0x09, // tap detection
75  REG_PD = 0x0a // tap debounce count
76  // 0x0b-0x1f reserved
77  } MMA7660_REG_T;
78 
79  // interrupt enable register bits
80  typedef enum { INTR_NONE = 0x00, // disabled
81  INTR_FBINT = 0x01, // front/back
82  INTR_PLINT = 0x02, // up/down/right/left
83  INTR_PDINT = 0x04, // tap detection
84  INTR_ASINT = 0x08, // exit autosleep
85  INTR_GINT = 0x10, // measurement intr
86  INTR_SHINTZ = 0x20, // shake on Z
87  INTR_SHINTY = 0x40, // shake on Y
88  INTR_SHINTX = 0x80 // shake on X
89  } MMA7660_INTR_T;
90 
91  // operating mode register bits
92  typedef enum { MODE_MODE = 0x01, // determines mode with MODE_TON
93  // 0x02 reserved
94  MODE_TON = 0x04, // determines mode with MODE_MODE
95  MODE_AWE = 0x08, // auto-wake
96  MODE_ASE = 0x10, // auto-sleep
97  MODE_SCPS = 0x20, // sleep count pre-scale
98  MODE_IPP = 0x40, // intr out push-pull/open drain
99  MODE_IAH = 0x80 // intr active low/high
100  } MMA7660_MODE_T;
101 
102  // tilt BackFront (BF) bits
103  typedef enum { BF_UNKNOWN = 0x00,
104  BF_LYING_FRONT = 0x01,
105  BF_LYING_BACK = 0x02
106  } MMA7660_TILT_BF_T;
107 
108  // tilt LandscapePortrait (LP) bits
109  typedef enum { LP_UNKNOWN = 0x00,
110  LP_LANDSCAPE_LEFT = 0x01,
111  LP_LANDSCAPE_RIGHT = 0x02,
112  LP_VERT_DOWN = 0x05,
113  LP_VERT_UP = 0x06
114  } MMA7660_TILT_LP_T;
115 
116  // sample rate (auto sleep) values
117  typedef enum { AUTOSLEEP_120 = 0x00,
118  AUTOSLEEP_64 = 0x01,
119  AUTOSLEEP_32 = 0x02,
120  AUTOSLEEP_16 = 0x03,
121  AUTOSLEEP_8 = 0x04,
122  AUTOSLEEP_4 = 0x05,
123  AUTOSLEEP_2 = 0x06,
124  AUTOSLEEP_1 = 0x07
125  } MMA7660_AUTOSLEEP_T;
126 
133  MMA7660(int bus, uint8_t address = MMA7660_DEFAULT_I2C_ADDR);
134 
138  ~MMA7660();
139 
147  bool writeByte(uint8_t reg, uint8_t byte);
148 
155  uint8_t readByte(uint8_t reg);
156 
164  void getRawValues(int *x, int *y, int *z);
165 
173  void getAcceleration(float *ax, float *ay, float *az);
174 
182  int getVerifiedAxis(MMA7660_REG_T axis);
183 
189  uint8_t getVerifiedTilt();
190 
197  void setModeActive();
198 
206  void setModeStandby();
207 
215  uint8_t tiltBackFront();
216 
224  uint8_t tiltLandscapePortrait();
225 
231  bool tiltTap();
232 
238  bool tiltShake();
239 
249  void installISR(int pin, void (*isr)(void *), void *arg);
250 
255  void uninstallISR();
256 
265  bool setInterruptBits(uint8_t ibits);
266 
274  bool setSampleRate(MMA7660_AUTOSLEEP_T sr);
275 
276  private:
277  bool m_isrInstalled;
278  mraa_i2c_context m_i2c;
279  mraa_gpio_context m_gpio;
280  uint8_t m_addr;
281  };
282 }
283 
284 
bool writeByte(uint8_t reg, uint8_t byte)
Definition: mma7660.cxx:65
void setModeStandby()
Definition: mma7660.cxx:103
void installISR(int pin, void(*isr)(void *), void *arg)
Definition: mma7660.cxx:198
bool tiltTap()
Definition: mma7660.cxx:178
C++ API for the MMA7660 I2C 3-axis digital accelerometer.
Definition: mma7660.h:61
bool setSampleRate(MMA7660_AUTOSLEEP_T sr)
Definition: mma7660.cxx:232
bool tiltShake()
Definition: mma7660.cxx:188
Definition: a110x.h:29
bool setInterruptBits(uint8_t ibits)
Definition: mma7660.cxx:227
void setModeActive()
Definition: mma7660.cxx:91
void getRawValues(int *x, int *y, int *z)
Definition: mma7660.cxx:84
uint8_t readByte(uint8_t reg)
Definition: mma7660.cxx:79
uint8_t tiltBackFront()
Definition: mma7660.cxx:159
uint8_t tiltLandscapePortrait()
Definition: mma7660.cxx:168
uint8_t getVerifiedTilt()
Definition: mma7660.cxx:142
void getAcceleration(float *ax, float *ay, float *az)
Definition: mma7660.cxx:237
void uninstallISR()
Definition: mma7660.cxx:217
int getVerifiedAxis(MMA7660_REG_T axis)
Definition: mma7660.cxx:116
~MMA7660()
Definition: mma7660.cxx:56
MMA7660(int bus, uint8_t address=MMA7660_DEFAULT_I2C_ADDR)
Definition: mma7660.cxx:34