Yang Meng / Mbed 2 deprecated 207_program

Dependencies:   mbed N5110

Committer:
2016110307
Date:
Tue Apr 23 16:04:18 2019 +0000
Revision:
0:97418ec4c37d
Fundamental functions have achieved.

Who changed what in which revision?

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