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