@@ -61,14 +61,53 @@ class ModulinoClass {
61
61
friend class Module ;
62
62
protected:
63
63
HardwareI2C* _wire;
64
+ friend class ModulinoHub ;
65
+ friend class ModulinoHubPort ;
64
66
};
65
67
66
68
extern ModulinoClass Modulino;
67
69
70
+ // Forward declaration of ModulinoHub
71
+ class ModulinoHub ;
72
+
73
+ class ModulinoHubPort {
74
+ public:
75
+ ModulinoHubPort (int port, ModulinoHub* hub) : _port(port), _hub(hub) {}
76
+ int select ();
77
+ int clear ();
78
+ private:
79
+ int _port;
80
+ ModulinoHub* _hub;
81
+ };
82
+
83
+ class ModulinoHub {
84
+ public:
85
+ ModulinoHub (int address = 0x70 ) : _address(address){ }
86
+ ModulinoHubPort* port (int _port) {
87
+ return new ModulinoHubPort (_port, this );
88
+ }
89
+ int select (int port) {
90
+ Modulino._wire ->beginTransmission (_address);
91
+ Modulino._wire ->write (1 << port);
92
+ return Modulino._wire ->endTransmission ();
93
+ }
94
+ int clear () {
95
+ Modulino._wire ->beginTransmission (_address);
96
+ Modulino._wire ->write ((uint8_t )0 );
97
+ return Modulino._wire ->endTransmission ();
98
+ }
99
+
100
+ int address () {
101
+ return _address;
102
+ }
103
+ private:
104
+ int _address;
105
+ };
106
+
68
107
class Module : public Printable {
69
108
public:
70
- Module (uint8_t address = 0xFF , const char * name = " " )
71
- : address(address), name((char *)name) {}
109
+ Module (uint8_t address = 0xFF , const char * name = " " , ModulinoHubPort* hubPort = nullptr )
110
+ : address(address), name((char *)name), hubPort(hubPort) {}
72
111
virtual ~Module () {}
73
112
bool begin () {
74
113
if (address >= 0x7F ) {
@@ -89,6 +128,9 @@ class Module : public Printable {
89
128
if (address >= 0x7F ) {
90
129
return false ;
91
130
}
131
+ if (hubPort != nullptr ) {
132
+ hubPort->select ();
133
+ }
92
134
Modulino._wire ->requestFrom (address, howmany + 1 );
93
135
auto start = millis ();
94
136
while ((Modulino._wire ->available () == 0 ) && (millis () - start < 100 )) {
@@ -104,17 +146,26 @@ class Module : public Printable {
104
146
while (Modulino._wire ->available ()) {
105
147
Modulino._wire ->read ();
106
148
}
149
+ if (hubPort != nullptr ) {
150
+ hubPort->clear ();
151
+ }
107
152
return true ;
108
153
}
109
154
bool write (uint8_t * buf, int howmany) {
110
155
if (address >= 0x7F ) {
111
156
return false ;
112
157
}
158
+ if (hubPort != nullptr ) {
159
+ hubPort->select ();
160
+ }
113
161
Modulino._wire ->beginTransmission (address);
114
162
for (int i = 0 ; i < howmany; i++) {
115
163
Modulino._wire ->write (buf[i]);
116
164
}
117
165
Modulino._wire ->endTransmission ();
166
+ if (hubPort != nullptr ) {
167
+ hubPort->clear ();
168
+ }
118
169
return true ;
119
170
}
120
171
bool nonDefaultAddress () {
@@ -124,8 +175,14 @@ class Module : public Printable {
124
175
return p.print (name);
125
176
}
126
177
bool scan (uint8_t addr) {
178
+ if (hubPort != nullptr ) {
179
+ hubPort->select ();
180
+ }
127
181
Modulino._wire ->beginTransmission (addr / 2 ); // multply by 2 to match address in fw main.c
128
182
auto ret = Modulino._wire ->endTransmission ();
183
+ if (hubPort != nullptr ) {
184
+ hubPort->clear ();
185
+ }
129
186
if (ret == 0 ) {
130
187
// could also ask for 1 byte and check if it's truely a modulino of that kind
131
188
return true ;
@@ -136,12 +193,15 @@ class Module : public Printable {
136
193
uint8_t address;
137
194
uint8_t pinstrap_address;
138
195
char * name;
196
+ ModulinoHubPort* hubPort = nullptr ;
139
197
};
140
198
141
199
class ModulinoButtons : public Module {
142
200
public:
143
- ModulinoButtons (uint8_t address = 0xFF )
144
- : Module(address, " BUTTONS" ) {}
201
+ ModulinoButtons (uint8_t address = 0xFF , ModulinoHubPort* hubPort = nullptr )
202
+ : Module(address, " BUTTONS" , hubPort) {}
203
+ ModulinoButtons (ModulinoHubPort* hubPort, uint8_t address = 0xFF )
204
+ : Module(address, " BUTTONS" , hubPort) {}
145
205
PinStatus isPressed (int index) {
146
206
return last_status[index] ? HIGH : LOW;
147
207
}
@@ -197,8 +257,10 @@ class ModulinoButtons : public Module {
197
257
198
258
class ModulinoJoystick : public Module {
199
259
public:
200
- ModulinoJoystick (uint8_t address = 0xFF )
201
- : Module(address, " JOYSTICK" ) {}
260
+ ModulinoJoystick (uint8_t address = 0xFF , ModulinoHubPort* hubPort = nullptr )
261
+ : Module(address, " JOYSTICK" , hubPort) {}
262
+ ModulinoJoystick (ModulinoHubPort* hubPort, uint8_t address = 0xFF )
263
+ : Module(address, " JOYSTICK" , hubPort) {}
202
264
bool update () {
203
265
uint8_t buf[3 ];
204
266
auto res = read ((uint8_t *)buf, 3 );
@@ -249,8 +311,10 @@ class ModulinoJoystick : public Module {
249
311
250
312
class ModulinoBuzzer : public Module {
251
313
public:
252
- ModulinoBuzzer (uint8_t address = 0xFF )
253
- : Module(address, " BUZZER" ) {}
314
+ ModulinoBuzzer (uint8_t address = 0xFF , ModulinoHubPort* hubPort = nullptr )
315
+ : Module(address, " BUZZER" , hubPort) {}
316
+ ModulinoBuzzer (ModulinoHubPort* hubPort, uint8_t address = 0xFF )
317
+ : Module(address, " BUZZER" , hubPort) {}
254
318
void (tone)(size_t freq, size_t len_ms) {
255
319
uint8_t buf[8 ];
256
320
memcpy (&buf[0 ], &freq, 4 );
@@ -276,8 +340,10 @@ class ModulinoBuzzer : public Module {
276
340
277
341
class ModulinoVibro : public Module {
278
342
public:
279
- ModulinoVibro (uint8_t address = 0xFF )
280
- : Module(address, " VIBRO" ) {}
343
+ ModulinoVibro (uint8_t address = 0xFF , ModulinoHubPort* hubPort = nullptr )
344
+ : Module(address, " VIBRO" , hubPort) {}
345
+ ModulinoVibro (ModulinoHubPort* hubPort, uint8_t address = 0xFF )
346
+ : Module(address, " VIBRO" , hubPort) {}
281
347
void on (size_t len_ms, bool block, int power = MAXIMUM ) {
282
348
uint8_t buf[12 ];
283
349
uint32_t freq = 1000 ;
@@ -326,8 +392,12 @@ class ModulinoColor {
326
392
327
393
class ModulinoPixels : public Module {
328
394
public:
329
- ModulinoPixels (uint8_t address = 0xFF )
330
- : Module(address, " LEDS" ) {
395
+ ModulinoPixels (uint8_t address = 0xFF , ModulinoHubPort* hubPort = nullptr )
396
+ : Module(address, " LEDS" , hubPort) {
397
+ memset ((uint8_t *)data, 0xE0 , NUMLEDS * 4 );
398
+ }
399
+ ModulinoPixels (ModulinoHubPort* hubPort, uint8_t address = 0xFF )
400
+ : Module(address, " LEDS" , hubPort) {
331
401
memset ((uint8_t *)data, 0xE0 , NUMLEDS * 4 );
332
402
}
333
403
void set (int idx, ModulinoColor rgb, uint8_t brightness = 25 ) {
@@ -366,9 +436,11 @@ class ModulinoPixels : public Module {
366
436
367
437
class ModulinoKnob : public Module {
368
438
public:
369
- ModulinoKnob (uint8_t address = 0xFF )
370
- : Module(address, " ENCODER" ) {}
371
- bool begin () {
439
+ ModulinoKnob (uint8_t address = 0xFF , ModulinoHubPort* hubPort = nullptr )
440
+ : Module(address, " ENCODER" , hubPort) {}
441
+ ModulinoKnob (ModulinoHubPort* hubPort, uint8_t address = 0xFF )
442
+ : Module(address, " ENCODER" , hubPort) {}
443
+ bool begin () {
372
444
auto ret = Module::begin ();
373
445
if (ret) {
374
446
auto _val = get ();
@@ -453,6 +525,8 @@ extern ModulinoColor WHITE;
453
525
454
526
class ModulinoMovement : public Module {
455
527
public:
528
+ ModulinoMovement (ModulinoHubPort* hubPort = nullptr )
529
+ : Module(0xFF , " MOVEMENT" , hubPort) {}
456
530
bool begin () {
457
531
if (_imu == nullptr ) {
458
532
_imu = new LSM6DSOXClass (*((TwoWire*)getWire ()), 0x6A );
@@ -505,6 +579,8 @@ class ModulinoMovement : public Module {
505
579
506
580
class ModulinoThermo : public Module {
507
581
public:
582
+ ModulinoThermo (ModulinoHubPort* hubPort = nullptr )
583
+ : Module(0xFF , " THERMO" , hubPort) {}
508
584
bool begin () {
509
585
if (_sensor == nullptr ) {
510
586
_sensor = new HS300xClass (*((TwoWire*)getWire ()));
@@ -535,6 +611,8 @@ class ModulinoThermo: public Module {
535
611
536
612
class ModulinoPressure : public Module {
537
613
public:
614
+ ModulinoPressure (ModulinoHubPort* hubPort = nullptr )
615
+ : Module(0xFF , " PRESSURE" , hubPort) {}
538
616
bool begin () {
539
617
if (_barometer == nullptr ) {
540
618
_barometer = new LPS22HBClass (*((TwoWire*)getWire ()));
@@ -569,6 +647,8 @@ class ModulinoPressure : public Module {
569
647
570
648
class ModulinoLight : public Module {
571
649
public:
650
+ ModulinoLight (ModulinoHubPort* hubPort = nullptr )
651
+ : Module(0xFF , " LIGHT" , hubPort) {}
572
652
bool begin () {
573
653
if (_light == nullptr ) {
574
654
_light = new LTR381RGBClass (*((TwoWire*)getWire ()), 0x53 );
@@ -723,6 +803,8 @@ class _distance_api {
723
803
724
804
class ModulinoDistance : public Module {
725
805
public:
806
+ ModulinoDistance (ModulinoHubPort* hubPort = nullptr )
807
+ : Module(0xFF , " DISTANCE" , hubPort) {}
726
808
bool begin () {
727
809
// try scanning for 0x29 since the library contains a while(true) on begin()
728
810
getWire ()->beginTransmission (0x29 );
@@ -787,7 +869,9 @@ class ModulinoDistance : public Module {
787
869
788
870
class ModulinoRelay : public Module {
789
871
public:
790
- ModulinoRelay (uint8_t address = 0xFF )
872
+ ModulinoRelay (uint8_t address = 0xFF , ModulinoHubPort* hubPort = nullptr )
873
+ : Module(address, " RELAY" , hubPort) {}
874
+ ModulinoRelay (ModulinoHubPort* hubPort, uint8_t address = 0xFF )
791
875
: Module(address, " RELAY" ) {}
792
876
bool update () {
793
877
uint8_t buf[3 ];
0 commit comments