Albert Tan-Mulligan / Mbed 2 deprecated ELEC2645_Project_el18ajst

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Gamepad.h Source File

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