Added operator[] for BusIn and BusOut to add access to particular bit in random-iterator fashion

pull/808/head
Przemek Wirkus 2014-12-19 14:06:46 +00:00
parent 6fa4b469f4
commit c4fc8e68eb
4 changed files with 35 additions and 0 deletions

View File

@ -58,10 +58,16 @@ public:
*/
void mode(PinMode pull);
static DigitalIn din_dummy;
#ifdef MBED_OPERATORS
/** A shorthand for read()
*/
operator int();
/** Access to particular bit in random-iterator fashion
*/
DigitalIn & operator[] (unsigned int index);
#endif
protected:

View File

@ -56,12 +56,18 @@ public:
*/
int read();
static DigitalOut dout_dummy;
#ifdef MBED_OPERATORS
/** A shorthand for write()
*/
BusOut& operator= (int v);
BusOut& operator= (BusOut& rhs);
/** Access to particular bit in random-iterator fashion
*/
DigitalOut& operator[] (unsigned int index);
/** A shorthand for read()
*/
operator int();

View File

@ -17,6 +17,8 @@
namespace mbed {
DigitalIn BusIn::din_dummy(NC);
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) {
PinName pins[16] = {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15};
@ -61,6 +63,16 @@ void BusIn::mode(PinMode pull) {
BusIn::operator int() {
return read();
}
DigitalIn& BusIn::operator[] (unsigned int index) {
//MBED_ASSERT(index >= MBED_BUS_SIZE);
//MBED_ASSERT(_pin[index]);
if (index >= 16 || _pin[index] == NULL) {
return din_dummy;
}
return *_pin[index];
}
#endif
} // namespace mbed

View File

@ -17,6 +17,8 @@
namespace mbed {
DigitalOut BusOut::dout_dummy(NC);
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) {
PinName pins[16] = {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15};
@ -68,6 +70,15 @@ BusOut& BusOut::operator= (BusOut& rhs) {
return *this;
}
DigitalOut& BusOut::operator[] (unsigned int index) {
//MBED_ASSERT(index >= MBED_BUS_SIZE);
//MBED_ASSERT(_pin[index]);
if (index >= 16 || _pin[index] == NULL) {
return dout_dummy;
}
return *_pin[index];
}
BusOut::operator int() {
return read();
}