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