ELEC2645 (2017/18) / Mbed 2 deprecated el17yw

Dependencies:   mbed

Committer:
RickYu
Date:
Mon Mar 05 09:14:14 2018 +0000
Revision:
0:4d3c9411e8f2
Child:
2:421fb0670c5c
beginning of the project

Who changed what in which revision?

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