Class used to interface with the handheld gamepad.

Fork of Gamepad by Craig Evans

Committer:
el15mh
Date:
Wed May 03 21:14:16 2017 +0000
Revision:
21:0adaa3883d1e
Parent:
20:c0959710291b
fully working program

Who changed what in which revision?

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