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