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