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