-
Notifications
You must be signed in to change notification settings - Fork 5
/
mma8652.cpp
370 lines (335 loc) · 7.54 KB
/
mma8652.cpp
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/**
* Copyright (c) 2014 panStamp <[email protected]>
*
* This file is part of the panStamp project.
*
* panStamp is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* any later version.
*
* panStamp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with panStamp; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*
* Author: Daniel Berenguer
* Creation date: 08/05/2013
*/
#include "Wire.h"
#include "mma8652.h"
/**
* init
*
* Initialize MMA8652 sensor
*/
void MMA8652::init(TwoWire &wirePort)
{
_i2cPort = &wirePort;
ctrlReg4 = 0;
// Place MMA8652 in standby mode
standBy();
delay(1);
// Accelerometer range of +/-2g range with 0.244mg/LSB
write(MMA8652_XYZ_DATA_CFG, 0);
delay(1);
// Enable interrupts - Open-drain output
// Pulse function interrupt can wake-up system
// Orientation interrupt can wake-up system
// Free-fall/motion function interrupt can wake-up system
write(MMA8652_CTRL_REG3, 0x39);
delay(1);
// Enable Pulse detection Interrupt
// Enable Orientation Interrupt
// Enable Free-fall/motion Interrupt
write(MMA8652_CTRL_REG4, ctrlReg4);
delay(1);
// All interrupt sources hooked to INT2 pin
write(MMA8652_CTRL_REG5, 0);
delay(1);
// Enter active mode
active();
}
/**
* read
*
* Read single byte from register
*
* @param address Register address
*/
uint8_t MMA8652::read(uint8_t address)
{
_i2cPort->beginTransmission(MMA8652_CTRL_ID_DEFAULT);
_i2cPort->write(address);
_i2cPort->endTransmission(false);
_i2cPort->requestFrom((int)MMA8652_CTRL_ID_DEFAULT, (int)1);
while(!_i2cPort->available());
return _i2cPort->read();
}
/**
* read
*
* Read multiple bytes from register
*
* @param address Register address
* @param buffer output buffer
* @param length amount of bytes to be read
*
* @return amount of bytes read
*/
uint8_t MMA8652::read(uint8_t address, uint8_t *buffer, uint8_t length)
{
uint8_t i;
_i2cPort->beginTransmission(MMA8652_CTRL_ID_DEFAULT);
_i2cPort->write(address);
_i2cPort->endTransmission(false);
_i2cPort->requestFrom((int)MMA8652_CTRL_ID_DEFAULT, (int)length);
for(i=0 ; i<length ; i++)
{
while(!_i2cPort->available());
buffer[i] = _i2cPort->read();
}
return i;
}
/**
* write
*
* Write single byte in register
*
* @param address Register address
* @param data
*/
void MMA8652::write(uint8_t address, uint8_t data)
{
_i2cPort->beginTransmission(MMA8652_CTRL_ID_DEFAULT);
_i2cPort->write(address);
_i2cPort->write(data);
_i2cPort->endTransmission();
}
/**
* reset
*
* Reset device
*/
void MMA8652::reset(void)
{
standBy();
delay(1);
write(MMA8652_CTRL_REG2, RST_MASK);
delay(1);
active();
}
/**
* readXYZ
*
* Read XYZ axis. Place result in global variable axis
* Each count equals to 1/1024 gr
*/
void MMA8652::readXYZ(void)
{
uint8_t buf[7];
uint16_t twoCompl;
read(MMA8652_STATUS_00, buf, 7);
twoCompl = ((buf[1] << 8) | buf[2]) >> 4;
if (buf[1] > 0x7F)
axis.x = (~twoCompl + 1) * (-1);
else
axis.x = twoCompl;
twoCompl = ((buf[3] << 8) | buf[4]) >> 4;
if (buf[3] > 0x7F)
axis.y = (~twoCompl + 1) * (-1);
else
axis.y = twoCompl;
twoCompl = ((buf[5] << 8) | buf[6]) >> 4;
if (buf[5] > 0x7F)
axis.z = (~twoCompl + 1) * (-1);
else
axis.z = twoCompl;
}
/**
* enableInt
*
* Enable interrupt and attach interrupt to output pin
*
* @param source Source of interrupt - mask (SRC_ASLP_MASK, SRC_FIFO_MASK, SRC_TRANS_MASK
* SRC_LNDPRT_MASK, SRC_PULSE_MASK, SRC_FF_MT_MASK, SRC_DRDY_MASK)
*/
void MMA8652::enableInt(uint8_t source)
{
// Enable interrupt
ctrlReg4 |= source;
write(MMA8652_CTRL_REG4, ctrlReg4);
}
/**
* disableInt
*
* Disable interrupt
*
* @param source Source of interrupt - mask (SRC_ASLP_MASK, SRC_FIFO_MASK, SRC_TRANS_MASK
* SRC_LNDPRT_MASK, SRC_PULSE_MASK, SRC_FF_MT_MASK, SRC_DRDY_MASK)
*/
void MMA8652::disableInt(uint8_t source)
{
// Disable interrupt
ctrlReg4 &= ~source;
write(MMA8652_CTRL_REG4, ctrlReg4);
}
/**
* enableTapInt
*
* Enable tap interrupt and attach interrupt to output pin
*
* @param sensibility 0 is the highest sensibility, 255 is the lowest
*/
void MMA8652::enableTapInt(uint8_t sensibility, bool doubleTap)
{
uint8_t cfg;
// Enter stand-by mode
standBy();
delay(1);
enableInt(SRC_PULSE_MASK);
delay(1);
if (doubleTap)
cfg = 0x2A;
else
cfg = 0x15;
// Enable single pulse detection on each axis
write(MMA8652_PULSE_CFG, cfg);
delay(1);
write(MMA8652_PULSE_THSX, sensibility);
delay(1);
write(MMA8652_PULSE_THSY, sensibility);
delay(1);
write(MMA8652_PULSE_THSZ, sensibility);
delay(1);
// Back to active mode
active();
}
/**
* disableTapInt
*
* Disable tap interrupt
*/
void MMA8652::disableTapInt(void)
{
// Enter stand-by mode
standBy();
delay(1);
disableInt(SRC_PULSE_MASK);
delay(1);
// Disable Portrait/Ladscape orientation detection
write(MMA8652_PULSE_CFG, 0);
delay(1);
// Back to active mode
active();
}
/**
* enableFreeFallInt
*
* Enable free-fall interrupt and attach interrupt to output pin
*
* @param sensibility 0 is the highest sensibility, 255 is the lowest
*/
void MMA8652::enableFreeFallInt(uint8_t sensibility)
{
// Enter stand-by mode
standBy();
delay(1);
enableInt(SRC_FF_MT_MASK);
delay(1);
// Enable single pulse detection on each axis
write(MMA8652_FF_MT_CFG, 0xF8); // Motion detection - XYZ events enabled
delay(1);
write(MMA8652_FF_MT_THS, sensibility);
delay(1);
// Back to active mode
active();
}
/**
* disableFreeFallInt
*
* Disable free-fall interrupt
*/
void MMA8652::disableFreeFallInt(void)
{
// Enter stand-by mode
standBy();
delay(1);
disableInt(SRC_FF_MT_MASK);
delay(1);
// Disable Portrait/Ladscape orientation detection
write(MMA8652_FF_MT_CFG, 0);
delay(1);
// Back to active mode
active();
}
/**
* enablePlInt
*
* Enable portrait-landscape orientation interrupt and attach interrupt
* to output pin
*/
void MMA8652::enablePlInt(void)
{
// Enter stand-by mode
standBy();
delay(1);
enableInt(SRC_LNDPRT_MASK);
delay(1);
// Enable Portrait/Ladscape orientation detection
write(MMA8652_PL_CFG, PL_EN_MASK);
delay(1);
// Back to active mode
active();
}
/**
* disablePlInt
*
* Disable portrait-landscape orientation interrupt
*/
void MMA8652::disablePlInt(void)
{
// Enter stand-by mode
standBy();
delay(1);
disableInt(SRC_LNDPRT_MASK);
delay(1);
// Disable Portrait/Ladscape orientation detection
write(MMA8652_PL_CFG, 0);
delay(1);
// Back to active mode
active();
}
/**
* sleep
*
* Enter sleep mode, ready to be brought back to the active mode by an
* interrupt
*/
void MMA8652::sleep(void)
{
if (_IS_PL_INT_ENABLED())
{
// Disable Portrait/Landscape orientation interrupt, which is incompatible
// With the current sleep mode
disablePlInt();
delay(1);
}
// Re-enter active mode first
active();
delay(2);
// Enter stand-by mode
standBy();
delay(2);
write(MMA8652_ASLP_COUNT, 1);
delay(2);
write(MMA8652_CTRL_REG2, 0x1C);
delay(1);
// Back to active mode
active();
}