Digital Joystick
Joystick.h@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 | //remove repetition |
alex89_2 | 0:9c486d50434e | 2 | #ifndef MBED_JOYSTICK_H |
alex89_2 | 0:9c486d50434e | 3 | #define MBED_JOYSTICK_H |
alex89_2 | 0:9c486d50434e | 4 | |
alex89_2 | 0:9c486d50434e | 5 | //required to use mbed functions |
alex89_2 | 0:9c486d50434e | 6 | #include "mbed.h" |
LeoHsueh | 6:d354f6e3bd9b | 7 | #include "FunctionPointer.h" |
alex89_2 | 0:9c486d50434e | 8 | |
LeoHsueh | 6:d354f6e3bd9b | 9 | #define JOYSTICK_NONE 0x00 |
LeoHsueh | 6:d354f6e3bd9b | 10 | #define JOYSTICK_UP 0x01 |
LeoHsueh | 6:d354f6e3bd9b | 11 | #define JOYSTICK_DOWN 0x02 |
LeoHsueh | 6:d354f6e3bd9b | 12 | #define JOYSTICK_LEFT 0x04 |
LeoHsueh | 6:d354f6e3bd9b | 13 | #define JOYSTICK_RIGHT 0x08 |
LeoHsueh | 6:d354f6e3bd9b | 14 | #define JOYSTICK_PRESS 0x10 |
alex89_2 | 0:9c486d50434e | 15 | |
LeoHsueh | 6:d354f6e3bd9b | 16 | /** Digital Joystick class |
alex89_2 | 2:8805f880e231 | 17 | * |
LeoHsueh | 6:d354f6e3bd9b | 18 | * Used for an digital joystick. |
alex89_2 | 0:9c486d50434e | 19 | * |
alex89_2 | 0:9c486d50434e | 20 | * Example: |
LeoHsueh | 6:d354f6e3bd9b | 21 | * @code |
LeoHsueh | 6:d354f6e3bd9b | 22 | * #include "mbed.h" |
LeoHsueh | 6:d354f6e3bd9b | 23 | * #include "Joystick.h" |
alex89_2 | 2:8805f880e231 | 24 | * |
LeoHsueh | 6:d354f6e3bd9b | 25 | * Joystick joystick(P2_3, P0_15, P2_4, P0_16, P0_17); |
alex89_2 | 2:8805f880e231 | 26 | * |
LeoHsueh | 6:d354f6e3bd9b | 27 | * int main() { |
LeoHsueh | 6:d354f6e3bd9b | 28 | * joystick.getStatus(); |
LeoHsueh | 6:d354f6e3bd9b | 29 | * } |
LeoHsueh | 6:d354f6e3bd9b | 30 | * |
LeoHsueh | 6:d354f6e3bd9b | 31 | * @endcode |
alex89_2 | 0:9c486d50434e | 32 | */ |
alex89_2 | 0:9c486d50434e | 33 | |
LeoHsueh | 5:31e3324d4c4b | 34 | class Joystick { |
LeoHsueh | 5:31e3324d4c4b | 35 | public: |
LeoHsueh | 6:d354f6e3bd9b | 36 | typedef enum { |
LeoHsueh | 6:d354f6e3bd9b | 37 | none = JOYSTICK_NONE, |
LeoHsueh | 6:d354f6e3bd9b | 38 | up = JOYSTICK_UP, |
LeoHsueh | 6:d354f6e3bd9b | 39 | down = JOYSTICK_DOWN, |
LeoHsueh | 6:d354f6e3bd9b | 40 | left = JOYSTICK_LEFT, |
LeoHsueh | 6:d354f6e3bd9b | 41 | right = JOYSTICK_RIGHT, |
LeoHsueh | 6:d354f6e3bd9b | 42 | press = JOYSTICK_PRESS |
LeoHsueh | 6:d354f6e3bd9b | 43 | } Status; |
LeoHsueh | 6:d354f6e3bd9b | 44 | |
LeoHsueh | 5:31e3324d4c4b | 45 | /** Create a Joystick HID for using regular mbed pins |
LeoHsueh | 5:31e3324d4c4b | 46 | * |
LeoHsueh | 5:31e3324d4c4b | 47 | * @param up Joystick Up |
LeoHsueh | 5:31e3324d4c4b | 48 | * @param down Joystick Down |
LeoHsueh | 5:31e3324d4c4b | 49 | * @param left Joystick Left |
LeoHsueh | 5:31e3324d4c4b | 50 | * @param right Joystick Right |
LeoHsueh | 5:31e3324d4c4b | 51 | * @param press Joystick Press |
LeoHsueh | 5:31e3324d4c4b | 52 | */ |
LeoHsueh | 5:31e3324d4c4b | 53 | Joystick(PinName up, PinName down, PinName left, PinName right, PinName press); |
alex89_2 | 0:9c486d50434e | 54 | |
LeoHsueh | 6:d354f6e3bd9b | 55 | void functions(FunctionPointer *functionNone, FunctionPointer *functionUp, FunctionPointer *functionDown, |
LeoHsueh | 6:d354f6e3bd9b | 56 | FunctionPointer *functionLeft, FunctionPointer *functionRight, FunctionPointer *functionPress); |
LeoHsueh | 6:d354f6e3bd9b | 57 | |
LeoHsueh | 6:d354f6e3bd9b | 58 | /** Get status |
LeoHsueh | 5:31e3324d4c4b | 59 | * Read the joystick status |
LeoHsueh | 5:31e3324d4c4b | 60 | * |
LeoHsueh | 6:d354f6e3bd9b | 61 | * @code |
LeoHsueh | 6:d354f6e3bd9b | 62 | * switch (joystick.getStatus()) { |
LeoHsueh | 6:d354f6e3bd9b | 63 | * case Joystick::none: |
LeoHsueh | 6:d354f6e3bd9b | 64 | * break; |
LeoHsueh | 6:d354f6e3bd9b | 65 | * case Joystick::up: |
LeoHsueh | 6:d354f6e3bd9b | 66 | * break; |
LeoHsueh | 6:d354f6e3bd9b | 67 | * case Joystick::down: |
LeoHsueh | 6:d354f6e3bd9b | 68 | * break; |
LeoHsueh | 6:d354f6e3bd9b | 69 | * case Joystick::left: |
LeoHsueh | 6:d354f6e3bd9b | 70 | * break; |
LeoHsueh | 6:d354f6e3bd9b | 71 | * case Joystick::right: |
LeoHsueh | 6:d354f6e3bd9b | 72 | * break; |
LeoHsueh | 6:d354f6e3bd9b | 73 | * case Joystick::press: |
LeoHsueh | 6:d354f6e3bd9b | 74 | * break; |
LeoHsueh | 6:d354f6e3bd9b | 75 | * default: |
LeoHsueh | 6:d354f6e3bd9b | 76 | * break; |
LeoHsueh | 6:d354f6e3bd9b | 77 | * } |
LeoHsueh | 6:d354f6e3bd9b | 78 | * @endcode |
LeoHsueh | 6:d354f6e3bd9b | 79 | * |
LeoHsueh | 6:d354f6e3bd9b | 80 | * @returns A uint8_t values representing the bits |
LeoHsueh | 5:31e3324d4c4b | 81 | */ |
LeoHsueh | 6:d354f6e3bd9b | 82 | Status getStatus(); |
LeoHsueh | 6:d354f6e3bd9b | 83 | |
LeoHsueh | 6:d354f6e3bd9b | 84 | void poll(); |
LeoHsueh | 5:31e3324d4c4b | 85 | |
LeoHsueh | 5:31e3324d4c4b | 86 | private: |
LeoHsueh | 5:31e3324d4c4b | 87 | |
LeoHsueh | 5:31e3324d4c4b | 88 | /** Regular mbed pins bus |
LeoHsueh | 5:31e3324d4c4b | 89 | */ |
LeoHsueh | 5:31e3324d4c4b | 90 | DigitalIn _up, _down, _left, _right, _press; |
LeoHsueh | 6:d354f6e3bd9b | 91 | |
LeoHsueh | 6:d354f6e3bd9b | 92 | FunctionPointer *_functionNone, *_functionUp, *_functionDown, *_functionLeft, *_functionRight, *_functionPress; |
alex89_2 | 0:9c486d50434e | 93 | }; |
alex89_2 | 0:9c486d50434e | 94 | |
LeoHsueh | 5:31e3324d4c4b | 95 | #endif |