Skip to content

Commit 8c50826

Browse files
committed
Removed extra checks for operator[] and replaced them with MBED_ASSERT for cleaner code
1 parent d068a2b commit 8c50826

File tree

7 files changed

+26
-40
lines changed

7 files changed

+26
-40
lines changed

libraries/mbed/api/BusIn.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ class BusIn {
6868
return _nc_mask;
6969
}
7070

71-
static DigitalIn din_dummy;
72-
7371
#ifdef MBED_OPERATORS
7472
/** A shorthand for read()
7573
*/

libraries/mbed/api/BusInOut.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ class BusInOut {
8282
return _nc_mask;
8383
}
8484

85-
static DigitalInOut dinout_dummy;
86-
8785
#ifdef MBED_OPERATORS
8886
/** A shorthand for write()
8987
*/

libraries/mbed/api/BusOut.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ class BusOut {
6666
return _nc_mask;
6767
}
6868

69-
static DigitalOut dout_dummy;
70-
7169
#ifdef MBED_OPERATORS
7270
/** A shorthand for write()
7371
*/

libraries/mbed/common/BusIn.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
namespace mbed {
1919

20-
DigitalIn BusIn::din_dummy(NC);
21-
2220
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) {
2321
PinName pins[16] = {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15};
2422

@@ -73,11 +71,8 @@ BusIn::operator int() {
7371
}
7472

7573
DigitalIn& BusIn::operator[] (int index) {
76-
MBED_ASSERT(index < 0 || index >= 16);
74+
MBED_ASSERT(index >= 0 && index <= 16);
7775
MBED_ASSERT(_pin[index]);
78-
if (index >= 16 || _pin[index] == NULL) {
79-
return din_dummy;
80-
}
8176
return *_pin[index];
8277
}
8378

libraries/mbed/common/BusInOut.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
namespace mbed {
1919

20-
DigitalInOut BusInOut::dinout_dummy(NC);
21-
2220
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) {
2321
PinName pins[16] = {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15};
2422

@@ -103,11 +101,8 @@ BusInOut& BusInOut::operator= (BusInOut& rhs) {
103101
}
104102

105103
DigitalInOut& BusInOut::operator[] (int index) {
106-
MBED_ASSERT(index < 0 || index >= 16);
104+
MBED_ASSERT(index >= 0 && index <= 16);
107105
MBED_ASSERT(_pin[index]);
108-
if (index >= 16 || _pin[index] == NULL) {
109-
return dinout_dummy;
110-
}
111106
return *_pin[index];
112107
}
113108

libraries/mbed/common/BusOut.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
namespace mbed {
1919

20-
DigitalOut BusOut::dout_dummy(NC);
21-
2220
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) {
2321
PinName pins[16] = {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15};
2422

@@ -79,11 +77,8 @@ BusOut& BusOut::operator= (BusOut& rhs) {
7977
}
8078

8179
DigitalOut& BusOut::operator[] (int index) {
82-
MBED_ASSERT(index < 0 || index >= 16);
80+
MBED_ASSERT(index >= 0 && index <= 16);
8381
MBED_ASSERT(_pin[index]);
84-
if (index >= 16 || _pin[index] == NULL) {
85-
return dout_dummy;
86-
}
8782
return *_pin[index];
8883
}
8984

libraries/tests/mbed/bus_out/main.cpp

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#include "mbed.h"
22
#include "test_env.h"
33

4+
namespace {
45
BusOut bus_out(LED1, LED2, LED3, LED4);
6+
PinName led_pins[4] = {LED1, LED2, LED3, LED4}; // Temp, used to map pins in bus_out
7+
}
58

69
int main()
710
{
@@ -26,21 +29,18 @@ int main()
2629
}
2730

2831
// Checking if DigitalOut is correctly set as connected
29-
for (int i=0; i<4; i++) {
30-
printf("MBED: BusOut.bit[%d] is %s\r\n", i, bus_out[i].is_connected() ? "connected" : "not connected");
32+
for (int i=0; i < 4; i++) {
33+
printf("MBED: BusOut.bit[%d] is %s\r\n",
34+
i,
35+
(led_pins[i] != NC && bus_out[i].is_connected())
36+
? "connected"
37+
: "not connected");
3138
}
3239

33-
if (LED1 != NC && bus_out[0].is_connected() == 0) {
34-
break;
35-
}
36-
if (LED1 != NC && bus_out[1].is_connected() == 0) {
37-
break;
38-
}
39-
if (LED1 != NC && bus_out[2].is_connected() == 0) {
40-
break;
41-
}
42-
if (LED1 != NC && bus_out[3].is_connected() == 0) {
43-
break;
40+
for (int i=0; i < 4; i++) {
41+
if (led_pins[i] != NC && bus_out[0].is_connected() == 0) {
42+
break;
43+
}
4444
}
4545

4646
// Write mask all LEDs
@@ -58,18 +58,25 @@ int main()
5858
break;
5959
}
6060

61-
printf("MBED: Blinking LEDs...\r\n");
61+
printf("MBED: Blinking LEDs: \r\n");
6262

6363
// Just a quick LED blinking...
6464
for (int i=0; i<4; i++) {
65-
if (bus_out[i].is_connected()) {
65+
if (led_pins[i] != NC && bus_out[i].is_connected()) {
6666
bus_out[i] = 1;
67+
printf("%c", 'A' + i);
68+
} else {
69+
printf(".");
6770
}
6871
wait(0.2);
69-
if (bus_out[i].is_connected()) {
72+
if (led_pins[i] != NC && bus_out[i].is_connected()) {
7073
bus_out[i] = 0;
74+
printf("%c", 'a' + i);
75+
} else {
76+
printf(".");
7177
}
7278
}
79+
printf("\r\n");
7380

7481
notify_completion(result);
7582
}

0 commit comments

Comments
 (0)