James Heavey / Mbed 2 deprecated 2665-Breakout-Game

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 #include "FXOS8700CQ.h"
00006 
00007 // Forward declaration of the classes that we use from the mbed library
00008 // This avoids the need for us to include the huge mbed.h header inside our
00009 // own library API
00010 namespace mbed
00011 {
00012 class AnalogIn;
00013 class InterruptIn;
00014 class PwmOut;
00015 class Timeout;
00016 }
00017 
00018 #define TOL 0.1f
00019 #define RAD2DEG 57.2957795131f
00020 
00021 /** Enum for direction */
00022 enum Direction {
00023     CENTRE,  /**< joystick centred */
00024     N,       /**< pushed North (0)*/
00025     NE,      /**< pushed North-East (45) */
00026     E,       /**< pushed East (90) */
00027     SE,      /**< pushed South-East (135) */
00028     S,       /**< pushed South (180) */
00029     SW,      /**< pushed South-West (225) */
00030     W,       /**< pushed West (270) */
00031     NW       /**< pushed North-West (315) */
00032 };
00033 
00034 /** Vector 2D struct */
00035 struct Vector2D {
00036     float x; /**< float for x value */
00037     float y; /**< float for y value */
00038 };
00039 
00040 /** Polar coordinate struct */
00041 struct Polar {
00042     float mag;  /**< float for magnitude */
00043     float angle; /**< float for angle (in degrees) */
00044 };
00045 
00046 /** Gamepad Class
00047 @brief Library for interfacing with ELEC2645 Gamepad PCB, University of Leeds
00048 @author Dr Craig A. Evans
00049 @author Dr Alex Valanvanis
00050 @date Febraury 2017
00051 */
00052 class Gamepad
00053 {
00054     public:
00055     /** Gamepad events 
00056  * @brief List of events that can be registered on the gamepad
00057  */
00058 enum GamepadEvent {
00059     A_PRESSED,     ///< Button A has been pressed
00060     B_PRESSED,     ///< Button B has been pressed
00061     X_PRESSED,     ///< Button X has been pressed
00062     Y_PRESSED,     ///< Button Y has been pressed
00063     L_PRESSED,     ///< Button L has been pressed
00064     R_PRESSED,     ///< Button R has been pressed
00065     BACK_PRESSED,  ///< Button "Back" has been pressed
00066     START_PRESSED, ///< Button "Start" has been pressed
00067     JOY_PRESSED,   ///< Joystick button has been pressed
00068     N_EVENTS       ///< A dummy flag that marks the end of the list
00069 };
00070 private:
00071     mbed::PwmOut *_led1;
00072     mbed::PwmOut *_led2;
00073     mbed::PwmOut *_led3;
00074     mbed::PwmOut *_led4;
00075     mbed::PwmOut *_led5;
00076     mbed::PwmOut *_led6;
00077 
00078     mbed::InterruptIn *_button_A;
00079     mbed::InterruptIn *_button_B;
00080     mbed::InterruptIn *_button_X;
00081     mbed::InterruptIn *_button_Y;
00082     mbed::InterruptIn *_button_L;
00083     mbed::InterruptIn *_button_R;
00084     mbed::InterruptIn *_button_back;
00085     mbed::InterruptIn *_button_start;
00086     mbed::InterruptIn *_button_joystick;
00087 
00088     mbed::AnalogIn *_vert;
00089     mbed::AnalogIn *_horiz;
00090 
00091     mbed::PwmOut   *_buzzer;
00092     mbed::AnalogIn *_pot;
00093 
00094     mbed::Timeout *_timeout;
00095 
00096     std::bitset<N_EVENTS> _event_state; ///< A binary list of buttons that has been pressed
00097 
00098     // centred x,y values
00099     float _x0;
00100     float _y0;
00101 
00102 public:
00103 
00104     /** Constructor */
00105     Gamepad();
00106 
00107     /** Destructor */
00108     ~Gamepad();
00109 
00110     /** Initialise all peripherals and configure interrupts */
00111     void init();
00112 
00113     /** Turn all LEDs on */
00114     void leds_on();
00115 
00116     /** Turn all LEDs off */
00117     void leds_off();
00118 
00119     /** Set all LEDs to duty-cycle
00120     *@param value in range 0.0 to 1.0
00121     */
00122     void leds(float val) const;
00123 
00124     /** Set LED to duty-cycle
00125     *@param led number (0 to 5)
00126     *@param value in range 0.0 to 1.0
00127     */
00128     void led(int n,float val) const;
00129 
00130     /** Read potentiometer
00131     *@returns potentiometer value in range 0.0 to 1.0
00132     */
00133     float read_pot() const;
00134 
00135     /** Play tone on piezo
00136     * @param frequency in Hz
00137     * @param duration of tone in seconds
00138     */
00139     void tone(float frequency, float duration);
00140 
00141     /**
00142      * @brief Check whether an event flag has been set and clear it
00143      * @param id[in] The ID of the event to test
00144      * @return true if the event occurred
00145      */
00146     bool check_event(GamepadEvent const id);
00147     
00148     /** Reset all button flags */
00149     void reset_flags();
00150 
00151     /** Get magnitude of joystick movement
00152     * @returns value in range 0.0 to 1.0
00153     */
00154 
00155     float get_mag();
00156 
00157     /** Get angle of joystick movement
00158     * @returns value in range 0.0 to 359.9. 0.0 corresponds to N, 180.0 to S. -1.0 is central
00159     */
00160     float get_angle();
00161 
00162     /** Gets joystick direction
00163     * @returns an enum: CENTRE, N, NE, E, SE, S, SW, W, NW,
00164     */
00165     Direction get_direction();    // N,NE,E,SE etc.
00166 
00167     /** Gets raw cartesian co-ordinates of joystick
00168     * @returns a struct with x,y members, each in the range 0.0 to 1.0
00169     */
00170     Vector2D get_coord();         // cartesian co-ordinates x,y
00171 
00172     /** Gets cartesian coordinates mapped to circular grid
00173     * @returns a struct with x,y members, each in the range 0.0 to 1.0
00174     */
00175     Vector2D get_mapped_coord();  // x,y mapped to circle
00176 
00177     /** Gets polar coordinates of the joystick
00178     * @returns a struct contains mag and angle
00179     */
00180     Polar get_polar();            // mag and angle in struct form
00181 
00182 private:
00183     void init_buttons();
00184     void tone_off();
00185 
00186     void a_isr();
00187     void b_isr();
00188     void x_isr();
00189     void y_isr();
00190     void l_isr();
00191     void r_isr();
00192     void back_isr();
00193     void start_isr();
00194     void joy_isr();
00195 };
00196 
00197 #endif