@@ -56,14 +56,53 @@ class ModulinoClass {
56
56
friend class Module ;
57
57
protected:
58
58
HardwareI2C* _wire;
59
+ friend class ModulinoHub ;
60
+ friend class ModulinoHubPort ;
59
61
};
60
62
61
63
extern ModulinoClass Modulino;
62
64
65
+ // Forward declaration of ModulinoHub
66
+ class ModulinoHub ;
67
+
68
+ class ModulinoHubPort {
69
+ public:
70
+ ModulinoHubPort (int port, ModulinoHub* hub) : _port(port), _hub(hub) {}
71
+ int select ();
72
+ int clear ();
73
+ private:
74
+ int _port;
75
+ ModulinoHub* _hub;
76
+ };
77
+
78
+ class ModulinoHub {
79
+ public:
80
+ ModulinoHub (int address = 0x70 ) : _address(address){ }
81
+ ModulinoHubPort* port (int _port) {
82
+ return new ModulinoHubPort (_port, this );
83
+ }
84
+ int select (int port) {
85
+ Modulino._wire ->beginTransmission (_address);
86
+ Modulino._wire ->write (1 << port);
87
+ return Modulino._wire ->endTransmission ();
88
+ }
89
+ int clear () {
90
+ Modulino._wire ->beginTransmission (_address);
91
+ Modulino._wire ->write ((uint8_t )0 );
92
+ return Modulino._wire ->endTransmission ();
93
+ }
94
+
95
+ int address () {
96
+ return _address;
97
+ }
98
+ private:
99
+ int _address;
100
+ };
101
+
63
102
class Module : public Printable {
64
103
public:
65
- Module (uint8_t address = 0xFF , const char * name = " " )
66
- : address(address), name((char *)name) {}
104
+ Module (uint8_t address = 0xFF , const char * name = " " , ModulinoHubPort* hubPort = nullptr )
105
+ : address(address), name((char *)name), hubPort(hubPort) {}
67
106
virtual ~Module () {}
68
107
bool begin () {
69
108
if (address >= 0x7F ) {
@@ -84,6 +123,9 @@ class Module : public Printable {
84
123
if (address >= 0x7F ) {
85
124
return false ;
86
125
}
126
+ if (hubPort != nullptr ) {
127
+ hubPort->select ();
128
+ }
87
129
Modulino._wire ->requestFrom (address, howmany + 1 );
88
130
auto start = millis ();
89
131
while ((Modulino._wire ->available () == 0 ) && (millis () - start < 100 )) {
@@ -99,17 +141,26 @@ class Module : public Printable {
99
141
while (Modulino._wire ->available ()) {
100
142
Modulino._wire ->read ();
101
143
}
144
+ if (hubPort != nullptr ) {
145
+ hubPort->clear ();
146
+ }
102
147
return true ;
103
148
}
104
149
bool write (uint8_t * buf, int howmany) {
105
150
if (address >= 0x7F ) {
106
151
return false ;
107
152
}
153
+ if (hubPort != nullptr ) {
154
+ hubPort->select ();
155
+ }
108
156
Modulino._wire ->beginTransmission (address);
109
157
for (int i = 0 ; i < howmany; i++) {
110
158
Modulino._wire ->write (buf[i]);
111
159
}
112
160
Modulino._wire ->endTransmission ();
161
+ if (hubPort != nullptr ) {
162
+ hubPort->clear ();
163
+ }
113
164
return true ;
114
165
}
115
166
bool nonDefaultAddress () {
@@ -119,8 +170,14 @@ class Module : public Printable {
119
170
return p.print (name);
120
171
}
121
172
bool scan (uint8_t addr) {
173
+ if (hubPort != nullptr ) {
174
+ hubPort->select ();
175
+ }
122
176
Modulino._wire ->beginTransmission (addr / 2 ); // multply by 2 to match address in fw main.c
123
177
auto ret = Modulino._wire ->endTransmission ();
178
+ if (hubPort != nullptr ) {
179
+ hubPort->clear ();
180
+ }
124
181
if (ret == 0 ) {
125
182
// could also ask for 1 byte and check if it's truely a modulino of that kind
126
183
return true ;
@@ -131,12 +188,15 @@ class Module : public Printable {
131
188
uint8_t address;
132
189
uint8_t pinstrap_address;
133
190
char * name;
191
+ ModulinoHubPort* hubPort = nullptr ;
134
192
};
135
193
136
194
class ModulinoButtons : public Module {
137
195
public:
138
- ModulinoButtons (uint8_t address = 0xFF )
139
- : Module(address, " BUTTONS" ) {}
196
+ ModulinoButtons (uint8_t address = 0xFF , ModulinoHubPort* hubPort = nullptr )
197
+ : Module(address, " BUTTONS" , hubPort) {}
198
+ ModulinoButtons (ModulinoHubPort* hubPort, uint8_t address = 0xFF )
199
+ : Module(address, " BUTTONS" , hubPort) {}
140
200
PinStatus isPressed (int index) {
141
201
return last_status[index] ? HIGH : LOW;
142
202
}
@@ -173,8 +233,10 @@ class ModulinoButtons : public Module {
173
233
174
234
class ModulinoJoystick : public Module {
175
235
public:
176
- ModulinoJoystick (uint8_t address = 0xFF )
177
- : Module(address, " JOYSTICK" ) {}
236
+ ModulinoJoystick (uint8_t address = 0xFF , ModulinoHubPort* hubPort = nullptr )
237
+ : Module(address, " JOYSTICK" , hubPort) {}
238
+ ModulinoJoystick (ModulinoHubPort* hubPort, uint8_t address = 0xFF )
239
+ : Module(address, " JOYSTICK" , hubPort) {}
178
240
bool update () {
179
241
uint8_t buf[3 ];
180
242
auto res = read ((uint8_t *)buf, 3 );
@@ -225,8 +287,10 @@ class ModulinoJoystick : public Module {
225
287
226
288
class ModulinoBuzzer : public Module {
227
289
public:
228
- ModulinoBuzzer (uint8_t address = 0xFF )
229
- : Module(address, " BUZZER" ) {}
290
+ ModulinoBuzzer (uint8_t address = 0xFF , ModulinoHubPort* hubPort = nullptr )
291
+ : Module(address, " BUZZER" , hubPort) {}
292
+ ModulinoBuzzer (ModulinoHubPort* hubPort, uint8_t address = 0xFF )
293
+ : Module(address, " BUZZER" , hubPort) {}
230
294
void (tone)(size_t freq, size_t len_ms) {
231
295
uint8_t buf[8 ];
232
296
memcpy (&buf[0 ], &freq, 4 );
@@ -252,8 +316,10 @@ class ModulinoBuzzer : public Module {
252
316
253
317
class ModulinoVibro : public Module {
254
318
public:
255
- ModulinoVibro (uint8_t address = 0xFF )
256
- : Module(address, " VIBRO" ) {}
319
+ ModulinoVibro (uint8_t address = 0xFF , ModulinoHubPort* hubPort = nullptr )
320
+ : Module(address, " VIBRO" , hubPort) {}
321
+ ModulinoVibro (ModulinoHubPort* hubPort, uint8_t address = 0xFF )
322
+ : Module(address, " VIBRO" , hubPort) {}
257
323
void on (size_t len_ms, bool block, int power = MAXIMUM ) {
258
324
uint8_t buf[12 ];
259
325
uint32_t freq = 1000 ;
@@ -302,8 +368,12 @@ class ModulinoColor {
302
368
303
369
class ModulinoPixels : public Module {
304
370
public:
305
- ModulinoPixels (uint8_t address = 0xFF )
306
- : Module(address, " LEDS" ) {
371
+ ModulinoPixels (uint8_t address = 0xFF , ModulinoHubPort* hubPort = nullptr )
372
+ : Module(address, " LEDS" , hubPort) {
373
+ memset ((uint8_t *)data, 0xE0 , NUMLEDS * 4 );
374
+ }
375
+ ModulinoPixels (ModulinoHubPort* hubPort, uint8_t address = 0xFF )
376
+ : Module(address, " LEDS" , hubPort) {
307
377
memset ((uint8_t *)data, 0xE0 , NUMLEDS * 4 );
308
378
}
309
379
void set (int idx, ModulinoColor rgb, uint8_t brightness = 25 ) {
@@ -342,9 +412,11 @@ class ModulinoPixels : public Module {
342
412
343
413
class ModulinoKnob : public Module {
344
414
public:
345
- ModulinoKnob (uint8_t address = 0xFF )
346
- : Module(address, " ENCODER" ) {}
347
- bool begin () {
415
+ ModulinoKnob (uint8_t address = 0xFF , ModulinoHubPort* hubPort = nullptr )
416
+ : Module(address, " ENCODER" , hubPort) {}
417
+ ModulinoKnob (ModulinoHubPort* hubPort, uint8_t address = 0xFF )
418
+ : Module(address, " ENCODER" , hubPort) {}
419
+ bool begin () {
348
420
auto ret = Module::begin ();
349
421
if (ret) {
350
422
// check for set() bug
@@ -407,6 +479,8 @@ extern ModulinoColor WHITE;
407
479
408
480
class ModulinoMovement : public Module {
409
481
public:
482
+ ModulinoMovement (ModulinoHubPort* hubPort = nullptr )
483
+ : Module(0xFF , " MOVEMENT" , hubPort) {}
410
484
bool begin () {
411
485
if (_imu == nullptr ) {
412
486
_imu = new LSM6DSOXClass (*((TwoWire*)getWire ()), 0x6A );
@@ -447,6 +521,8 @@ class ModulinoMovement : public Module {
447
521
448
522
class ModulinoThermo : public Module {
449
523
public:
524
+ ModulinoThermo (ModulinoHubPort* hubPort = nullptr )
525
+ : Module(0xFF , " THERMO" , hubPort) {}
450
526
bool begin () {
451
527
if (_sensor == nullptr ) {
452
528
_sensor = new HS300xClass (*((TwoWire*)getWire ()));
@@ -477,6 +553,8 @@ class ModulinoThermo: public Module {
477
553
478
554
class ModulinoPressure : public Module {
479
555
public:
556
+ ModulinoPressure (ModulinoHubPort* hubPort = nullptr )
557
+ : Module(0xFF , " PRESSURE" , hubPort) {}
480
558
bool begin () {
481
559
if (_barometer == nullptr ) {
482
560
_barometer = new LPS22HBClass (*((TwoWire*)getWire ()));
@@ -511,6 +589,8 @@ class ModulinoPressure : public Module {
511
589
512
590
class ModulinoLight : public Module {
513
591
public:
592
+ ModulinoLight (ModulinoHubPort* hubPort = nullptr )
593
+ : Module(0xFF , " LIGHT" , hubPort) {}
514
594
bool begin () {
515
595
if (_light == nullptr ) {
516
596
_light = new LTR381RGBClass (*((TwoWire*)getWire ()), 0x53 );
@@ -665,6 +745,8 @@ class _distance_api {
665
745
666
746
class ModulinoDistance : public Module {
667
747
public:
748
+ ModulinoDistance (ModulinoHubPort* hubPort = nullptr )
749
+ : Module(0xFF , " DISTANCE" , hubPort) {}
668
750
bool begin () {
669
751
// try scanning for 0x29 since the library contains a while(true) on begin()
670
752
getWire ()->beginTransmission (0x29 );
@@ -729,7 +811,9 @@ class ModulinoDistance : public Module {
729
811
730
812
class ModulinoRelay : public Module {
731
813
public:
732
- ModulinoRelay (uint8_t address = 0xFF )
814
+ ModulinoRelay (uint8_t address = 0xFF , ModulinoHubPort* hubPort = nullptr )
815
+ : Module(address, " RELAY" , hubPort) {}
816
+ ModulinoRelay (ModulinoHubPort* hubPort, uint8_t address = 0xFF )
733
817
: Module(address, " RELAY" ) {}
734
818
bool update () {
735
819
uint8_t buf[3 ];
0 commit comments