summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/mma7660/mma7660.cxx
blob: 2215221d9cb948942a7279faa59836cb0a7b44fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * Author: Jon Trulson <jtrulson@ics.com>
 * Copyright (c) 2015 Intel Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <iostream>
#include <stdexcept>
#include <string>

#include "mma7660.h"

using namespace upm;
using namespace std;


MMA7660::MMA7660(int bus, uint8_t address)
{
  m_addr = address;
  m_isrInstalled = false;

  // setup our i2c link
  if ( !(m_i2c = mraa_i2c_init(bus)) )
    {
      throw std::invalid_argument(std::string(__FUNCTION__) +
                                  ": mraa_i2c_init() failed");
      return;
    }
      
  mraa_result_t rv;
  
  if ( (rv = mraa_i2c_address(m_i2c, m_addr)) != MRAA_SUCCESS)
    {
      throw std::invalid_argument(std::string(__FUNCTION__) +
                                  ": mraa_i2c_address() failed");
      return;
    }
}

MMA7660::~MMA7660()
{
  if (m_isrInstalled)
    uninstallISR();

  setModeStandby();
  mraa_i2c_stop(m_i2c);
}

bool MMA7660::writeByte(uint8_t reg, uint8_t byte)
{
  mraa_result_t rv = mraa_i2c_write_byte_data(m_i2c, byte, reg);

  if (rv != MRAA_SUCCESS)
    {
      return false;
    }

  return true;
}

uint8_t MMA7660::readByte(uint8_t reg)
{
  return mraa_i2c_read_byte_data(m_i2c, reg);
}

void MMA7660::getRawValues(int *x, int *y, int *z)
{
  *x = getVerifiedAxis(REG_XOUT);
  *y = getVerifiedAxis(REG_YOUT);
  *z = getVerifiedAxis(REG_ZOUT);
}

#ifdef JAVACALLBACK
int *MMA7660::getRawValues()
{
  int *values = new int[3];
  getRawValues(&values[0], &values[1], &values[2]);
  return values;
}
#endif

void MMA7660::setModeActive()
{
  uint8_t modeReg = readByte(REG_MODE);

  // The D2 (TON bit) should be cleared, and the MODE bit set

  modeReg &= ~MODE_TON;
  modeReg |= MODE_MODE;

  writeByte(REG_MODE, modeReg);
}

void MMA7660::setModeStandby()
{
  uint8_t modeReg = readByte(REG_MODE);

  // the D0 (mode bit) and D2 (TON bit) should be cleared.

  modeReg &= ~MODE_TON;
  modeReg &= ~MODE_MODE;

  writeByte(REG_MODE, modeReg);
}

// read an axis value, verifying it's validity
int MMA7660::getVerifiedAxis(MMA7660_REG_T axis)
{
  // We only want one of the 3 axes

  if (axis > 2)
    {
      throw std::out_of_range(std::string(__FUNCTION__) +
                              ": axis must be 0, 1, or 2.");
      return 0;
    }

  // we need to check the alert bit and sign bits if the alert bit is
  // set, this means that the register was being updated when the
  // register was read, so re-read until it's clear.
  
  uint8_t val;
  do {
    val = readByte(axis);

    // check alert bit
  } while (val & 0x40);

  // shift the sign bit over, and compensate
  return (char(val << 2) / 4);
}

// read the tilt register, verifying it's validity
uint8_t MMA7660::getVerifiedTilt()
{
  // we need to check the alert bit and sign bits if the alert bit is
  // set, this means that the register was being updated when the
  // register was read, so re-read until it's clear.
  
  uint8_t val;
  do {
    val = readByte(REG_TILT);

    // check alert bit
  } while (val & 0x40);

  return val;
}

uint8_t MMA7660::tiltBackFront()
{
  uint8_t val = getVerifiedTilt();

  // mask off the bits we don't care about
  val &= 0x03;
  return val;
}

uint8_t MMA7660::tiltLandscapePortrait()
{
  uint8_t val = getVerifiedTilt();

  // mask off the bits we don't care about
  val >>= 2;
  val &= 0x07;
  return val;
}

bool MMA7660::tiltTap()
{
  uint8_t val = getVerifiedTilt();

  if (val & 0x20)
    return true;
  else
    return false;
}

bool MMA7660::tiltShake()
{
  uint8_t val = getVerifiedTilt();

  if (val & 0x80)
    return true;
  else
    return false;
}

#ifdef JAVACALLBACK
void MMA7660::installISR(int pin, jobject runnable)
{
        installISR(pin, mraa_java_isr_callback, runnable);
}
#endif

void MMA7660::installISR(int pin, void (*isr)(void *), void *arg)
{
  if (m_isrInstalled)
    uninstallISR();

  if ( !(m_gpio = mraa_gpio_init(pin)) )
    {
      throw std::invalid_argument(std::string(__FUNCTION__) +
                                  ": mraa_gpio_init() failed, invalid pin?");
      return;
    }

  mraa_gpio_dir(m_gpio, MRAA_GPIO_IN);

  // install our interrupt handler
  mraa_gpio_isr(m_gpio, MRAA_GPIO_EDGE_RISING, 
                isr, arg);
  m_isrInstalled = true;
}

void MMA7660::uninstallISR()
{
  if (!m_isrInstalled)
    return;

  mraa_gpio_isr_exit(m_gpio);
  m_isrInstalled = false;
  mraa_gpio_close(m_gpio);
}

bool MMA7660::setInterruptBits(uint8_t ibits)
{
  return writeByte(REG_INTSU, ibits);
}

bool MMA7660::setSampleRate(MMA7660_AUTOSLEEP_T sr)
{
  return writeByte(REG_SR, sr);
}

void MMA7660::getAcceleration(float *ax, float *ay, float *az)
{
  int x, y, z;

  getRawValues(&x, &y, &z);

  // 21.33, typical counts/g

  *ax = x/21.33;
  *ay = y/21.33;
  *az = z/21.33;
}

#ifdef JAVACALLBACK
float *MMA7660::getAcceleration()
{
  float *values = new float[3];
  getAcceleration(&values[0], &values[1], &values[2]);
  return values;
}
#endif