Yang Zhenye 201199680

Dependencies:   mbed

Committer:
yangzhenye
Date:
Fri May 15 07:27:23 2020 +0000
Revision:
9:a5a82b3d1102
Parent:
0:ac2868313b41
el17zy Yang Zhenye

Who changed what in which revision?

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