-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathAdafruit_SensorLab.cpp
949 lines (805 loc) · 28.3 KB
/
Adafruit_SensorLab.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
/**************************************************************************/
/*!
@file Adafruit_SensorLab.cpp
@author Limor Fried (Adafruit Industries)
Arduino library for scientific sensor readings/fusions/manipulations
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
*/
/**************************************************************************/
#include <Adafruit_SensorLab.h>
/**************************************************************************/
/*!
@brief Mapper helper function for floating point values
@param x Value to map
@param in_min Input range minimum for x
@param in_max Input range maximum for x
@param out_min Ouput range maximum for return
@param out_max Ouput range maximum for return
@returns Mapped value
*/
/**************************************************************************/
float Adafruit_SensorLab::mapf(float x, float in_min, float in_max,
float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
/**************************************************************************/
/*!
@brief Instantiate the SensorLab object
@param i2c The I2C hardware interface, default is Wire
*/
/**************************************************************************/
Adafruit_SensorLab::Adafruit_SensorLab(TwoWire *i2c) { _i2c = i2c; }
/**************************************************************************/
/*!
@brief Initialize the sensorlab manager and begin I2C
@param I2C_Frequency desired I2C clock rate - default is 100KHz
*/
/**************************************************************************/
void Adafruit_SensorLab::begin(uint32_t I2C_Frequency) {
_i2c->begin();
_i2c->setClock(I2C_Frequency);
yield();
}
/**************************************************************************/
/*!
@brief Detect if we have a valid ADXL34x sensor attached and sets
the default accelerometer sensor if so
@return True if found
*/
/**************************************************************************/
bool Adafruit_SensorLab::detectADXL34X(void) {
bool addr1D = scanI2C(0x1D);
bool addr53 = scanI2C(0x53);
if (!addr1D && !addr53) {
return false; // no I2C device that could possibly work found!
}
_adxl34x = new Adafruit_ADXL343(343, _i2c);
if ((addr1D && _adxl34x->begin(0x1D)) || (addr53 && _adxl34x->begin(0x53))) {
// yay found a ADXL34x
Serial.println(F("Found a ADXL34x accelerometer"));
accelerometer = _adxl34x;
return true;
}
delete _adxl34x;
return false;
}
/**************************************************************************/
/*!
@brief Detect if we have a valid AHTX0 (AHT10 & AHT20) sensor
attached and sets the default temperature and humidity sensors if so.
@return True if sensor is detected, False otherwise.
*/
/**************************************************************************/
bool Adafruit_SensorLab::detectAHTX0(void) {
bool addr38 = scanI2C(0x38);
if (!addr38) {
return false; // not even maybe!
}
_ahtx0 = new Adafruit_AHTX0();
if (addr38 && _ahtx0->begin(_i2c)) {
// we got one!
Serial.println(F("Found an AHT20 Temperature & Humidity Sensor"));
if (!humidity)
humidity = _ahtx0->getHumiditySensor();
if (!temperature)
temperature = _ahtx0->getTemperatureSensor();
return true;
}
delete _ahtx0;
return false;
}
/**************************************************************************/
/*!
@brief Detect if we have a valid ADXL34x sensor attached and sets
the default accelerometer sensor if so
@return True if found
*/
/**************************************************************************/
bool Adafruit_SensorLab::detectLSM303A(void) {
bool addr19 = scanI2C(0x19);
if (!addr19) {
return false; // no I2C device that could possibly work found!
}
_lsm303a = new Adafruit_LSM303_Accel_Unified(303);
if (_lsm303a->begin(0x19, _i2c)) {
// yay found a LSM303 Accel
Serial.println(F("Found a LSM303 accelerometer"));
accelerometer = _lsm303a;
return true;
}
delete _lsm303a;
return false;
}
/**************************************************************************/
/*!
@brief Detect if we have a valid LSM6DS33 sensor attached and sets
the default temperature, accelerometer and gyroscope sensors if so
@return True if found
*/
/**************************************************************************/
bool Adafruit_SensorLab::detectLSM6DS33(void) {
bool addr6A = scanI2C(0x6A);
bool addr6B = scanI2C(0x6B);
if (!addr6A && !addr6B) {
return false; // no I2C device that could possibly work found!
}
_lsm6ds33 = new Adafruit_LSM6DS33();
if ((addr6A && _lsm6ds33->begin_I2C(0x6A, _i2c)) ||
(addr6B && _lsm6ds33->begin_I2C(0x6B, _i2c))) {
// yay found a LSM6DS33
Serial.println(F("Found a LSM6DS33 IMU"));
if (!accelerometer)
accelerometer = _lsm6ds33->getAccelerometerSensor();
if (!gyroscope)
gyroscope = _lsm6ds33->getGyroSensor();
if (!temperature) {
temperature = _lsm6ds33->getTemperatureSensor();
}
return true;
}
delete _lsm6ds33;
return false;
}
/**************************************************************************/
/*!
@brief Detect if we have a valid LSM6DS3TR-C sensor attached and sets
the default temperature, accelerometer and gyroscope sensors if so
@return True if found
*/
/**************************************************************************/
bool Adafruit_SensorLab::detectLSM6DS3TRC(void) {
bool addr6A = scanI2C(0x6A);
bool addr6B = scanI2C(0x6B);
if (!addr6A && !addr6B) {
return false; // no I2C device that could possibly work found!
}
_lsm6ds3trc = new Adafruit_LSM6DS3TRC();
if ((addr6A && _lsm6ds3trc->begin_I2C(0x6A, _i2c)) ||
(addr6B && _lsm6ds3trc->begin_I2C(0x6B, _i2c))) {
// yay found a LSM6DS3TR-C
Serial.println(F("Found a LSM6DS3TR-C IMU"));
if (!accelerometer)
accelerometer = _lsm6ds3trc->getAccelerometerSensor();
if (!gyroscope)
gyroscope = _lsm6ds3trc->getGyroSensor();
if (!temperature) {
temperature = _lsm6ds3trc->getTemperatureSensor();
}
return true;
}
delete _lsm6ds3trc;
return false;
}
/**************************************************************************/
/*!
@brief Detect if we have a valid LSM6DSOX sensor attached and sets
the default temperature, accelerometer and gyroscope sensors if so
@return True if found
*/
/**************************************************************************/
bool Adafruit_SensorLab::detectLSM6DSOX(void) {
bool addr6A = scanI2C(0x6A);
bool addr6B = scanI2C(0x6B);
if (!addr6A && !addr6B) {
return false; // no I2C device that could possibly work found!
}
_lsm6dsox = new Adafruit_LSM6DSOX();
if ((addr6A && _lsm6dsox->begin_I2C(0x6A, _i2c)) ||
(addr6B && _lsm6dsox->begin_I2C(0x6B, _i2c))) {
// yay found a LSM6DSOX
Serial.println(F("Found a LSM6DSOX IMU"));
if (!accelerometer)
accelerometer = _lsm6dsox->getAccelerometerSensor();
if (!gyroscope)
gyroscope = _lsm6dsox->getGyroSensor();
if (!temperature) {
temperature = _lsm6dsox->getTemperatureSensor();
}
return true;
}
delete _lsm6dsox;
return false;
}
/**************************************************************************/
/*!
@brief Detect if we have a valid LSM9DS1 sensor attached and sets
the default temperature, accelerometer and gyroscope sensors if so
@return True if found
*/
/**************************************************************************/
bool Adafruit_SensorLab::detectLSM9DS1(void) {
bool addr6B = scanI2C(0x6B);
if (!addr6B) {
return false; // no I2C device that could possibly work found!
}
_lsm9ds1 = new Adafruit_LSM9DS1(_i2c, 9051);
if (_lsm9ds1->begin()) {
// yay found a LSM9DS1
Serial.println(F("Found a LSM9DS1 IMU"));
if (!accelerometer)
accelerometer = &_lsm9ds1->getAccel();
if (!gyroscope)
gyroscope = &_lsm9ds1->getGyro();
if (!magnetometer)
magnetometer = &_lsm9ds1->getMag();
if (!temperature) {
temperature = &_lsm9ds1->getTemp();
}
return true;
}
delete _lsm9ds1;
return false;
}
/**************************************************************************/
/*!
@brief Detect if we have a valid LSM9DS0 sensor attached and sets
the default temperature, accelerometer, mag and gyroscope sensors if so
@return True if found
*/
/**************************************************************************/
bool Adafruit_SensorLab::detectLSM9DS0(void) {
bool addr6B = scanI2C(0x6B);
bool addr1D = scanI2C(0x1D);
if (!addr6B || !addr1D) {
return false; // no I2C device that could possibly work found!
}
_lsm9ds0 = new Adafruit_LSM9DS0(_i2c, 9050);
if (_lsm9ds0->begin()) {
// yay found a LSM9DS0
Serial.println(F("Found a LSM9DS0 IMU"));
if (!accelerometer)
accelerometer = &_lsm9ds0->getAccel();
if (!gyroscope)
gyroscope = &_lsm9ds0->getGyro();
if (!magnetometer)
magnetometer = &_lsm9ds0->getMag();
if (!temperature) {
temperature = &_lsm9ds0->getTemp();
}
return true;
}
delete _lsm9ds0;
return false;
}
/**************************************************************************/
/*!
@brief Detect if we have a valid ICM20649 sensor attached and sets
the default temperature, accelerometer and gyroscope sensors if so
@return True if found
*/
/**************************************************************************/
bool Adafruit_SensorLab::detectICM20649(void) {
bool addr68 = scanI2C(0x68);
bool addr69 = scanI2C(0x69);
if (!addr68 && !addr69) {
return false; // no I2C device that could possibly work found!
}
_icm20649 = new Adafruit_ICM20649();
if ((addr68 && _icm20649->begin_I2C(0x68, _i2c)) ||
(addr69 && _icm20649->begin_I2C(0x69, _i2c))) {
// yay found an ICM20649
Serial.println(F("Found an ICM20649 IMU"));
if (!accelerometer)
accelerometer = _icm20649->getAccelerometerSensor();
if (!gyroscope)
gyroscope = _icm20649->getGyroSensor();
if (!temperature) {
temperature = _icm20649->getTemperatureSensor();
}
return true;
}
delete _icm20649;
return false;
}
/**************************************************************************/
/*!
@brief Detect if we have a valid HTS221 sensor attached and sets
the default temperature and humidity sensors if so
@return True if found
*/
/**************************************************************************/
bool Adafruit_SensorLab::detectHTS221(void) {
bool addr5f = scanI2C(0x5F);
if (!addr5f) {
return false; // not even maybe!
}
_hts221 = new Adafruit_HTS221();
if (addr5f && _hts221->begin_I2C(0x5F, _i2c)) {
// we got one!
Serial.println(F("Found a HTS221 Humidity+Temperature sensor"));
if (!humidity)
humidity = _hts221->getHumiditySensor();
if (!temperature)
temperature = _hts221->getTemperatureSensor();
return true;
}
delete _hts221;
return false;
}
/**************************************************************************/
/*!
@brief Detect if we have a valid ISM330DHC sensor attached and sets
the default temperature, accelerometer and gyroscope sensors if so
@return True if found
*/
/**************************************************************************/
bool Adafruit_SensorLab::detectISM330DHCX(void) {
bool addr6A = scanI2C(0x6A);
bool addr6B = scanI2C(0x6B);
if (!addr6A && !addr6B) {
return false; // no I2C device that could possibly work found!
}
_ism330dhcx = new Adafruit_ISM330DHCX();
if ((addr6A && _ism330dhcx->begin_I2C(0x6A, _i2c)) ||
(addr6B && _ism330dhcx->begin_I2C(0x6B, _i2c))) {
// yay found a ISM330
Serial.println(F("Found a ISM330DHCX IMU"));
if (!accelerometer)
accelerometer = _ism330dhcx->getAccelerometerSensor();
if (!gyroscope)
gyroscope = _ism330dhcx->getGyroSensor();
if (!temperature) {
temperature = _ism330dhcx->getTemperatureSensor();
}
return true;
}
delete _ism330dhcx;
return false;
}
/**************************************************************************/
/*!
@brief Detect if we have a valid FXOS8700 sensor attached and sets
the default accelerometer and magnetometer sensors if so
@return True if found
*/
/**************************************************************************/
bool Adafruit_SensorLab::detectFXOS8700(void) {
bool addr1C = scanI2C(0x1C);
bool addr1D = scanI2C(0x1D);
bool addr1E = scanI2C(0x1E);
bool addr1F = scanI2C(0x1F);
if (!addr1C && !addr1D && !addr1E && !addr1F) {
return false; // no I2C device that could possibly work found!
}
_fxos8700 = new Adafruit_FXOS8700(0x8700A, 0x8700B);
if ((addr1C && _fxos8700->begin(0x1C, _i2c)) ||
(addr1D && _fxos8700->begin(0x1D, _i2c)) ||
(addr1E && _fxos8700->begin(0x1E, _i2c)) ||
(addr1F && _fxos8700->begin(0x1F, _i2c))) {
// yay found a FXOS8700
Serial.println(F("Found a FXOS8700 eCompass"));
if (!accelerometer)
accelerometer = _fxos8700->getAccelerometerSensor();
if (!magnetometer)
magnetometer = _fxos8700->getMagnetometerSensor();
return true;
}
delete _fxos8700;
return false;
}
/**************************************************************************/
/*!
@brief Detect if we have a valid FXAS21002C sensor attached and sets
the default gyroscope sensor if so
@return True if found
*/
/**************************************************************************/
bool Adafruit_SensorLab::detectFXAS21002(void) {
bool addr20 = scanI2C(0x20);
bool addr21 = scanI2C(0x21);
if (!addr20 && !addr21) {
return false; // no I2C device that could possibly work found!
}
_fxas21002 = new Adafruit_FXAS21002C(21002);
if ((addr20 && _fxas21002->begin(0x20, _i2c)) ||
(addr21 && _fxas21002->begin(0x21, _i2c))) {
// yay found a FXAS21002C
Serial.println(F("Found a FXAS21002C gyro"));
if (!gyroscope)
gyroscope = _fxas21002;
return true;
}
delete _fxas21002;
return false;
}
/**************************************************************************/
/*!
@brief Detect if we have a valid LIS3MDL sensor attached and sets
the default magnetometer sensor if so
@return True if found
*/
/**************************************************************************/
bool Adafruit_SensorLab::detectLIS3MDL(void) {
bool addr1E = scanI2C(0x1E);
bool addr1C = scanI2C(0x1C);
if (!addr1C && !addr1E) {
return false; // no I2C device that could possibly work found!
}
_lis3mdl = new Adafruit_LIS3MDL();
if ((addr1E && _lis3mdl->begin_I2C(0x1E, _i2c)) ||
(addr1C && _lis3mdl->begin_I2C(0x1C, _i2c))) {
// yay found a LIS3MDL
Serial.println(F("Found a LIS3MDL IMU"));
if (!magnetometer)
magnetometer = _lis3mdl;
return true;
}
delete _lis3mdl;
return false;
}
/**************************************************************************/
/*!
@brief Detect if we have a valid LIS2MDL sensor attached and sets
the default magnetometer sensor if so
@return True if found
*/
/**************************************************************************/
bool Adafruit_SensorLab::detectLIS2MDL(void) {
bool addr1E = scanI2C(0x1E);
if (!addr1E) {
return false; // no I2C device that could possibly work found!
}
_lis2mdl = new Adafruit_LIS2MDL();
if (_lis2mdl->begin(0x1E, _i2c)) {
// yay found a LIS2MDL
Serial.println(F("Found a LIS2MDL IMU"));
if (!magnetometer)
magnetometer = _lis2mdl;
return true;
}
delete _lis2mdl;
return false;
}
/**************************************************************************/
/*!
@brief Detect if we have a valid MLX90393 sensor attached and sets
the default magnetometer sensor if so
@return True if found
*/
/**************************************************************************/
bool Adafruit_SensorLab::detectMLX90393(void) {
bool addr0C = scanI2C(0x0C);
bool addr18 = scanI2C(0x18);
if (!addr0C && !addr18) {
return false; // no I2C device that could possibly work found!
}
_mlx90393 = new Adafruit_MLX90393();
if ((addr0C && _mlx90393->begin_I2C(0x0C, _i2c)) ||
(addr18 && _mlx90393->begin_I2C(0x18, _i2c))) {
// Found an MLX90393!
Serial.println(F("Found a MLX90393 IMU"));
// Set to lowest gain and aim for sensitivity = ~0.3 for X, Y, and Z
_mlx90393->setGain(MLX90393_GAIN_1X);
_mlx90393->setResolution(MLX90393_X, MLX90393_RES_17);
_mlx90393->setResolution(MLX90393_Y, MLX90393_RES_17);
_mlx90393->setResolution(MLX90393_Z, MLX90393_RES_16);
// Use maximum oversampling for better precision
_mlx90393->setOversampling(MLX90393_OSR_3);
// Use decent filtering to get better precision with fast sampling
_mlx90393->setFilter(MLX90393_FILTER_5);
if (!magnetometer)
magnetometer = _mlx90393;
return true;
}
delete _mlx90393;
return false;
}
/**************************************************************************/
/*!
@brief Detect if we have a valid MMC5603 sensor attached and sets
the default magnetometer sensor if so
@return True if found
*/
/**************************************************************************/
bool Adafruit_SensorLab::detectMMC5603(void) {
bool addr30 = scanI2C(0x30);
if (!addr30) {
return false; // no I2C device that could possibly work found!
}
_mmc5603 = new Adafruit_MMC5603(12345);
if ((addr30 && _mmc5603->begin(0x30, _i2c))) {
// yay found a MMC5603
Serial.println(F("Found a MMC5603 IMU"));
if (!magnetometer)
magnetometer = _mmc5603;
return true;
}
delete _mmc5603;
return false;
}
/**************************************************************************/
/*!
@brief Detect if we have a valid MPU6050 sensor attached and sets
the default temperature, accelerometer and gyroscope sensors if so
@return True if found
*/
/**************************************************************************/
bool Adafruit_SensorLab::detectMPU6050(void) {
bool addr68 = scanI2C(0x68);
bool addr69 = scanI2C(0x69);
if (!addr68 && !addr69) {
return false; // no I2C device that could possibly work found!
}
_mpu6050 = new Adafruit_MPU6050();
if ((addr68 && _mpu6050->begin(0x68, _i2c)) ||
(addr69 && _mpu6050->begin(0x69, _i2c))) {
// yay found an MPU6050
Serial.println(F("Found an MPU6050 IMU"));
if (!accelerometer)
accelerometer = _mpu6050->getAccelerometerSensor();
if (!gyroscope)
gyroscope = _mpu6050->getGyroSensor();
if (!temperature) {
temperature = _mpu6050->getTemperatureSensor();
}
return true;
}
delete _mpu6050;
return false;
}
/**************************************************************************/
/*!
@brief Detect if we have a valid MSA301 sensor attached and sets
the default accelerometer sensor if so
@return True if found
*/
/**************************************************************************/
bool Adafruit_SensorLab::detectMSA301(void) {
bool addr26 = scanI2C(0x26);
if (!addr26) {
return false; // no I2C device that could possibly work found!
}
_msa301 = new Adafruit_MSA301();
if ((addr26 && _msa301->begin(0x26, _i2c))) {
// yay found a MSA301
Serial.println(F("Found a MSA301 accelerometer"));
accelerometer = _msa301;
return true;
}
delete _msa301;
return false;
}
/**************************************************************************/
/*!
@brief Detect if we have a valid BMP280 sensor attached and sets
the default pressure and temperature sensor if so
@return True if found
*/
/**************************************************************************/
bool Adafruit_SensorLab::detectBMP280(void) {
bool addr77 = scanI2C(0x77);
bool addr76 = scanI2C(0x76);
if (!addr77 && !addr76) {
return false; // no I2C device that could possibly work found!
}
_bmp280 = new Adafruit_BMP280(_i2c);
if ((addr77 && _bmp280->begin(0x77)) || (addr76 && _bmp280->begin(0x76))) {
// yay found a BMP280
Serial.println(F("Found a BMP280 Pressure sensor"));
if (!pressure)
pressure = _bmp280->getPressureSensor();
if (!temperature)
temperature = _bmp280->getTemperatureSensor();
return true;
}
delete _bmp280;
return false;
}
/**************************************************************************/
/*!
@brief Detect if we have a valid LPS25 sensor attached and sets
the default pressure and temperature sensor if so
@return True if found
*/
/**************************************************************************/
bool Adafruit_SensorLab::detectLPS2X(void) {
bool addr5d = scanI2C(0x5D);
bool addr5c = scanI2C(0x5C);
if (!addr5d && !addr5c) {
return false; // no I2C device that could possibly work found!
}
_lps2x = new Adafruit_LPS25();
if ((addr5d && _lps2x->begin_I2C(0x5D, _i2c)) ||
(addr5c && _lps2x->begin_I2C(0x5C, _i2c))) {
// yay found a LPS2X
Serial.println(F("Found a LPS25 Pressure sensor"));
if (!pressure)
pressure = _lps2x->getPressureSensor();
if (!temperature)
temperature = _lps2x->getTemperatureSensor();
return true;
}
delete _lps2x;
return false;
}
/**************************************************************************/
/*!
@brief Detect if we have a valid DPS310 sensor attached and sets
the default pressure and temperature sensor if so
@return True if found
*/
/**************************************************************************/
bool Adafruit_SensorLab::detectDPS310(void) {
bool addr77 = scanI2C(0x77);
bool addr76 = scanI2C(0x76);
if (!addr77 && !addr76) {
return false; // no I2C device that could possibly work found!
}
Adafruit_DPS310 *_dps310 = new Adafruit_DPS310();
if ((addr77 && _dps310->begin_I2C(0x77, _i2c)) ||
(addr76 && _dps310->begin_I2C(0x76, _i2c))) {
// yay found a DPS310
Serial.println(F("Found a DPS310 Pressure sensor"));
if (!pressure)
pressure = _dps310->getPressureSensor();
if (!temperature)
temperature = _dps310->getTemperatureSensor();
return true;
}
delete _dps310;
return false;
}
/**************************************************************************/
/*!
@brief Detect if we have a valid BME280 sensor attached and sets
the default humidity, pressure and temperature sensor if so
@return True if found
*/
/**************************************************************************/
bool Adafruit_SensorLab::detectBME280(void) {
bool addr77 = scanI2C(0x77);
bool addr76 = scanI2C(0x76);
Serial.println("Looking for BME280");
if (!addr77 && !addr76) {
return false; // no I2C device that could possibly work found!
}
_bme280 = new Adafruit_BME280();
if ((addr77 && _bme280->begin(0x77, _i2c)) ||
(addr76 && _bme280->begin(0x76, _i2c))) {
// yay found a BME280
Serial.println(F("Found a BME280 Pressure+Humidity sensor"));
if (!pressure)
pressure = _bme280->getPressureSensor();
if (!humidity)
humidity = _bme280->getHumiditySensor();
if (!temperature)
temperature = _bme280->getTemperatureSensor();
return true;
}
delete _bme280;
return false;
}
/**************************************************************************/
/*!
@brief Look for any known accelerometer-providing sensor on I2C
@return A pointer to the Adafruit_Sensor device that can be queried
for sensor events. NULL on failure to find any matching sensor.
*/
/**************************************************************************/
Adafruit_Sensor *Adafruit_SensorLab::getAccelerometer(void) {
if (accelerometer) {
return accelerometer; // we already did this process
}
if (detectADXL34X() || detectLSM6DS33() || detectLSM6DS3TRC() ||
detectLSM6DSOX() || detectFXOS8700() || detectICM20649() ||
detectISM330DHCX() || detectMPU6050() || detectMSA301() ||
detectLSM303A() || detectLSM9DS1() || detectLSM9DS0()) {
return accelerometer;
}
// Nothing detected
return NULL;
}
/**************************************************************************/
/*!
@brief Look for any known magnetometer-providing sensor on I2C
@return A pointer to the Adafruit_Sensor device that can be queried
for sensor events. NULL on failure to find any matching sensor.
*/
/**************************************************************************/
Adafruit_Sensor *Adafruit_SensorLab::getMagnetometer(void) {
if (magnetometer) {
return magnetometer; // we already did this process
}
if (detectLIS3MDL() || detectLIS2MDL() || detectFXOS8700() ||
detectLSM9DS1() || detectLSM9DS0() || detectMLX90393() ||
detectMMC5603()) {
return magnetometer;
}
// Nothing detected
return NULL;
}
/**************************************************************************/
/*!
@brief Look for any known gyroscope-providing sensor on I2C
@return A pointer to the Adafruit_Sensor device that can be queried
for sensor events. NULL on failure to find any matching sensor.
*/
/**************************************************************************/
Adafruit_Sensor *Adafruit_SensorLab::getGyroscope(void) {
if (gyroscope) {
return gyroscope; // we already did this process
}
if (detectLSM6DS33() || detectLSM6DS3TRC() || detectLSM6DSOX() ||
detectICM20649() || detectISM330DHCX() || detectFXAS21002() ||
detectMPU6050() || detectLSM9DS1() || detectLSM9DS0()) {
return gyroscope;
}
// Nothing detected
return NULL;
}
/**************************************************************************/
/*!
@brief Look for any known barometric pressure-providing sensor on I2C
@return A pointer to the Adafruit_Sensor device that can be queried
for sensor events. NULL on failure to find any matching sensor.
*/
/**************************************************************************/
Adafruit_Sensor *Adafruit_SensorLab::getPressureSensor(void) {
if (pressure) {
return pressure; // we already did this process
}
if (detectBMP280() || detectBME280() || detectDPS310() || detectLPS2X()) {
return pressure;
}
// Nothing detected
return NULL;
}
/**************************************************************************/
/*!
@brief Look for any known temperature-providing sensor on I2C
@return A pointer to the Adafruit_Sensor device that can be queried
for sensor events. NULL on failure to find any matching sensor.
*/
/**************************************************************************/
Adafruit_Sensor *Adafruit_SensorLab::getTemperatureSensor(void) {
if (temperature) {
return temperature; // we already did this process
}
if (detectBMP280() || detectBME280() || detectDPS310() || detectLPS2X() ||
detectAHTX0()) {
return temperature;
}
// Nothing detected
return NULL;
}
/**************************************************************************/
/*!
@brief Look for any known humidity-providing sensor on I2C
@return A pointer to the Adafruit_Sensor device that can be queried
for sensor events. NULL on failure to find any matching sensor.
*/
/**************************************************************************/
Adafruit_Sensor *Adafruit_SensorLab::getHumiditySensor(void) {
if (humidity) {
return humidity; // we already did this process
}
if (detectBME280() || detectHTS221() || detectAHTX0()) {
return humidity;
}
// Nothing detected
return NULL;
}
/*!
* @brief Calculates the approximate altitude using barometric pressure and the
* supplied 'zero' point (e.g. sea level) hPa as a reference.
* @param currentPressure_hPa The pressure at your current location
* @param originPressure_hPa The pressure at your 'zero' point
* @return The approximate altitude in meters.
*/
float Adafruit_SensorLab::calculateAltitude(float currentPressure_hPa,
float originPressure_hPa) {
return 44330 * (1.0 - pow(currentPressure_hPa / originPressure_hPa, 0.1903));
}
bool Adafruit_SensorLab::scanI2C(uint8_t addr) {
yield();
// A basic scanner, see if it ACK's
_i2c->beginTransmission(addr);
bool f = (_i2c->endTransmission() == 0);
if (f) {
// Serial.print("Found addr 0x");
// Serial.println(addr, HEX);
}
return f;
}