Implement basic functions

Dependencies:   mbed N5110

Committer:
Wuuu
Date:
Fri May 03 13:05:24 2019 +0000
Revision:
0:64bd9f996099
Flappy Bird, which with header programme

Who changed what in which revision?

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