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