Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 00003 BusOut mbedleds(LED1,LED2,LED3,LED4); 00004 //BusOut/In is faster than multiple DigitalOut/Ins 00005 00006 class Nav_Switch 00007 { 00008 public: 00009 Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire); 00010 int read(); 00011 //boolean functions to test each switch 00012 bool up(); 00013 bool down(); 00014 bool left(); 00015 bool right(); 00016 bool fire(); 00017 //automatic read on RHS 00018 operator int (); 00019 //index to any switch array style 00020 bool operator[](int index) { 00021 return _pins[index]; 00022 }; 00023 private: 00024 BusIn _pins; 00025 00026 }; 00027 Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire): 00028 _pins(up, down, left, right, fire) 00029 { 00030 _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise 00031 wait(0.001); //delays just a bit for pullups to pull inputs high 00032 } 00033 inline bool Nav_Switch::up() 00034 { 00035 return !(_pins[0]); 00036 } 00037 inline bool Nav_Switch::down() 00038 { 00039 return !(_pins[1]); 00040 } 00041 inline bool Nav_Switch::left() 00042 { 00043 return !(_pins[2]); 00044 } 00045 inline bool Nav_Switch::right() 00046 { 00047 return !(_pins[3]); 00048 } 00049 inline bool Nav_Switch::fire() 00050 { 00051 return !(_pins[4]); 00052 } 00053 inline int Nav_Switch::read() 00054 { 00055 return _pins.read(); 00056 } 00057 inline Nav_Switch::operator int () 00058 { 00059 return _pins.read(); 00060 } 00061 00062 Nav_Switch myNav( p9, p6, p7, p5, p8); //pin order on Sparkfun breakout 00063 00064 int main() 00065 { 00066 while(1) { 00067 //with pullups a button hit is a "0" - "~" inverts data to leds 00068 mbedleds = ~(myNav & 0x0F); //update leds with nav switch direction inputs 00069 if(myNav.fire()) mbedleds = 0x0F; //special all leds on case for fire (center button) 00070 //or use - if(myNav[4]==0) mbedleds = 0x0F; //can index a switch bit like this 00071 wait(0.02); 00072 } 00073 }
Generated on Thu Jul 14 2022 22:00:29 by
1.7.2