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