Zikang Qian / Mbed 2 deprecated el17z2q

Dependencies:   mbed

Fork of el17z2q by ELEC2645 (2017/18)

Committer:
yzjdxl
Date:
Mon May 07 22:11:43 2018 +0000
Branch:
GameEngine
Revision:
2:6dc7bc55c1cb
Parent:
0:f707ce1010fa
publish

Who changed what in which revision?

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