Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Gamepad/Gamepad.h@15:e3fc18eb519e, 2017-05-05 (annotated)
- Committer:
- el15as
- Date:
- Fri May 05 12:14:48 2017 +0000
- Revision:
- 15:e3fc18eb519e
Final version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el15as | 15:e3fc18eb519e | 1 | #ifndef GAMEPAD_H |
el15as | 15:e3fc18eb519e | 2 | #define GAMEPAD_H |
el15as | 15:e3fc18eb519e | 3 | |
el15as | 15:e3fc18eb519e | 4 | #include <bitset> |
el15as | 15:e3fc18eb519e | 5 | |
el15as | 15:e3fc18eb519e | 6 | // Forward declaration of the classes that we use from the mbed library |
el15as | 15:e3fc18eb519e | 7 | // This avoids the need for us to include the huge mbed.h header inside our |
el15as | 15:e3fc18eb519e | 8 | // own library API |
el15as | 15:e3fc18eb519e | 9 | namespace mbed |
el15as | 15:e3fc18eb519e | 10 | { |
el15as | 15:e3fc18eb519e | 11 | class AnalogIn; |
el15as | 15:e3fc18eb519e | 12 | class InterruptIn; |
el15as | 15:e3fc18eb519e | 13 | class PwmOut; |
el15as | 15:e3fc18eb519e | 14 | class Timeout; |
el15as | 15:e3fc18eb519e | 15 | } |
el15as | 15:e3fc18eb519e | 16 | |
el15as | 15:e3fc18eb519e | 17 | #define TOL 0.1f |
el15as | 15:e3fc18eb519e | 18 | #define RAD2DEG 57.2957795131f |
el15as | 15:e3fc18eb519e | 19 | |
el15as | 15:e3fc18eb519e | 20 | /** Enum for direction */ |
el15as | 15:e3fc18eb519e | 21 | enum Direction { |
el15as | 15:e3fc18eb519e | 22 | CENTRE, /**< joystick centred */ |
el15as | 15:e3fc18eb519e | 23 | N, /**< pushed North (0)*/ |
el15as | 15:e3fc18eb519e | 24 | NE, /**< pushed North-East (45) */ |
el15as | 15:e3fc18eb519e | 25 | E, /**< pushed East (90) */ |
el15as | 15:e3fc18eb519e | 26 | SE, /**< pushed South-East (135) */ |
el15as | 15:e3fc18eb519e | 27 | S, /**< pushed South (180) */ |
el15as | 15:e3fc18eb519e | 28 | SW, /**< pushed South-West (225) */ |
el15as | 15:e3fc18eb519e | 29 | W, /**< pushed West (270) */ |
el15as | 15:e3fc18eb519e | 30 | NW /**< pushed North-West (315) */ |
el15as | 15:e3fc18eb519e | 31 | }; |
el15as | 15:e3fc18eb519e | 32 | |
el15as | 15:e3fc18eb519e | 33 | /** Vector 2D struct */ |
el15as | 15:e3fc18eb519e | 34 | struct Vector2D { |
el15as | 15:e3fc18eb519e | 35 | float x; /**< float for x value */ |
el15as | 15:e3fc18eb519e | 36 | float y; /**< float for y value */ |
el15as | 15:e3fc18eb519e | 37 | }; |
el15as | 15:e3fc18eb519e | 38 | |
el15as | 15:e3fc18eb519e | 39 | /** Vector 2D struct with integers*/ |
el15as | 15:e3fc18eb519e | 40 | struct intVector2D { |
el15as | 15:e3fc18eb519e | 41 | int x; /**< int for x value */ |
el15as | 15:e3fc18eb519e | 42 | int y; /**< int for y value */ |
el15as | 15:e3fc18eb519e | 43 | }; |
el15as | 15:e3fc18eb519e | 44 | |
el15as | 15:e3fc18eb519e | 45 | /** Polar coordinate struct */ |
el15as | 15:e3fc18eb519e | 46 | struct Polar { |
el15as | 15:e3fc18eb519e | 47 | float mag; /**< float for magnitude */ |
el15as | 15:e3fc18eb519e | 48 | float angle; /**< float for angle (in degrees) */ |
el15as | 15:e3fc18eb519e | 49 | }; |
el15as | 15:e3fc18eb519e | 50 | |
el15as | 15:e3fc18eb519e | 51 | /** Gamepad Class |
el15as | 15:e3fc18eb519e | 52 | @brief Library for interfacing with ELEC2645 Gamepad PCB, University of Leeds |
el15as | 15:e3fc18eb519e | 53 | @author Dr Craig A. Evans |
el15as | 15:e3fc18eb519e | 54 | @author Dr Alex Valanvanis |
el15as | 15:e3fc18eb519e | 55 | @date Febraury 2017 |
el15as | 15:e3fc18eb519e | 56 | */ |
el15as | 15:e3fc18eb519e | 57 | class Gamepad |
el15as | 15:e3fc18eb519e | 58 | { |
el15as | 15:e3fc18eb519e | 59 | public: |
el15as | 15:e3fc18eb519e | 60 | /** Gamepad events |
el15as | 15:e3fc18eb519e | 61 | * @brief List of events that can be registered on the gamepad |
el15as | 15:e3fc18eb519e | 62 | */ |
el15as | 15:e3fc18eb519e | 63 | enum GamepadEvent { |
el15as | 15:e3fc18eb519e | 64 | A_PRESSED, ///< Button A has been pressed |
el15as | 15:e3fc18eb519e | 65 | B_PRESSED, ///< Button B has been pressed |
el15as | 15:e3fc18eb519e | 66 | X_PRESSED, ///< Button X has been pressed |
el15as | 15:e3fc18eb519e | 67 | Y_PRESSED, ///< Button Y has been pressed |
el15as | 15:e3fc18eb519e | 68 | L_PRESSED, ///< Button L has been pressed |
el15as | 15:e3fc18eb519e | 69 | R_PRESSED, ///< Button R has been pressed |
el15as | 15:e3fc18eb519e | 70 | BACK_PRESSED, ///< Button "Back" has been pressed |
el15as | 15:e3fc18eb519e | 71 | START_PRESSED, ///< Button "Start" has been pressed |
el15as | 15:e3fc18eb519e | 72 | JOY_PRESSED, ///< Joystick button has been pressed |
el15as | 15:e3fc18eb519e | 73 | N_EVENTS ///< A dummy flag that marks the end of the list |
el15as | 15:e3fc18eb519e | 74 | }; |
el15as | 15:e3fc18eb519e | 75 | private: |
el15as | 15:e3fc18eb519e | 76 | mbed::PwmOut *_led1; |
el15as | 15:e3fc18eb519e | 77 | mbed::PwmOut *_led2; |
el15as | 15:e3fc18eb519e | 78 | mbed::PwmOut *_led3; |
el15as | 15:e3fc18eb519e | 79 | mbed::PwmOut *_led4; |
el15as | 15:e3fc18eb519e | 80 | mbed::PwmOut *_led5; |
el15as | 15:e3fc18eb519e | 81 | mbed::PwmOut *_led6; |
el15as | 15:e3fc18eb519e | 82 | |
el15as | 15:e3fc18eb519e | 83 | mbed::InterruptIn *_button_A; |
el15as | 15:e3fc18eb519e | 84 | mbed::InterruptIn *_button_B; |
el15as | 15:e3fc18eb519e | 85 | mbed::InterruptIn *_button_X; |
el15as | 15:e3fc18eb519e | 86 | mbed::InterruptIn *_button_Y; |
el15as | 15:e3fc18eb519e | 87 | mbed::InterruptIn *_button_L; |
el15as | 15:e3fc18eb519e | 88 | mbed::InterruptIn *_button_R; |
el15as | 15:e3fc18eb519e | 89 | mbed::InterruptIn *_button_back; |
el15as | 15:e3fc18eb519e | 90 | mbed::InterruptIn *_button_start; |
el15as | 15:e3fc18eb519e | 91 | mbed::InterruptIn *_button_joystick; |
el15as | 15:e3fc18eb519e | 92 | |
el15as | 15:e3fc18eb519e | 93 | mbed::AnalogIn *_vert; |
el15as | 15:e3fc18eb519e | 94 | mbed::AnalogIn *_horiz; |
el15as | 15:e3fc18eb519e | 95 | |
el15as | 15:e3fc18eb519e | 96 | mbed::PwmOut *_buzzer; |
el15as | 15:e3fc18eb519e | 97 | mbed::AnalogIn *_pot; |
el15as | 15:e3fc18eb519e | 98 | |
el15as | 15:e3fc18eb519e | 99 | mbed::Timeout *_timeout; |
el15as | 15:e3fc18eb519e | 100 | |
el15as | 15:e3fc18eb519e | 101 | std::bitset<N_EVENTS> _event_state; ///< A binary list of buttons that has been pressed |
el15as | 15:e3fc18eb519e | 102 | |
el15as | 15:e3fc18eb519e | 103 | // centred x,y values |
el15as | 15:e3fc18eb519e | 104 | float _x0; |
el15as | 15:e3fc18eb519e | 105 | float _y0; |
el15as | 15:e3fc18eb519e | 106 | |
el15as | 15:e3fc18eb519e | 107 | public: |
el15as | 15:e3fc18eb519e | 108 | |
el15as | 15:e3fc18eb519e | 109 | /** Constructor */ |
el15as | 15:e3fc18eb519e | 110 | Gamepad(); |
el15as | 15:e3fc18eb519e | 111 | |
el15as | 15:e3fc18eb519e | 112 | /** Destructor */ |
el15as | 15:e3fc18eb519e | 113 | ~Gamepad(); |
el15as | 15:e3fc18eb519e | 114 | |
el15as | 15:e3fc18eb519e | 115 | /** Initialise all peripherals and configure interrupts */ |
el15as | 15:e3fc18eb519e | 116 | void init(); |
el15as | 15:e3fc18eb519e | 117 | |
el15as | 15:e3fc18eb519e | 118 | /** Turn all LEDs on */ |
el15as | 15:e3fc18eb519e | 119 | void leds_on(); |
el15as | 15:e3fc18eb519e | 120 | |
el15as | 15:e3fc18eb519e | 121 | /** Turn all LEDs off */ |
el15as | 15:e3fc18eb519e | 122 | void leds_off(); |
el15as | 15:e3fc18eb519e | 123 | |
el15as | 15:e3fc18eb519e | 124 | /** Set all LEDs to duty-cycle |
el15as | 15:e3fc18eb519e | 125 | *@param value in range 0.0 to 1.0 |
el15as | 15:e3fc18eb519e | 126 | */ |
el15as | 15:e3fc18eb519e | 127 | void leds(float val) const; |
el15as | 15:e3fc18eb519e | 128 | |
el15as | 15:e3fc18eb519e | 129 | /** Set LED to duty-cycle |
el15as | 15:e3fc18eb519e | 130 | *@param led number (0 to 5) |
el15as | 15:e3fc18eb519e | 131 | *@param value in range 0.0 to 1.0 |
el15as | 15:e3fc18eb519e | 132 | */ |
el15as | 15:e3fc18eb519e | 133 | void led(int n,float val) const; |
el15as | 15:e3fc18eb519e | 134 | |
el15as | 15:e3fc18eb519e | 135 | /** Read potentiometer |
el15as | 15:e3fc18eb519e | 136 | *@returns potentiometer value in range 0.0 to 1.0 |
el15as | 15:e3fc18eb519e | 137 | */ |
el15as | 15:e3fc18eb519e | 138 | float read_pot() const; |
el15as | 15:e3fc18eb519e | 139 | |
el15as | 15:e3fc18eb519e | 140 | /** Play tone on piezo |
el15as | 15:e3fc18eb519e | 141 | * @param frequency in Hz |
el15as | 15:e3fc18eb519e | 142 | * @param duration of tone in seconds |
el15as | 15:e3fc18eb519e | 143 | */ |
el15as | 15:e3fc18eb519e | 144 | void tone(float frequency, float duration); |
el15as | 15:e3fc18eb519e | 145 | |
el15as | 15:e3fc18eb519e | 146 | /** |
el15as | 15:e3fc18eb519e | 147 | * @brief Check whether an event flag has been set and clear it |
el15as | 15:e3fc18eb519e | 148 | * @param id[in] The ID of the event to test |
el15as | 15:e3fc18eb519e | 149 | * @return true if the event occurred |
el15as | 15:e3fc18eb519e | 150 | */ |
el15as | 15:e3fc18eb519e | 151 | bool check_event(GamepadEvent const id); |
el15as | 15:e3fc18eb519e | 152 | |
el15as | 15:e3fc18eb519e | 153 | /** Manually clear event flag |
el15as | 15:e3fc18eb519e | 154 | */ |
el15as | 15:e3fc18eb519e | 155 | |
el15as | 15:e3fc18eb519e | 156 | void reset_event(GamepadEvent const id); |
el15as | 15:e3fc18eb519e | 157 | |
el15as | 15:e3fc18eb519e | 158 | /** Get magnitude of joystick movement |
el15as | 15:e3fc18eb519e | 159 | * @returns value in range 0.0 to 1.0 |
el15as | 15:e3fc18eb519e | 160 | */ |
el15as | 15:e3fc18eb519e | 161 | |
el15as | 15:e3fc18eb519e | 162 | float get_mag(); |
el15as | 15:e3fc18eb519e | 163 | |
el15as | 15:e3fc18eb519e | 164 | /** Get angle of joystick movement |
el15as | 15:e3fc18eb519e | 165 | * @returns value in range 0.0 to 359.9. 0.0 corresponds to N, 180.0 to S. -1.0 is central |
el15as | 15:e3fc18eb519e | 166 | */ |
el15as | 15:e3fc18eb519e | 167 | float get_angle(); |
el15as | 15:e3fc18eb519e | 168 | |
el15as | 15:e3fc18eb519e | 169 | /** Gets joystick direction |
el15as | 15:e3fc18eb519e | 170 | * @returns an enum: CENTRE, N, NE, E, SE, S, SW, W, NW, |
el15as | 15:e3fc18eb519e | 171 | */ |
el15as | 15:e3fc18eb519e | 172 | Direction get_direction(); // N,NE,E,SE etc. |
el15as | 15:e3fc18eb519e | 173 | |
el15as | 15:e3fc18eb519e | 174 | /** Gets raw cartesian co-ordinates of joystick |
el15as | 15:e3fc18eb519e | 175 | * @returns a struct with x,y members, each in the range 0.0 to 1.0 |
el15as | 15:e3fc18eb519e | 176 | */ |
el15as | 15:e3fc18eb519e | 177 | Vector2D get_coord(); // cartesian co-ordinates x,y |
el15as | 15:e3fc18eb519e | 178 | |
el15as | 15:e3fc18eb519e | 179 | /** Gets cartesian coordinates mapped to circular grid |
el15as | 15:e3fc18eb519e | 180 | * @returns a struct with x,y members, each in the range 0.0 to 1.0 |
el15as | 15:e3fc18eb519e | 181 | */ |
el15as | 15:e3fc18eb519e | 182 | Vector2D get_mapped_coord(); // x,y mapped to circle |
el15as | 15:e3fc18eb519e | 183 | |
el15as | 15:e3fc18eb519e | 184 | /** Gets polar coordinates of the joystick |
el15as | 15:e3fc18eb519e | 185 | * @returns a struct contains mag and angle |
el15as | 15:e3fc18eb519e | 186 | */ |
el15as | 15:e3fc18eb519e | 187 | Polar get_polar(); // mag and angle in struct form |
el15as | 15:e3fc18eb519e | 188 | |
el15as | 15:e3fc18eb519e | 189 | private: |
el15as | 15:e3fc18eb519e | 190 | void init_buttons(); |
el15as | 15:e3fc18eb519e | 191 | void tone_off(); |
el15as | 15:e3fc18eb519e | 192 | |
el15as | 15:e3fc18eb519e | 193 | void a_isr(); |
el15as | 15:e3fc18eb519e | 194 | void b_isr(); |
el15as | 15:e3fc18eb519e | 195 | void x_isr(); |
el15as | 15:e3fc18eb519e | 196 | void y_isr(); |
el15as | 15:e3fc18eb519e | 197 | void l_isr(); |
el15as | 15:e3fc18eb519e | 198 | void r_isr(); |
el15as | 15:e3fc18eb519e | 199 | void back_isr(); |
el15as | 15:e3fc18eb519e | 200 | void start_isr(); |
el15as | 15:e3fc18eb519e | 201 | void joy_isr(); |
el15as | 15:e3fc18eb519e | 202 | }; |
el15as | 15:e3fc18eb519e | 203 | |
el15as | 15:e3fc18eb519e | 204 | #endif |