ELEC2645 (2018/19) / Mbed 2 deprecated el17szs

Dependencies:   mbed

Committer:
shahidsajid
Date:
Tue Mar 12 09:48:10 2019 +0000
Revision:
0:bed27dc63dea
Child:
6:3e50f2cf4366
initial commit;

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