Digital Joystick
Joystick.cpp@6:d354f6e3bd9b, 2015-02-26 (annotated)
- Committer:
- LeoHsueh
- Date:
- Thu Feb 26 11:50:10 2015 +0000
- Revision:
- 6:d354f6e3bd9b
- Parent:
- 5:31e3324d4c4b
Add function call.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alex89_2 | 0:9c486d50434e | 1 | #include "Joystick.h" |
alex89_2 | 0:9c486d50434e | 2 | |
LeoHsueh | 5:31e3324d4c4b | 3 | /** Create a Joystick HID for using regular mbed pins |
LeoHsueh | 5:31e3324d4c4b | 4 | * |
LeoHsueh | 5:31e3324d4c4b | 5 | * @param up Joystick Up |
LeoHsueh | 5:31e3324d4c4b | 6 | * @param down Joystick Down |
LeoHsueh | 5:31e3324d4c4b | 7 | * @param left Joystick Left |
LeoHsueh | 5:31e3324d4c4b | 8 | * @param right Joystick Right |
LeoHsueh | 5:31e3324d4c4b | 9 | * @param press Joystick Press |
LeoHsueh | 5:31e3324d4c4b | 10 | */ |
LeoHsueh | 5:31e3324d4c4b | 11 | Joystick::Joystick(PinName up, PinName down, PinName left, PinName right, PinName press) : |
LeoHsueh | 5:31e3324d4c4b | 12 | _up(up), _down(down), _left(left), _right(right), _press(press) { |
alex89_2 | 0:9c486d50434e | 13 | |
alex89_2 | 0:9c486d50434e | 14 | } |
alex89_2 | 0:9c486d50434e | 15 | |
LeoHsueh | 6:d354f6e3bd9b | 16 | void Joystick::functions(FunctionPointer *functionNone, FunctionPointer *functionUp, FunctionPointer *functionDown, |
LeoHsueh | 6:d354f6e3bd9b | 17 | FunctionPointer *functionLeft, FunctionPointer *functionRight, FunctionPointer *functionPress) { |
LeoHsueh | 6:d354f6e3bd9b | 18 | _functionNone = functionNone; |
LeoHsueh | 6:d354f6e3bd9b | 19 | _functionUp = functionUp; |
LeoHsueh | 6:d354f6e3bd9b | 20 | _functionDown = functionDown; |
LeoHsueh | 6:d354f6e3bd9b | 21 | _functionLeft = functionLeft; |
LeoHsueh | 6:d354f6e3bd9b | 22 | _functionRight = functionRight; |
LeoHsueh | 6:d354f6e3bd9b | 23 | _functionPress = functionPress; |
LeoHsueh | 6:d354f6e3bd9b | 24 | } |
LeoHsueh | 6:d354f6e3bd9b | 25 | |
LeoHsueh | 6:d354f6e3bd9b | 26 | /** Get status |
LeoHsueh | 5:31e3324d4c4b | 27 | * Read the joystick status |
LeoHsueh | 5:31e3324d4c4b | 28 | * |
LeoHsueh | 6:d354f6e3bd9b | 29 | * @returns Status of joystick |
LeoHsueh | 5:31e3324d4c4b | 30 | */ |
LeoHsueh | 6:d354f6e3bd9b | 31 | Joystick::Status Joystick::getStatus() { |
LeoHsueh | 5:31e3324d4c4b | 32 | if (!_up) { |
LeoHsueh | 6:d354f6e3bd9b | 33 | return up; |
LeoHsueh | 5:31e3324d4c4b | 34 | } |
LeoHsueh | 5:31e3324d4c4b | 35 | if (!_down) { |
LeoHsueh | 6:d354f6e3bd9b | 36 | return down; |
LeoHsueh | 5:31e3324d4c4b | 37 | } |
LeoHsueh | 5:31e3324d4c4b | 38 | if (!_left) { |
LeoHsueh | 6:d354f6e3bd9b | 39 | return left; |
LeoHsueh | 5:31e3324d4c4b | 40 | } |
LeoHsueh | 5:31e3324d4c4b | 41 | if (!_right) { |
LeoHsueh | 6:d354f6e3bd9b | 42 | return right; |
LeoHsueh | 5:31e3324d4c4b | 43 | } |
LeoHsueh | 5:31e3324d4c4b | 44 | if (!_press) { |
LeoHsueh | 6:d354f6e3bd9b | 45 | return press; |
LeoHsueh | 5:31e3324d4c4b | 46 | } |
LeoHsueh | 6:d354f6e3bd9b | 47 | return none; |
alex89_2 | 0:9c486d50434e | 48 | } |
LeoHsueh | 6:d354f6e3bd9b | 49 | |
LeoHsueh | 6:d354f6e3bd9b | 50 | void Joystick::poll() { |
LeoHsueh | 6:d354f6e3bd9b | 51 | switch (getStatus()) { |
LeoHsueh | 6:d354f6e3bd9b | 52 | case none: |
LeoHsueh | 6:d354f6e3bd9b | 53 | _functionNone->call(); |
LeoHsueh | 6:d354f6e3bd9b | 54 | break; |
LeoHsueh | 6:d354f6e3bd9b | 55 | case up: |
LeoHsueh | 6:d354f6e3bd9b | 56 | _functionUp->call(); |
LeoHsueh | 6:d354f6e3bd9b | 57 | break; |
LeoHsueh | 6:d354f6e3bd9b | 58 | case down: |
LeoHsueh | 6:d354f6e3bd9b | 59 | _functionDown->call(); |
LeoHsueh | 6:d354f6e3bd9b | 60 | break; |
LeoHsueh | 6:d354f6e3bd9b | 61 | case left: |
LeoHsueh | 6:d354f6e3bd9b | 62 | _functionLeft->call(); |
LeoHsueh | 6:d354f6e3bd9b | 63 | break; |
LeoHsueh | 6:d354f6e3bd9b | 64 | case right: |
LeoHsueh | 6:d354f6e3bd9b | 65 | _functionRight->call(); |
LeoHsueh | 6:d354f6e3bd9b | 66 | break; |
LeoHsueh | 6:d354f6e3bd9b | 67 | case press: |
LeoHsueh | 6:d354f6e3bd9b | 68 | _functionPress->call(); |
LeoHsueh | 6:d354f6e3bd9b | 69 | break; |
LeoHsueh | 6:d354f6e3bd9b | 70 | default: |
LeoHsueh | 6:d354f6e3bd9b | 71 | break; |
LeoHsueh | 6:d354f6e3bd9b | 72 | } |
LeoHsueh | 6:d354f6e3bd9b | 73 | } |