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