Repository navigation
Expand file tree
/
Copy pathL3G4200D.cpp
More file actions
1508 lines (1361 loc) · 44.6 KB
/
Copy pathL3G4200D.cpp
File metadata and controls
1508 lines (1361 loc) · 44.6 KB
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
// I2Cdev library collection - L3G4200D I2C device class
// Based on STMicroelectronics L3G4200D datasheet rev. 3, 12/2010
// 7/31/2013 by Jonathan Arnett <j3rn@j3rn.com>
// Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
//
// Changelog:
// 2013-07-31 - initial release
/* ============================================
I2Cdev device library code is placed under the MIT license
Copyright (c) 2011 Jonathan Arnett, Jeff Rowberg
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 "L3G4200D.h"
/** Default constructor, uses default I2C address.
* @see L3G4200D_DEFAULT_ADDRESS
*/
L3G4200D::L3G4200D() {
devAddr = L3G4200D_DEFAULT_ADDRESS;
}
/** Specific address constructor.
* @param address I2C address
* @see L3G4200D_DEFAULT_ADDRESS
* @see L3G4200D_ADDRESS
*/
L3G4200D::L3G4200D(uint8_t address) {
devAddr = address;
}
/** Power on and prepare for general usage.
* All values are defaults except for the power on bit in CTRL_REG_1
* @see L3G4200D_RA_CTRL_REG1
* @see L3G4200D_RA_CTRL_REG2
* @see L3G4200D_RA_CTRL_REG3
* @see L3G4200D_RA_CTRL_REG4
* @see L3G4200D_RA_CTRL_REG5
*/
void L3G4200D::initialize() {
I2Cdev::writeByte(devAddr, L3G4200D_RA_CTRL_REG1, 0b00001111);
I2Cdev::writeByte(devAddr, L3G4200D_RA_CTRL_REG2, 0b00000000);
I2Cdev::writeByte(devAddr, L3G4200D_RA_CTRL_REG3, 0b00000000);
I2Cdev::writeByte(devAddr, L3G4200D_RA_CTRL_REG4, 0b00000000);
I2Cdev::writeByte(devAddr, L3G4200D_RA_CTRL_REG5, 0b00000000);
}
/** Verify the I2C connection.
* Make sure the device is connected and responds as expected.
* @return True if connection is valid, false otherwise
*/
bool L3G4200D::testConnection() {
return getDeviceID() == 0b11010011;
}
// WHO_AM_I register, read-only
/** Get the Device ID.
* The WHO_AM_I register holds the device's id
* @return Device ID (should be 0b11010011, 109, 0x69)
* @see L3G4200D_RA_WHO_AM_I
*/
uint8_t L3G4200D::getDeviceID() {
I2Cdev::readByte(devAddr, L3G4200D_RA_WHO_AM_I, buffer);
return buffer[0];
}
// CTRL_REG1 register, r/w
/** Set the output data rate
* @param rate The new data output rate (can be 100, 200, 400, or 800)
* @see L3G4200D_RA_CTRL_REG1
* @see L3G4200D_ODR_BIT
* @see L3G4200D_ODR_LENGTH
* @see L3G4200D_RATE_100
* @see L3G4200D_RATE_200
* @see L3G4200D_RATE_400
* @see L3G4200D_RATE_800
*/
void L3G4200D::setOutputDataRate(uint16_t rate) {
uint8_t writeVal;
if (rate == 100) {
writeVal = L3G4200D_RATE_100;
} else if (rate == 200) {
writeVal = L3G4200D_RATE_200;
} else if (rate == 400) {
writeVal = L3G4200D_RATE_400;
} else {
writeVal = L3G4200D_RATE_800;
}
I2Cdev::writeBits(devAddr, L3G4200D_RA_CTRL_REG1, L3G4200D_ODR_BIT,
L3G4200D_ODR_LENGTH, writeVal);
}
/** Get the current output data rate
* @return Current data output rate
* @see L3G4200D_RA_CTRL_REG1
* @see L3G4200D_ODR_BIT
* @see L3G4200D_ODR_LENGTH
* @see L3G4200D_RATE_100
* @see L3G4200D_RATE_200
* @see L3G4200D_RATE_400
* @see L3G4200D_RATE_800
*/
uint16_t L3G4200D::getOutputDataRate() {
I2Cdev::readBits(devAddr, L3G4200D_RA_CTRL_REG1, L3G4200D_ODR_BIT,
L3G4200D_ODR_LENGTH, buffer);
uint8_t rate = buffer[0];
if (rate == L3G4200D_RATE_100) {
return 100;
} else if (rate == L3G4200D_RATE_200) {
return 200;
} else if (rate == L3G4200D_RATE_400) {
return 400;
}
return 800;
}
/** Set the bandwidth cut-off mode
* @param mode The new bandwidth cut-off mode
* @see L3G4200D_RA_CTRL_REG1
* @see L3G4200D_BW_BIT
* @see L3G4200D_BW_LENGTH
* @see L3G4200D_BW_LOW
* @see L3G4200D_BW_MED_LOW
* @see L3G4200D_BW_MED_HIGH
* @see L3G4200D_BW_HIGH
*/
void L3G4200D::setBandwidthCutOffMode(uint8_t mode) {
I2Cdev::writeBits(devAddr, L3G4200D_RA_CTRL_REG1, L3G4200D_BW_BIT,
L3G4200D_BW_LENGTH, mode);
}
/** Get the current bandwidth cut-off mode
* @return Current bandwidth cut off mode
* @see L3G4200D_RA_CTRL_REG1
* @see L3G4200D_BW_BIT
* @see L3G4200D_BW_LENGTH
* @see L3G4200D_BW_LOW
* @see L3G4200D_BW_MED_LOW
* @see L3G4200D_BW_MED_HIGH
* @see L3G4200D_BW_HIGH
*/
uint8_t L3G4200D::getBandwidthCutOffMode() {
I2Cdev::readBits(devAddr, L3G4200D_RA_CTRL_REG1, L3G4200D_BW_BIT,
L3G4200D_BW_LENGTH, buffer);
return buffer[0];
}
/** Gets the current bandwidth cutoff based on ODR and BW
* @return Float value of the bandwidth cut off
* @see L3G4200D_RA_CTRL_REG1
* @see L3G4200D_ODR_BIT
* @see L3G4200D_ODR_LENGTH
* @see L3G4200D_RATE_100
* @see L3G4200D_RATE_200
* @see L3G4200D_RATE_400
* @see L3G4200D_RATE_800
* @see L3G4200D_BW_BIT
* @see L3G4200D_BW_LENGTH
* @see L3G4200D_BW_LOW
* @see L3G4200D_BW_MED_LOW
* @see L3G4200D_BW_MED_HIGH
* @see L3G4200D_BW_HIGH
*/
float L3G4200D::getBandwidthCutOff() {
uint16_t dataRate = getOutputDataRate();
uint8_t bandwidthMode = getBandwidthCutOffMode();
if (dataRate == 100) {
if (bandwidthMode == L3G4200D_BW_LOW) {
return 12.5;
} else {
return 25.0;
}
} else if (dataRate == 200) {
if (bandwidthMode == L3G4200D_BW_LOW) {
return 12.5;
} else if (bandwidthMode == L3G4200D_BW_MED_LOW) {
return 25.0;
} else if (bandwidthMode == L3G4200D_BW_MED_HIGH) {
return 50.0;
} else {
return 70.0;
}
} else if (dataRate == 400) {
if (bandwidthMode == L3G4200D_BW_LOW) {
return 20.0;
} else if (bandwidthMode == L3G4200D_BW_MED_LOW) {
return 25.0;
} else if (bandwidthMode == L3G4200D_BW_MED_HIGH) {
return 50.0;
} else {
return 110.0;
}
} else {
if (bandwidthMode == L3G4200D_BW_LOW) {
return 30.0;
} else if (bandwidthMode == L3G4200D_BW_MED_LOW) {
return 35.0;
} else if (bandwidthMode == L3G4200D_BW_MED_HIGH) {
return 50.0;
} else {
return 110.0;
}
}
}
/** Set power on or off
* @param enabled The new power setting (true for on, false for off)
* @see L3G4200D_RA_CTRL_REG1
* @see L3G4200D_PD_BIT
*/
void L3G4200D::setPowerOn(bool on) {
I2Cdev::writeBit(devAddr, L3G4200D_RA_CTRL_REG1, L3G4200D_PD_BIT, on);
}
/** Get the current power state
* @return Powered on state (true for on, false for off)
* @see L3G4200D_RA_CTRL_REG1
* @see L3G4200D_PD_BIT
*/
bool L3G4200D::getPowerOn() {
I2Cdev::readBit(devAddr, L3G4200D_RA_CTRL_REG1, L3G4200D_PD_BIT, buffer);
return buffer[0];
}
/** Enables or disables the ability to get Z data
* @param enabled The new enabled state of the Z axis
* @see L3G4200D_RA_CTRL_REG1
* @see L3G4200D_ZEN_BIT
*/
void L3G4200D::setZEnabled(bool enabled) {
I2Cdev::writeBit(devAddr, L3G4200D_RA_CTRL_REG1, L3G4200D_ZEN_BIT, enabled);
}
/** Get whether Z axis data is enabled
* @return True if the Z axis is enabled, false otherwise
* @see L3G4200D_RA_CTRL_REG1
* @see L3G4200D_ZEN_BIT
*/
bool L3G4200D::getZEnabled() {
I2Cdev::readBit(devAddr, L3G4200D_RA_CTRL_REG1, L3G4200D_ZEN_BIT, buffer);
return buffer[0];
}
/** Enables or disables the ability to get Y data
* @param enabled The new enabled state of the Y axis
* @see L3G4200D_RA_CTRL_REG1
* @see L3G4200D_YEN_BIT
*/
void L3G4200D::setYEnabled(bool enabled) {
I2Cdev::writeBit(devAddr, L3G4200D_RA_CTRL_REG1, L3G4200D_YEN_BIT, enabled);
}
/** Get whether Y axis data is enabled
* @return True if the Y axis is enabled, false otherwise
* @see L3G4200D_RA_CTRL_REG1
* @see L3G4200D_YEN_BIT
*/
bool L3G4200D::getYEnabled() {
I2Cdev::readBit(devAddr, L3G4200D_RA_CTRL_REG1, L3G4200D_YEN_BIT, buffer);
return buffer[0];
}
/** Enables or disables the ability to get X data
* @param enabled The new enabled state of the X axis
* @see L3G4200D_RA_CTRL_REG1
* @see L3G4200D_XEN_BIT
*/
void L3G4200D::setXEnabled(bool enabled) {
I2Cdev::writeBit(devAddr, L3G4200D_RA_CTRL_REG1, L3G4200D_XEN_BIT, enabled);
}
/** Get whether X axis data is enabled
* @return True if the X axis is enabled, false otherwise
* @see L3G4200D_RA_CTRL_REG1
* @see L3G4200D_XEN_BIT
*/
bool L3G4200D::getXEnabled() {
I2Cdev::readBit(devAddr, L3G4200D_RA_CTRL_REG1, L3G4200D_XEN_BIT, buffer);
return buffer[0];
}
// CTRL_REG2 register, r/w
/** Set the high pass mode
* @param mode The new high pass mode
* @see L3G4200D_RA_CTRL_REG2
* @see L3G4200D_HPM_BIT
* @see L3G4200D_HPM_LENGTH
* @see L3G4200D_HPM_HRF
* @see L3G4200D_HPM_REFERENCE
* @see L3G4200D_HPM_NORMAL
* @see L3G4200D_HPM_AUTORESET
*/
void L3G4200D::setHighPassMode(uint8_t mode) {
I2Cdev::writeBits(devAddr, L3G4200D_RA_CTRL_REG2, L3G4200D_HPM_BIT,
L3G4200D_HPM_LENGTH, mode);
}
/** Get the high pass mode
* @return High pass mode
* @see L3G4200D_RA_CTRL_REG2
* @see L3G4200D_HPM_BIT
* @see L3G4200D_HPM_LENGTH
* @see L3G4200D_HPM_HRF
* @see L3G4200D_HPM_REFERENCE
* @see L3G4200D_HPM_NORMAL
* @see L3G4200D_HPM_AUTORESET
*/
uint8_t L3G4200D::getHighPassMode() {
I2Cdev::readBits(devAddr, L3G4200D_RA_CTRL_REG2, L3G4200D_HPM_BIT,
L3G4200D_HPM_LENGTH, buffer);
return buffer[0];
}
/** Set the high pass filter cut off frequency level (1 - 10)
* @param level The new level for the hpcf, using one of the defined levels
* @see L3G4200D_RA_CTRL_REG2
* @see L3G4200D_HPCF_BIT
* @see L3G4200D_HPCF_LENGTH
* @see L3G4200D_HPCF1
* @see L3G4200D_HPCF2
* @see L3G4200D_HPCF3
* @see L3G4200D_HPCF4
* @see L3G4200D_HPCF5
* @see L3G4200D_HPCF6
* @see L3G4200D_HPCF7
* @see L3G4200D_HPCF8
* @see L3G4200D_HPCF9
* @see L3G4200D_HPCF10
*/
void L3G4200D::setHighPassFilterCutOffFrequencyLevel(uint8_t level) {
I2Cdev::writeBits(devAddr, L3G4200D_RA_CTRL_REG2, L3G4200D_HPCF_BIT,
L3G4200D_HPCF_LENGTH, level);
}
/** Get the high pass filter cut off frequency level (1 - 10)
* @return High pass filter cut off frequency level
* @see L3G4200D_RA_CTRL_REG2
* @see L3G4200D_HPCF_BIT
* @see L3G4200D_HPCF_LENGTH
* @see L3G4200D_HPCF1
* @see L3G4200D_HPCF2
* @see L3G4200D_HPCF3
* @see L3G4200D_HPCF4
* @see L3G4200D_HPCF5
* @see L3G4200D_HPCF6
* @see L3G4200D_HPCF7
* @see L3G4200D_HPCF8
* @see L3G4200D_HPCF9
* @see L3G4200D_HPCF10
*/
uint8_t L3G4200D::getHighPassFilterCutOffFrequencyLevel() {
I2Cdev::readBits(devAddr, L3G4200D_RA_CTRL_REG2, L3G4200D_HPCF_BIT,
L3G4200D_HPCF_LENGTH, buffer);
return buffer[0];
}
// CTRL_REG3 register, r/w
/** Set the INT1 interrupt enabled state
* @param enabled New enabled state for the INT1 interrupt
* @see L3G4200D_RA_CTRL_REG3
* @see L3G4200D_I1_INT1_BIT
*/
void L3G4200D::setINT1InterruptEnabled(bool enabled) {
I2Cdev::writeBit(devAddr, L3G4200D_RA_CTRL_REG3, L3G4200D_I1_INT1_BIT,
enabled);
}
/** Get the INT1 interrupt enabled state
* @return True if the INT1 interrupt is enabled, false otherwise
* @see L3G4200D_RA_CTRL_REG3
* @see L3G4200D_I1_INT1_BIT
*/
bool L3G4200D::getINT1InterruptEnabled() {
I2Cdev::readBit(devAddr, L3G4200D_RA_CTRL_REG3, L3G4200D_I1_INT1_BIT,
buffer);
return buffer[0];
}
/** Set the INT1 boot status enabled state
* @param enabled New enabled state for the INT1 boot status
* @see L3G4200D_RA_CTRL_REG3
* @see L3G4200D_I1_BOOT_BIT
*/
void L3G4200D::setINT1BootStatusEnabled(bool enabled) {
I2Cdev::writeBit(devAddr, L3G4200D_RA_CTRL_REG3, L3G4200D_I1_BOOT_BIT,
enabled);
}
/** Get the INT1 boot status enabled state
* @return INT1 boot status status
* @see L3G4200D_RA_CTRL_REG3
* @see L3G4200D_I1_BOOT_BIT
*/
bool L3G4200D::getINT1BootStatusEnabled() {
I2Cdev::readBit(devAddr, L3G4200D_RA_CTRL_REG3, L3G4200D_I1_BOOT_BIT,
buffer);
return buffer[0];
}
/** Interrupts the active INT1 configuration
* @see L3G4200D_RA_CTRL_REG3
* @see L3G4200D_H_LACTIVE_BIT
*/
void L3G4200D::interruptActiveINT1Config() {
I2Cdev::writeBit(devAddr, L3G4200D_RA_CTRL_REG3, L3G4200D_H_LACTIVE_BIT, 1);
}
/** Set output mode to push-pull or open-drain
* @param mode New output mode
* @see L3G4200D_RA_CTRL_REG3
* @see L3G4200D_PP_OD_BIT
* @see L3G4200D_PUSH_PULL
* @see L3G4200D_OPEN_DRAIN
*/
void L3G4200D::setOutputMode(bool mode) {
I2Cdev::writeBit(devAddr, L3G4200D_RA_CTRL_REG3, L3G4200D_PP_OD_BIT,
mode);
}
/** Get whether mode is push-pull or open drain
* @return Output mode (TRUE for push-pull, FALSE for open-drain)
* @see L3G4200D_RA_CTRL_REG3
* @see L3G4200D_PP_OD_BIT
* @see L3G4200D_PUSH_PULL
* @see L3G4200D_OPEN_DRAIN
*/
bool L3G4200D::getOutputMode() {
I2Cdev::readBit(devAddr, L3G4200D_RA_CTRL_REG3, L3G4200D_PP_OD_BIT,
buffer);
return buffer[0];
}
/** Set data ready interrupt enabled state on INT2 pin
* @param enabled New INT2 data ready interrupt enabled state
* @see L3G4200D_RA_CTRL_REG3
* @see L3G4200D_I2_DRDY_BIT
*/
void L3G4200D::setINT2DataReadyEnabled(bool enabled) {
I2Cdev::writeBit(devAddr, L3G4200D_RA_CTRL_REG3, L3G4200D_I2_DRDY_BIT,
enabled);
}
/** Get whether the data ready interrupt is enabled on the INT2 pin
* @return True if the INT2 data ready interrupt is enabled, false otherwise
* @see L3G4200D_RA_CTRL_REG3
* @see L3G4200D_I2_DRDY_BIT
*/
bool L3G4200D::getINT2DataReadyEnabled() {
I2Cdev::readBit(devAddr, L3G4200D_RA_CTRL_REG3, L3G4200D_I2_DRDY_BIT,
buffer);
return buffer[0];
}
/** Set whether the INT2 FIFO watermark interrupt is enabled
* The sensor contains a 32-slot FIFO buffer for storing data so that it may be
* read later. If enabled, the sensor will generate an interrupt on the
* INT2/DRDY pin when the watermark has been reached. The watermark can be
* configured through the setFIFOWatermark function.
* @param enabled New enabled state of the INT2 FIFO watermark
* @see L3G4200D_RA_CTRL_REG3
* @see L3G4200D_I2_WTM_BIT
*/
void L3G4200D::setINT2FIFOWatermarkInterruptEnabled(bool enabled) {
I2Cdev::writeBit(devAddr, L3G4200D_RA_CTRL_REG3, L3G4200D_I2_WTM_BIT,
enabled);
}
/** Get the INT2 FIFO watermark interrupt enabled state
* @return true if the FIFO watermark is enabled, false otherwise
* @see L3G4200D_RA_CTRL_REG3
* @see L3G4200D_I2_WTM_BIT
*/
bool L3G4200D::getINT2FIFOWatermarkInterruptEnabled() {
I2Cdev::readBit(devAddr, L3G4200D_RA_CTRL_REG3, L3G4200D_I2_WTM_BIT,
buffer);
return buffer[0];
}
/** Set whether an interrupt is triggered on INT2 when the FIFO is overrun
* @param enabled New FIFO overrun interrupt enabled state
* @see L3G4200D_RA_CTRL_REG3
* @see L3G4200D_I2_ORUN_BIT
*/
void L3G4200D::setINT2FIFOOverrunInterruptEnabled(bool enabled) {
I2Cdev::writeBit(devAddr, L3G4200D_RA_CTRL_REG3, L3G4200D_I2_ORUN_BIT,
enabled);
}
/** Get whether an interrupt is triggered on INT2 when the FIFO is overrun
* @return True if the INT2 FIFO overrun interrupt is enabled, false otherwise
* @see L3G4200D_RA_CTRL_REG3
* @see L3G4200D_I2_ORUN_BIT
*/
bool L3G4200D::getINT2FIFOOverrunInterruptEnabled() {
I2Cdev::readBit(devAddr, L3G4200D_RA_CTRL_REG3, L3G4200D_I2_ORUN_BIT,
buffer);
return buffer[0];
}
/** Set whether an interrupt is triggered on INT2 when the FIFO buffer is empty
* @param enabled New INT2 FIFO empty interrupt state
* @see L3G4200D_RA_CTRL_REG3
* @see L3G4200D_I2_EMPTY_BIT
*/
void L3G4200D::setINT2FIFOEmptyInterruptEnabled(bool enabled) {
I2Cdev::writeBit(devAddr, L3G4200D_RA_CTRL_REG3, L3G4200D_I2_EMPTY_BIT,
enabled);
}
/** Get whether the INT2 FIFO empty interrupt is enabled
* @returns Trur if the INT2 FIFO empty interrupt is enabled, false otherwise
* @see L3G4200D_RA_CTRL_REG3
* @see L3G4200D_I2_EMPTY_BIT
*/
bool L3G4200D::getINT2FIFOEmptyInterruptEnabled() {
I2Cdev::readBit(devAddr, L3G4200D_RA_CTRL_REG3, L3G4200D_I2_EMPTY_BIT,
buffer);
return buffer[0];
}
// CTRL_REG4 register, r/w
/** Set the Block Data Update (BDU) enabled state
* @param enabled New BDU enabled state
* @see L3G4200D_RA_CTRL_REG4
* @see L3G4200D_BDU_BIT
*/
void L3G4200D::setBlockDataUpdateEnabled(bool enabled) {
I2Cdev::writeBit(devAddr, L3G4200D_RA_CTRL_REG4, L3G4200D_BDU_BIT, enabled);
}
/** Get the BDU enabled state
* @return True if Block Data Update is enabled, false otherwise
* @see L3G4200D_RA_CTRL_REG4
* @see L3G4200D_BDU_BIT
*/
bool L3G4200D::getBlockDataUpdateEnabled() {
I2Cdev::readBit(devAddr, L3G4200D_RA_CTRL_REG4, L3G4200D_BDU_BIT, buffer);
return buffer[0];
}
/** Set the data endian modes
* In Big Endian mode, the Most Significat Byte (MSB) is on the lower address,
* and the Least Significant Byte (LSB) is on the higher address. Little Endian
* mode reverses this order. Little Endian is the default mode.
* @param endianness New endian mode
* @see L3G4200D_RA_CTRL_REG4
* @see L3G4200D_BLE_BIT
* @see L3G4200D_BIG_ENDIAN
* @see L3G4200D_LITTLE_ENDIAN
*/
void L3G4200D::setEndianMode(bool endianness) {
I2Cdev::writeBit(devAddr, L3G4200D_RA_CTRL_REG4, L3G4200D_BLE_BIT,
endianness);
}
/** Get the data endian mode
* @return Current endian mode
* @see L3G4200D_RA_CTRL_REG4
* @see L3G4200D_BLE_BIT
* @see L3G4200D_BIG_ENDIAN
* @see L3G4200D_LITTLE_ENDIAN
*/
bool L3G4200D::getEndianMode() {
I2Cdev::readBit(devAddr, L3G4200D_RA_CTRL_REG4, L3G4200D_BLE_BIT,
buffer);
return buffer[0];
}
/** Set the full scale of the data output (in dps)
* @param scale The new scale of the data output (250, 500, 2000)
* @see L3G4200D_RA_CTRL_REG4
* @see L3G4200D_FS_BIT
* @see L3G4200D_FS_LENGTH
* @see L3G4200D_FS_250
* @see L3G4200D_FS_500
* @see L3G4200D_FS_2000
*/
void L3G4200D::setFullScale(uint16_t scale) {
uint8_t writeBits;
if (scale == 250) {
writeBits = L3G4200D_FS_250;
} else if (scale == 500) {
writeBits = L3G4200D_FS_500;
} else {
writeBits = L3G4200D_FS_2000;
}
I2Cdev::writeBits(devAddr, L3G4200D_RA_CTRL_REG4, L3G4200D_FS_BIT,
L3G4200D_FS_LENGTH, writeBits);
}
/** Get the current full scale of the output data (in dps)
* @return Current scale of the output data
* @see L3G4200D_RA_CTRL_REG4
* @see L3G4200D_FS_BIT
* @see L3G4200D_FS_LENGTH
* @see L3G4200D_FS_250
* @see L3G4200D_FS_500
* @see L3G4200D_FS_2000
*/
uint16_t L3G4200D::getFullScale() {
I2Cdev::readBits(devAddr, L3G4200D_RA_CTRL_REG4,
L3G4200D_FS_BIT, L3G4200D_FS_LENGTH, buffer);
uint8_t readBits = buffer[0];
if (readBits == L3G4200D_FS_250) {
return 250;
} else if (readBits == L3G4200D_FS_500) {
return 500;
} else {
return 2000;
}
}
/** Set the self test mode
* @param mode New self test mode (Normal, 0, 1)
* @see L3G4200D_RA_CTRL_REG4
* @see L3G4200D_ST_BIT
* @see L3G4200D_ST_LENGTH
* @see L3G4200D_SELF_TEST_NORMAL
* @see L3G4200D_SELF_TEST_0
* @see L3G4200D_SELF_TEST_1
*/
void L3G4200D::setSelfTestMode(uint8_t mode) {
I2Cdev::writeBits(devAddr, L3G4200D_RA_CTRL_REG4, L3G4200D_ST_BIT,
L3G4200D_ST_LENGTH, mode);
}
/** Get the current self test mode
* @return Current self test mode
* @see L3G4200D_RA_CTRL_REG4
* @see L3G4200D_ST_BIT
* @see L3G4200D_ST_LENGTH
* @see L3G4200D_SELF_TEST_NORMAL
* @see L3G4200D_SELF_TEST_0
* @see L3G4200D_SELF_TEST_1
*/
uint8_t L3G4200D::getSelfTestMode() {
I2Cdev::readBits(devAddr, L3G4200D_RA_CTRL_REG4, L3G4200D_ST_BIT,
L3G4200D_ST_LENGTH, buffer);
return buffer[0];
}
/** Set the SPI mode
* @param mode New SPI mode
* @see L3G4200D_RA_CTRL_REG4
* @see L3G4200D_SIM_BIT
* @see L3G4200D_SPI_4_WIRE
* @see L3G4200D_SPI_3_WIRE
*/
void L3G4200D::setSPIMode(bool mode) {
I2Cdev::writeBit(devAddr, L3G4200D_RA_CTRL_REG4, L3G4200D_SIM_BIT, mode);
}
/** Get the SPI mode
* @return Current SPI mode
* @see L3G4200D_RA_CTRL_REG4
* @see L3G4200D_SIM_BIT
* @see L3G4200D_SPI_4_WIRE
* @see L3G4200D_SPI_3_WIRE
*/
bool L3G4200D::getSPIMode() {
I2Cdev::readBit(devAddr, L3G4200D_RA_CTRL_REG4, L3G4200D_SIM_BIT,
buffer);
return buffer[0];
}
// CTRL_REG5 register, r/w
/** Reboots the FIFO memory content
* @see L3G4200D_RA_CTRL_REG5
* @see L3G4200D_BOOT_BIT
*/
void L3G4200D::rebootMemoryContent() {
I2Cdev::writeBit(devAddr, L3G4200D_RA_CTRL_REG5, L3G4200D_BOOT_BIT, true);
}
/** Set whether the FIFO buffer is enabled
* @param enabled New enabled state of the FIFO buffer
* @see L3G4200D_RA_CTRL_REG5
* @see L3G4200D_FIFO_EN_BIT
*/
void L3G4200D::setFIFOEnabled(bool enabled) {
I2Cdev::writeBit(devAddr, L3G4200D_RA_CTRL_REG5, L3G4200D_FIFO_EN_BIT,
enabled);
}
/** Get whether the FIFO buffer is enabled
* @return True if the FIFO buffer is enabled, false otherwise
* @see L3G4200D_RA_CTRL_REG5
* @see L3G4200D_FIFO_EN_BIT
*/
bool L3G4200D::getFIFOEnabled() {
I2Cdev::readBit(devAddr, L3G4200D_RA_CTRL_REG5, L3G4200D_FIFO_EN_BIT,
buffer);
return buffer[0];
}
/** Set the high pass filter enabled state
* @param enabled New high pass filter enabled state
* @see L3G4200D_RA_CTRL_REG5
* @see L3G4200D_HPEN_BIT
*/
void L3G4200D::setHighPassFilterEnabled(bool enabled) {
I2Cdev::writeBit(devAddr, L3G4200D_RA_CTRL_REG5, L3G4200D_HPEN_BIT,
enabled);
}
/** Get whether the high pass filter is enabled
* @return True if the high pass filter is enabled, false otherwise
* @see L3G4200D_RA_CTRL_REG5
* @see L3G4200D_HPEN_BIT
*/
bool L3G4200D::getHighPassFilterEnabled() {
I2Cdev::readBit(devAddr, L3G4200D_RA_CTRL_REG5, L3G4200D_HPEN_BIT,
buffer);
return buffer[0];
}
/** Sets the filter mode to one of the four provided.
* This function also uses the setHighPassFilterEnabled function in order to set
* the mode. That function does not haved to be called in addition to this one.
* In addition to setting the filter for the data in the FIFO buffer
* (controlled by the bits written to OUT_SEL), this function also sets the
* filter used for interrupt generation (the bits written to INT1_SEL) to be the
* same as the filter used for the FIFO buffer.
* @param filter New method to be used when filtering data
* @see L3G4200D_RA_CTRL_REG5
* @see L3G4200D_INT1_SEL_BIT
* @see L3G4200D_INT1_SEL_LENGTH
* @see L3G4200D_OUT_SEL_BIT
* @see L3G4200D_OUT_SEL_LENGTH
* @see L3G4200D_NON_HIGH_PASS
* @see L3G4200D_HIGH_PASS
* @see L3G4200D_LOW_PASS
* @see L3G4200D_LOW_HIGH_PASS
*/
void L3G4200D::setDataFilter(uint8_t filter) {
if (filter == L3G4200D_HIGH_PASS || filter == L3G4200D_LOW_HIGH_PASS) {
setHighPassFilterEnabled(true);
} else {
setHighPassFilterEnabled(false);
}
I2Cdev::writeBits(devAddr, L3G4200D_RA_CTRL_REG5, L3G4200D_OUT_SEL_BIT,
L3G4200D_OUT_SEL_LENGTH, filter);
I2Cdev::writeBits(devAddr, L3G4200D_RA_CTRL_REG5, L3G4200D_INT1_SEL_BIT,
L3G4200D_INT1_SEL_LENGTH, filter);
}
/** Gets the data filter currently in use
* @return Defined value that represents the filter in use
* @see L3G4200D_RA_CTRL_REG5
* @see L3G4200D_OUT_SEL_BIT
* @see L3G4200D_OUT_SEL_LENGTH
* @see L3G4200D_NON_HIGH_PASS
* @see L3G4200D_HIGH_PASS
* @see L3G4200D_LOW_PASS
* @see L3G4200D_LOW_HIGH_PASS
*/
uint8_t L3G4200D::getDataFilter() {
I2Cdev::readBits(devAddr, L3G4200D_RA_CTRL_REG5, L3G4200D_OUT_SEL_BIT,
L3G4200D_OUT_SEL_LENGTH, buffer);
uint8_t outBits = buffer[0];
if (outBits == L3G4200D_NON_HIGH_PASS || outBits == L3G4200D_HIGH_PASS) {
return outBits;
}
if (getHighPassFilterEnabled()) {
return L3G4200D_LOW_HIGH_PASS;
} else {
return L3G4200D_LOW_PASS;
}
}
// REFERENCE/DATACAPTURE register, r/w
/** Set the reference value for interrupt generation
* @param reference New reference value for interrupt generation
* @see L3G4200D_RA_REFERENCE
*/
void L3G4200D::setInterruptReference(uint8_t reference) {
I2Cdev::writeByte(devAddr, L3G4200D_RA_REFERENCE, reference);
}
/** Get the 8-bit reference value for interrupt generation
* @return 8-bit reference value for interrupt generation
* @see L3G4200D_RA_REFERENCE
*/
uint8_t L3G4200D::getInterruptReference() {
I2Cdev::readByte(devAddr, L3G4200D_RA_REFERENCE, buffer);
return buffer[0];
}
// OUT_TEMP register, read-only
/** Gets the current temperature reading from the sensor
* @return Current temperature
* @see L3G4200D_RA_OUT_TEMP
*/
uint8_t L3G4200D::getTemperature() {
I2Cdev::readByte(devAddr, L3G4200D_RA_OUT_TEMP, buffer);
return buffer[0];
}
// STATUS register, read-only
/** Get whether new data overwrote the last set of data before it was read
* @return True if the last set of data was overwritten before being read, false
* otherwise
* @see L3G4200D_RA_STATUS
* @see L3G4200D_ZYXOR_BIT
*/
bool L3G4200D::getXYZOverrun() {
I2Cdev::readBit(devAddr, L3G4200D_RA_STATUS, L3G4200D_ZYXOR_BIT,
buffer);
return buffer[0];
}
/** Get whether new Z data overwrote the last set of data before it was read
* @return True if the last set of Z data was overwritten before being read,
* false otherwise
* @see L3G4200D_RA_STATUS
* @see L3G4200D_ZOR_BIT
*/
bool L3G4200D::getZOverrun() {
I2Cdev::readBit(devAddr, L3G4200D_RA_STATUS, L3G4200D_ZOR_BIT,
buffer);
return buffer[0];
}
/** Get whether new Y data overwrote the last set of data before it was read
* @return True if the last set of Y data was overwritten before being read,
* false otherwise
* @see L3G4200D_RA_STATUS
* @see L3G4200D_YOR_BIT
*/
bool L3G4200D::getYOverrun() {
I2Cdev::readBit(devAddr, L3G4200D_RA_STATUS, L3G4200D_YOR_BIT,
buffer);
return buffer[0];
}
/** Get whether new X data overwrote the last set of data before it was read
* @return True if the last set of X data was overwritten before being read,
* false otherwise
* @see L3G4200D_RA_STATUS
* @see L3G4200D_XOR_BIT
*/
bool L3G4200D::getXOverrun() {
I2Cdev::readBit(devAddr, L3G4200D_RA_STATUS, L3G4200D_XOR_BIT,
buffer);
return buffer[0];
}
/** Get whether there is new data avaialable
* @return True if there is new data available, false otherwise
* @see L3G4200D_RA_STATUS
* @see L3G4200D_ZYXDA_BIT
*/
bool L3G4200D::getXYZDataAvailable() {
I2Cdev::readBit(devAddr, L3G4200D_RA_STATUS, L3G4200D_ZYXDA_BIT,
buffer);
return buffer[0];
}
/** Get whether there is new Z data avaialable
* @return True if there is new Z data available, false otherwise
* @see L3G4200D_RA_STATUS
* @see L3G4200D_ZDA_BIT
*/
bool L3G4200D::getZDataAvailable() {
I2Cdev::readBit(devAddr, L3G4200D_RA_STATUS, L3G4200D_ZDA_BIT,
buffer);
return buffer[0];
}
/** Get whether there is new Y data avaialable
* @return True if there is new Y data available, false otherwise
* @see L3G4200D_RA_STATUS
* @see L3G4200D_YDA_BIT
*/
bool L3G4200D::getYDataAvailable() {
I2Cdev::readBit(devAddr, L3G4200D_RA_STATUS, L3G4200D_YDA_BIT,
buffer);
return buffer[0];
}
/** Get whether there is new X data avaialable
* @return True if there is new X data available, false otherwise
* @see L3G4200D_RA_STATUS
* @see L3G4200D_XDA_BIT
*/
bool L3G4200D::getXDataAvailable() {
I2Cdev::readBit(devAddr, L3G4200D_RA_STATUS, L3G4200D_XDA_BIT,
buffer);
return buffer[0];
}
// OUT_* registers, read-only
/** Get the angular velocity for all 3 axes
* Due to the fact that this device supports two difference Endian modes, both
* must be accounted for when reading data. In Little Endian mode, the first
* byte (lowest address) is the least significant and in Big Endian mode the
* first byte is the most significant.
* @param x 16-bit integer container for the X-axis angular velocity
* @param y 16-bit integer container for the Y-axis angular velocity
* @param z 16-bit integer container for the Z-axis angular velocity
*/
void L3G4200D::getAngularVelocity(int16_t* x, int16_t* y, int16_t* z) {
*x = getAngularVelocityX();
*y = getAngularVelocityY();
*z = getAngularVelocityZ();
}
/** Get the angular velocity about the X-axis
* @return Angular velocity about the X-axis
* @see L3G4200D_RA_OUT_X_L
* @see L3G4200D_RA_OUT_X_H
*/
int16_t L3G4200D::getAngularVelocityX() {
uint8_t bufferL[6];
uint8_t bufferH[6];
I2Cdev::readBytes(devAddr, L3G4200D_RA_OUT_X_L, 1, bufferL);
I2Cdev::readBytes(devAddr, L3G4200D_RA_OUT_X_H, 1, bufferH);
if (getEndianMode() == L3G4200D_BIG_ENDIAN) {
return (((int16_t) bufferL[0]) << 8) | bufferH[0];
} else {
return (((int16_t) bufferH[0]) << 8) | bufferL[0];
}
}
/** Get the angular velocity about the Y-axis
* @return Angular velocity about the Y-axis
* @see L3G4200D_RA_OUT_Y_L
* @see L3G4200D_RA_OUT_Y_H
*/
int16_t L3G4200D::getAngularVelocityY() {
uint8_t bufferL[6];
uint8_t bufferH[6];
I2Cdev::readBytes(devAddr, L3G4200D_RA_OUT_Y_L, 1, bufferL);
I2Cdev::readBytes(devAddr, L3G4200D_RA_OUT_Y_H, 1, bufferH);
if (getEndianMode() == L3G4200D_BIG_ENDIAN) {
return (((int16_t) bufferL[0]) << 8) | bufferH[0];
} else {
return (((int16_t) bufferH[0]) << 8) | bufferL[0];
}
}
/** Get the angular velocity about the Z-axis
* @return Angular velocity about the Z-axis
* @see L3G4200D_RA_OUT_Z_L
* @see L3G4200D_RA_OUT_Z_H
*/
int16_t L3G4200D::getAngularVelocityZ() {
uint8_t bufferL[6];
uint8_t bufferH[6];
I2Cdev::readBytes(devAddr, L3G4200D_RA_OUT_Z_L, 1, bufferL);
I2Cdev::readBytes(devAddr, L3G4200D_RA_OUT_Z_H, 1, bufferH);
if (getEndianMode() == L3G4200D_BIG_ENDIAN) {
return (((int16_t) bufferL[0]) << 8) | bufferH[0];
} else {
return (((int16_t) bufferH[0]) << 8) | bufferL[0];
}
}
// FIFO_CTRL register, r/w
/** Set the FIFO mode to one of the defined modes