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