ELEC2645 (2019/20) / Mbed 2 deprecated el18loc_final

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