ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18nw

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_os.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 class AnalogOut;
00016 class Ticker;
00017 }
00018 
00019 #define TOL 0.1f
00020 #define RAD2DEG 57.2957795131f
00021 
00022 /** Enum for direction */
00023 enum Direction {
00024     CENTRE,  /**< joystick centred */
00025     N,       /**< pushed North (0)*/
00026     NE,      /**< pushed North-East (45) */
00027     E,       /**< pushed East (90) */
00028     SE,      /**< pushed South-East (135) */
00029     S,       /**< pushed South (180) */
00030     SW,      /**< pushed South-West (225) */
00031     W,       /**< pushed West (270) */
00032     NW       /**< pushed North-West (315) */
00033 };
00034 
00035 /** Vector 2D struct */
00036 struct Vector2D {
00037     float x; /**< float for x value */
00038     float y; /**< float for y value */
00039 };
00040 
00041 /** Polar coordinate struct */
00042 struct Polar {
00043     float mag;  /**< float for magnitude */
00044     float angle; /**< float for angle (in degrees) */
00045 };
00046 
00047 /** Gamepad Class
00048  * @brief Library for interfacing with ELEC2645 Gamepad PCB, University of Leeds
00049  * @author Dr Craig A. Evans
00050  * @author Dr Alex Valavanis
00051  */
00052 class Gamepad
00053 {
00054 
00055 private:
00056     mbed::PwmOut *_led1;
00057     mbed::PwmOut *_led2;
00058     mbed::PwmOut *_led3;
00059     mbed::PwmOut *_led4;
00060     mbed::PwmOut *_led5;
00061     mbed::PwmOut *_led6;
00062 
00063     mbed::InterruptIn *_button_A;
00064     mbed::InterruptIn *_button_B;
00065     mbed::InterruptIn *_button_X;
00066     mbed::InterruptIn *_button_Y;
00067     mbed::InterruptIn *_button_start;
00068 
00069     mbed::AnalogIn *_vert;
00070     mbed::AnalogIn *_horiz;
00071 
00072     mbed::AnalogOut  *_speaker;
00073     mbed::AnalogIn *_pot1;
00074     mbed::AnalogIn *_pot2;
00075 
00076     mbed::Timeout *_timeout;
00077     mbed::Ticker *_ticker;
00078 
00079 
00080     
00081     // centred x,y values
00082     float _x0;
00083     float _y0;
00084 
00085 public:
00086     /** Constructor */
00087     Gamepad();
00088 
00089     /** Initialise all peripherals and configure interrupts */
00090     void init();
00091 
00092     /** Turn all LEDs on */
00093     void leds_on();
00094 
00095     /** Turn all LEDs off */
00096     void leds_off();
00097 
00098     /** Set all LEDs to duty-cycle
00099     *@param value in range 0.0 to 1.0
00100     */
00101     void leds(float val) const;
00102 
00103     /** Set LED to duty-cycle
00104     *@param led number (0 to 5)
00105     *@param value in range 0.0 to 1.0
00106     */
00107     void led(int n,float val) const;
00108 
00109     /** Read potentiometer 1 value
00110     *@returns potentiometer value in range 0.0 to 1.0
00111     */
00112     float read_pot1() const;
00113 
00114     /** Read potentiometer 2 value
00115     *@returns potentiometer value in range 0.0 to 1.0
00116     */
00117     float read_pot2() const;
00118 
00119     /** Play tone on piezo (NOTE: Frequencys below 1kHz can damage the speaker)
00120     * @param frequency in Hz
00121     * @param duration of tone in seconds
00122     */
00123     void tone(float frequency, float duration);
00124 
00125 
00126 
00127 
00128     /** Get magnitude of joystick movement
00129     * @returns value in range 0.0 to 1.0
00130     */
00131     float get_mag();
00132 
00133     /** Get angle of joystick movement
00134     * @returns value in range 0.0 to 359.9. 0.0 corresponds to N, 180.0 to S. -1.0 is central
00135     */
00136     float get_angle();
00137 
00138     /** Gets joystick direction
00139     * @returns an enum: CENTRE, N, NE, E, SE, S, SW, W, NW,
00140     */
00141     Direction get_direction();    // N,NE,E,SE etc.
00142 
00143     /** Gets raw cartesian co-ordinates of joystick
00144     * @returns a struct with x,y members, each in the range 0.0 to 1.0
00145     */
00146     Vector2D get_coord();         // cartesian co-ordinates x,y
00147 
00148     /** Gets cartesian coordinates mapped to circular grid
00149     * @returns a struct with x,y members, each in the range 0.0 to 1.0
00150     */
00151     Vector2D get_mapped_coord();  // x,y mapped to circle
00152 
00153     /** Gets polar coordinates of the joystick
00154     * @returns a struct contains mag and angle
00155     */
00156     Polar get_polar();            // mag and angle in struct form
00157 
00158 
00159     /** Resets all button states. Useful for calling inbetween scenes
00160     *   where you do not want button presses from the previous scene effecting
00161     *   the current scene
00162     */
00163     void reset_buttons();
00164 
00165     /** Returns true if A has been pressed
00166     * @returns a bool corresponding to A being pressed
00167     */
00168 
00169     bool A_pressed();
00170 
00171     /** Returns true if A is held
00172     * @returns a bool corresponding to A being held
00173     *
00174     */
00175     bool A_held();
00176     
00177 
00178     /** Returns true if B has been pressed
00179     * @returns a bool corresponding to B being pressed
00180     */
00181     bool B_pressed();
00182 
00183     /** Returns true if B is held
00184     * @returns a bool corresponding to B being held
00185     *
00186     */
00187     bool B_held();
00188 
00189     /** Returns true if B has been pressed
00190     * @returns a bool corresponding to B being pressed
00191     */
00192     bool X_pressed();
00193 
00194     /** Returns true if X is held
00195     * @returns a bool corresponding to X being held
00196     *
00197     */
00198     bool X_held();    
00199 
00200     /** Returns true if Y has been pressed
00201     * @returns a bool corresponding to Y being pressed
00202     */
00203     bool Y_pressed();
00204 
00205     /** Returns true if Y is held
00206     * @returns a bool corresponding to Y being held
00207     *
00208     */
00209     bool Y_held();
00210 
00211 
00212     /** Returns true if start has been pressed
00213     * @returns a bool corresponding to start being pressed
00214     */
00215     bool start_pressed();
00216 
00217     /** Returns true if start is held
00218     * @returns a bool corresponding to start being held
00219     *
00220     */
00221     bool start_held();
00222 
00223 
00224 
00225 
00226 private:
00227 
00228     void tone_off();
00229     void flip_DAC();
00230 
00231     volatile bool A_fall;
00232     void A_fall_interrupt();
00233 
00234     volatile bool B_fall;
00235     void B_fall_interrupt();
00236     
00237     volatile bool X_fall;
00238     void X_fall_interrupt();
00239 
00240     volatile bool Y_fall;
00241     void Y_fall_interrupt();
00242 
00243     volatile bool start_fall;
00244     void start_fall_interrupt();
00245 
00246 
00247 
00248 
00249 };
00250 
00251 #endif