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.
Gamepad.h
00001 /** Gamepad Class 00002 * @brief Does nothing useful 00003 * @author Du Xianjie 00004 * @date May, 2019 00005 */ 00006 #ifndef GAMEPAD_H 00007 #define GAMEPAD_H 00008 00009 #include <bitset> 00010 00011 // Forward declaration of the classes that we use from the mbed library 00012 // This avoids the need for us to include the huge mbed.h header inside our 00013 // own library API 00014 namespace mbed 00015 { 00016 class AnalogIn; 00017 class InterruptIn; 00018 class PwmOut; 00019 class Timeout; 00020 } 00021 00022 #define TOL 0.1f 00023 #define RAD2DEG 57.2957795131f 00024 00025 /** Enum for direction */ 00026 enum Direction { 00027 CENTRE, /**< joystick centred */ 00028 N, /**< pushed North (0)*/ 00029 NE, /**< pushed North-East (45) */ 00030 E, /**< pushed East (90) */ 00031 SE, /**< pushed South-East (135) */ 00032 S, /**< pushed South (180) */ 00033 SW, /**< pushed South-West (225) */ 00034 W, /**< pushed West (270) */ 00035 NW /**< pushed North-West (315) */ 00036 }; 00037 00038 /** Vector 2D struct */ 00039 struct Vector2D { 00040 float x; /**< float for x value */ 00041 float y; /**< float for y value */ 00042 }; 00043 00044 /** Polar coordinate struct */ 00045 struct Polar { 00046 float mag; /**< float for magnitude */ 00047 float angle; /**< float for angle (in degrees) */ 00048 }; 00049 00050 /** Gamepad Class 00051 @brief Library for interfacing with ELEC2645 Gamepad PCB, University of Leeds 00052 @author Dr Craig A. Evans 00053 @author Dr Alex Valanvanis 00054 @date Febraury 2017 00055 */ 00056 class Gamepad 00057 { 00058 public: 00059 /** Gamepad events 00060 * @brief List of events that can be registered on the gamepad 00061 */ 00062 enum GamepadEvent { 00063 A_PRESSED, ///< Button A has been pressed 00064 B_PRESSED, ///< Button B has been pressed 00065 X_PRESSED, ///< Button X has been pressed 00066 Y_PRESSED, ///< Button Y has been pressed 00067 L_PRESSED, ///< Button L has been pressed 00068 R_PRESSED, ///< Button R has been pressed 00069 BACK_PRESSED, ///< Button "Back" has been pressed 00070 START_PRESSED, ///< Button "Start" has been pressed 00071 JOY_PRESSED, ///< Joystick button has been pressed 00072 N_EVENTS ///< A dummy flag that marks the end of the list 00073 }; 00074 private: 00075 mbed::PwmOut *_led1; 00076 mbed::PwmOut *_led2; 00077 mbed::PwmOut *_led3; 00078 mbed::PwmOut *_led4; 00079 mbed::PwmOut *_led5; 00080 mbed::PwmOut *_led6; 00081 00082 mbed::InterruptIn *_button_A; 00083 mbed::InterruptIn *_button_B; 00084 mbed::InterruptIn *_button_X; 00085 mbed::InterruptIn *_button_Y; 00086 mbed::InterruptIn *_button_L; 00087 mbed::InterruptIn *_button_R; 00088 mbed::InterruptIn *_button_back; 00089 mbed::InterruptIn *_button_start; 00090 mbed::InterruptIn *_button_joystick; 00091 00092 mbed::AnalogIn *_vert; 00093 mbed::AnalogIn *_horiz; 00094 00095 mbed::PwmOut *_buzzer; 00096 mbed::AnalogIn *_pot; 00097 00098 mbed::Timeout *_timeout; 00099 00100 std::bitset<N_EVENTS> _event_state; ///< A binary list of buttons that has been pressed 00101 00102 // centred x,y values 00103 float _x0; 00104 float _y0; 00105 00106 public: 00107 00108 /** Constructor */ 00109 Gamepad(); 00110 00111 /** Destructor */ 00112 ~Gamepad(); 00113 00114 /** Initialise all peripherals and configure interrupts */ 00115 void init(); 00116 00117 /** Turn all LEDs on */ 00118 void leds_on(); 00119 00120 /** Turn all LEDs off */ 00121 void leds_off(); 00122 00123 /** Set all LEDs to duty-cycle 00124 *@param value in range 0.0 to 1.0 00125 */ 00126 void leds(float val) const; 00127 00128 /** Set LED to duty-cycle 00129 *@param led number (0 to 5) 00130 *@param value in range 0.0 to 1.0 00131 */ 00132 void led(int n,float val) const; 00133 00134 /** Read potentiometer 00135 *@returns potentiometer value in range 0.0 to 1.0 00136 */ 00137 float read_pot() const; 00138 00139 /** Play tone on piezo 00140 * @param frequency in Hz 00141 * @param duration of tone in seconds 00142 */ 00143 void tone(float frequency, float duration); 00144 00145 /** 00146 * @brief Check whether an event flag has been set and clear it 00147 * @param id[in] The ID of the event to test 00148 * @return true if the event occurred 00149 */ 00150 bool check_event(GamepadEvent const id); 00151 00152 /** Get magnitude of joystick movement 00153 * @returns value in range 0.0 to 1.0 00154 */ 00155 00156 float get_mag(); 00157 00158 /** Get angle of joystick movement 00159 * @returns value in range 0.0 to 359.9. 0.0 corresponds to N, 180.0 to S. -1.0 is central 00160 */ 00161 float get_angle(); 00162 00163 /** Gets joystick direction 00164 * @returns an enum: CENTRE, N, NE, E, SE, S, SW, W, NW, 00165 */ 00166 Direction get_direction(); // N,NE,E,SE etc. 00167 00168 /** Gets raw cartesian co-ordinates of joystick 00169 * @returns a struct with x,y members, each in the range 0.0 to 1.0 00170 */ 00171 Vector2D get_coord(); // cartesian co-ordinates x,y 00172 00173 /** Gets cartesian coordinates mapped to circular grid 00174 * @returns a struct with x,y members, each in the range 0.0 to 1.0 00175 */ 00176 Vector2D get_mapped_coord(); // x,y mapped to circle 00177 00178 /** Gets polar coordinates of the joystick 00179 * @returns a struct contains mag and angle 00180 */ 00181 Polar get_polar(); // mag and angle in struct form 00182 00183 private: 00184 void init_buttons(); 00185 void tone_off(); 00186 00187 void a_isr(); 00188 void b_isr(); 00189 void x_isr(); 00190 void y_isr(); 00191 void l_isr(); 00192 void r_isr(); 00193 void back_isr(); 00194 void start_isr(); 00195 void joy_isr(); 00196 }; 00197 00198 #endif
Generated on Mon Jul 18 2022 14:19:10 by
