Ellis Blackford Stroud 201155309

Dependencies:   mbed FATFileSystem

Committer:
ellisbhastroud
Date:
Thu May 09 12:01:42 2019 +0000
Revision:
17:98127ac75195
Parent:
0:c6ceddb241df
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

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