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