A rouge-like rpg, heavily inspired on the binding of isaac. Running on a FRDM-K64F Mbed board. C++.

Dependencies:   mbed MotionSensor

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