-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathcontroller.cpp
More file actions
1488 lines (1401 loc) · 54.4 KB
/
controller.cpp
File metadata and controls
1488 lines (1401 loc) · 54.4 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
/*!
* @file src/components/i2c/controller.cpp
*
* Controller for the i2c.proto API
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Copyright (c) Brent Rubell 2025 for Adafruit Industries.
*
* BSD license, all text here must be included in any redistribution.
*
*/
#include "controller.h"
#include "drivers/drvBase.h"
#include "drivers/drvOutputBase.h"
/*!
@brief Lambda function to create a drvBase driver instance
@param i2c
The desired I2C interface.
@param addr
The desired i2c device address.
@param mux_channel
The desired I2C multiplexer channel.
@param driver_name
The i2c driver's name.
*/
using FnCreateI2CSensorDriver =
std::function<drvBase *(TwoWire *, uint16_t, uint32_t, const char *)>;
// Factory for creating a new I2C SENSOR drivers
// NOTE: When you add a new SENSOR driver, make sure to add it to the factory!
static const std::map<std::string, FnCreateI2CSensorDriver> I2cFactorySensor = {
{"adt7410",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvAdt7410(i2c, addr, mux_channel, driver_name);
}},
{"aht20",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvAhtx0(i2c, addr, mux_channel, driver_name);
}},
{"am2301b",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvAhtx0(i2c, addr, mux_channel, driver_name);
}},
{"am2315c",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvAhtx0(i2c, addr, mux_channel, driver_name);
}},
{"as5600",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvAs5600(i2c, addr, mux_channel, driver_name);
}},
{"dht20",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvAhtx0(i2c, addr, mux_channel, driver_name);
}},
{"bh1750",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvBh1750(i2c, addr, mux_channel, driver_name);
}},
{"bme280",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvBme280(i2c, addr, mux_channel, driver_name);
}},
{"bme680",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvBme680(i2c, addr, mux_channel, driver_name);
}},
{"bme688",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvBme680(i2c, addr, mux_channel, driver_name);
}},
{"bmp388",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvBmp3xx(i2c, addr, mux_channel, driver_name);
}},
{"bmp390",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvBmp3xx(i2c, addr, mux_channel, driver_name);
}},
{"bmp280",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvBmp280(i2c, addr, mux_channel, driver_name);
}},
{"dps310",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvDps310(i2c, addr, mux_channel, driver_name);
}},
{"d6t1a",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvD6t1a(i2c, addr, mux_channel, driver_name);
}},
{"ds2484",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvDs2484(i2c, addr, mux_channel, driver_name);
}},
{"ens160",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvEns160(i2c, addr, mux_channel, driver_name);
}},
{"ens161",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvEns160(i2c, addr, mux_channel, driver_name);
}},
{"hdc302x",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvHdc302x(i2c, addr, mux_channel, driver_name);
}},
{"hts221",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvHts221(i2c, addr, mux_channel, driver_name);
}},
{"htu21d",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvHtu21d(i2c, addr, mux_channel, driver_name);
}},
{"ina219",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvIna219(i2c, addr, mux_channel, driver_name);
}},
{"ina228",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvIna228(i2c, addr, mux_channel, driver_name);
}},
{"ina237",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvIna237(i2c, addr, mux_channel, driver_name);
}},
{"ina238",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvIna238(i2c, addr, mux_channel, driver_name);
}},
{"ina260",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvIna260(i2c, addr, mux_channel, driver_name);
}},
{"lc709203f",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvLc709203f(i2c, addr, mux_channel, driver_name);
}},
{"lps3xhw",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvLps3xhw(i2c, addr, mux_channel, driver_name);
}},
{"lps22hb",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvLps22hb(i2c, addr, mux_channel, driver_name);
}},
{"lps25hb",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvLps25hb(i2c, addr, mux_channel, driver_name);
}},
{"ltr329",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvLtr329_Ltr303(i2c, addr, mux_channel, driver_name);
}},
{"ltr303",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvLtr329_Ltr303(i2c, addr, mux_channel, driver_name);
}},
{"ltr390",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvLtr390(i2c, addr, mux_channel, driver_name);
}},
{"max17048",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvMax1704x(i2c, addr, mux_channel, driver_name);
}},
{"mcp3421",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvMax1704x(i2c, addr, mux_channel, driver_name);
}},
{"mcp9808",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvMcp9808(i2c, addr, mux_channel, driver_name);
}},
{"mlx90632b",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvMLX90632(i2c, addr, mux_channel, driver_name);
}},
{"mlx90632d_med",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvMLX90632(i2c, addr, mux_channel, driver_name);
}},
{"mlx90632d_ext",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
drvMLX90632 *drv = new drvMLX90632(i2c, addr, mux_channel, driver_name);
drv->ConfigureAndPrintSensorInfo(true);
return drv;
}},
{"mpl115a2",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvMpl115a2(i2c, addr, mux_channel, driver_name);
}},
{"mprls",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvMprls(i2c, addr, mux_channel, driver_name);
}},
{"ms8607",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvMs8607(i2c, addr, mux_channel, driver_name);
}},
{"nau7802",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvNau7802(i2c, addr, mux_channel, driver_name);
}},
{"pct2075",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvPct2075(i2c, addr, mux_channel, driver_name);
}},
{"pmsa003i",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvPm25(i2c, addr, mux_channel, driver_name);
}},
{"qmc5883p",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvQmc5883p(i2c, addr, mux_channel, driver_name);
}},
{"scd40",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvScd4x(i2c, addr, mux_channel, driver_name);
}},
{"scd41",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvScd4x(i2c, addr, mux_channel, driver_name);
}},
{"scd30",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvScd30(i2c, addr, mux_channel, driver_name);
}},
{"sgp40",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvSgp40(i2c, addr, mux_channel, driver_name);
}},
{"sht3x",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvSht3x(i2c, addr, mux_channel, driver_name);
}},
{"sht30_shell",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvSht3x(i2c, addr, mux_channel, driver_name);
}},
{"sht30_mesh",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvSht3x(i2c, addr, mux_channel, driver_name);
}},
{"sht40",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvSht4x(i2c, addr, mux_channel, driver_name);
}},
{"sht41",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvSht4x(i2c, addr, mux_channel, driver_name);
}},
{"sht45",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvSht4x(i2c, addr, mux_channel, driver_name);
}},
{"sen5x",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvSen5x(i2c, addr, mux_channel, driver_name);
}},
{"sen55",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvSen5x(i2c, addr, mux_channel, driver_name);
}},
{"sen54",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvSen5x(i2c, addr, mux_channel, driver_name);
}},
{"sen50",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvSen5x(i2c, addr, mux_channel, driver_name);
}},
{"shtc3",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvShtc3(i2c, addr, mux_channel, driver_name);
}},
{"si7021",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvSi7021(i2c, addr, mux_channel, driver_name);
}},
{"stemma_soil",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvSen5x(i2c, addr, mux_channel, driver_name);
}},
{"tmp117",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvTmp117(i2c, addr, mux_channel, driver_name);
}},
{"tsl2591",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvTsl2591(i2c, addr, mux_channel, driver_name);
}},
{"vncl4020",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvVncl4020(i2c, addr, mux_channel, driver_name);
}},
{"veml7700",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvVeml7700(i2c, addr, mux_channel, driver_name);
}},
{"vncl4040",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvVncl4040(i2c, addr, mux_channel, driver_name);
}},
{"vl53l0x",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvVl53l0x(i2c, addr, mux_channel, driver_name);
}},
{"vl53l1x",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvVl53l1x(i2c, addr, mux_channel, driver_name);
}},
{"vl53l4cd",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvVl53l4cd(i2c, addr, mux_channel, driver_name);
}},
{"vl53l4cx",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvVl53l4cx(i2c, addr, mux_channel, driver_name);
}},
{"vl6180x",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvBase * {
return new drvVl6180x(i2c, addr, mux_channel, driver_name);
}}}; ///< I2C driver factory
static const std::unordered_map<uint16_t, std::vector<const char *>>
map_address_to_drivers = {
{0x0A, {"d6t1a"}},
{0x0B, {"lc709203f"}},
{0x12, {"pmsa003i"}},
{0x13, {"vncl4020"}},
{0x18, {"ds2484", "mcp9808", "mprls"}},
{0x19, {"mcp9808"}},
{0x1A, {"mcp9808"}},
{0x1B, {"mcp9808"}},
{0x1C, {"mcp9808"}},
{0x1D, {"mcp9808"}},
{0x1E, {"mcp9808"}},
{0x1F, {"mcp9808"}},
{0x23, {"bh1750"}},
{0x28, {"pct2075"}},
{0x29,
{"ltr303", "pct2075", "tsl2591", "veml7700", "vl53l1x", "vl53l4cd",
"vl53l4cx", "vl6180x"}},
{0x2A, {"nau7802"}},
{0x2C, {"qmc5883p"}},
{0x36, {"as5600"}},
{0x38, {"aht20", "max17048"}},
{0x39, {"tsl2591"}},
{0x3A, {"mlx90632"}},
{0x3B, {"mlx90632"}},
{0x40,
{"htu21d", "htu31d", "ina219", "ina228", "ina237", "ina238", "ina260",
"ms8607", "si7021", "stemma_soil"}},
{0x41, {"htu31d", "ina219", "ina228", "ina237", "ina238", "ina260"}},
{0x44,
{"hdc302x", "ina228", "ina237", "ina238", "ina260", "sht3x", "sht4x"}},
{0x45, {"hdc302x", "ina228", "ina237", "ina238", "ina260", "sht3x"}},
{0x46, {"hdc302x"}},
{0x47, {"hdc302x"}},
{0x48, {"adt7410", "pct2075", "tmp117"}},
{0x49, {"adt7410", "pct2075", "tmp117", "tsl2591"}},
{0x4A, {"adt7410", "pct2075", "tmp117"}},
{0x4B, {"adt7410", "pct2075", "tmp117"}},
{0x4C, {"pct2075"}},
{0x4D, {"pct2075"}},
{0x4E, {"pct2075"}},
{0x4F, {"pct2075"}},
{0x51, {"vcnl4200"}},
{0x52, {"ens161", "ens160"}},
{0x53, {"ens161", "ens160", "ltr390"}},
{0x58, {"sgp30"}},
{0x59, {"sgp40"}},
{0x5C,
{"bh1750", "lps22hb", "lps25hb", "lps28dfw", "lps33hw", "lps35hw"}},
{0x5D, {"lps22hb", "lps25hb", "lps28dfw", "lps33hw", "lps35hw"}},
{0x5F, {"hts2221"}},
{0x60, {"mpl115a2", "vncl4040"}},
{0x61, {"scd30"}},
{0x62, {"scd40"}},
{0x68, {"mcp3421"}},
{0x69, {"sen55"}},
{0x6B, {"sen66"}},
{0x70, {"pct2075", "shtc3"}},
{0x71, {"pct2075"}},
{0x72, {"pct2075"}},
{0x73, {"pct2075"}},
{0x74, {"pct2075"}},
{0x75, {"pct2075"}},
{0x76,
{"bme280", "bme680", "bmp280", "bmp388", "bmp390", "dps310", "ms8607",
"pct2075"}},
{0x77,
{"bme280", "bme680", "bmp280", "bmp388", "bmp390", "dps310",
"pct2075"}}}; ///< I2C address to driver map
/*!
@brief Lambda function to create a drvOutputBase instance
@param i2c
The desired I2C interface.
@param addr
The desired i2c device address.
@param mux_channel
The desired I2C multiplexer channel.
@param driver_name
The i2c output driver's name.
*/
using FnCreateI2cOutputDrv =
std::function<drvOutputBase *(TwoWire *, uint16_t, uint32_t, const char *)>;
// Factory for creating a new i2c OUTPUT driver
// NOTE: When adding a new OUTPUT driver, make sure to add it to the map below!
static const std::map<std::string, FnCreateI2cOutputDrv> I2cFactoryOutput = {
{"quadalphanum",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvOutputBase * {
return new drvOutQuadAlphaNum(i2c, addr, mux_channel, driver_name);
}},
{"7seg",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvOutputBase * {
return new drvOut7Seg(i2c, addr, mux_channel, driver_name);
}},
{"charlcd",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvOutputBase * {
return new drvOutCharLcd(i2c, addr, mux_channel, driver_name);
}},
{"ssd1306",
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
const char *driver_name) -> drvOutputBase * {
return new drvOutSsd1306(i2c, addr, mux_channel, driver_name);
}}}; ///< I2C output driver factory
/*!
@brief Creates an I2C driver by name
@param driver_name
The name of the I2C driver.
@param i2c
The I2C bus.
@param addr
The I2C device address.
@param i2c_mux_channel
The I2C MUX channel.
@param status
The I2cDeviceStatus message.
@returns A pointer to the I2C driver.
*/
drvBase *CreateI2cSensorDrv(const char *driver_name, TwoWire *i2c,
uint16_t addr, uint32_t i2c_mux_channel,
wippersnapper_i2c_I2cDeviceStatus &status) {
auto it = I2cFactorySensor.find(driver_name);
if (it == I2cFactorySensor.end()) {
status =
wippersnapper_i2c_I2cDeviceStatus_I2C_DEVICE_STATUS_FAIL_UNSUPPORTED_SENSOR;
return nullptr;
}
status = wippersnapper_i2c_I2cDeviceStatus_I2C_DEVICE_STATUS_SUCCESS;
return it->second(i2c, addr, i2c_mux_channel, driver_name);
}
/*!
@brief Creates an I2C driver by name
@param driver_name
The name of the I2C driver.
@param i2c
The I2C bus.
@param addr
The I2C device address.
@param i2c_mux_channel
The I2C MUX channel.
@param status
The I2cDeviceStatus message.
@returns A pointer to the I2C driver.
*/
drvOutputBase *CreateI2cOutputDrv(const char *driver_name, TwoWire *i2c,
uint16_t addr, uint32_t i2c_mux_channel,
wippersnapper_i2c_I2cDeviceStatus &status) {
auto it = I2cFactoryOutput.find(driver_name);
if (it == I2cFactoryOutput.end()) {
status =
wippersnapper_i2c_I2cDeviceStatus_I2C_DEVICE_STATUS_FAIL_UNSUPPORTED_SENSOR;
return nullptr;
}
status = wippersnapper_i2c_I2cDeviceStatus_I2C_DEVICE_STATUS_SUCCESS;
return it->second(i2c, addr, i2c_mux_channel, driver_name);
}
/***********************************************************************/
/*!
@brief Obtains possible candidate drivers for a given I2C address.
@param addr
The desired I2C address.
@returns A vector of pointers to candidate drivers.
*/
/***********************************************************************/
std::vector<const char *> GetDriversForAddress(uint16_t addr) {
std::vector<const char *> candidates;
std::unordered_map<uint16_t, std::vector<const char *>>::const_iterator
candidate = map_address_to_drivers.find(addr);
if (candidate != map_address_to_drivers.end()) {
candidates = candidate->second;
}
return candidates;
}
/*!
@brief I2cController constructor
*/
I2cController::I2cController() {
_i2c_model = new I2cModel();
_i2c_output_model = new I2cOutputModel();
_i2c_bus_default = new I2cHardware();
}
/*!
@brief I2cController destructor
*/
I2cController::~I2cController() {
if (_i2c_model)
delete _i2c_model;
if (_i2c_output_model)
delete _i2c_output_model;
if (_i2c_bus_default)
delete _i2c_bus_default;
}
/*!
@brief Removes an I2C driver from the controller and frees memory
@param address
The desired I2C device's address.
@param is_output_device
True if the driver is an output device, False otherwise.
@returns True if the driver was removed, False otherwise.
*/
bool I2cController::RemoveDriver(uint32_t address, bool is_output_device) {
if (!is_output_device) {
// Safely remove the i2c sensor driver from the vector and free memory
for (drvBase *driver : _i2c_drivers) {
if (driver == nullptr)
continue;
if (driver->GetAddress() != address)
continue;
auto it = std::find(_i2c_drivers.begin(), _i2c_drivers.end(), driver);
if (it != _i2c_drivers.end()) {
_i2c_drivers.erase(it);
}
delete driver;
return true;
}
} else {
// This was an output driver type, safely remove the i2c output driver from
// the vector and free memory
for (drvOutputBase *driver : _i2c_drivers_output) {
if (driver == nullptr)
continue;
if (driver->GetAddress() != address)
continue;
auto it = std::find(_i2c_drivers_output.begin(),
_i2c_drivers_output.end(), driver);
if (it != _i2c_drivers_output.end()) {
_i2c_drivers_output.erase(it);
}
delete driver;
return true;
}
}
// We didn't find the driver to remove
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to find driver to remove!");
return false;
}
/*!
@brief Returns if the I2C bus has been created successfully.
@param is_alt_bus
True if the alt. I2C bus is being queried, False otherwise.
@returns True if the I2C bus has already been created, False otherwise.
*/
bool I2cController::IsBusStatusOK(bool is_alt_bus) {
bool is_ok = false;
if (is_alt_bus) {
is_ok = (_i2c_bus_alt->GetBusStatus() ==
wippersnapper_i2c_I2cBusStatus_I2C_BUS_STATUS_SUCCESS);
} else {
is_ok = (_i2c_bus_default->GetBusStatus() ==
wippersnapper_i2c_I2cBusStatus_I2C_BUS_STATUS_SUCCESS);
}
return is_ok;
}
/*!
@brief Publishes an I2cDeviceAddedorReplaced message to the broker
@param device_descriptor
The I2cDeviceDescriptor message.
@param device_status
The I2cDeviceStatus message.
@returns True if the I2cDeviceAddedorReplaced message was published
successfully, False otherwise.
*/
bool I2cController::PublishI2cDeviceAddedorReplaced(
const wippersnapper_i2c_I2cDeviceDescriptor &device_descriptor,
const wippersnapper_i2c_I2cDeviceStatus &device_status) {
// If we're in offline mode, don't publish out to IO
if (WsV2._sdCardV2->isModeOffline())
return true; // Back out if we're in offline mode
// Encode the I2cDeviceAddedorReplaced message and publish it to IO
if (!_i2c_model->encodeMsgI2cDeviceAddedorReplaced(
device_descriptor, _i2c_bus_default->GetBusStatus(), device_status)) {
WS_DEBUG_PRINTLN(
"[i2c] ERROR: Unable to encode I2cDeviceAddedorReplaced message!");
return false;
}
if (!WsV2.PublishSignal(
wippersnapper_signal_DeviceToBroker_i2c_device_added_replaced_tag,
_i2c_model->GetMsgI2cDeviceAddedOrReplaced())) {
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to publish I2cDeviceAddedorReplaced "
"message to IO!");
return false;
}
return true;
}
/*!
@brief Implements handling for a I2cDeviceRemove message
@param stream
A pointer to the pb_istream_t stream.
@returns True if the I2cDeviceRemove message was handled, False
otherwise.
*/
bool I2cController::Handle_I2cDeviceRemove(pb_istream_t *stream) {
// Attempt to decode an I2cDeviceRemove message
WS_DEBUG_PRINTLN("[i2c] Decoding I2cDeviceRemove message...");
if (!_i2c_model->DecodeI2cDeviceRemove(stream)) {
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to decode I2cDeviceRemove message!");
return false;
}
// TODO [Online]: Implement the rest of this function
// TODO: Remember - can be on either bus! (default or alt)
// TODO: Remember to handle removal of a mux device or a device on a mux
// strlen(descriptor.i2c_bus_sda) == 0
wippersnapper_i2c_I2cDeviceRemove *msgRemove =
_i2c_model->GetI2cDeviceRemoveMsg();
if (!msgRemove->has_i2c_device_description) {
WS_DEBUG_PRINTLN("[i2c] ERROR: I2cDeviceRemove message missing required "
"device description!");
return false;
}
bool did_remove = true;
// Check for default bus
if (strlen(msgRemove->i2c_device_description.i2c_bus_scl) == 0 &&
strlen(msgRemove->i2c_device_description.i2c_bus_sda) == 0) {
WS_DEBUG_PRINTLN("[i2c] Removing device from default bus...");
if (!_i2c_bus_default->HasMux()) {
if (!RemoveDriver(msgRemove->i2c_device_description.i2c_device_address,
msgRemove->is_output_device)) {
WS_DEBUG_PRINTLN(
"[i2c] ERROR: Failed to remove i2c device from default bus!");
did_remove = false;
}
} else {
// Bus has a I2C MUX attached
// Case 1: Is the I2C device connected to a MUX?
if (msgRemove->i2c_device_description.i2c_mux_address != 0xFFFF &&
msgRemove->i2c_device_description.i2c_mux_channel >= 0) {
_i2c_bus_default->SelectMuxChannel(
msgRemove->i2c_device_description.i2c_mux_channel);
if (!RemoveDriver(msgRemove->i2c_device_description.i2c_device_address,
msgRemove->is_output_device)) {
WS_DEBUG_PRINTLN(
"[i2c] ERROR: Failed to remove i2c device from default bus!");
did_remove = false;
}
}
// Case 2: Is the I2C device a MUX?
if (msgRemove->i2c_device_description.i2c_device_address ==
msgRemove->i2c_device_description.i2c_mux_address) {
wippersnapper_i2c_I2cBusScanned scan_results;
_i2c_bus_default->ScanMux(&scan_results);
for (int i = 0; i < scan_results.i2c_bus_found_devices_count; i++) {
// Select the channel and remove the device
_i2c_bus_default->SelectMuxChannel(
scan_results.i2c_bus_found_devices[i].i2c_mux_channel);
RemoveDriver(scan_results.i2c_bus_found_devices[i].i2c_device_address,
msgRemove->is_output_device);
}
_i2c_bus_default->RemoveMux();
}
}
}
// TODO: Check for Alt. I2C Bus
// Publush with did_remove to the response
return true;
}
/*!
@brief Attempts to initialize a MUX on the bus.
@param name
The name of the MUX to initialize, used to set number
of channels.
@param address
The MUX's I2C address.
@param is_alt_bus
True if the alternative I2C bus is being used, False
otherwise.
@returns True if the MUX was successfully initialized, False
otherwise.
*/
bool I2cController::InitMux(const char *name, uint32_t address,
bool is_alt_bus) {
if (is_alt_bus) {
if (!_i2c_bus_alt->HasMux()) {
if (!_i2c_bus_alt->AddMuxToBus(address, name)) {
return false;
}
}
} else {
if (!_i2c_bus_default->HasMux()) {
if (!_i2c_bus_default->AddMuxToBus(address, name)) {
return false;
}
}
}
// TODO [Online]: Publish back out to IO here!
return true;
}
/*!
@brief Configures the MUX channel on the bus.
@param stream
Pointer to the pb_istream
@returns True if the I2C bus was successfully scanned and the
I2cBusScan message was published to IO, False otherwise.
*/
bool I2cController::Handle_I2cBusScan(pb_istream_t *stream) {
WS_DEBUG_PRINTLN("[i2c] Decoding I2cDeviceAddOrReplace message...");
// Attempt to decode I2cBusScan message
if (!_i2c_model->DecodeI2cBusScan(stream)) {
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to decode I2cBusScan message!");
return false;
}
_i2c_model->ClearI2cBusScanned();
wippersnapper_i2c_I2cBusScanned *scan_results =
_i2c_model->GetI2cBusScannedMsg();
bool scan_success = true;
// Case 1: Scan the default I2C bus
if (_i2c_model->GetI2cBusScanMsg()->scan_default_bus) {
// Was the default bus initialized correctly and ready to scan?
WS_DEBUG_PRINT("Bus State: ");
WS_DEBUG_PRINTLN(_i2c_bus_default->GetBusStatus());
WS_DEBUG_PRINTLN(IsBusStatusOK());
if (IsBusStatusOK()) {
if (!_i2c_bus_default->ScanBus(scan_results)) {
WS_DEBUG_PRINTLN("[i2c] ERROR: Failed to scan default I2C bus!");
scan_success = false;
}
} else {
WS_DEBUG_PRINTLN("[i2c] ERROR: Default I2C bus state is stuck, please "
"reset the board!");
scan_success = false;
}
}
// Case 2: Optionally scan the alternative I2C bus
if (_i2c_model->GetI2cBusScanMsg()->scan_alt_bus) {
// Is the alt bus initialized?
if (_i2c_bus_alt == nullptr) {
_i2c_bus_alt = new I2cHardware(
_i2c_model->GetI2cBusScanMsg()->i2c_alt_bus_descriptor.i2c_bus_sda,
_i2c_model->GetI2cBusScanMsg()->i2c_alt_bus_descriptor.i2c_bus_sda);
// Was the default bus initialized correctly and ready to scan?
if (IsBusStatusOK(true)) {
if (!_i2c_bus_alt->ScanBus(scan_results)) {
WS_DEBUG_PRINTLN("[i2c] ERROR: Failed to scan alt. I2C bus!");
scan_success = false;
}
} else {
WS_DEBUG_PRINTLN("[i2c] ERROR: alt. I2C bus state is stuck, please "
"reset the board!");
scan_success = false;
}
}
}
// Case 3: Optionally scan MUX attached to the default bus
if (_i2c_model->GetI2cBusScanMsg()->scan_default_bus_mux) {
if (_i2c_bus_default->HasMux()) {
if (!_i2c_bus_default->ScanMux(scan_results)) {
WS_DEBUG_PRINTLN("[i2c] ERROR: Failed to scan I2C MUX on default bus!");
scan_success = false;
}
}
}
// Case 4: Optionally scan MUX attached to the alt. bus
if (_i2c_model->GetI2cBusScanMsg()->scan_alt_bus) {
if (_i2c_bus_alt->HasMux()) {
if (!_i2c_bus_alt->ScanMux(scan_results)) {
WS_DEBUG_PRINTLN("[i2c] ERROR: Failed to scan I2C MUX on alt. bus!");
scan_success = false;
}
}
}
// Printout content of scan_results
WS_DEBUG_PRINT("[i2c] Scan found ");
WS_DEBUG_PRINT(scan_results->i2c_bus_found_devices_count);
WS_DEBUG_PRINTLN(" devices.");
for (int i = 0; i < scan_results->i2c_bus_found_devices_count; i++) {
WS_DEBUG_PRINTLN(i);
WS_DEBUG_PRINT("Address: ");
WS_DEBUG_PRINTLN(scan_results->i2c_bus_found_devices[i].i2c_device_address,
HEX);
WS_DEBUG_PRINT("SCL: ");
WS_DEBUG_PRINTLN(scan_results->i2c_bus_found_devices[i].i2c_bus_scl);
WS_DEBUG_PRINT("SDA: ");
WS_DEBUG_PRINTLN(scan_results->i2c_bus_found_devices[i].i2c_bus_sda);
WS_DEBUG_PRINT("MUX Address: ");
WS_DEBUG_PRINTLN(scan_results->i2c_bus_found_devices[i].i2c_mux_address);
WS_DEBUG_PRINT("MUX Channel: ");
WS_DEBUG_PRINTLN(scan_results->i2c_bus_found_devices[i].i2c_mux_channel);
}
// TODO: Encode and publish out to IO!
// TODO: Take scan_success into account here
return true;
}
/*!
@brief Handler for an I2cDeviceOutputWrite message
@param stream
A pointer to the pb_istream_t stream.
@returns True if the callback was successfully executed by the driver,
False otherwise.
*/
bool I2cController::Handle_I2cDeviceOutputWrite(pb_istream_t *stream) {
WS_DEBUG_PRINTLN("[i2c] Decoding I2cDeviceOutputWrite message...");
// Attempt to decode an I2cDeviceOutputWrite message
if (!_i2c_model->DecodeI2cDeviceOutputWrite(stream)) {
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to decode I2cDeviceOutputWrite "
"message!");
return false;
}
wippersnapper_i2c_I2cDeviceDescriptor descriptor =
_i2c_model->GetI2cDeviceOutputWriteMsg()->i2c_device_description;
// Attempt to find the driver
drvOutputBase *driver = nullptr;
for (auto *drv : _i2c_drivers_output) {
if (drv == nullptr)
continue;
if (drv->GetAddress() != descriptor.i2c_device_address)
continue;
driver = drv;
break;
}
if (driver == nullptr) {
WS_DEBUG_PRINT("[i2c] ERROR: Unable to find driver for device at addr 0x");
WS_DEBUG_PRINTLN(descriptor.i2c_device_address, HEX);
return false;
}
// Optionally configure the I2C MUX
uint32_t mux_channel = driver->GetMuxChannel();
WS_DEBUG_PRINTLN(mux_channel);
if (driver->HasMux()) {
ConfigureMuxChannel(mux_channel, driver->HasAltI2CBus());
}
// Determine which driver cb function to use
if (_i2c_model->GetI2cDeviceOutputWriteMsg()->which_output_msg ==
wippersnapper_i2c_I2cDeviceOutputWrite_write_led_backpack_tag) {
WS_DEBUG_PRINTLN("[i2c] Writing to LED backpack...");
driver->WriteMessage(_i2c_model->GetI2cDeviceOutputWriteMsg()
->output_msg.write_led_backpack.message);
} else if (_i2c_model->GetI2cDeviceOutputWriteMsg()->which_output_msg ==
wippersnapper_i2c_I2cDeviceOutputWrite_write_char_lcd_tag) {
WS_DEBUG_PRINTLN("[i2c] Writing to char LCD...");
if (!driver->WriteMessageCharLCD(&_i2c_model->GetI2cDeviceOutputWriteMsg()
->output_msg.write_char_lcd)) {
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to write to char LCD!");
return false;
}
} else if (_i2c_model->GetI2cDeviceOutputWriteMsg()->which_output_msg ==
wippersnapper_i2c_I2cDeviceOutputWrite_write_oled_tag) {
WS_DEBUG_PRINTLN("[i2c] Writing to SSD1306 OLED...");
// Note: In the future, we can expand this to support other OLEDs by
// creating and checking a tag within the write oled msg (e.g. SSD1327,
// etc.)
driver->WriteMessageSSD1306(_i2c_model->GetI2cDeviceOutputWriteMsg()
->output_msg.write_oled.message);
} else {
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to determine I2C Output Write type!");
return false;
}
return true;
}