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
Diff: InputManager.h
- Revision:
- 19:89c3eeb3761b
- Parent:
- 18:709ea375b0df
--- a/InputManager.h Mon May 11 04:40:23 2015 +0000 +++ b/InputManager.h Thu Mar 25 03:43:10 2021 +0000 @@ -3,14 +3,61 @@ #include "N5110.h" #include "PinDetect.h" -#include "Joystick.h" +#include "InputManager.h" /// @file InputManager.h + +/** @brief Enum used for the 8-directions of the joystick. */ +enum JoystickDirection {CENTER, UP, DOWN, LEFT, RIGHT, UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT, UNKNOWN}; + +/// Joystick class +class Joystick +{ + public: + + /** Creates a new Joystick object + * @param x Pin connected to the horizontal potentiometer of the joystick + * @param y Pin connected to the vertical potentiometer of the joystick + * @param button Pin connected to the button of the thumb joystick + */ + Joystick(PinName x, PinName y, PinName button); + + /** @brief Deconstructor. Frees allocated memory */ + ~Joystick(); + + /** @brief Updates the current direction and button status of the joystick */ + void update(); + + /** Calibrates the joystick. The joystick must be centered while this function is called */ + void calibrate(); // Calibrates joystick by updating the center positions + + /** Returns the current JoystickDirection based on last update + * @return The current JoystickDirection. + */ + int getDirection() {return dir;} + + /** Reads the value of the button + * @return 1 if pressed, 0 otherwise + */ + int readButton() {return *btn;}; + + /** 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). **/ + static const float DEAD_ZONE = 0.1; + + private: + AnalogIn *xPot; + AnalogIn *yPot; + DigitalIn *btn; + float dx, dy; + float centerX, centerY; + JoystickDirection dir; +}; + struct Input { /** Used as identificator for the different buttons */ - enum Button{ButtonA, ButtonB, ButtonC}; + enum Button{ButtonA, ButtonB, ButtonC, ButtonD}; }; /// Used to manage user input from buttons and thumb joystick @@ -22,11 +69,12 @@ * @param pinA Pin connected to button A * @param pinB Pin connected to button B * @param pinc Pin connected to button C + * @param pind Pin connected to button D * @param x Pin connected to the horizontal potentiometer of the joystick * @param y Pin connected to the vertical potentiometer of the joystick * @param button Pin connected to the button of the thumb joystick */ - InputManager(PinName pinA, PinName pinB, PinName pinC, PinName joyH, PinName joyV, PinName joyBtn); + InputManager(PinName pinA, PinName pinB, PinName pinC, PinName pinD, PinName joyH, PinName joyV, PinName joyBtn); /** Deconstructor. Frees allocated memory related to the buttons and the joystick **/ ~InputManager(); @@ -51,6 +99,7 @@ PinDetect *btnA; PinDetect *btnB; PinDetect *btnC; + PinDetect *btnD; /** Returns a pointer to the actual button object * @param button The requested button.