![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
a
main.cpp@0:55bb59452ebc, 2016-09-02 (annotated)
- Committer:
- rmaran6
- Date:
- Fri Sep 02 17:35:19 2016 +0000
- Revision:
- 0:55bb59452ebc
joystick
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rmaran6 | 0:55bb59452ebc | 1 | #include "mbed.h" |
rmaran6 | 0:55bb59452ebc | 2 | |
rmaran6 | 0:55bb59452ebc | 3 | BusOut mbedleds(LED1,LED2,LED3,LED4); |
rmaran6 | 0:55bb59452ebc | 4 | //BusOut/In is faster than multiple DigitalOut/Ins |
rmaran6 | 0:55bb59452ebc | 5 | |
rmaran6 | 0:55bb59452ebc | 6 | class Nav_Switch |
rmaran6 | 0:55bb59452ebc | 7 | { |
rmaran6 | 0:55bb59452ebc | 8 | public: |
rmaran6 | 0:55bb59452ebc | 9 | Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire); |
rmaran6 | 0:55bb59452ebc | 10 | int read(); |
rmaran6 | 0:55bb59452ebc | 11 | //boolean functions to test each switch |
rmaran6 | 0:55bb59452ebc | 12 | bool up(); |
rmaran6 | 0:55bb59452ebc | 13 | bool down(); |
rmaran6 | 0:55bb59452ebc | 14 | bool left(); |
rmaran6 | 0:55bb59452ebc | 15 | bool right(); |
rmaran6 | 0:55bb59452ebc | 16 | bool fire(); |
rmaran6 | 0:55bb59452ebc | 17 | //automatic read on RHS |
rmaran6 | 0:55bb59452ebc | 18 | operator int (); |
rmaran6 | 0:55bb59452ebc | 19 | //index to any switch array style |
rmaran6 | 0:55bb59452ebc | 20 | bool operator[](int index) { |
rmaran6 | 0:55bb59452ebc | 21 | return _pins[index]; |
rmaran6 | 0:55bb59452ebc | 22 | }; |
rmaran6 | 0:55bb59452ebc | 23 | private: |
rmaran6 | 0:55bb59452ebc | 24 | BusIn _pins; |
rmaran6 | 0:55bb59452ebc | 25 | |
rmaran6 | 0:55bb59452ebc | 26 | }; |
rmaran6 | 0:55bb59452ebc | 27 | Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire): |
rmaran6 | 0:55bb59452ebc | 28 | _pins(up, down, left, right, fire) |
rmaran6 | 0:55bb59452ebc | 29 | { |
rmaran6 | 0:55bb59452ebc | 30 | _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise |
rmaran6 | 0:55bb59452ebc | 31 | wait(0.001); //delays just a bit for pullups to pull inputs high |
rmaran6 | 0:55bb59452ebc | 32 | } |
rmaran6 | 0:55bb59452ebc | 33 | inline bool Nav_Switch::up() |
rmaran6 | 0:55bb59452ebc | 34 | { |
rmaran6 | 0:55bb59452ebc | 35 | return !(_pins[0]); |
rmaran6 | 0:55bb59452ebc | 36 | } |
rmaran6 | 0:55bb59452ebc | 37 | inline bool Nav_Switch::down() |
rmaran6 | 0:55bb59452ebc | 38 | { |
rmaran6 | 0:55bb59452ebc | 39 | return !(_pins[1]); |
rmaran6 | 0:55bb59452ebc | 40 | } |
rmaran6 | 0:55bb59452ebc | 41 | inline bool Nav_Switch::left() |
rmaran6 | 0:55bb59452ebc | 42 | { |
rmaran6 | 0:55bb59452ebc | 43 | return !(_pins[2]); |
rmaran6 | 0:55bb59452ebc | 44 | } |
rmaran6 | 0:55bb59452ebc | 45 | inline bool Nav_Switch::right() |
rmaran6 | 0:55bb59452ebc | 46 | { |
rmaran6 | 0:55bb59452ebc | 47 | return !(_pins[3]); |
rmaran6 | 0:55bb59452ebc | 48 | } |
rmaran6 | 0:55bb59452ebc | 49 | inline bool Nav_Switch::fire() |
rmaran6 | 0:55bb59452ebc | 50 | { |
rmaran6 | 0:55bb59452ebc | 51 | return !(_pins[4]); |
rmaran6 | 0:55bb59452ebc | 52 | } |
rmaran6 | 0:55bb59452ebc | 53 | inline int Nav_Switch::read() |
rmaran6 | 0:55bb59452ebc | 54 | { |
rmaran6 | 0:55bb59452ebc | 55 | return _pins.read(); |
rmaran6 | 0:55bb59452ebc | 56 | } |
rmaran6 | 0:55bb59452ebc | 57 | inline Nav_Switch::operator int () |
rmaran6 | 0:55bb59452ebc | 58 | { |
rmaran6 | 0:55bb59452ebc | 59 | return _pins.read(); |
rmaran6 | 0:55bb59452ebc | 60 | } |
rmaran6 | 0:55bb59452ebc | 61 | |
rmaran6 | 0:55bb59452ebc | 62 | Nav_Switch myNav( p9, p6, p7, p5, p8); //pin order on Sparkfun breakout |
rmaran6 | 0:55bb59452ebc | 63 | |
rmaran6 | 0:55bb59452ebc | 64 | int main() |
rmaran6 | 0:55bb59452ebc | 65 | { |
rmaran6 | 0:55bb59452ebc | 66 | while(1) { |
rmaran6 | 0:55bb59452ebc | 67 | //with pullups a button hit is a "0" - "~" inverts data to leds |
rmaran6 | 0:55bb59452ebc | 68 | mbedleds = ~(myNav & 0x0F); //update leds with nav switch direction inputs |
rmaran6 | 0:55bb59452ebc | 69 | if(myNav.fire()) mbedleds = 0x0F; //special all leds on case for fire (center button) |
rmaran6 | 0:55bb59452ebc | 70 | //or use - if(myNav[4]==0) mbedleds = 0x0F; //can index a switch bit like this |
rmaran6 | 0:55bb59452ebc | 71 | wait(0.02); |
rmaran6 | 0:55bb59452ebc | 72 | } |
rmaran6 | 0:55bb59452ebc | 73 | } |
rmaran6 | 0:55bb59452ebc | 74 |