BusIn HelloWorld

Fork of BusIn_HelloWorld by Mbed

Use

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 in a single pass. In general this abstraction can be used to make code less cluttered, clearer, and take less time to write.

Note

Please pay attention to the ordering of pins in the initialization. The order pins are initialized in are the reverse order that bits are OR'd together.

API

API reference.

Import librarymbed

No documentation found.
Revision:
5:81a0449aa2d5
Parent:
4:252fbf8e71db
Child:
6:e1f4151bb580
--- a/main.cpp	Fri Mar 27 22:35:08 2015 +0000
+++ b/main.cpp	Tue Mar 31 00:45:43 2015 +0000
@@ -16,20 +16,26 @@
 
 #include "mbed.h"
 
-BusIn nibble(D0, D1, D2, D3); // change pins according to platform
+BusIn nibble(D0, D1, D2, D3); // Change these pins to buttons on your board.
 
 int main() {
+    
+    // Optional: set mode as PullUp/PullDown/PullNone/OpenDrain
+    nibble.mode(PullNone); 
+    
     while(1) {
-        switch(nibble) { // read the bus
-            case 0x0: printf("0b0000, D3,D2,D1,D0 are off");break;
-            case 0x1: printf("0b0001,          D0 is on")  ;break;
-            case 0x2: printf("0b0010,       D1    is on")  ;break; 
-            case 0x3: printf("0b0011,       D1,D0 are on") ;break;
-            case 0x4: printf("0b0100,    D2       is on")  ;break;
+        // check bits set in nibble
+        switch(~nibble & nibble.mask()) { // read the bus and mask out bits not being used
+            case 0x0: printf("0b0000, D3,D2,D1,D0 are low \n\r");break;
+            case 0x1: printf("0b0001,          D0 is high \n\r");break;
+            case 0x2: printf("0b0010,       D1    is high \n\r");break; 
+            case 0x3: printf("0b0011,       D1,D0 are high\n\r");break;
+            case 0x4: printf("0b0100,    D2       is high \n\r");break;
             // ...
-            case 0x8: printf("0b1000, D3          is on")  ;break; 
+            case 0x8: printf("0b1000, D3          is high \n\r");break; 
             // ...
-            case 0xF: printf("0b1111, D3,D2,D1,D0 are on") ;break;
+            case 0xF: printf("0b1111, D3,D2,D1,D0 are high\n\r");break;
         }
+        wait(1);
     }
 }
\ No newline at end of file