Yang Hongxiao 201199678

Dependencies:   mbed

Committer:
YHX
Date:
Thu May 14 17:00:34 2020 +0000
Revision:
1:a6ead8050c23
Parent:
0:4b02786450c0
Yang Hongxiao; el17hy; 201199678

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 1:a6ead8050c23 46 @brief Library for interfacing with ELEC2645 Gamepad PCB, University of Leeds
YHX 1:a6ead8050c23 47 @author Dr Craig A. Evans
YHX 1:a6ead8050c23 48 @author Dr Alex Valanvanis
YHX 1:a6ead8050c23 49 @date Febraury 2017
YHX 1:a6ead8050c23 50 */
YHX 0:4b02786450c0 51 class Gamepad
YHX 0:4b02786450c0 52 {
YHX 1:a6ead8050c23 53 public:
YHX 1:a6ead8050c23 54 /** Gamepad events
YHX 0:4b02786450c0 55 * @brief List of events that can be registered on the gamepad
YHX 0:4b02786450c0 56 */
YHX 0:4b02786450c0 57 enum GamepadEvent {
YHX 0:4b02786450c0 58 A_PRESSED, ///< Button A has been pressed
YHX 0:4b02786450c0 59 B_PRESSED, ///< Button B has been pressed
YHX 0:4b02786450c0 60 X_PRESSED, ///< Button X has been pressed
YHX 0:4b02786450c0 61 Y_PRESSED, ///< Button Y has been pressed
YHX 0:4b02786450c0 62 L_PRESSED, ///< Button L has been pressed
YHX 0:4b02786450c0 63 R_PRESSED, ///< Button R has been pressed
YHX 0:4b02786450c0 64 BACK_PRESSED, ///< Button "Back" has been pressed
YHX 0:4b02786450c0 65 START_PRESSED, ///< Button "Start" has been pressed
YHX 0:4b02786450c0 66 JOY_PRESSED, ///< Joystick button has been pressed
YHX 0:4b02786450c0 67 N_EVENTS ///< A dummy flag that marks the end of the list
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 1:a6ead8050c23 102
YHX 0:4b02786450c0 103 /** Constructor */
YHX 0:4b02786450c0 104 Gamepad();
YHX 0:4b02786450c0 105
YHX 0:4b02786450c0 106 /** Destructor */
YHX 0:4b02786450c0 107 ~Gamepad();
YHX 0:4b02786450c0 108
YHX 0:4b02786450c0 109 /** Initialise all peripherals and configure interrupts */
YHX 0:4b02786450c0 110 void init();
YHX 0:4b02786450c0 111
YHX 0:4b02786450c0 112 /** Turn all LEDs on */
YHX 0:4b02786450c0 113 void leds_on();
YHX 0:4b02786450c0 114
YHX 0:4b02786450c0 115 /** Turn all LEDs off */
YHX 0:4b02786450c0 116 void leds_off();
YHX 0:4b02786450c0 117
YHX 0:4b02786450c0 118 /** Set all LEDs to duty-cycle
YHX 0:4b02786450c0 119 *@param value in range 0.0 to 1.0
YHX 0:4b02786450c0 120 */
YHX 0:4b02786450c0 121 void leds(float val) const;
YHX 0:4b02786450c0 122
YHX 0:4b02786450c0 123 /** Set LED to duty-cycle
YHX 0:4b02786450c0 124 *@param led number (0 to 5)
YHX 0:4b02786450c0 125 *@param value in range 0.0 to 1.0
YHX 0:4b02786450c0 126 */
YHX 0:4b02786450c0 127 void led(int n,float val) const;
YHX 0:4b02786450c0 128
YHX 0:4b02786450c0 129 /** Read potentiometer
YHX 0:4b02786450c0 130 *@returns potentiometer value in range 0.0 to 1.0
YHX 0:4b02786450c0 131 */
YHX 0:4b02786450c0 132 float read_pot() const;
YHX 0:4b02786450c0 133
YHX 0:4b02786450c0 134 /** Play tone on piezo
YHX 0:4b02786450c0 135 * @param frequency in Hz
YHX 0:4b02786450c0 136 * @param duration of tone in seconds
YHX 0:4b02786450c0 137 */
YHX 0:4b02786450c0 138 void tone(float frequency, float duration);
YHX 0:4b02786450c0 139
YHX 0:4b02786450c0 140 /**
YHX 0:4b02786450c0 141 * @brief Check whether an event flag has been set and clear it
YHX 0:4b02786450c0 142 * @param id[in] The ID of the event to test
YHX 0:4b02786450c0 143 * @return true if the event occurred
YHX 0:4b02786450c0 144 */
YHX 0:4b02786450c0 145 bool check_event(GamepadEvent const id);
YHX 0:4b02786450c0 146
YHX 0:4b02786450c0 147 /** Get magnitude of joystick movement
YHX 0:4b02786450c0 148 * @returns value in range 0.0 to 1.0
YHX 0:4b02786450c0 149 */
YHX 1:a6ead8050c23 150
YHX 0:4b02786450c0 151 float get_mag();
YHX 0:4b02786450c0 152
YHX 0:4b02786450c0 153 /** Get angle of joystick movement
YHX 0:4b02786450c0 154 * @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 155 */
YHX 0:4b02786450c0 156 float get_angle();
YHX 0:4b02786450c0 157
YHX 0:4b02786450c0 158 /** Gets joystick direction
YHX 0:4b02786450c0 159 * @returns an enum: CENTRE, N, NE, E, SE, S, SW, W, NW,
YHX 0:4b02786450c0 160 */
YHX 0:4b02786450c0 161 Direction get_direction(); // N,NE,E,SE etc.
YHX 0:4b02786450c0 162
YHX 0:4b02786450c0 163 /** Gets raw cartesian co-ordinates of joystick
YHX 0:4b02786450c0 164 * @returns a struct with x,y members, each in the range 0.0 to 1.0
YHX 0:4b02786450c0 165 */
YHX 0:4b02786450c0 166 Vector2D get_coord(); // cartesian co-ordinates x,y
YHX 0:4b02786450c0 167
YHX 0:4b02786450c0 168 /** Gets cartesian coordinates mapped to circular grid
YHX 0:4b02786450c0 169 * @returns a struct with x,y members, each in the range 0.0 to 1.0
YHX 0:4b02786450c0 170 */
YHX 0:4b02786450c0 171 Vector2D get_mapped_coord(); // x,y mapped to circle
YHX 0:4b02786450c0 172
YHX 0:4b02786450c0 173 /** Gets polar coordinates of the joystick
YHX 0:4b02786450c0 174 * @returns a struct contains mag and angle
YHX 0:4b02786450c0 175 */
YHX 0:4b02786450c0 176 Polar get_polar(); // mag and angle in struct form
YHX 0:4b02786450c0 177
YHX 0:4b02786450c0 178 private:
YHX 0:4b02786450c0 179 void init_buttons();
YHX 0:4b02786450c0 180 void tone_off();
YHX 0:4b02786450c0 181
YHX 0:4b02786450c0 182 void a_isr();
YHX 0:4b02786450c0 183 void b_isr();
YHX 0:4b02786450c0 184 void x_isr();
YHX 0:4b02786450c0 185 void y_isr();
YHX 0:4b02786450c0 186 void l_isr();
YHX 0:4b02786450c0 187 void r_isr();
YHX 0:4b02786450c0 188 void back_isr();
YHX 0:4b02786450c0 189 void start_isr();
YHX 0:4b02786450c0 190 void joy_isr();
YHX 0:4b02786450c0 191 };
YHX 0:4b02786450c0 192
YHX 0:4b02786450c0 193 #endif