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