The temporary file

Dependencies:   mbed

Committer:
sjuzyz
Date:
Sat Apr 11 11:21:08 2020 +0000
Revision:
0:db648efbaa56
mide

Who changed what in which revision?

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