A rouge-like rpg, heavily inspired on the binding of isaac. Running on a FRDM-K64F Mbed board. C++.

Dependencies:   mbed MotionSensor

Committer:
el17sm
Date:
Sat Apr 20 22:58:15 2019 +0000
Revision:
9:304079450898
Parent:
0:8e92b66a0755
Child:
21:be18f33da757
Gamepad now lets buttons to be held

Who changed what in which revision?

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