ELEC2645 (2018/19) / Mbed 2 deprecated el17ph

Dependencies:   mbed

Committer:
el17ph
Date:
Sat May 11 14:58:43 2019 +0000
Revision:
0:8fb740fa6356
last

Who changed what in which revision?

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