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