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