James Heavey / Mbed 2 deprecated 2665-Breakout-Game

Dependencies:   mbed

Committer:
jamesheavey
Date:
Tue Jan 05 01:14:11 2021 +0000
Revision:
0:92b180c8d407
test

Who changed what in which revision?

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