upm  0.4.1
Sensor/Actuator repository for libmraa (v0.8.0)
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 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
31 #include "../IsrCallback.h"
32 #endif
33 
34 #define MMA7660_I2C_BUS 0
35 #define MMA7660_DEFAULT_I2C_ADDR 0x4c
36 
37 namespace upm {
38 
67  class MMA7660 {
68  public:
69 
70  // MMA7660 registers
71  typedef enum { REG_XOUT = 0x00,
72  REG_YOUT = 0x01,
73  REG_ZOUT = 0x02,
74  REG_TILT = 0x03,
75  REG_SRST = 0x04, // Sampling Rate Status
76  REG_SPCNT = 0x05, // sleep count
77  REG_INTSU = 0x06, // Interrupt setup
78  REG_MODE = 0x07, // operating mode
79  REG_SR = 0x08, // auto-wake/sleep, SPS, and debounce
80  REG_PDET = 0x09, // tap detection
81  REG_PD = 0x0a // tap debounce count
82  // 0x0b-0x1f reserved
83  } MMA7660_REG_T;
84 
85  // interrupt enable register bits
86  typedef enum { INTR_NONE = 0x00, // disabled
87  INTR_FBINT = 0x01, // front/back
88  INTR_PLINT = 0x02, // up/down/right/left
89  INTR_PDINT = 0x04, // tap detection
90  INTR_ASINT = 0x08, // exit auto-sleep
91  INTR_GINT = 0x10, // measurement intr
92  INTR_SHINTZ = 0x20, // shake on Z
93  INTR_SHINTY = 0x40, // shake on Y
94  INTR_SHINTX = 0x80 // shake on X
95  } MMA7660_INTR_T;
96 
97  // operating mode register bits
98  typedef enum { MODE_MODE = 0x01, // determines mode with MODE_TON
99  // 0x02 reserved
100  MODE_TON = 0x04, // determines mode with MODE_MODE
101  MODE_AWE = 0x08, // auto-wake
102  MODE_ASE = 0x10, // auto-sleep
103  MODE_SCPS = 0x20, // sleep count prescale
104  MODE_IPP = 0x40, // intr out push-pull/open drain
105  MODE_IAH = 0x80 // intr active low/high
106  } MMA7660_MODE_T;
107 
108  // tilt BackFront (BF) bits
109  typedef enum { BF_UNKNOWN = 0x00,
110  BF_LYING_FRONT = 0x01,
111  BF_LYING_BACK = 0x02
112  } MMA7660_TILT_BF_T;
113 
114  // tilt LandscapePortrait (LP) bits
115  typedef enum { LP_UNKNOWN = 0x00,
116  LP_LANDSCAPE_LEFT = 0x01,
117  LP_LANDSCAPE_RIGHT = 0x02,
118  LP_VERT_DOWN = 0x05,
119  LP_VERT_UP = 0x06
120  } MMA7660_TILT_LP_T;
121 
122  // sample rate (auto-sleep) values
123  typedef enum { AUTOSLEEP_120 = 0x00,
124  AUTOSLEEP_64 = 0x01,
125  AUTOSLEEP_32 = 0x02,
126  AUTOSLEEP_16 = 0x03,
127  AUTOSLEEP_8 = 0x04,
128  AUTOSLEEP_4 = 0x05,
129  AUTOSLEEP_2 = 0x06,
130  AUTOSLEEP_1 = 0x07
131  } MMA7660_AUTOSLEEP_T;
132 
139  MMA7660(int bus, uint8_t address = MMA7660_DEFAULT_I2C_ADDR);
140 
144  ~MMA7660();
145 
153  bool writeByte(uint8_t reg, uint8_t byte);
154 
161  uint8_t readByte(uint8_t reg);
162 
170  void getRawValues(int *x, int *y, int *z);
171 
172 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
173 
178  int *getRawValues();
179 #endif
180 
188  void getAcceleration(float *ax, float *ay, float *az);
189 
190 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
191 
196  float *getAcceleration();
197 #endif
198 
206  int getVerifiedAxis(MMA7660_REG_T axis);
207 
213  uint8_t getVerifiedTilt();
214 
221  void setModeActive();
222 
230  void setModeStandby();
231 
239  uint8_t tiltBackFront();
240 
248  uint8_t tiltLandscapePortrait();
249 
255  bool tiltTap();
256 
262  bool tiltShake();
263 
273 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
274  void installISR(int pin, IsrCallback *cb);
275 #else
276  void installISR(int pin, void (*isr)(void *), void *arg);
277 #endif
278 
282  void uninstallISR();
283 
292  bool setInterruptBits(uint8_t ibits);
293 
301  bool setSampleRate(MMA7660_AUTOSLEEP_T sr);
302 
303  private:
304 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
305  void installISR(int pin, void (*isr)(void *), void *arg);
306 #endif
307 
308  bool m_isrInstalled;
309  mraa_i2c_context m_i2c;
310  mraa_gpio_context m_gpio;
311  uint8_t m_addr;
312  };
313 }
314 
315 
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:67
bool setSampleRate(MMA7660_AUTOSLEEP_T sr)
Definition: mma7660.cxx:251
bool tiltShake()
Definition: mma7660.cxx:199
Definition: a110x.h:33
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