jim hamblen
/
Nav_Switch_Hello_World
ver 1.0
Embed:
(wiki syntax)
Show/hide line numbers
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 Nav_Switch myNav( p9, p6, p7, p5, p8); //pin order on Sparkfun breakout 00062 00063 int main() 00064 { 00065 while(1) { 00066 //with pullups a button hit is a "0" - "~" inverts data to leds 00067 mbedleds = ~(myNav & 0x0F); //update leds with nav switch direction inputs 00068 if(myNav.fire()) mbedleds = 0x0F; //special all leds on case for fire (center button) 00069 //or use - if(myNav[4]==0) mbedleds = 0x0F; //can index a switch bit like this 00070 wait(0.02); 00071 } 00072 }
Generated on Sun Jul 17 2022 06:04:01 by 1.7.2