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