Josh Davy / Mbed OS Flip_OS_5

Dependencies:   el17jd

Committer:
joshdavy
Date:
Tue Mar 12 12:38:34 2019 +0000
Revision:
0:4916a63a6cbf
initial commit

Who changed what in which revision?

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