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