A complex 2D-dungeon game on LPC1768 in SWJTU-Leeds Joint School XJEL2645 project. Referenced from the framework contributed by https://os.mbed.com/users/Siriagus/code/SimplePlatformGame/
Dependencies: mbed N5110 ShiftReg PinDetect
InputManager.h@21:e19709a07756, 2021-03-25 (annotated)
- Committer:
- hugohu
- Date:
- Thu Mar 25 03:56:22 2021 +0000
- Branch:
- BRAVEHEART
- Revision:
- 21:e19709a07756
- Parent:
- 19:89c3eeb3761b
Combined some files, removed codes for my personal needs, created new maps, new ways to play.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Siriagus | 4:d6661b976359 | 1 | #ifndef INPUT_H |
Siriagus | 4:d6661b976359 | 2 | #define INPUT_H |
Siriagus | 4:d6661b976359 | 3 | |
Siriagus | 4:d6661b976359 | 4 | #include "N5110.h" |
Siriagus | 4:d6661b976359 | 5 | #include "PinDetect.h" |
hugohu | 19:89c3eeb3761b | 6 | #include "InputManager.h" |
Siriagus | 4:d6661b976359 | 7 | |
Siriagus | 17:d6a3b29cab31 | 8 | /// @file InputManager.h |
Siriagus | 10:f2488a0ecab7 | 9 | |
hugohu | 19:89c3eeb3761b | 10 | |
hugohu | 19:89c3eeb3761b | 11 | /** @brief Enum used for the 8-directions of the joystick. */ |
hugohu | 19:89c3eeb3761b | 12 | enum JoystickDirection {CENTER, UP, DOWN, LEFT, RIGHT, UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT, UNKNOWN}; |
hugohu | 19:89c3eeb3761b | 13 | |
hugohu | 19:89c3eeb3761b | 14 | /// Joystick class |
hugohu | 19:89c3eeb3761b | 15 | class Joystick |
hugohu | 19:89c3eeb3761b | 16 | { |
hugohu | 19:89c3eeb3761b | 17 | public: |
hugohu | 19:89c3eeb3761b | 18 | |
hugohu | 19:89c3eeb3761b | 19 | /** Creates a new Joystick object |
hugohu | 19:89c3eeb3761b | 20 | * @param x Pin connected to the horizontal potentiometer of the joystick |
hugohu | 19:89c3eeb3761b | 21 | * @param y Pin connected to the vertical potentiometer of the joystick |
hugohu | 19:89c3eeb3761b | 22 | * @param button Pin connected to the button of the thumb joystick |
hugohu | 19:89c3eeb3761b | 23 | */ |
hugohu | 19:89c3eeb3761b | 24 | Joystick(PinName x, PinName y, PinName button); |
hugohu | 19:89c3eeb3761b | 25 | |
hugohu | 19:89c3eeb3761b | 26 | /** @brief Deconstructor. Frees allocated memory */ |
hugohu | 19:89c3eeb3761b | 27 | ~Joystick(); |
hugohu | 19:89c3eeb3761b | 28 | |
hugohu | 19:89c3eeb3761b | 29 | /** @brief Updates the current direction and button status of the joystick */ |
hugohu | 19:89c3eeb3761b | 30 | void update(); |
hugohu | 19:89c3eeb3761b | 31 | |
hugohu | 19:89c3eeb3761b | 32 | /** Calibrates the joystick. The joystick must be centered while this function is called */ |
hugohu | 19:89c3eeb3761b | 33 | void calibrate(); // Calibrates joystick by updating the center positions |
hugohu | 19:89c3eeb3761b | 34 | |
hugohu | 19:89c3eeb3761b | 35 | /** Returns the current JoystickDirection based on last update |
hugohu | 19:89c3eeb3761b | 36 | * @return The current JoystickDirection. |
hugohu | 19:89c3eeb3761b | 37 | */ |
hugohu | 19:89c3eeb3761b | 38 | int getDirection() {return dir;} |
hugohu | 19:89c3eeb3761b | 39 | |
hugohu | 19:89c3eeb3761b | 40 | /** Reads the value of the button |
hugohu | 19:89c3eeb3761b | 41 | * @return 1 if pressed, 0 otherwise |
hugohu | 19:89c3eeb3761b | 42 | */ |
hugohu | 19:89c3eeb3761b | 43 | int readButton() {return *btn;}; |
hugohu | 19:89c3eeb3761b | 44 | |
hugohu | 19:89c3eeb3761b | 45 | /** Square set around the center of the joystick where the input is ignored. The axes are treated seperately. Can be varied from 0 (no dead-zone) to 0.5 (max value for dx and dy). **/ |
hugohu | 19:89c3eeb3761b | 46 | static const float DEAD_ZONE = 0.1; |
hugohu | 19:89c3eeb3761b | 47 | |
hugohu | 19:89c3eeb3761b | 48 | private: |
hugohu | 19:89c3eeb3761b | 49 | AnalogIn *xPot; |
hugohu | 19:89c3eeb3761b | 50 | AnalogIn *yPot; |
hugohu | 19:89c3eeb3761b | 51 | DigitalIn *btn; |
hugohu | 19:89c3eeb3761b | 52 | float dx, dy; |
hugohu | 19:89c3eeb3761b | 53 | float centerX, centerY; |
hugohu | 19:89c3eeb3761b | 54 | JoystickDirection dir; |
hugohu | 19:89c3eeb3761b | 55 | }; |
hugohu | 19:89c3eeb3761b | 56 | |
Siriagus | 9:da608ae65df9 | 57 | struct Input |
Siriagus | 9:da608ae65df9 | 58 | { |
Siriagus | 10:f2488a0ecab7 | 59 | /** Used as identificator for the different buttons */ |
hugohu | 19:89c3eeb3761b | 60 | enum Button{ButtonA, ButtonB, ButtonC, ButtonD}; |
Siriagus | 9:da608ae65df9 | 61 | }; |
Siriagus | 9:da608ae65df9 | 62 | |
Siriagus | 10:f2488a0ecab7 | 63 | /// Used to manage user input from buttons and thumb joystick |
Siriagus | 4:d6661b976359 | 64 | class InputManager |
Siriagus | 4:d6661b976359 | 65 | { |
Siriagus | 4:d6661b976359 | 66 | public: |
Siriagus | 4:d6661b976359 | 67 | |
Siriagus | 10:f2488a0ecab7 | 68 | /** Creates a new InputManager object |
Siriagus | 10:f2488a0ecab7 | 69 | * @param pinA Pin connected to button A |
Siriagus | 10:f2488a0ecab7 | 70 | * @param pinB Pin connected to button B |
Siriagus | 10:f2488a0ecab7 | 71 | * @param pinc Pin connected to button C |
hugohu | 19:89c3eeb3761b | 72 | * @param pind Pin connected to button D |
Siriagus | 10:f2488a0ecab7 | 73 | * @param x Pin connected to the horizontal potentiometer of the joystick |
Siriagus | 10:f2488a0ecab7 | 74 | * @param y Pin connected to the vertical potentiometer of the joystick |
Siriagus | 10:f2488a0ecab7 | 75 | * @param button Pin connected to the button of the thumb joystick |
Siriagus | 10:f2488a0ecab7 | 76 | */ |
hugohu | 19:89c3eeb3761b | 77 | InputManager(PinName pinA, PinName pinB, PinName pinC, PinName pinD, PinName joyH, PinName joyV, PinName joyBtn); |
Siriagus | 10:f2488a0ecab7 | 78 | |
Siriagus | 10:f2488a0ecab7 | 79 | /** Deconstructor. Frees allocated memory related to the buttons and the joystick **/ |
Siriagus | 4:d6661b976359 | 80 | ~InputManager(); |
Siriagus | 4:d6661b976359 | 81 | |
Siriagus | 4:d6661b976359 | 82 | Joystick *joystick; |
Siriagus | 10:f2488a0ecab7 | 83 | |
Siriagus | 10:f2488a0ecab7 | 84 | /** @brief Adds a button interrupt which is invoked when the button is pressed. Button needs to be released for the interrupt to occur again. |
Siriagus | 10:f2488a0ecab7 | 85 | * @param button Name of the button. |
Siriagus | 10:f2488a0ecab7 | 86 | * @param func Callback function. |
Siriagus | 10:f2488a0ecab7 | 87 | */ |
Siriagus | 10:f2488a0ecab7 | 88 | void addBtnPressInterrupt(Input::Button button, void (*func)(void)); |
Siriagus | 10:f2488a0ecab7 | 89 | |
Siriagus | 10:f2488a0ecab7 | 90 | |
Siriagus | 10:f2488a0ecab7 | 91 | /** Reads the current value of a button. |
Siriagus | 10:f2488a0ecab7 | 92 | * @param button The button we want to read. |
Siriagus | 10:f2488a0ecab7 | 93 | * @return Returns 1 if button is pressed, 0 otherwise. |
Siriagus | 10:f2488a0ecab7 | 94 | */ |
Siriagus | 10:f2488a0ecab7 | 95 | int read(Input::Button button); |
Siriagus | 10:f2488a0ecab7 | 96 | |
Siriagus | 10:f2488a0ecab7 | 97 | private: |
Siriagus | 10:f2488a0ecab7 | 98 | /// Button objects |
Siriagus | 4:d6661b976359 | 99 | PinDetect *btnA; |
Siriagus | 4:d6661b976359 | 100 | PinDetect *btnB; |
Siriagus | 4:d6661b976359 | 101 | PinDetect *btnC; |
hugohu | 19:89c3eeb3761b | 102 | PinDetect *btnD; |
Siriagus | 4:d6661b976359 | 103 | |
Siriagus | 10:f2488a0ecab7 | 104 | /** Returns a pointer to the actual button object |
Siriagus | 10:f2488a0ecab7 | 105 | * @param button The requested button. |
Siriagus | 10:f2488a0ecab7 | 106 | * @return Pointer to the button. |
Siriagus | 10:f2488a0ecab7 | 107 | */ |
Siriagus | 18:709ea375b0df | 108 | PinDetect* getBtnPtr(Input::Button button); |
Siriagus | 4:d6661b976359 | 109 | }; |
Siriagus | 4:d6661b976359 | 110 | |
Siriagus | 4:d6661b976359 | 111 | #endif |
Siriagus | 4:d6661b976359 | 112 |