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