upm  0.5.1
Sensor/Actuator repository for libmraa (v0.9.1)
 All Data Structures Files Functions Variables Enumerations Enumerator Macros Groups Pages
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 
63  class MMA7660 {
64  public:
65 
66  // MMA7660 registers
67  typedef enum { REG_XOUT = 0x00,
68  REG_YOUT = 0x01,
69  REG_ZOUT = 0x02,
70  REG_TILT = 0x03,
71  REG_SRST = 0x04, // Sampling Rate Status
72  REG_SPCNT = 0x05, // sleep count
73  REG_INTSU = 0x06, // Interrupt setup
74  REG_MODE = 0x07, // operating mode
75  REG_SR = 0x08, // auto-wake/sleep, SPS, and debounce
76  REG_PDET = 0x09, // tap detection
77  REG_PD = 0x0a // tap debounce count
78  // 0x0b-0x1f reserved
79  } MMA7660_REG_T;
80 
81  // interrupt enable register bits
82  typedef enum { INTR_NONE = 0x00, // disabled
83  INTR_FBINT = 0x01, // front/back
84  INTR_PLINT = 0x02, // up/down/right/left
85  INTR_PDINT = 0x04, // tap detection
86  INTR_ASINT = 0x08, // exit auto-sleep
87  INTR_GINT = 0x10, // measurement intr
88  INTR_SHINTZ = 0x20, // shake on Z
89  INTR_SHINTY = 0x40, // shake on Y
90  INTR_SHINTX = 0x80 // shake on X
91  } MMA7660_INTR_T;
92 
93  // operating mode register bits
94  typedef enum { MODE_MODE = 0x01, // determines mode with MODE_TON
95  // 0x02 reserved
96  MODE_TON = 0x04, // determines mode with MODE_MODE
97  MODE_AWE = 0x08, // auto-wake
98  MODE_ASE = 0x10, // auto-sleep
99  MODE_SCPS = 0x20, // sleep count prescale
100  MODE_IPP = 0x40, // intr out push-pull/open drain
101  MODE_IAH = 0x80 // intr active low/high
102  } MMA7660_MODE_T;
103 
104  // tilt BackFront (BF) bits
105  typedef enum { BF_UNKNOWN = 0x00,
106  BF_LYING_FRONT = 0x01,
107  BF_LYING_BACK = 0x02
108  } MMA7660_TILT_BF_T;
109 
110  // tilt LandscapePortrait (LP) bits
111  typedef enum { LP_UNKNOWN = 0x00,
112  LP_LANDSCAPE_LEFT = 0x01,
113  LP_LANDSCAPE_RIGHT = 0x02,
114  LP_VERT_DOWN = 0x05,
115  LP_VERT_UP = 0x06
116  } MMA7660_TILT_LP_T;
117 
118  // sample rate (auto-sleep) values
119  typedef enum { AUTOSLEEP_120 = 0x00,
120  AUTOSLEEP_64 = 0x01,
121  AUTOSLEEP_32 = 0x02,
122  AUTOSLEEP_16 = 0x03,
123  AUTOSLEEP_8 = 0x04,
124  AUTOSLEEP_4 = 0x05,
125  AUTOSLEEP_2 = 0x06,
126  AUTOSLEEP_1 = 0x07
127  } MMA7660_AUTOSLEEP_T;
128 
135  MMA7660(int bus, uint8_t address = MMA7660_DEFAULT_I2C_ADDR);
136 
140  ~MMA7660();
141 
149  bool writeByte(uint8_t reg, uint8_t byte);
150 
157  uint8_t readByte(uint8_t reg);
158 
166  void getRawValues(int *x, int *y, int *z);
167 
168 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
169 
174  int *getRawValues();
175 #endif
176 
184  void getAcceleration(float *ax, float *ay, float *az);
185 
186 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
187 
192  float *getAcceleration();
193 #endif
194 
202  int getVerifiedAxis(MMA7660_REG_T axis);
203 
209  uint8_t getVerifiedTilt();
210 
217  void setModeActive();
218 
226  void setModeStandby();
227 
235  uint8_t tiltBackFront();
236 
244  uint8_t tiltLandscapePortrait();
245 
251  bool tiltTap();
252 
258  bool tiltShake();
259 
269 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
270  void installISR(int pin, jobject runnable);
271 #else
272  void installISR(int pin, void (*isr)(void *), void *arg);
273 #endif
274 
278  void uninstallISR();
279 
288  bool setInterruptBits(uint8_t ibits);
289 
297  bool setSampleRate(MMA7660_AUTOSLEEP_T sr);
298 
299  private:
300 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
301  void installISR(int pin, void (*isr)(void *), void *arg);
302 #endif
303 
304  bool m_isrInstalled;
305  mraa_i2c_context m_i2c;
306  mraa_gpio_context m_gpio;
307  uint8_t m_addr;
308  };
309 }
310 
311 
bool writeByte(uint8_t reg, uint8_t byte)
Definition: mma7660.cxx:67
void setModeStandby()
Definition: mma7660.cxx:114
void installISR(int pin, void(*isr)(void *), void *arg)
Definition: mma7660.cxx:216
bool tiltTap()
Definition: mma7660.cxx:189
API for the MMA7660 I2C 3-Axis Digital Accelerometer.
Definition: mma7660.h:63
bool setSampleRate(MMA7660_AUTOSLEEP_T sr)
Definition: mma7660.cxx:251
bool tiltShake()
Definition: mma7660.cxx:199
bool setInterruptBits(uint8_t ibits)
Definition: mma7660.cxx:246
void setModeActive()
Definition: mma7660.cxx:102
void getRawValues(int *x, int *y, int *z)
Definition: mma7660.cxx:86
uint8_t readByte(uint8_t reg)
Definition: mma7660.cxx:81
uint8_t tiltBackFront()
Definition: mma7660.cxx:170
uint8_t tiltLandscapePortrait()
Definition: mma7660.cxx:179
uint8_t getVerifiedTilt()
Definition: mma7660.cxx:154
void getAcceleration(float *ax, float *ay, float *az)
Definition: mma7660.cxx:256
void uninstallISR()
Definition: mma7660.cxx:236
int getVerifiedAxis(MMA7660_REG_T axis)
Definition: mma7660.cxx:127
~MMA7660()
Definition: mma7660.cxx:58
MMA7660(int bus, uint8_t address=MMA7660_DEFAULT_I2C_ADDR)
Definition: mma7660.cxx:35