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