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 N5110 ShiftReg PinDetect
InputManager.h
00001 #ifndef INPUT_H 00002 #define INPUT_H 00003 00004 #include "N5110.h" 00005 #include "PinDetect.h" 00006 #include "InputManager.h " 00007 00008 /// @file InputManager.h 00009 00010 00011 /** @brief Enum used for the 8-directions of the joystick. */ 00012 enum JoystickDirection {CENTER, UP, DOWN, LEFT, RIGHT, UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT, UNKNOWN}; 00013 00014 /// Joystick class 00015 class Joystick 00016 { 00017 public: 00018 00019 /** Creates a new Joystick object 00020 * @param x Pin connected to the horizontal potentiometer of the joystick 00021 * @param y Pin connected to the vertical potentiometer of the joystick 00022 * @param button Pin connected to the button of the thumb joystick 00023 */ 00024 Joystick(PinName x, PinName y, PinName button); 00025 00026 /** @brief Deconstructor. Frees allocated memory */ 00027 ~Joystick(); 00028 00029 /** @brief Updates the current direction and button status of the joystick */ 00030 void update(); 00031 00032 /** Calibrates the joystick. The joystick must be centered while this function is called */ 00033 void calibrate(); // Calibrates joystick by updating the center positions 00034 00035 /** Returns the current JoystickDirection based on last update 00036 * @return The current JoystickDirection. 00037 */ 00038 int getDirection() {return dir;} 00039 00040 /** Reads the value of the button 00041 * @return 1 if pressed, 0 otherwise 00042 */ 00043 int readButton() {return *btn;}; 00044 00045 /** 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). **/ 00046 static const float DEAD_ZONE = 0.1; 00047 00048 private: 00049 AnalogIn *xPot; 00050 AnalogIn *yPot; 00051 DigitalIn *btn; 00052 float dx, dy; 00053 float centerX, centerY; 00054 JoystickDirection dir; 00055 }; 00056 00057 struct Input 00058 { 00059 /** Used as identificator for the different buttons */ 00060 enum Button{ButtonA, ButtonB, ButtonC, ButtonD}; 00061 }; 00062 00063 /// Used to manage user input from buttons and thumb joystick 00064 class InputManager 00065 { 00066 public: 00067 00068 /** Creates a new InputManager object 00069 * @param pinA Pin connected to button A 00070 * @param pinB Pin connected to button B 00071 * @param pinc Pin connected to button C 00072 * @param pind Pin connected to button D 00073 * @param x Pin connected to the horizontal potentiometer of the joystick 00074 * @param y Pin connected to the vertical potentiometer of the joystick 00075 * @param button Pin connected to the button of the thumb joystick 00076 */ 00077 InputManager(PinName pinA, PinName pinB, PinName pinC, PinName pinD, PinName joyH, PinName joyV, PinName joyBtn); 00078 00079 /** Deconstructor. Frees allocated memory related to the buttons and the joystick **/ 00080 ~InputManager(); 00081 00082 Joystick *joystick; 00083 00084 /** @brief Adds a button interrupt which is invoked when the button is pressed. Button needs to be released for the interrupt to occur again. 00085 * @param button Name of the button. 00086 * @param func Callback function. 00087 */ 00088 void addBtnPressInterrupt(Input::Button button, void (*func)(void)); 00089 00090 00091 /** Reads the current value of a button. 00092 * @param button The button we want to read. 00093 * @return Returns 1 if button is pressed, 0 otherwise. 00094 */ 00095 int read(Input::Button button); 00096 00097 private: 00098 /// Button objects 00099 PinDetect *btnA; 00100 PinDetect *btnB; 00101 PinDetect *btnC; 00102 PinDetect *btnD; 00103 00104 /** Returns a pointer to the actual button object 00105 * @param button The requested button. 00106 * @return Pointer to the button. 00107 */ 00108 PinDetect* getBtnPtr(Input::Button button); 00109 }; 00110 00111 #endif 00112
Generated on Tue Jul 12 2022 21:30:55 by
