200943373MAZE

Dependencies:   mbed

Fork of 200943373MAZE by hongyun AHN

Committer:
hongyunAHN
Date:
Thu May 04 14:08:31 2017 +0000
Revision:
1:bd92ef8d00ac
200943373  hongyun AHN

Who changed what in which revision?

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