ELEC2645 (2018/19) / Mbed 2 deprecated el17szs

Dependencies:   mbed

Committer:
shahidsajid
Date:
Thu Apr 18 09:31:52 2019 +0000
Revision:
6:3e50f2cf4366
Parent:
0:bed27dc63dea
Child:
20:9d21599fe350
Checks for collision (ball hitting bat)

Who changed what in which revision?

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