-
Notifications
You must be signed in to change notification settings - Fork 10
/
ADXL345.cpp
1700 lines (1534 loc) · 58.5 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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2014 Cagdas Caglak cagdascaglak@gmail.com http://expcodes.blogspot.com.tr/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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/>.
*/
#include "ADXL345.h"
using namespace cacaosd_i2cport;
namespace cacaosd_adxl345 {
ADXL345::ADXL345(I2cPort *i2c) {
this->i2c = i2c;
}
ADXL345::~ADXL345() {
delete i2c;
}
void ADXL345::setDeviceAddress(uint8_t device_address) {
this->device_address = device_address;
}
uint8_t ADXL345::getDeviceAddress() {
return this->device_address;
}
/** Power on and prepare for general usage.
* This will activate the accelerometer, so be sure to adjust the power settings
* after you call this method if you want it to enter standby mode, or another
* less demanding mode of operation.
*/
void ADXL345::initialize() {
i2c->writeByte(ADXL345_RA_POWER_CTL, 0);
setAutoSleepEnabled(true);
setMeasureEnabled(true);
}
/** Get Device ID.
* The DEVID register holds a fixed device ID code of 0xE5 (345 octal).
* @return Device ID (should be 0xE5, 229 dec, 345 oct)
* @see ADXL345_RA_DEVID
*/
uint8_t ADXL345::getDeviceID() {
return i2c->readByte(ADXL345_RA_DEVID);
}
/** Get tap threshold.
* The THRESH_TAP register is eight bits and holds the threshold value for tap
* interrupts. The data format is unsigned, therefore, the magnitude of the tap
* event is compared with the value in THRESH_TAP for normal tap detection. The
* scale factor is 62.5 mg/LSB (that is, 0xFF = 16 g). A value of 0 may result
* in undesirable behavior if single tap/double tap interrupts are enabled.
* @return Tap threshold (scaled at 62.5 mg/LSB)
* @see ADXL345_RA_THRESH_TAP
*/
uint8_t ADXL345::getTapThreshold() {
return i2c->readByte(ADXL345_RA_THRESH_TAP);
}
/** Set tap threshold.
* @param threshold Tap magnitude threshold (scaled at 62.5 mg/LSB)
* @see ADXL345_RA_THRESH_TAP
* @see getTapThreshold()
*/
void ADXL345::setTapThreshold(uint8_t threshold) {
i2c->writeByte(ADXL345_RA_THRESH_TAP, threshold);
}
/** Get axis offsets.
* @param offsets array of offset
* @see getOffset()
* @see ADXL345_RA_OFSX
* @see ADXL345_RA_OFSY
* @see ADXL345_RA_OFSZ
*/
void ADXL345::getOffset(int8_t *offsets) {
offsets[0] = this->getOffsetX();
offsets[1] = this->getOffsetY();
offsets[2] = this->getOffsetZ();
}
/** Set axis offsets.
* @param x X axis offset value
* @param y Y axis offset value
* @param z Z axis offset value
* @see getOffset()
* @see ADXL345_RA_OFSX
* @see ADXL345_RA_OFSY
* @see ADXL345_RA_OFSZ
*/
void ADXL345::setOffset(int8_t x, int8_t y, int8_t z) {
i2c->writeByte(ADXL345_RA_OFSX, x);
i2c->writeByte(ADXL345_RA_OFSY, y);
i2c->writeByte(ADXL345_RA_OFSZ, z);
}
/** Get X axis offset.
* @return X axis offset value
* @see getOffset()
* @see ADXL345_RA_OFSX
*/
int8_t ADXL345::getOffsetX() {
return i2c->readByte(ADXL345_RA_OFSX);
}
/** Set X axis offset.
* @param x X axis offset value
* @see getOffset()
* @see ADXL345_RA_OFSX
*/
void ADXL345::setOffsetX(int8_t x) {
i2c->writeByte(ADXL345_RA_OFSX, x);
}
/** Get Y axis offset.
* @return Y axis offset value
* @see ADXL345_RA_OFSY
*/
int8_t ADXL345::getOffsetY() {
return i2c->readByte(ADXL345_RA_OFSY);
}
/** Set Y axis offset.
* @param y Y axis offset value
* @see ADXL345_RA_OFSY
*/
void ADXL345::setOffsetY(int8_t y) {
i2c->writeByte(ADXL345_RA_OFSY, y);
}
/** Get Z axis offset.
* @return Z axis offset value
* @see ADXL345_RA_OFSZ
*/
int8_t ADXL345::getOffsetZ() {
return i2c->readByte(ADXL345_RA_OFSZ);
}
/** Set Z axis offset.
* @param z Z axis offset value
* @see ADXL345_RA_OFSZ
*/
void ADXL345::setOffsetZ(int8_t z) {
i2c->writeByte(ADXL345_RA_OFSZ, z);
}
/** Get tap duration.
* The DUR register is eight bits and contains an unsigned time value
* representing the maximum time that an event must be above the THRESH_TAP
* threshold to qualify as a tap event. The scale factor is 625 us/LSB. A value
* of 0 disables the single tap/ double tap functions.
* @return Tap duration (scaled at 625 us/LSB)
* @see ADXL345_RA_DUR
*/
uint8_t ADXL345::getTapDuration() {
return i2c->readByte(ADXL345_RA_DUR);
}
/** Set tap duration.
* @param duration Tap duration (scaled at 625 us/LSB)
* @see getTapDuration()
* @see ADXL345_RA_DUR
*/
void ADXL345::setTapDuration(uint8_t duration) {
i2c->writeByte(ADXL345_RA_DUR, duration);
}
/** Get tap duration.
* The latent register is eight bits and contains an unsigned time value
* representing the wait time from the detection of a tap event to the start of
* the time window (defined by the window register) during which a possible
* second tap event can be detected. The scale factor is 1.25 ms/LSB. A value of
* 0 disables the double tap function.
* @return Tap latency (scaled at 1.25 ms/LSB)
* @see ADXL345_RA_LATENT
*/
uint8_t ADXL345::getDoubleTapLatency() {
return i2c->readByte(ADXL345_RA_LATENT);
}
/** Set tap duration.
* @param latency Tap latency (scaled at 1.25 ms/LSB)
* @see getDoubleTapLatency()
* @see ADXL345_RA_LATENT
*/
void ADXL345::setDoubleTapLatency(uint8_t latency) {
i2c->writeByte(ADXL345_RA_LATENT, latency);
}
/** Get double tap window.
* The window register is eight bits and contains an unsigned time value
* representing the amount of time after the expiration of the latency time
* (determined by the latent register) during which a second valid tap can
* begin. The scale factor is 1.25 ms/LSB. A value of 0 disables the double tap
* function.
* @return Double tap window (scaled at 1.25 ms/LSB)
* @see ADXL345_RA_WINDOW
*/
uint8_t ADXL345::getDoubleTapWindow() {
return i2c->readByte(ADXL345_RA_WINDOW);
}
/** Set double tap window.
* @param window Double tap window (scaled at 1.25 ms/LSB)
* @see getDoubleTapWindow()
* @see ADXL345_RA_WINDOW
*/
void ADXL345::setDoubleTapWindow(uint8_t window) {
i2c->writeByte(ADXL345_RA_WINDOW, window);
}
/** Get activity threshold.
* The THRESH_ACT register is eight bits and holds the threshold value for
* detecting activity. The data format is unsigned, so the magnitude of the
* activity event is compared with the value in the THRESH_ACT register. The
* scale factor is 62.5 mg/LSB. A value of 0 may result in undesirable behavior
* if the activity interrupt is enabled.
* @return Activity threshold (scaled at 62.5 mg/LSB)
* @see ADXL345_RA_THRESH_ACT
*/
uint8_t ADXL345::getActivityThreshold() {
return i2c->readByte(ADXL345_RA_THRESH_ACT);
}
/** Set activity threshold.
* @param threshold Activity threshold (scaled at 62.5 mg/LSB)
* @see getActivityThreshold()
* @see ADXL345_RA_THRESH_ACT
*/
void ADXL345::setActivityThreshold(uint8_t threshold) {
i2c->writeByte(ADXL345_RA_THRESH_ACT, threshold);
}
/** Get inactivity threshold.
* The THRESH_INACT register is eight bits and holds the threshold value for
* detecting inactivity. The data format is unsigned, so the magnitude of the
* inactivity event is compared with the value in the THRESH_INACT register. The
* scale factor is 62.5 mg/LSB. A value of 0 may result in undesirable behavior
* if the inactivity interrupt is enabled.
* @return Inactivity threshold (scaled at 62.5 mg/LSB)
* @see ADXL345_RA_THRESH_INACT
*/
uint8_t ADXL345::getInactivityThreshold() {
return i2c->readByte(ADXL345_RA_THRESH_INACT);
}
/** Set inactivity threshold.
* @param threshold Inctivity threshold (scaled at 62.5 mg/LSB)
* @see getInctivityThreshold()
* @see ADXL345_RA_THRESH_INACT
*/
void ADXL345::setInactivityThreshold(uint8_t threshold) {
i2c->writeByte(ADXL345_RA_THRESH_INACT, threshold);
}
/** Set inactivity time.
* The TIME_INACT register is eight bits and contains an unsigned time value
* representing the amount of time that acceleration must be less than the value
* in the THRESH_INACT register for inactivity to be declared. The scale factor
* is 1 sec/LSB. Unlike the other interrupt functions, which use unfiltered data
* (see the Threshold sectionof the datasheet), the inactivity function uses
* filtered output data. At least one output sample must be generated for the
* inactivity interrupt to be triggered. This results in the function appearing
* unresponsive if the TIME_INACT register is set to a value less than the time
* constant of the output data rate. A value of 0 results in an interrupt when
* the output data is less than the value in the THRESH_INACT register.
* @return Inactivity time (scaled at 1 sec/LSB)
* @see ADXL345_RA_TIME_INACT
*/
uint8_t ADXL345::getInactivityTime() {
return i2c->readByte(ADXL345_RA_TIME_INACT);
}
/** Set inactivity time.
* @param time Inactivity time (scaled at 1 sec/LSB)
* @see getInctivityTime()
* @see ADXL345_RA_TIME_INACT
*/
void ADXL345::setInactivityTime(uint8_t time) {
i2c->writeByte(ADXL345_RA_TIME_INACT, time);
}
/** Get activity AC/DC coupling.
* A setting of 0 selects dc-coupled operation, and a setting of 1 enables
* ac-coupled operation. In dc-coupled operation, the current acceleration
* magnitude is compared directly with THRESH_ACT and THRESH_INACT to determine
* whether activity or inactivity is detected.
*
* In ac-coupled operation for activity detection, the acceleration value at the
* start of activity detection is taken as a reference value. New samples of
* acceleration are then compared to this reference value, and if the magnitude
* of the difference exceeds the THRESH_ACT value, the device triggers an
* activity interrupt.
*
* Similarly, in ac-coupled operation for inactivity detection, a reference
* value is used for comparison and is updated whenever the device exceeds the
* inactivity threshold. After the reference value is selected, the device
* compares the magnitude of the difference between the reference value and the
* current acceleration with THRESH_INACT. If the difference is less than the
* value in THRESH_INACT for the time in TIME_INACT, the device is considered
* inactive and the inactivity interrupt is triggered.
*
* @return Activity coupling (0 = DC, 1 = AC)
* @see ADXL345_RA_ACT_INACT_CTL
* @see ADXL345_AIC_ACT_AC_BIT
*/
bool ADXL345::getActivityAC() {
return i2c->readBit(ADXL345_RA_ACT_INACT_CTL, ADXL345_AIC_ACT_AC_BIT);
}
/** Set activity AC/DC coupling.
* @param enabled Activity AC/DC coupling (TRUE for AC, FALSE for DC)
* @see getActivityAC()
* @see ADXL345_RA_ACT_INACT_CTL
* @see ADXL345_AIC_ACT_AC_BIT
*/
void ADXL345::setActivityAC(bool enabled) {
i2c->writeBit(ADXL345_RA_ACT_INACT_CTL, enabled,
ADXL345_AIC_ACT_AC_BIT);
}
/** Get X axis activity monitoring inclusion.
* For all "get[In]Activity*Enabled()" methods: a setting of 1 enables x-, y-,
* or z-axis participation in detecting activity or inactivity. A setting of 0
* excludes the selected axis from participation. If all axes are excluded, the
* function is disabled. For activity detection, all participating axes are
* logically OR�ed, causing the activity function to trigger when any of the
* participating axes exceeds the threshold. For inactivity detection, all
* participating axes are logically AND�ed, causing the inactivity function to
* trigger only if all participating axes are below the threshold for the
* specified time.
* @return X axis activity monitoring enabled value
* @see getActivityAC()
* @see ADXL345_RA_ACT_INACT_CTL
* @see ADXL345_AIC_ACT_X_BIT
*/
bool ADXL345::getActivityXEnabled() {
return i2c->readBit(ADXL345_RA_ACT_INACT_CTL, ADXL345_AIC_ACT_X_BIT);
}
/** Set X axis activity monitoring inclusion.
* @param enabled X axis activity monitoring inclusion value
* @see getActivityAC()
* @see getActivityXEnabled()
* @see ADXL345_RA_ACT_INACT_CTL
* @see ADXL345_AIC_ACT_X_BIT
*/
void ADXL345::setActivityXEnabled(bool enabled) {
i2c->writeBit(ADXL345_RA_ACT_INACT_CTL, enabled,
ADXL345_AIC_ACT_X_BIT);
}
/** Get Y axis activity monitoring.
* @return Y axis activity monitoring enabled value
* @see getActivityAC()
* @see getActivityXEnabled()
* @see ADXL345_RA_ACT_INACT_CTL
* @see ADXL345_AIC_ACT_Y_BIT
*/
bool ADXL345::getActivityYEnabled() {
return i2c->readBit(ADXL345_RA_ACT_INACT_CTL, ADXL345_AIC_ACT_Y_BIT);
}
/** Set Y axis activity monitoring inclusion.
* @param enabled Y axis activity monitoring inclusion value
* @see getActivityAC()
* @see getActivityXEnabled()
* @see ADXL345_RA_ACT_INACT_CTL
* @see ADXL345_AIC_ACT_Y_BIT
*/
void ADXL345::setActivityYEnabled(bool enabled) {
i2c->writeBit(ADXL345_RA_ACT_INACT_CTL, enabled,
ADXL345_AIC_ACT_Y_BIT);
}
/** Get Z axis activity monitoring.
* @return Z axis activity monitoring enabled value
* @see getActivityAC()
* @see getActivityXEnabled()
* @see ADXL345_RA_ACT_INACT_CTL
* @see ADXL345_AIC_ACT_Z_BIT
*/
bool ADXL345::getActivityZEnabled() {
return i2c->readBit(ADXL345_RA_ACT_INACT_CTL, ADXL345_AIC_ACT_Z_BIT);
}
/** Set Z axis activity monitoring inclusion.
* @param enabled Z axis activity monitoring inclusion value
* @see getActivityAC()
* @see getActivityXEnabled()
* @see ADXL345_RA_ACT_INACT_CTL
* @see ADXL345_AIC_ACT_Z_BIT
*/
void ADXL345::setActivityZEnabled(bool enabled) {
i2c->writeBit(ADXL345_RA_ACT_INACT_CTL, enabled,
ADXL345_AIC_ACT_Z_BIT);
}
/** Get inactivity AC/DC coupling.
* @return Inctivity coupling (0 = DC, 1 = AC)
* @see getActivityAC()
* @see ADXL345_RA_ACT_INACT_CTL
* @see ADXL345_AIC_INACT_AC_BIT
*/
bool ADXL345::getInactivityAC() {
return i2c->readBit(ADXL345_RA_ACT_INACT_CTL, ADXL345_AIC_INACT_AC_BIT);
}
/** Set inctivity AC/DC coupling.
* @param enabled Inactivity AC/DC coupling (TRUE for AC, FALSE for DC)
* @see getActivityAC()
* @see ADXL345_RA_ACT_INACT_CTL
* @see ADXL345_AIC_INACT_AC_BIT
*/
void ADXL345::setInactivityAC(bool enabled) {
i2c->writeBit(ADXL345_RA_ACT_INACT_CTL, enabled,
ADXL345_AIC_INACT_AC_BIT);
}
/** Get X axis inactivity monitoring.
* @return Y axis inactivity monitoring enabled value
* @see getActivityAC()
* @see getActivityXEnabled()
* @see ADXL345_RA_ACT_INACT_CTL
* @see ADXL345_AIC_INACT_X_BIT
*/
bool ADXL345::getInactivityXEnabled() {
return i2c->readBit(ADXL345_RA_ACT_INACT_CTL, ADXL345_AIC_INACT_X_BIT);
}
/** Set X axis activity monitoring inclusion.
* @param enabled X axis inactivity monitoring inclusion value
* @see getActivityAC()
* @see getActivityXEnabled()
* @see ADXL345_RA_ACT_INACT_CTL
* @see ADXL345_AIC_INACT_X_BIT
*/
void ADXL345::setInactivityXEnabled(bool enabled) {
i2c->writeBit(ADXL345_RA_ACT_INACT_CTL, enabled,
ADXL345_AIC_INACT_X_BIT);
}
/** Get Y axis inactivity monitoring.
* @return Y axis inactivity monitoring enabled value
* @see getActivityAC()
* @see getActivityXEnabled()
* @see ADXL345_RA_ACT_INACT_CTL
* @see ADXL345_AIC_INACT_Y_BIT
*/
bool ADXL345::getInactivityYEnabled() {
return i2c->readBit(ADXL345_RA_ACT_INACT_CTL, ADXL345_AIC_INACT_Y_BIT);
}
/** Set Y axis inactivity monitoring inclusion.
* @param enabled Y axis inactivity monitoring inclusion value
* @see getActivityAC()
* @see getActivityXEnabled()
* @see ADXL345_RA_ACT_INACT_CTL
* @see ADXL345_AIC_INACT_Y_BIT
*/
void ADXL345::setInactivityYEnabled(bool enabled) {
i2c->writeBit(ADXL345_RA_ACT_INACT_CTL, enabled,
ADXL345_AIC_INACT_Y_BIT);
}
/** Get Z axis inactivity monitoring.
* @return Z axis inactivity monitoring enabled value
* @see getActivityAC()
* @see getActivityXEnabled()
* @see ADXL345_RA_ACT_INACT_CTL
* @see ADXL345_AIC_INACT_Z_BIT
*/
bool ADXL345::getInactivityZEnabled() {
return i2c->readBit(ADXL345_RA_ACT_INACT_CTL, ADXL345_AIC_INACT_Z_BIT);
}
/** Set Z axis inactivity monitoring inclusion.
* @param enabled Z axis activity monitoring inclusion value
* @see getActivityAC()
* @see getActivityXEnabled()
* @see ADXL345_RA_ACT_INACT_CTL
* @see ADXL345_AIC_INACT_Z_BIT
*/
void ADXL345::setInactivityZEnabled(bool enabled) {
i2c->writeBit(ADXL345_RA_ACT_INACT_CTL, enabled,
ADXL345_AIC_INACT_Z_BIT);
}
// THRESH_FF register
/** Get freefall threshold value.
* The THRESH_FF register is eight bits and holds the threshold value, in
* unsigned format, for free-fall detection. The acceleration on all axes is
* compared with the value in THRESH_FF to determine if a free-fall event
* occurred. The scale factor is 62.5 mg/LSB. Note that a value of 0 mg may
* result in undesirable behavior if the free-fall interrupt is enabled. Values
* between 300 mg and 600 mg (0x05 to 0x09) are recommended.
* @return Freefall threshold value (scaled at 62.5 mg/LSB)
* @see ADXL345_RA_THRESH_FF
*/
uint8_t ADXL345::getFreefallThreshold() {
return i2c->readByte(ADXL345_AIC_INACT_Z_BIT);
}
/** Set freefall threshold value.
* @param threshold Freefall threshold value (scaled at 62.5 mg/LSB)
* @see getFreefallThreshold()
* @see ADXL345_RA_THRESH_FF
*/
void ADXL345::setFreefallThreshold(uint8_t threshold) {
i2c->writeByte(ADXL345_RA_THRESH_FF, threshold);
}
// TIME_FF register
/** Get freefall time value.
* The TIME_FF register is eight bits and stores an unsigned time value
* representing the minimum time that the value of all axes must be less than
* THRESH_FF to generate a free-fall interrupt. The scale factor is 5 ms/LSB. A
* value of 0 may result in undesirable behavior if the free-fall interrupt is
* enabled. Values between 100 ms and 350 ms (0x14 to 0x46) are recommended.
* @return Freefall time value (scaled at 5 ms/LSB)
* @see getFreefallThreshold()
* @see ADXL345_RA_TIME_FF
*/
uint8_t ADXL345::getFreefallTime() {
return i2c->readByte(ADXL345_RA_TIME_FF);
}
/** Set freefall time value.
* @param threshold Freefall time value (scaled at 5 ms/LSB)
* @see getFreefallTime()
* @see ADXL345_RA_TIME_FF
*/
void ADXL345::setFreefallTime(uint8_t time) {
i2c->writeByte(ADXL345_RA_TIME_FF, time);
}
// TAP_AXES register
/** Get double-tap fast-movement suppression.
* Setting the suppress bit suppresses double tap detection if acceleration
* greater than the value in THRESH_TAP is present between taps. See the Tap
* Detection section in the datasheet for more details.
* @return Double-tap fast-movement suppression value
* @see getTapThreshold()
* @see ADXL345_RA_TAP_AXES
* @see ADXL345_TAPAXIS_SUP_BIT
*/
bool ADXL345::getTapAxisSuppress() {
return i2c->readBit(ADXL345_RA_TAP_AXES, ADXL345_TAPAXIS_SUP_BIT);
}
/** Set double-tap fast-movement suppression.
* @param enabled Double-tap fast-movement suppression value
* @see getTapAxisSuppress()
* @see ADXL345_RA_TAP_AXES
* @see ADXL345_TAPAXIS_SUP_BIT
*/
void ADXL345::setTapAxisSuppress(bool enabled) {
i2c->writeBit(ADXL345_RA_TAP_AXES, enabled, ADXL345_TAPAXIS_SUP_BIT);
}
/** Get double-tap fast-movement suppression.
* A setting of 1 in the TAP_X enable bit enables x-axis participation in tap
* detection. A setting of 0 excludes the selected axis from participation in
* tap detection.
* @return Double-tap fast-movement suppression value
* @see getTapThreshold()
* @see ADXL345_RA_TAP_AXES
* @see ADXL345_TAPAXIS_X_BIT
*/
bool ADXL345::getTapAxisXEnabled() {
return i2c->readBit(ADXL345_RA_TAP_AXES, ADXL345_TAPAXIS_X_BIT);
}
/** Set tap detection X axis inclusion.
* @param enabled X axis tap detection enabled value
* @see getTapAxisXEnabled()
* @see ADXL345_RA_TAP_AXES
* @see ADXL345_TAPAXIS_X_BIT
*/
void ADXL345::setTapAxisXEnabled(bool enabled) {
i2c->writeBit(ADXL345_RA_TAP_AXES, enabled, ADXL345_TAPAXIS_X_BIT);
}
/** Get tap detection Y axis inclusion.
* A setting of 1 in the TAP_Y enable bit enables y-axis participation in tap
* detection. A setting of 0 excludes the selected axis from participation in
* tap detection.
* @return Double-tap fast-movement suppression value
* @see getTapThreshold()
* @see ADXL345_RA_TAP_AXES
* @see ADXL345_TAPAXIS_Y_BIT
*/
bool ADXL345::getTapAxisYEnabled() {
return i2c->readBit(ADXL345_RA_TAP_AXES, ADXL345_TAPAXIS_Y_BIT);
}
/** Set tap detection Y axis inclusion.
* @param enabled Y axis tap detection enabled value
* @see getTapAxisYEnabled()
* @see ADXL345_RA_TAP_AXES
* @see ADXL345_TAPAXIS_Y_BIT
*/
void ADXL345::setTapAxisYEnabled(bool enabled) {
i2c->writeBit(ADXL345_RA_TAP_AXES, enabled, ADXL345_TAPAXIS_Y_BIT);
}
/** Get tap detection Z axis inclusion.
* A setting of 1 in the TAP_Z enable bit enables z-axis participation in tap
* detection. A setting of 0 excludes the selected axis from participation in
* tap detection.
* @return Double-tap fast-movement suppression value
* @see getTapThreshold()
* @see ADXL345_RA_TAP_AXES
* @see ADXL345_TAPAXIS_Z_BIT
*/
bool ADXL345::getTapAxisZEnabled() {
return i2c->readBit(ADXL345_RA_TAP_AXES, ADXL345_TAPAXIS_Z_BIT);
}
/** Set tap detection Z axis inclusion.
* @param enabled Z axis tap detection enabled value
* @see getTapAxisZEnabled()
* @see ADXL345_RA_TAP_AXES
* @see ADXL345_TAPAXIS_Z_BIT
*/
void ADXL345::setTapAxisZEnabled(bool enabled) {
i2c->writeBit(ADXL345_RA_TAP_AXES, enabled, ADXL345_TAPAXIS_Z_BIT);
}
// ACT_TAP_STATUS register
/** Get X axis activity source flag.
* These bits indicate the first axis involved in a tap or activity event. A
* setting of 1 corresponds to involvement in the event, and a setting of 0
* corresponds to no involvement. When new data is available, these bits are not
* cleared but are overwritten by the new data. The ACT_TAP_STATUS register
* should be read before clearing the interrupt. Disabling an axis from
* participation clears the corresponding source bit when the next activity or
* single tap/double tap event occurs.
* @return X axis activity source flag
* @see ADXL345_RA_ACT_TAP_STATUS
* @see ADXL345_TAPSTAT_ACTX_BIT
*/
bool ADXL345::getActivitySourceX() {
return i2c->readBit(ADXL345_RA_ACT_TAP_STATUS,
ADXL345_TAPSTAT_ACTX_BIT);
}
/** Get Y axis activity source flag.
* @return Y axis activity source flag
* @see getActivitySourceX()
* @see ADXL345_RA_ACT_TAP_STATUS
* @see ADXL345_TAPSTAT_ACTY_BIT
*/
bool ADXL345::getActivitySourceY() {
return i2c->readBit(ADXL345_RA_ACT_TAP_STATUS,
ADXL345_TAPSTAT_ACTY_BIT);
}
/** Get Z axis activity source flag.
* @return Z axis activity source flag
* @see getActivitySourceX()
* @see ADXL345_RA_ACT_TAP_STATUS
* @see ADXL345_TAPSTAT_ACTZ_BIT
*/
bool ADXL345::getActivitySourceZ() {
return i2c->readBit(ADXL345_RA_ACT_TAP_STATUS,
ADXL345_TAPSTAT_ACTZ_BIT);
}
/** Get sleep mode flag.
* A setting of 1 in the asleep bit indicates that the part is asleep, and a
* setting of 0 indicates that the part is not asleep. This bit toggles only if
* the device is configured for auto sleep. See the AUTO_SLEEP Bit section of
* the datasheet for more information on autosleep mode.
* @return Sleep mode enabled flag
* @see ADXL345_RA_ACT_TAP_STATUS
* @see ADXL345_TAPSTAT_ASLEEP_BIT
*/
bool ADXL345::getAsleep() {
return i2c->readBit(ADXL345_RA_ACT_TAP_STATUS,
ADXL345_TAPSTAT_ASLEEP_BIT);
}
/** Get X axis tap source flag.
* @return X axis tap source flag
* @see getActivitySourceX()
* @see ADXL345_RA_ACT_TAP_STATUS
* @see ADXL345_TAPSTAT_TAPX_BIT
*/
bool ADXL345::getTapSourceX() {
return i2c->readBit(ADXL345_RA_ACT_TAP_STATUS,
ADXL345_TAPSTAT_TAPX_BIT);
}
/** Get Y axis tap source flag.
* @return Y axis tap source flag
* @see getActivitySourceX()
* @see ADXL345_RA_ACT_TAP_STATUS
* @see ADXL345_TAPSTAT_TAPY_BIT
*/
bool ADXL345::getTapSourceY() {
return i2c->readBit(ADXL345_RA_ACT_TAP_STATUS,
ADXL345_TAPSTAT_TAPY_BIT);
}
/** Get Z axis tap source flag.
* @return Z axis tap source flag
* @see getActivitySourceX()
* @see ADXL345_RA_ACT_TAP_STATUS
* @see ADXL345_TAPSTAT_TAPZ_BIT
*/
bool ADXL345::getTapSourceZ() {
return i2c->readBit(ADXL345_RA_ACT_TAP_STATUS,
ADXL345_TAPSTAT_TAPZ_BIT);
}
// BW_RATE register
/** Get low power enabled status.
* A setting of 0 in the LOW_POWER bit selects normal operation, and a setting
* of 1 selects reduced power operation, which has somewhat higher noise (see
* the Power Modes section of the datasheet for details).
* @return Low power enabled status
* @see ADXL345_RA_BW_RATE
* @see ADXL345_BW_LOWPOWER_BIT
*/
bool ADXL345::getLowPowerEnabled() {
return i2c->readBit(ADXL345_RA_BW_RATE, ADXL345_BW_LOWPOWER_BIT);
}
/** Set low power enabled status.
* @see getLowPowerEnabled()
* @param enabled Low power enable setting
* @see ADXL345_RA_BW_RATE
* @see ADXL345_BW_LOWPOWER_BIT
*/
void ADXL345::setLowPowerEnabled(bool enabled) {
i2c->writeBit(ADXL345_RA_BW_RATE, enabled, ADXL345_BW_LOWPOWER_BIT);
}
/** Get measurement data rate.
* These bits select the device bandwidth and output data rate (see Table 7 and
* Table 8 in the datasheet for details). The default value is 0x0A, which
* translates to a 100 Hz output data rate. An output data rate should be
* selected that is appropriate for the communication protocol and frequency
* selected. Selecting too high of an output data rate with a low communication
* speed results in samples being discarded.
* @return Data rate (0x0 - 0xF)
* @see ADXL345_RA_BW_RATE
* @see ADXL345_BW_RATE_BIT
* @see ADXL345_BW_RATE_LENGTH
*/
uint8_t ADXL345::getRate() {
return i2c->readMoreBits(ADXL345_RA_BW_RATE, ADXL345_BW_RATE_LENGTH,
ADXL345_BW_LOWPOWER_BIT);
}
/** Set measurement data rate.
* 0x7 = 12.5Hz
* 0x8 = 25Hz, increasing or decreasing by factors of 2, so:
* 0x9 = 50Hz
* 0xA = 100Hz
* @param rate New data rate (0x0 - 0xF)
* @see ADXL345_RATE_100
* @see ADXL345_RA_BW_RATE
* @see ADXL345_BW_RATE_BIT
* @see ADXL345_BW_RATE_LENGTH
*/
void ADXL345::setRate(uint8_t rate) {
i2c->writeMoreBits(ADXL345_RA_BW_RATE, rate, ADXL345_BW_RATE_LENGTH,
ADXL345_BW_LOWPOWER_BIT);
}
// POWER_CTL register
/** Get activity/inactivity serial linkage status.
* A setting of 1 in the link bit with both the activity and inactivity
* functions enabled delays the start of the activity function until
* inactivity is detected. After activity is detected, inactivity detection
* begins, preventing the detection of activity. This bit serially links the
* activity and inactivity functions. When this bit is set to 0, the inactivity
* and activity functions are concurrent. Additional information can be found
* in the Link Mode section of the datasheet.
*
* When clearing the link bit, it is recommended that the part be placed into
* standby mode and then set back to measurement mode with a subsequent write.
* This is done to ensure that the device is properly biased if sleep mode is
* manually disabled; otherwise, the first few samples of data after the link
* bit is cleared may have additional noise, especially if the device was asleep
* when the bit was cleared.
*
* @return Link status
* @see ADXL345_RA_POWER_CTL
* @see ADXL345_PCTL_LINK_BIT
*/
bool ADXL345::getLinkEnabled() {
return i2c->readBit(ADXL345_RA_POWER_CTL, ADXL345_PCTL_LINK_BIT);
}
/** Set activity/inactivity serial linkage status.
* @param enabled New link status
* @see ADXL345_RA_POWER_CTL
* @see ADXL345_PCTL_LINK_BIT
*/
void ADXL345::setLinkEnabled(bool enabled) {
i2c->writeBit(ADXL345_RA_POWER_CTL, enabled, ADXL345_PCTL_LINK_BIT);
}
/** Get auto-sleep enabled status.
* If the link bit is set, a setting of 1 in the AUTO_SLEEP bit enables the
* auto-sleep functionality. In this mode, the ADXL345 auto-matically switches
* to sleep mode if the inactivity function is enabled and inactivity is
* detected (that is, when acceleration is below the THRESH_INACT value for at
* least the time indicated by TIME_INACT). If activity is also enabled, the
* ADXL345 automatically wakes up from sleep after detecting activity and
* returns to operation at the output data rate set in the BW_RATE register. A
* setting of 0 in the AUTO_SLEEP bit disables automatic switching to sleep
* mode. See the description of the Sleep Bit in this section of the datasheet
* for more information on sleep mode.
*
* If the link bit is not set, the AUTO_SLEEP feature is disabled and setting
* the AUTO_SLEEP bit does not have an impact on device operation. Refer to the
* Link Bit section or the Link Mode section for more information on utilization
* of the link feature.
*
* When clearing the AUTO_SLEEP bit, it is recommended that the part be placed
* into standby mode and then set back to measure-ment mode with a subsequent
* write. This is done to ensure that the device is properly biased if sleep
* mode is manually disabled; otherwise, the first few samples of data after the
* AUTO_SLEEP bit is cleared may have additional noise, especially if the device
* was asleep when the bit was cleared.
*
* @return Auto-sleep enabled status
* @see getActivityThreshold()
* @see getInactivityThreshold()
* @see getInactivityTime()
* @see ADXL345_RA_POWER_CTL
* @see ADXL345_PCTL_AUTOSLEEP_BIT
*/
bool ADXL345::getAutoSleepEnabled() {
return i2c->readBit(ADXL345_RA_POWER_CTL, ADXL345_PCTL_AUTOSLEEP_BIT);
}
/** Set auto-sleep enabled status.
* @param enabled New auto-sleep status
* @see getAutoSleepEnabled()
* @see ADXL345_RA_POWER_CTL
* @see ADXL345_PCTL_AUTOSLEEP_BIT
*/
void ADXL345::setAutoSleepEnabled(bool enabled) {
i2c->writeBit(ADXL345_RA_POWER_CTL, enabled,
ADXL345_PCTL_AUTOSLEEP_BIT);
}
/** Get measurement enabled status.
* A setting of 0 in the measure bit places the part into standby mode, and a
* setting of 1 places the part into measurement mode. The ADXL345 powers up in
* standby mode with minimum power consumption.
* @return Measurement enabled status
* @see ADXL345_RA_POWER_CTL
* @see ADXL345_PCTL_MEASURE_BIT
*/
bool ADXL345::getMeasureEnabled() {
return i2c->readBit(ADXL345_RA_POWER_CTL, ADXL345_PCTL_MEASURE_BIT);
}
/** Set measurement enabled status.
* @param enabled Measurement enabled status
* @see getMeasureEnabled()
* @see ADXL345_RA_POWER_CTL
* @see ADXL345_PCTL_MEASURE_BIT
*/
void ADXL345::setMeasureEnabled(bool enabled) {
i2c->writeBit(ADXL345_RA_POWER_CTL, enabled, ADXL345_PCTL_MEASURE_BIT);
}
/** Get sleep mode enabled status.
* A setting of 0 in the sleep bit puts the part into the normal mode of
* operation, and a setting of 1 places the part into sleep mode. Sleep mode
* suppresses DATA_READY, stops transmission of data to FIFO, and switches the
* sampling rate to one specified by the wakeup bits. In sleep mode, only the
* activity function can be used. When the DATA_READY interrupt is suppressed,
* the output data registers (Register 0x32 to Register 0x37) are still updated
* at the sampling rate set by the wakeup bits (D1:D0).
*
* When clearing the sleep bit, it is recommended that the part be placed into
* standby mode and then set back to measurement mode with a subsequent write.
* This is done to ensure that the device is properly biased if sleep mode is
* manually disabled; otherwise, the first few samples of data after the sleep
* bit is cleared may have additional noise, especially if the device was asleep
* when the bit was cleared.
*
* @return Sleep enabled status
* @see ADXL345_RA_POWER_CTL
* @see ADXL345_PCTL_SLEEP_BIT
*/
bool ADXL345::getSleepEnabled() {
return i2c->readBit(ADXL345_RA_POWER_CTL, ADXL345_PCTL_SLEEP_BIT);
}
/** Set sleep mode enabled status.
* @param Sleep mode enabled status
* @see getSleepEnabled()
* @see ADXL345_RA_POWER_CTL
* @see ADXL345_PCTL_SLEEP_BIT
*/
void ADXL345::setSleepEnabled(bool enabled) {
i2c->writeBit(ADXL345_RA_POWER_CTL, enabled, ADXL345_PCTL_SLEEP_BIT);
}
/** Get wakeup frequency.
* These bits control the frequency of readings in sleep mode as described in
* Table 20 in the datasheet. (That is, 0 = 8Hz, 1 = 4Hz, 2 = 2Hz, 3 = 1Hz)
* @return Wakeup frequency (0x0 - 0x3, indicating 8/4/2/1Hz respectively)
* @see ADXL345_RA_POWER_CTL
* @see ADXL345_PCTL_SLEEP_BIT
*/
uint8_t ADXL345::getWakeupFrequency() {
return i2c->readMoreBits(ADXL345_RA_POWER_CTL,
ADXL345_PCTL_WAKEUP_LENGTH, ADXL345_PCTL_WAKEUP_BIT);
}
/** Set wakeup frequency.
* @param frequency Wakeup frequency (0x0 - 0x3, indicating 8/4/2/1Hz respectively)
* @see getWakeupFrequency()
* @see ADXL345_RA_POWER_CTL
* @see ADXL345_PCTL_SLEEP_BIT
*/
void ADXL345::setWakeupFrequency(uint8_t frequency) {
i2c->writeMoreBits(ADXL345_RA_POWER_CTL, frequency,
ADXL345_PCTL_WAKEUP_LENGTH, ADXL345_PCTL_WAKEUP_BIT);
}
// INT_ENABLE register
/** Get DATA_READY interrupt enabled status.
* Setting bits in this register to a value of 1 enables their respective
* functions to generate interrupts, whereas a value of 0 prevents the functions
* from generating interrupts. The DATA_READY, watermark, and overrun bits
* enable only the interrupt output; the functions are always enabled. It is
* recommended that interrupts be configured before enabling their outputs.
* @return DATA_READY interrupt enabled status.
* @see ADXL345_RA_INT_ENABLE
* @see ADXL345_INT_DATA_READY_BIT
*/
bool ADXL345::getIntDataReadyEnabled() {
return i2c->readBit(ADXL345_RA_INT_ENABLE, ADXL345_INT_DATA_READY_BIT);
}
/** Set DATA_READY interrupt enabled status.
* @param enabled New interrupt enabled status
* @see getIntDataReadyEnabled()
* @see ADXL345_RA_INT_ENABLE
* @see ADXL345_INT_DATA_READY_BIT
*/
void ADXL345::setIntDataReadyEnabled(bool enabled) {
i2c->writeBit(ADXL345_RA_INT_ENABLE, enabled,
ADXL345_INT_DATA_READY_BIT);
}
/** Set SINGLE_TAP interrupt enabled status.
* @param enabled New interrupt enabled status
* @see getIntDataReadyEnabled()
* @see ADXL345_RA_INT_ENABLE
* @see ADXL345_INT_SINGLE_TAP_BIT
*/
bool ADXL345::getIntSingleTapEnabled() {
return i2c->readBit(ADXL345_RA_INT_ENABLE, ADXL345_INT_SINGLE_TAP_BIT);
}
/** Set SINGLE_TAP interrupt enabled status.
* @param enabled New interrupt enabled status
* @see getIntDataReadyEnabled()
* @see ADXL345_RA_INT_ENABLE
* @see ADXL345_INT_SINGLE_TAP_BIT
*/
void ADXL345::setIntSingleTapEnabled(bool enabled) {
i2c->writeBit(ADXL345_RA_INT_ENABLE, enabled,
ADXL345_INT_SINGLE_TAP_BIT);