contains my game for the embedded systems project 2645

Dependencies:   mbed FXOS8700CQQQ

Committer:
OmarAlebiary
Date:
Tue May 07 15:17:21 2019 +0000
Revision:
40:13b8467526d0
Parent:
39:822b66b1c935
Final Submission. I have read and agreed with Statement of Academic Integrity

Who changed what in which revision?

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