-
Notifications
You must be signed in to change notification settings - Fork 45
/
ADXL345.cpp
567 lines (463 loc) · 12.6 KB
/
ADXL345.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
ADXL345.cpp - Class file for the ADXL345 Triple Axis Accelerometer Arduino Library.
Version: 1.1.0
(c) 2014 Korneliusz Jarzebski
www.jarzebski.pl
This program is free software: you can redistribute it and/or modify
it under the terms of the version 3 GNU General Public License as
published by the Free Software Foundation.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#include "ADXL345.h"
bool ADXL345::begin()
{
f.XAxis = 0;
f.YAxis = 0;
f.ZAxis = 0;
Wire.begin();
// Check ADXL345 REG DEVID
if (fastRegister8(ADXL345_REG_DEVID) != 0xE5)
{
return false;
}
// Enable measurement mode (0b00001000)
writeRegister8(ADXL345_REG_POWER_CTL, 0x08);
clearSettings();
return true;
}
// Set Range
void ADXL345::setRange(adxl345_range_t range)
{
// Get actual value register
uint8_t value = readRegister8(ADXL345_REG_DATA_FORMAT);
// Update the data rate
// (&) 0b11110000 (0xF0 - Leave HSB)
// (|) 0b0000xx?? (range - Set range)
// (|) 0b00001000 (0x08 - Set Full Res)
value &= 0xF0;
value |= range;
value |= 0x08;
writeRegister8(ADXL345_REG_DATA_FORMAT, value);
}
// Get Range
adxl345_range_t ADXL345::getRange(void)
{
return (adxl345_range_t)(readRegister8(ADXL345_REG_DATA_FORMAT) & 0x03);
}
// Set Data Rate
void ADXL345::setDataRate(adxl345_dataRate_t dataRate)
{
writeRegister8(ADXL345_REG_BW_RATE, dataRate);
}
// Get Data Rate
adxl345_dataRate_t ADXL345::getDataRate(void)
{
return (adxl345_dataRate_t)(readRegister8(ADXL345_REG_BW_RATE) & 0x0F);
}
// Low Pass Filter
Vector ADXL345::lowPassFilter(Vector vector, float alpha)
{
f.XAxis = vector.XAxis * alpha + (f.XAxis * (1.0 - alpha));
f.YAxis = vector.YAxis * alpha + (f.YAxis * (1.0 - alpha));
f.ZAxis = vector.ZAxis * alpha + (f.ZAxis * (1.0 - alpha));
return f;
}
// Read raw values
Vector ADXL345::readRaw(void)
{
r.XAxis = readRegister16(ADXL345_REG_DATAX0);
r.YAxis = readRegister16(ADXL345_REG_DATAY0);
r.ZAxis = readRegister16(ADXL345_REG_DATAZ0);
return r;
}
// Read normalized values
Vector ADXL345::readNormalize(float gravityFactor)
{
readRaw();
// (4 mg/LSB scale factor in Full Res) * gravity factor
n.XAxis = r.XAxis * 0.004 * gravityFactor;
n.YAxis = r.YAxis * 0.004 * gravityFactor;
n.ZAxis = r.ZAxis * 0.004 * gravityFactor;
return n;
}
// Read scaled values
Vector ADXL345::readScaled(void)
{
readRaw();
// (4 mg/LSB scale factor in Full Res)
n.XAxis = r.XAxis * 0.004;
n.YAxis = r.YAxis * 0.004;
n.ZAxis = r.ZAxis * 0.004;
return n;
}
void ADXL345::clearSettings(void)
{
setRange(ADXL345_RANGE_2G);
setDataRate(ADXL345_DATARATE_100HZ);
writeRegister8(ADXL345_REG_THRESH_TAP, 0x00);
writeRegister8(ADXL345_REG_DUR, 0x00);
writeRegister8(ADXL345_REG_LATENT, 0x00);
writeRegister8(ADXL345_REG_WINDOW, 0x00);
writeRegister8(ADXL345_REG_THRESH_ACT, 0x00);
writeRegister8(ADXL345_REG_THRESH_INACT, 0x00);
writeRegister8(ADXL345_REG_TIME_INACT, 0x00);
writeRegister8(ADXL345_REG_THRESH_FF, 0x00);
writeRegister8(ADXL345_REG_TIME_FF, 0x00);
uint8_t value;
value = readRegister8(ADXL345_REG_ACT_INACT_CTL);
value &= 0b10001000;
writeRegister8(ADXL345_REG_ACT_INACT_CTL, value);
value = readRegister8(ADXL345_REG_TAP_AXES);
value &= 0b11111000;
writeRegister8(ADXL345_REG_TAP_AXES, value);
}
// Set Tap Threshold (62.5mg / LSB)
void ADXL345::setTapThreshold(float threshold)
{
uint8_t scaled = constrain(threshold / 0.0625f, 0, 255);
writeRegister8(ADXL345_REG_THRESH_TAP, scaled);
}
// Get Tap Threshold (62.5mg / LSB)
float ADXL345::getTapThreshold(void)
{
return readRegister8(ADXL345_REG_THRESH_TAP) * 0.0625f;
}
// Set Tap Duration (625us / LSB)
void ADXL345::setTapDuration(float duration)
{
uint8_t scaled = constrain(duration / 0.000625f, 0, 255);
writeRegister8(ADXL345_REG_DUR, scaled);
}
// Get Tap Duration (625us / LSB)
float ADXL345::getTapDuration(void)
{
return readRegister8(ADXL345_REG_DUR) * 0.000625f;
}
// Set Double Tap Latency (1.25ms / LSB)
void ADXL345::setDoubleTapLatency(float latency)
{
uint8_t scaled = constrain(latency / 0.00125f, 0, 255);
writeRegister8(ADXL345_REG_LATENT, scaled);
}
// Get Double Tap Latency (1.25ms / LSB)
float ADXL345::getDoubleTapLatency()
{
return readRegister8(ADXL345_REG_LATENT) * 0.00125f;
}
// Set Double Tap Window (1.25ms / LSB)
void ADXL345::setDoubleTapWindow(float window)
{
uint8_t scaled = constrain(window / 0.00125f, 0, 255);
writeRegister8(ADXL345_REG_WINDOW, scaled);
}
// Get Double Tap Window (1.25ms / LSB)
float ADXL345::getDoubleTapWindow(void)
{
return readRegister8(ADXL345_REG_WINDOW) * 0.00125f;
}
// Set Activity Threshold (62.5mg / LSB)
void ADXL345::setActivityThreshold(float threshold)
{
uint8_t scaled = constrain(threshold / 0.0625f, 0, 255);
writeRegister8(ADXL345_REG_THRESH_ACT, scaled);
}
// Get Activity Threshold (65.5mg / LSB)
float ADXL345::getActivityThreshold(void)
{
return readRegister8(ADXL345_REG_THRESH_ACT) * 0.0625f;
}
// Set Inactivity Threshold (65.5mg / LSB)
void ADXL345::setInactivityThreshold(float threshold)
{
uint8_t scaled = constrain(threshold / 0.0625f, 0, 255);
writeRegister8(ADXL345_REG_THRESH_INACT, scaled);
}
// Get Incactivity Threshold (65.5mg / LSB)
float ADXL345::getInactivityThreshold(void)
{
return readRegister8(ADXL345_REG_THRESH_INACT) * 0.0625f;
}
// Set Inactivity Time (s / LSB)
void ADXL345::setTimeInactivity(uint8_t time)
{
writeRegister8(ADXL345_REG_TIME_INACT, time);
}
// Get Inactivity Time (s / LSB)
uint8_t ADXL345::getTimeInactivity(void)
{
return readRegister8(ADXL345_REG_TIME_INACT);
}
// Set Free Fall Threshold (65.5mg / LSB)
void ADXL345::setFreeFallThreshold(float threshold)
{
uint8_t scaled = constrain(threshold / 0.0625f, 0, 255);
writeRegister8(ADXL345_REG_THRESH_FF, scaled);
}
// Get Free Fall Threshold (65.5mg / LSB)
float ADXL345::getFreeFallThreshold(void)
{
return readRegister8(ADXL345_REG_THRESH_FF) * 0.0625f;
}
// Set Free Fall Duratiom (5ms / LSB)
void ADXL345::setFreeFallDuration(float duration)
{
uint8_t scaled = constrain(duration / 0.005f, 0, 255);
writeRegister8(ADXL345_REG_TIME_FF, scaled);
}
// Get Free Fall Duratiom
float ADXL345::getFreeFallDuration()
{
return readRegister8(ADXL345_REG_TIME_FF) * 0.005f;
}
void ADXL345::setActivityX(bool state)
{
writeRegisterBit(ADXL345_REG_ACT_INACT_CTL, 6, state);
}
bool ADXL345::getActivityX(void)
{
return readRegisterBit(ADXL345_REG_ACT_INACT_CTL, 6);
}
void ADXL345::setActivityY(bool state)
{
writeRegisterBit(ADXL345_REG_ACT_INACT_CTL, 5, state);
}
bool ADXL345::getActivityY(void)
{
return readRegisterBit(ADXL345_REG_ACT_INACT_CTL, 5);
}
void ADXL345::setActivityZ(bool state)
{
writeRegisterBit(ADXL345_REG_ACT_INACT_CTL, 4, state);
}
bool ADXL345::getActivityZ(void)
{
return readRegisterBit(ADXL345_REG_ACT_INACT_CTL, 4);
}
void ADXL345::setActivityXYZ(bool state)
{
uint8_t value;
value = readRegister8(ADXL345_REG_ACT_INACT_CTL);
if (state)
{
value |= 0b00111000;
} else
{
value &= 0b11000111;
}
writeRegister8(ADXL345_REG_ACT_INACT_CTL, value);
}
void ADXL345::setInactivityX(bool state)
{
writeRegisterBit(ADXL345_REG_ACT_INACT_CTL, 2, state);
}
bool ADXL345::getInactivityX(void)
{
return readRegisterBit(ADXL345_REG_ACT_INACT_CTL, 2);
}
void ADXL345::setInactivityY(bool state)
{
writeRegisterBit(ADXL345_REG_ACT_INACT_CTL, 1, state);
}
bool ADXL345::getInactivityY(void)
{
return readRegisterBit(ADXL345_REG_ACT_INACT_CTL, 1);
}
void ADXL345::setInactivityZ(bool state)
{
writeRegisterBit(ADXL345_REG_ACT_INACT_CTL, 0, state);
}
bool ADXL345::getInactivityZ(void)
{
return readRegisterBit(ADXL345_REG_ACT_INACT_CTL, 0);
}
void ADXL345::setInactivityXYZ(bool state)
{
uint8_t value;
value = readRegister8(ADXL345_REG_ACT_INACT_CTL);
if (state)
{
value |= 0b00000111;
} else
{
value &= 0b11111000;
}
writeRegister8(ADXL345_REG_ACT_INACT_CTL, value);
}
void ADXL345::setTapDetectionX(bool state)
{
writeRegisterBit(ADXL345_REG_TAP_AXES, 2, state);
}
bool ADXL345::getTapDetectionX(void)
{
return readRegisterBit(ADXL345_REG_TAP_AXES, 2);
}
void ADXL345::setTapDetectionY(bool state)
{
writeRegisterBit(ADXL345_REG_TAP_AXES, 1, state);
}
bool ADXL345::getTapDetectionY(void)
{
return readRegisterBit(ADXL345_REG_TAP_AXES, 1);
}
void ADXL345::setTapDetectionZ(bool state)
{
writeRegisterBit(ADXL345_REG_TAP_AXES, 0, state);
}
bool ADXL345::getTapDetectionZ(void)
{
return readRegisterBit(ADXL345_REG_TAP_AXES, 0);
}
void ADXL345::setTapDetectionXYZ(bool state)
{
uint8_t value;
value = readRegister8(ADXL345_REG_TAP_AXES);
if (state)
{
value |= 0b00000111;
} else
{
value &= 0b11111000;
}
writeRegister8(ADXL345_REG_TAP_AXES, value);
}
void ADXL345::useInterrupt(adxl345_int_t interrupt)
{
if (interrupt == 0)
{
writeRegister8(ADXL345_REG_INT_MAP, 0x00);
} else
{
writeRegister8(ADXL345_REG_INT_MAP, 0xFF);
}
writeRegister8(ADXL345_REG_INT_ENABLE, 0xFF);
}
Activites ADXL345::readActivites(void)
{
uint8_t data = readRegister8(ADXL345_REG_INT_SOURCE);
a.isOverrun = ((data >> ADXL345_OVERRUN) & 1);
a.isWatermark = ((data >> ADXL345_WATERMARK) & 1);
a.isFreeFall = ((data >> ADXL345_FREE_FALL) & 1);
a.isInactivity = ((data >> ADXL345_INACTIVITY) & 1);
a.isActivity = ((data >> ADXL345_ACTIVITY) & 1);
a.isDoubleTap = ((data >> ADXL345_DOUBLE_TAP) & 1);
a.isTap = ((data >> ADXL345_SINGLE_TAP) & 1);
a.isDataReady = ((data >> ADXL345_DATA_READY) & 1);
data = readRegister8(ADXL345_REG_ACT_TAP_STATUS);
a.isActivityOnX = ((data >> 6) & 1);
a.isActivityOnY = ((data >> 5) & 1);
a.isActivityOnZ = ((data >> 4) & 1);
a.isTapOnX = ((data >> 2) & 1);
a.isTapOnY = ((data >> 1) & 1);
a.isTapOnZ = ((data >> 0) & 1);
return a;
}
// Write byte to register
void ADXL345::writeRegister8(uint8_t reg, uint8_t value)
{
Wire.beginTransmission(ADXL345_ADDRESS);
#if ARDUINO >= 100
Wire.write(reg);
Wire.write(value);
#else
Wire.send(reg);
Wire.send(value);
#endif
Wire.endTransmission();
}
// Read byte to register
uint8_t ADXL345::fastRegister8(uint8_t reg)
{
uint8_t value;
Wire.beginTransmission(ADXL345_ADDRESS);
#if ARDUINO >= 100
Wire.write(reg);
#else
Wire.send(reg);
#endif
Wire.endTransmission();
Wire.requestFrom(ADXL345_ADDRESS, 1);
#if ARDUINO >= 100
value = Wire.read();
#else
value = Wire.receive();
#endif
Wire.endTransmission();
return value;
}
// Read byte from register
uint8_t ADXL345::readRegister8(uint8_t reg)
{
uint8_t value;
Wire.beginTransmission(ADXL345_ADDRESS);
#if ARDUINO >= 100
Wire.write(reg);
#else
Wire.send(reg);
#endif
Wire.endTransmission();
Wire.beginTransmission(ADXL345_ADDRESS);
Wire.requestFrom(ADXL345_ADDRESS, 1);
while(!Wire.available()) {};
#if ARDUINO >= 100
value = Wire.read();
#else
value = Wire.receive();
#endif
Wire.endTransmission();
return value;
}
// Read word from register
int16_t ADXL345::readRegister16(uint8_t reg)
{
int16_t value;
Wire.beginTransmission(ADXL345_ADDRESS);
#if ARDUINO >= 100
Wire.write(reg);
#else
Wire.send(reg);
#endif
Wire.endTransmission();
Wire.beginTransmission(ADXL345_ADDRESS);
Wire.requestFrom(ADXL345_ADDRESS, 2);
while(!Wire.available()) {};
#if ARDUINO >= 100
uint8_t vla = Wire.read();
uint8_t vha = Wire.read();
#else
uint8_t vla = Wire.receive();
uint8_t vha = Wire.receive();
#endif
Wire.endTransmission();
value = vha << 8 | vla;
return value;
}
void ADXL345::writeRegisterBit(uint8_t reg, uint8_t pos, bool state)
{
uint8_t value;
value = readRegister8(reg);
if (state)
{
value |= (1 << pos);
} else
{
value &= ~(1 << pos);
}
writeRegister8(reg, value);
}
bool ADXL345::readRegisterBit(uint8_t reg, uint8_t pos)
{
uint8_t value;
value = readRegister8(reg);
return ((value >> pos) & 1);
}