Yang Hongxiao 201199678

Dependencies:   mbed

Committer:
YHX
Date:
Sun Apr 26 13:17:59 2020 +0000
Revision:
0:4b02786450c0
Child:
1:a6ead8050c23
initial commit

Who changed what in which revision?

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