Skip to content

Commit 8509e3a

Browse files
author
Qinghao Shi
authored
Merge pull request #92 from maciejbocianski/move_bus_examples
add Bus examples
2 parents 5d1ad43 + 27735e9 commit 8509e3a

File tree

6 files changed

+126
-0
lines changed

6 files changed

+126
-0
lines changed

APIs_Drivers/BusInOut_ex_1/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# BusInOut example
2+
3+
This example shows how to use the BusInOut interface to create an artificial bus of pins and read from and write to them.
4+
5+
**Note**: Pay special attention to the ordering of the pins in the initialization.

APIs_Drivers/BusInOut_ex_1/main.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2014-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
BusInOut pins(D0, D1, D2); // Change these pins to buttons on your board.
9+
10+
int main() {
11+
while(1) {
12+
pins.output();
13+
pins = 0x3;
14+
ThisThread::sleep_for(1000);
15+
pins.input();
16+
ThisThread::sleep_for(1000);
17+
if(pins == 0x6) {
18+
printf("Hello!\n");
19+
}
20+
}
21+
}

APIs_Drivers/BusIn_ex_1/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# BusIn example
2+
3+
BusIn is an abstraction that takes any pins and makes them appear as though they are linearly memory mapped for ease of use. This abstraction is useful for checking multiple inputs at once. In general, you can use this abstraction to make code less cluttered, clearer and quicker to write.
4+
5+
**Note**: Please pay attention to the ordering of pins in the initialization. The order pins are initialized in the reverse order that bits are OR'd together.

APIs_Drivers/BusIn_ex_1/main.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2014-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
BusIn nibble(D0, D1, D2, D3); // Change these pins to buttons on your board.
9+
10+
int main()
11+
{
12+
13+
// Optional: set mode as PullUp/PullDown/PullNone/OpenDrain
14+
nibble.mode(PullNone);
15+
16+
while (1) {
17+
// check bits set in nibble
18+
switch (nibble & nibble.mask()) { // read the bus and mask out bits not being used
19+
case 0x0:
20+
printf("0b0000, D3,D2,D1,D0 are low \n\r");
21+
break;
22+
case 0x1:
23+
printf("0b0001, D0 is high \n\r");
24+
break;
25+
case 0x2:
26+
printf("0b0010, D1 is high \n\r");
27+
break;
28+
case 0x3:
29+
printf("0b0011, D1,D0 are high \n\r");
30+
break;
31+
case 0x4:
32+
printf("0b0100, D2 is high \n\r");
33+
break;
34+
case 0x5:
35+
printf("0b0101, D2, ,D0 are high \n\r");
36+
break;
37+
case 0x6:
38+
printf("0b0110, D2,D1 are high \n\r");
39+
break;
40+
case 0x7:
41+
printf("0b0111, D2,D1,D0 are high \n\r");
42+
break;
43+
case 0x8:
44+
printf("0b1000, D3 is high \n\r");
45+
break;
46+
// ...
47+
case 0xF:
48+
printf("0b1111, D3,D2,D1,D0 are high \n\r");
49+
break;
50+
}
51+
ThisThread::sleep_for(1000);
52+
}
53+
}

APIs_Drivers/BusOut_ex_1/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# BusOut example
2+
3+
You can use the BusOut interface to create an artificial bus of pins out of any digital ins. The pins don't need sequential hardware addressing. This example program counts up from 0 to 15 or binary 0000 to 1111. The effect of this is turning LED1-LED4 on or off according to the number going across the bus. The example code toggles LEDs on and off. If a tricolor LED is on the board, the LED changes colors.
4+
5+
**Note**: When initializing a BusOut object, you initialize pins in their bit order from right to left. Be careful because the bit order is the reverse of the object initializing order.
6+
7+
| Integer | binary | LED On(1) / Off(0)
8+
| ------- | ------ | ------------------
9+
| 0 | 0000 | LED4=0 LED3=0 LED2=0 LED1=0
10+
| 1 | 0001 | LED4=0 LED3=0 LED2=0 LED1=1
11+
| 2 | 0010 | LED4=0 LED3=0 LED2=1 LED1=
12+
| 3 | 0011 | LED4=0 LED3=0 LED2=1 LED1=1
13+
| 4 | 0100 | LED4=0 LED3=1 LED2=0 LED1=0
14+
| 5 | 0101 | LED4=0 LED3=1 LED2=0 LED1=1
15+
| 6 | 0110 | LED4=0 LED3=1 LED2=1 LED1=0
16+
| 7 | 0111 | LED4=0 LED3=1 LED2=1 LED1=1
17+
| 8 | 1000 | LED4=1 LED3=0 LED2=0 LED1=0
18+
| 9 | 1001 | LED4=1 LED3=0 LED2=0 LED1=1
19+
| 10 | 1010 | LED4=1 LED3=0 LED2=1 LED1=0
20+
| 11 | 1011 | LED4=1 LED3=0 LED2=1 LED1=1
21+
| 12 | 1100 | LED4=1 LED3=1 LED2=0 LED1=0
22+
| 13 | 1101 | LED4=1 LED3=1 LED2=0 LED1=1
23+
| 14 | 1110 | LED4=1 LED3=1 LED2=1 LED1=0
24+
| 15 | 1111 | LED4=1 LED3=1 LED2=1 LED1=1

APIs_Drivers/BusOut_ex_1/main.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (c) 2014-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
BusOut myleds(LED1, LED2, LED3, LED4);
9+
10+
int main()
11+
{
12+
while (1) {
13+
for (int i = 0; i < 16; i++) {
14+
myleds = i;
15+
ThisThread::sleep_for(250);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)