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 AnalogOut; 00015 class Ticker; 00016 class Timeout; 00017 } 00018 00019 #define TOL 0.1f 00020 #define RAD2DEG 57.2957795131f 00021 #define PI 3.14159265359 00022 00023 00024 /** Enum for direction */ 00025 enum Direction { 00026 CENTRE, /**< joystick centred */ 00027 N, /**< pushed North (0)*/ 00028 NE, /**< pushed North-East (45) */ 00029 E, /**< pushed East (90) */ 00030 SE, /**< pushed South-East (135) */ 00031 S, /**< pushed South (180) */ 00032 SW, /**< pushed South-West (225) */ 00033 W, /**< pushed West (270) */ 00034 NW /**< pushed North-West (315) */ 00035 }; 00036 00037 /** Vector 2D struct */ 00038 struct Vector2D { 00039 float x; /**< float for x value */ 00040 float y; /**< float for y value */ 00041 }; 00042 00043 /** Polar coordinate struct */ 00044 struct Polar { 00045 float mag; /**< float for magnitude */ 00046 float angle; /**< float for angle (in degrees) */ 00047 }; 00048 00049 /** Gamepad Class 00050 * @brief Library for interfacing with ELEC2645 Gamepad PCB, University of Leeds 00051 * @author Dr Craig A. Evans 00052 * @author Dr Alex Valavanis 00053 * @author Joshua Davy 00054 */ 00055 class Gamepad 00056 { 00057 00058 private: 00059 mbed::PwmOut *_led1; 00060 mbed::PwmOut *_led2; 00061 mbed::PwmOut *_led3; 00062 mbed::PwmOut *_led4; 00063 mbed::PwmOut *_led5; 00064 mbed::PwmOut *_led6; 00065 00066 mbed::InterruptIn *_button_A; 00067 mbed::InterruptIn *_button_B; 00068 mbed::InterruptIn *_button_X; 00069 mbed::InterruptIn *_button_Y; 00070 mbed::InterruptIn *_button_start; 00071 00072 mbed::AnalogIn *_vert; 00073 mbed::AnalogIn *_horiz; 00074 00075 mbed::AnalogIn *_pot1; 00076 mbed::AnalogIn *_pot2; 00077 mbed::AnalogIn *_adc; 00078 00079 mbed::AnalogOut *dac; 00080 mbed::Ticker *ticker; 00081 mbed::Timeout *timeout; 00082 mbed::Timeout *note_timeout; 00083 00084 // centred x,y values 00085 float _x0; 00086 float _y0; 00087 00088 float *_sample_array; 00089 const int *_notes; 00090 const int *_durations; 00091 00092 int _n; 00093 int _melody_length; 00094 volatile unsigned int _sample; 00095 volatile unsigned int _note; 00096 float _bpm; 00097 bool _repeat; 00098 00099 00100 public: 00101 /** Constructor */ 00102 Gamepad(); 00103 00104 /** Initialise all peripherals and configure interrupts */ 00105 void init(); 00106 00107 /** Turn all LEDs on */ 00108 void leds_on(); 00109 00110 /** Turn all LEDs off */ 00111 void leds_off(); 00112 00113 /** Set all LEDs to duty-cycle 00114 *@param value in range 0.0 to 1.0 00115 */ 00116 void leds(float val) const; 00117 00118 /** Set LED to duty-cycle 00119 *@param led number (0 to 5) 00120 *@param value in range 0.0 to 1.0 00121 */ 00122 void led(int n,float val) const; 00123 00124 /** Read potentiometer 1 value 00125 *@returns potentiometer value in range 0.0 to 1.0 00126 */ 00127 float read_pot1() const; 00128 00129 /** Read potentiometer 2 value 00130 *@returns potentiometer value in range 0.0 to 1.0 00131 */ 00132 float read_pot2() const; 00133 00134 /** Read padc value 00135 *@returns random adc value 00136 */ 00137 float read_adc() const; 00138 00139 /** Get magnitude of joystick movement 00140 * @returns value in range 0.0 to 1.0 00141 */ 00142 float get_mag(); 00143 00144 /** Get angle of joystick movement 00145 * @returns value in range 0.0 to 359.9. 0.0 corresponds to N, 180.0 to S. -1.0 is central 00146 */ 00147 float get_angle(); 00148 00149 /** Gets joystick direction 00150 * @returns an enum: CENTRE, N, NE, E, SE, S, SW, W, NW, 00151 */ 00152 Direction get_direction(); // N,NE,E,SE etc. 00153 00154 /** Gets raw cartesian co-ordinates of joystick 00155 * @returns a struct with x,y members, each in the range 0.0 to 1.0 00156 */ 00157 Vector2D get_coord(); // cartesian co-ordinates x,y 00158 00159 /** Gets cartesian coordinates mapped to circular grid 00160 * @returns a struct with x,y members, each in the range 0.0 to 1.0 00161 */ 00162 Vector2D get_mapped_coord(); // x,y mapped to circle 00163 00164 /** Gets polar coordinates of the joystick 00165 * @returns a struct contains mag and angle 00166 */ 00167 Polar get_polar(); // mag and angle in struct form 00168 00169 00170 /** Resets all button states. Useful for calling inbetween scenes 00171 * where you do not want button presses from the previous scene effecting 00172 * the current scene 00173 */ 00174 void reset_buttons(); 00175 00176 /** Returns true if A has been pressed 00177 * @returns a bool corresponding to A being pressed 00178 */ 00179 00180 bool A_pressed(); 00181 00182 /** Returns true if A is held 00183 * @returns a bool corresponding to A being held 00184 * 00185 */ 00186 bool A_held(); 00187 00188 00189 /** Returns true if B has been pressed 00190 * @returns a bool corresponding to B being pressed 00191 */ 00192 bool B_pressed(); 00193 00194 /** Returns true if B is held 00195 * @returns a bool corresponding to B being held 00196 * 00197 */ 00198 bool B_held(); 00199 00200 /** Returns true if B has been pressed 00201 * @returns a bool corresponding to B being pressed 00202 */ 00203 bool X_pressed(); 00204 00205 /** Returns true if X is held 00206 * @returns a bool corresponding to X being held 00207 * 00208 */ 00209 bool X_held(); 00210 00211 /** Returns true if Y has been pressed 00212 * @returns a bool corresponding to Y being pressed 00213 */ 00214 bool Y_pressed(); 00215 00216 /** Returns true if Y is held 00217 * @returns a bool corresponding to Y being held 00218 * 00219 */ 00220 bool Y_held(); 00221 00222 00223 /** Returns true if start has been pressed 00224 * @returns a bool corresponding to start being pressed 00225 */ 00226 bool start_pressed(); 00227 00228 /** Returns true if start is held 00229 * @returns a bool corresponding to start being held 00230 * 00231 */ 00232 bool start_held(); 00233 00234 /** Play a single tone for the specifed duration 00235 *@param note frequency (in Hz) 00236 *@param duration (in s) 00237 */ 00238 void tone(const float frequency,const float duration); 00239 00240 /** Play a melody 00241 *@param length of note array 00242 *@param array of note frequencies (in Hz) - 0 treated as a rest 00243 *@param array of note durations (4 corresponds to 1/4, 8 is 1/8 etc.) 00244 *@param beats per minute 00245 *@param whether to repeat or play just once 00246 */ 00247 void play_melody(int length,const int *notes,const int *durations,float bpm,bool repeat); 00248 00249 /** Set the BPM of the melody 00250 *@param beats per minute 00251 */ 00252 void set_bpm(float bpm); 00253 00254 /** Write an analog voltage to the speaker 00255 *@param voltage in range 0.0 to 1.0 (corresponds 0.0 to 3.3 V) 00256 */ 00257 void write_dac(float val); 00258 00259 00260 00261 00262 private: 00263 00264 volatile bool A_fall; 00265 void A_fall_interrupt(); 00266 00267 volatile bool B_fall; 00268 void B_fall_interrupt(); 00269 00270 volatile bool X_fall; 00271 void X_fall_interrupt(); 00272 00273 volatile bool Y_fall; 00274 void Y_fall_interrupt(); 00275 00276 volatile bool start_fall; 00277 void start_fall_interrupt(); 00278 00279 // Tone functions 00280 void ticker_isr(); 00281 void timeout_isr(); 00282 void note_timeout_isr(); 00283 void play_next_note(); 00284 00285 00286 00287 }; 00288 00289 // Note definitions from Arduino.cc 00290 #define NOTE_B0 31 00291 #define NOTE_C1 33 00292 #define NOTE_CS1 35 00293 #define NOTE_D1 37 00294 #define NOTE_DS1 39 00295 #define NOTE_E1 41 00296 #define NOTE_F1 44 00297 #define NOTE_FS1 46 00298 #define NOTE_G1 49 00299 #define NOTE_GS1 52 00300 #define NOTE_A1 55 00301 #define NOTE_AS1 58 00302 #define NOTE_B1 62 00303 #define NOTE_C2 65 00304 #define NOTE_CS2 69 00305 #define NOTE_D2 73 00306 #define NOTE_DS2 78 00307 #define NOTE_E2 82 00308 #define NOTE_F2 87 00309 #define NOTE_FS2 93 00310 #define NOTE_G2 98 00311 #define NOTE_GS2 104 00312 #define NOTE_A2 110 00313 #define NOTE_AS2 117 00314 #define NOTE_B2 123 00315 #define NOTE_C3 131 00316 #define NOTE_CS3 139 00317 #define NOTE_D3 147 00318 #define NOTE_DS3 156 00319 #define NOTE_E3 165 00320 #define NOTE_F3 175 00321 #define NOTE_FS3 185 00322 #define NOTE_G3 196 00323 #define NOTE_GS3 208 00324 #define NOTE_A3 220 00325 #define NOTE_AS3 233 00326 #define NOTE_B3 247 00327 #define NOTE_C4 262 00328 #define NOTE_CS4 277 00329 #define NOTE_D4 294 00330 #define NOTE_DS4 311 00331 #define NOTE_E4 330 00332 #define NOTE_F4 349 00333 #define NOTE_FS4 370 00334 #define NOTE_G4 392 00335 #define NOTE_GS4 415 00336 #define NOTE_A4 440 00337 #define NOTE_AS4 466 00338 #define NOTE_B4 494 00339 #define NOTE_C5 523 00340 #define NOTE_CS5 554 00341 #define NOTE_D5 587 00342 #define NOTE_DS5 622 00343 #define NOTE_E5 659 00344 #define NOTE_F5 698 00345 #define NOTE_FS5 740 00346 #define NOTE_G5 784 00347 #define NOTE_GS5 831 00348 #define NOTE_A5 880 00349 #define NOTE_AS5 932 00350 #define NOTE_B5 988 00351 #define NOTE_C6 1047 00352 #define NOTE_CS6 1109 00353 #define NOTE_D6 1175 00354 #define NOTE_DS6 1245 00355 #define NOTE_E6 1319 00356 #define NOTE_F6 1397 00357 #define NOTE_FS6 1480 00358 #define NOTE_G6 1568 00359 #define NOTE_GS6 1661 00360 #define NOTE_A6 1760 00361 #define NOTE_AS6 1865 00362 #define NOTE_B6 1976 00363 #define NOTE_C7 2093 00364 #define NOTE_CS7 2217 00365 #define NOTE_D7 2349 00366 #define NOTE_DS7 2489 00367 #define NOTE_E7 2637 00368 #define NOTE_F7 2794 00369 #define NOTE_FS7 2960 00370 #define NOTE_G7 3136 00371 #define NOTE_GS7 3322 00372 #define NOTE_A7 3520 00373 #define NOTE_AS7 3729 00374 #define NOTE_B7 3951 00375 #define NOTE_C8 4186 00376 #define NOTE_CS8 4435 00377 #define NOTE_D8 4699 00378 #define NOTE_DS8 4978 00379 #define REST 0 00380 00381 #endif
Generated on Fri Aug 5 2022 06:55:07 by
1.7.2