el15mh 200929957

Dependencies:   mbed

Committer:
el15mh
Date:
Thu May 04 17:39:23 2017 +0000
Revision:
10:989e5dbd12ee
Parent:
9:960dfc71c224
Documented and final revision

Who changed what in which revision?

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