Mochu Yao explorer game

Dependencies:   mbed

Committer:
el17my
Date:
Fri May 15 09:03:36 2020 +0000
Revision:
39:0debc17bad29
Parent:
1:ed745421d8c4
the final version

Who changed what in which revision?

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