Skip to content

Commit f44306a

Browse files
committed
ModulinoHub: port to non C011 modulinos
1 parent 342fb0f commit f44306a

File tree

1 file changed

+98
-7
lines changed

1 file changed

+98
-7
lines changed

src/Modulino.h

Lines changed: 98 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ class Module : public Printable {
192192
uint8_t address;
193193
uint8_t pinstrap_address;
194194
char* name;
195+
protected:
195196
ModulinoHubPort* hubPort = nullptr;
196197
};
197198

@@ -528,27 +529,46 @@ class ModulinoMovement : public Module {
528529
ModulinoMovement(ModulinoHubPort* hubPort = nullptr)
529530
: Module(0xFF, "MOVEMENT", hubPort) {}
530531
bool begin() {
532+
if (hubPort != nullptr) {
533+
hubPort->select();
534+
}
531535
if (_imu == nullptr) {
532536
_imu = new LSM6DSOXClass(*((TwoWire*)getWire()), 0x6A);
533537
}
534538
initialized = _imu->begin();
535539
__increaseI2CPriority();
540+
if (hubPort != nullptr) {
541+
hubPort->clear();
542+
}
536543
return initialized != 0;
537544
}
538545
operator bool() {
539546
return (initialized != 0);
540547
}
541548
int update() {
542549
if (initialized) {
550+
if (hubPort != nullptr) {
551+
hubPort->select();
552+
}
543553
int accel = _imu->readAcceleration(x, y, z);
544554
int gyro = _imu->readGyroscope(roll, pitch, yaw);
555+
if (hubPort != nullptr) {
556+
hubPort->clear();
557+
}
545558
return accel && gyro;
546559
}
547560
return 0;
548561
}
549562
int available() {
550563
if (initialized) {
551-
return _imu->accelerationAvailable() && _imu->gyroscopeAvailable();
564+
if (hubPort != nullptr) {
565+
hubPort->select();
566+
}
567+
auto ret = _imu->accelerationAvailable() && _imu->gyroscopeAvailable();
568+
if (hubPort != nullptr) {
569+
hubPort->clear();
570+
}
571+
return ret;
552572
}
553573
return 0;
554574
}
@@ -582,25 +602,45 @@ class ModulinoThermo: public Module {
582602
ModulinoThermo(ModulinoHubPort* hubPort = nullptr)
583603
: Module(0xFF, "THERMO", hubPort) {}
584604
bool begin() {
605+
if (hubPort != nullptr) {
606+
hubPort->select();
607+
}
585608
if (_sensor == nullptr) {
586609
_sensor = new HS300xClass(*((TwoWire*)getWire()));
587610
}
588611
initialized = _sensor->begin();
589612
__increaseI2CPriority();
613+
if (hubPort != nullptr) {
614+
hubPort->clear();
615+
}
590616
return initialized;
591617
}
592618
operator bool() {
593619
return (initialized != 0);
594620
}
595621
float getHumidity() {
596622
if (initialized) {
597-
return _sensor->readHumidity();
623+
if (hubPort != nullptr) {
624+
hubPort->select();
625+
}
626+
auto ret = _sensor->readHumidity();
627+
if (hubPort != nullptr) {
628+
hubPort->clear();
629+
}
630+
return ret;
598631
}
599632
return 0;
600633
}
601634
float getTemperature() {
602635
if (initialized) {
603-
return _sensor->readTemperature();
636+
if (hubPort != nullptr) {
637+
hubPort->select();
638+
}
639+
auto ret = _sensor->readTemperature();
640+
if (hubPort != nullptr) {
641+
hubPort->clear();
642+
}
643+
return ret;
604644
}
605645
return 0;
606646
}
@@ -614,6 +654,9 @@ class ModulinoPressure : public Module {
614654
ModulinoPressure(ModulinoHubPort* hubPort = nullptr)
615655
: Module(0xFF, "PRESSURE", hubPort) {}
616656
bool begin() {
657+
if (hubPort != nullptr) {
658+
hubPort->select();
659+
}
617660
if (_barometer == nullptr) {
618661
_barometer = new LPS22HBClass(*((TwoWire*)getWire()));
619662
}
@@ -623,20 +666,37 @@ class ModulinoPressure : public Module {
623666
getWire()->begin();
624667
}
625668
__increaseI2CPriority();
669+
if (hubPort != nullptr) {
670+
hubPort->clear();
671+
}
626672
return initialized != 0;
627673
}
628674
operator bool() {
629675
return (initialized != 0);
630676
}
631677
float getPressure() {
632678
if (initialized) {
633-
return _barometer->readPressure();
679+
if (hubPort != nullptr) {
680+
hubPort->select();
681+
}
682+
auto ret = _barometer->readPressure();
683+
if (hubPort != nullptr) {
684+
hubPort->clear();
685+
}
686+
return ret;
634687
}
635688
return 0;
636689
}
637690
float getTemperature() {
638691
if (initialized) {
639-
return _barometer->readTemperature();
692+
if (hubPort != nullptr) {
693+
hubPort->select();
694+
}
695+
auto ret = _barometer->readTemperature();
696+
if (hubPort != nullptr) {
697+
hubPort->clear();
698+
}
699+
return ret;
640700
}
641701
return 0;
642702
}
@@ -650,19 +710,31 @@ class ModulinoLight : public Module {
650710
ModulinoLight(ModulinoHubPort* hubPort = nullptr)
651711
: Module(0xFF, "LIGHT", hubPort) {}
652712
bool begin() {
713+
if (hubPort != nullptr) {
714+
hubPort->select();
715+
}
653716
if (_light == nullptr) {
654717
_light = new LTR381RGBClass(*((TwoWire*)getWire()), 0x53);
655718
}
656719
initialized = _light->begin();
657720
__increaseI2CPriority();
721+
if (hubPort != nullptr) {
722+
hubPort->clear();
723+
}
658724
return initialized != 0;
659725
}
660726
operator bool() {
661727
return (initialized != 0);
662728
}
663729
bool update() {
664730
if (initialized) {
665-
return _light->readAllSensors(r, g, b, rawlux, lux, ir);
731+
if (hubPort != nullptr) {
732+
hubPort->select();
733+
}
734+
auto ret = _light->readAllSensors(r, g, b, rawlux, lux, ir);
735+
if (hubPort != nullptr) {
736+
hubPort->clear();
737+
}
666738
}
667739
return 0;
668740
}
@@ -806,9 +878,16 @@ class ModulinoDistance : public Module {
806878
ModulinoDistance(ModulinoHubPort* hubPort = nullptr)
807879
: Module(0xFF, "DISTANCE", hubPort) {}
808880
bool begin() {
881+
882+
if (hubPort != nullptr) {
883+
hubPort->select();
884+
}
809885
// try scanning for 0x29 since the library contains a while(true) on begin()
810886
getWire()->beginTransmission(0x29);
811887
if (getWire()->endTransmission() != 0) {
888+
if (hubPort != nullptr) {
889+
hubPort->clear();
890+
}
812891
return false;
813892
}
814893
tof_sensor = new VL53L4CD((TwoWire*)getWire(), -1);
@@ -823,6 +902,9 @@ class ModulinoDistance : public Module {
823902
} else {
824903
delete tof_sensor_alt;
825904
tof_sensor_alt = nullptr;
905+
if (hubPort != nullptr) {
906+
hubPort->clear();
907+
}
826908
return false;
827909
}
828910
} else {
@@ -832,6 +914,9 @@ class ModulinoDistance : public Module {
832914
__increaseI2CPriority();
833915
api->setRangeTiming(20, 0);
834916
api->startRanging();
917+
if (hubPort != nullptr) {
918+
hubPort->clear();
919+
}
835920
return true;
836921
}
837922
operator bool() {
@@ -841,13 +926,19 @@ class ModulinoDistance : public Module {
841926
if (api == nullptr) {
842927
return false;
843928
}
844-
929+
930+
if (hubPort != nullptr) {
931+
hubPort->select();
932+
}
845933
uint8_t NewDataReady = 0;
846934
api->checkForDataReady(&NewDataReady);
847935
if (NewDataReady) {
848936
api->clearInterrupt();
849937
api->getResult(&results);
850938
}
939+
if (hubPort != nullptr) {
940+
hubPort->clear();
941+
}
851942
if (results.range_status == 0) {
852943
internal = results.distance_mm;
853944
} else {

0 commit comments

Comments
 (0)