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