Skip to content

Commit c4fc8e6

Browse files
committed
Added operator[] for BusIn and BusOut to add access to particular bit in random-iterator fashion
1 parent 6fa4b46 commit c4fc8e6

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

libraries/mbed/api/BusIn.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,16 @@ class BusIn {
5858
*/
5959
void mode(PinMode pull);
6060

61+
static DigitalIn din_dummy;
62+
6163
#ifdef MBED_OPERATORS
6264
/** A shorthand for read()
6365
*/
6466
operator int();
67+
68+
/** Access to particular bit in random-iterator fashion
69+
*/
70+
DigitalIn & operator[] (unsigned int index);
6571
#endif
6672

6773
protected:

libraries/mbed/api/BusOut.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,18 @@ class BusOut {
5656
*/
5757
int read();
5858

59+
static DigitalOut dout_dummy;
60+
5961
#ifdef MBED_OPERATORS
6062
/** A shorthand for write()
6163
*/
6264
BusOut& operator= (int v);
6365
BusOut& operator= (BusOut& rhs);
6466

67+
/** Access to particular bit in random-iterator fashion
68+
*/
69+
DigitalOut& operator[] (unsigned int index);
70+
6571
/** A shorthand for read()
6672
*/
6773
operator int();

libraries/mbed/common/BusIn.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace mbed {
1919

20+
DigitalIn BusIn::din_dummy(NC);
21+
2022
BusIn::BusIn(PinName p0, PinName p1, PinName p2, PinName p3, PinName p4, PinName p5, PinName p6, PinName p7, PinName p8, PinName p9, PinName p10, PinName p11, PinName p12, PinName p13, PinName p14, PinName p15) {
2123
PinName pins[16] = {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15};
2224

@@ -61,6 +63,16 @@ void BusIn::mode(PinMode pull) {
6163
BusIn::operator int() {
6264
return read();
6365
}
66+
67+
DigitalIn& BusIn::operator[] (unsigned int index) {
68+
//MBED_ASSERT(index >= MBED_BUS_SIZE);
69+
//MBED_ASSERT(_pin[index]);
70+
if (index >= 16 || _pin[index] == NULL) {
71+
return din_dummy;
72+
}
73+
return *_pin[index];
74+
}
75+
6476
#endif
6577

6678
} // namespace mbed

libraries/mbed/common/BusOut.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace mbed {
1919

20+
DigitalOut BusOut::dout_dummy(NC);
21+
2022
BusOut::BusOut(PinName p0, PinName p1, PinName p2, PinName p3, PinName p4, PinName p5, PinName p6, PinName p7, PinName p8, PinName p9, PinName p10, PinName p11, PinName p12, PinName p13, PinName p14, PinName p15) {
2123
PinName pins[16] = {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15};
2224

@@ -68,6 +70,15 @@ BusOut& BusOut::operator= (BusOut& rhs) {
6870
return *this;
6971
}
7072

73+
DigitalOut& BusOut::operator[] (unsigned int index) {
74+
//MBED_ASSERT(index >= MBED_BUS_SIZE);
75+
//MBED_ASSERT(_pin[index]);
76+
if (index >= 16 || _pin[index] == NULL) {
77+
return dout_dummy;
78+
}
79+
return *_pin[index];
80+
}
81+
7182
BusOut::operator int() {
7283
return read();
7384
}

0 commit comments

Comments
 (0)