deemo1

Dependencies:   mbed

Committer:
haoyan
Date:
Thu May 14 06:30:39 2020 +0000
Revision:
6:b59bc5e15cf3
Parent:
0:08b2bdddf22e
test

Who changed what in which revision?

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