Skip to content

Commit 7720989

Browse files
committed
Added operator[] and mask() function to BusInOut
This change follows changes in BUsIn and BusOUt API
1 parent db7e928 commit 7720989

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

libraries/mbed/api/BusInOut.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ class BusInOut {
7373
*/
7474
void mode(PinMode pull);
7575

76+
/** Binary mask of bus pins connected to actual pins (not NC pins)
77+
* If bus pin is in NC state make corresponding bit will be cleared (set to 0), else bit will be set to 1
78+
*
79+
* @returns
80+
* Binary mask of connected pins
81+
*/
82+
int mask() {
83+
return _nc_mask;
84+
}
85+
86+
static DigitalInOut dinout_dummy;
87+
7688
#ifdef MBED_OPERATORS
7789
/** A shorthand for write()
7890
*/
@@ -87,10 +99,15 @@ class BusInOut {
8799
protected:
88100
DigitalInOut* _pin[16];
89101

102+
/** Mask of NC pins, if bit [n] bit is set to 1, [n] pin in bus is in NC state
103+
*/
104+
int _nc_mask;
105+
90106
/* disallow copy constructor and assignment operators */
91107
private:
92108
BusInOut(const BusInOut&);
93109
BusInOut & operator = (const BusInOut&);
110+
DigitalInOut& operator[] (unsigned int index);
94111
};
95112

96113
} // namespace mbed

libraries/mbed/common/BusInOut.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,27 @@
1717

1818
namespace mbed {
1919

20+
DigitalInOut BusInOut::dinout_dummy(NC);
21+
2022
BusInOut::BusInOut(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

25+
_nc_mask = 0;
2326
for (int i=0; i<16; i++) {
2427
_pin[i] = (pins[i] != NC) ? new DigitalInOut(pins[i]) : 0;
28+
if (pins[i] != NC) {
29+
_nc_mask |= (1 << i);
30+
}
2531
}
2632
}
2733

2834
BusInOut::BusInOut(PinName pins[16]) {
35+
_nc_mask = 0;
2936
for (int i=0; i<16; i++) {
3037
_pin[i] = (pins[i] != NC) ? new DigitalInOut(pins[i]) : 0;
38+
if (pins[i] != NC) {
39+
_nc_mask |= (1 << i);
40+
}
3141
}
3242
}
3343

@@ -92,6 +102,16 @@ BusInOut& BusInOut::operator= (BusInOut& rhs) {
92102
return *this;
93103
}
94104

105+
DigitalInOut& BusInOut::operator[] (unsigned int index) {
106+
//MBED_ASSERT(index >= MBED_BUS_SIZE);
107+
//MBED_ASSERT(_pin[index]);
108+
if (index >= 16 || _pin[index] == NULL) {
109+
return dinout_dummy;
110+
}
111+
return *_pin[index];
112+
}
113+
114+
95115
BusInOut::operator int() {
96116
return read();
97117
}

0 commit comments

Comments
 (0)