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