Zhang Xin yu
/
ZhangXinyu201090208
zhangxinyu01text
Gamepad/Gamepad.h@22:d7870b7b442f, 2019-05-06 (annotated)
- Committer:
- Jenny121
- Date:
- Mon May 06 09:14:15 2019 +0000
- Revision:
- 22:d7870b7b442f
- Parent:
- 15:82e5a49cdbe4
the structs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 5:3c9407e2fe55 | 1 | #ifndef GAMEPAD_H |
eencae | 5:3c9407e2fe55 | 2 | #define GAMEPAD_H |
eencae | 5:3c9407e2fe55 | 3 | |
eencae | 5:3c9407e2fe55 | 4 | #include <bitset> |
eencae | 5:3c9407e2fe55 | 5 | |
eencae | 5:3c9407e2fe55 | 6 | // Forward declaration of the classes that we use from the mbed library |
eencae | 5:3c9407e2fe55 | 7 | // This avoids the need for us to include the huge mbed.h header inside our |
eencae | 5:3c9407e2fe55 | 8 | // own library API |
Jenny121 | 12:3952ba0683c7 | 9 | |
Jenny121 | 12:3952ba0683c7 | 10 | /** Gamepad Class |
Jenny121 | 12:3952ba0683c7 | 11 | * @brief with the para and elemets of LCD pad i sLCD |
Jenny121 | 12:3952ba0683c7 | 12 | * @author Zhang Xinyu |
Jenny121 | 12:3952ba0683c7 | 13 | * @school EE of SWJTU &leeds joint school |
Jenny121 | 12:3952ba0683c7 | 14 | * @date MAY 2019 |
Jenny121 | 12:3952ba0683c7 | 15 | */ |
Jenny121 | 12:3952ba0683c7 | 16 | |
eencae | 5:3c9407e2fe55 | 17 | namespace mbed |
eencae | 5:3c9407e2fe55 | 18 | { |
eencae | 5:3c9407e2fe55 | 19 | class AnalogIn; |
eencae | 5:3c9407e2fe55 | 20 | class InterruptIn; |
eencae | 5:3c9407e2fe55 | 21 | class PwmOut; |
eencae | 5:3c9407e2fe55 | 22 | class Timeout; |
eencae | 5:3c9407e2fe55 | 23 | } |
eencae | 5:3c9407e2fe55 | 24 | |
eencae | 5:3c9407e2fe55 | 25 | #define TOL 0.1f |
eencae | 5:3c9407e2fe55 | 26 | #define RAD2DEG 57.2957795131f |
eencae | 5:3c9407e2fe55 | 27 | |
Jenny121 | 12:3952ba0683c7 | 28 | /** Enum for direction |
Jenny121 | 12:3952ba0683c7 | 29 | * @ details defined the direction of joysticks |
Jenny121 | 12:3952ba0683c7 | 30 | * @ param pushed defined the CENTRE N,NE,E,SE,S,SW,W,NW acording to agnles |
Jenny121 | 12:3952ba0683c7 | 31 | */ |
eencae | 5:3c9407e2fe55 | 32 | enum Direction { |
Jenny121 | 12:3952ba0683c7 | 33 | CENTRE, |
Jenny121 | 12:3952ba0683c7 | 34 | N, |
Jenny121 | 12:3952ba0683c7 | 35 | NE, |
Jenny121 | 12:3952ba0683c7 | 36 | E, |
Jenny121 | 12:3952ba0683c7 | 37 | SE, |
Jenny121 | 12:3952ba0683c7 | 38 | S, |
Jenny121 | 12:3952ba0683c7 | 39 | SW, |
Jenny121 | 12:3952ba0683c7 | 40 | W, |
Jenny121 | 12:3952ba0683c7 | 41 | NW |
eencae | 5:3c9407e2fe55 | 42 | }; |
eencae | 5:3c9407e2fe55 | 43 | |
Jenny121 | 14:e24860b4f19c | 44 | /** vector struct */ |
eencae | 5:3c9407e2fe55 | 45 | struct Vector2D { |
Jenny121 | 15:82e5a49cdbe4 | 46 | float x; /** < char for x value position> */ |
Jenny121 | 15:82e5a49cdbe4 | 47 | float y; /** < char for y value position> */ |
Jenny121 | 15:82e5a49cdbe4 | 48 | |
eencae | 5:3c9407e2fe55 | 49 | }; |
eencae | 5:3c9407e2fe55 | 50 | |
Jenny121 | 13:29336cfc67b9 | 51 | /** Polar coordinate Struct |
Jenny121 | 12:3952ba0683c7 | 52 | * @ param the vara magnitude |
Jenny121 | 12:3952ba0683c7 | 53 | * @ param float for angle (in degrees) |
Jenny121 | 12:3952ba0683c7 | 54 | * @ details the vara and angle are use by other function |
Jenny121 | 12:3952ba0683c7 | 55 | */ |
Jenny121 | 22:d7870b7b442f | 56 | |
Jenny121 | 22:d7870b7b442f | 57 | /** vector struct */ |
eencae | 5:3c9407e2fe55 | 58 | struct Polar { |
Jenny121 | 22:d7870b7b442f | 59 | float vara; /** < char for vara magnatic value of joystick> */ |
Jenny121 | 22:d7870b7b442f | 60 | float angle; /** < char for angle by joysticks accoring to LCD> */ |
eencae | 5:3c9407e2fe55 | 61 | }; |
eencae | 5:3c9407e2fe55 | 62 | |
eencae | 5:3c9407e2fe55 | 63 | /** Gamepad Class |
Jenny121 | 12:3952ba0683c7 | 64 | @brief Library of the gamepad |
Jenny121 | 12:3952ba0683c7 | 65 | @author zhang xinyu |
Jenny121 | 12:3952ba0683c7 | 66 | @date May 2019 |
eencae | 5:3c9407e2fe55 | 67 | */ |
eencae | 5:3c9407e2fe55 | 68 | class Gamepad |
eencae | 5:3c9407e2fe55 | 69 | { |
eencae | 5:3c9407e2fe55 | 70 | public: |
eencae | 5:3c9407e2fe55 | 71 | /** Gamepad events |
eencae | 5:3c9407e2fe55 | 72 | * @brief List of events that can be registered on the gamepad |
Jenny121 | 12:3952ba0683c7 | 73 | * @ param defined the button in the pad |
eencae | 5:3c9407e2fe55 | 74 | */ |
eencae | 5:3c9407e2fe55 | 75 | enum GamepadEvent { |
eencae | 5:3c9407e2fe55 | 76 | A_PRESSED, ///< Button A has been pressed |
eencae | 5:3c9407e2fe55 | 77 | B_PRESSED, ///< Button B has been pressed |
eencae | 5:3c9407e2fe55 | 78 | X_PRESSED, ///< Button X has been pressed |
eencae | 5:3c9407e2fe55 | 79 | Y_PRESSED, ///< Button Y has been pressed |
eencae | 5:3c9407e2fe55 | 80 | L_PRESSED, ///< Button L has been pressed |
eencae | 5:3c9407e2fe55 | 81 | R_PRESSED, ///< Button R has been pressed |
eencae | 5:3c9407e2fe55 | 82 | BACK_PRESSED, ///< Button "Back" has been pressed |
eencae | 5:3c9407e2fe55 | 83 | START_PRESSED, ///< Button "Start" has been pressed |
eencae | 5:3c9407e2fe55 | 84 | JOY_PRESSED, ///< Joystick button has been pressed |
eencae | 5:3c9407e2fe55 | 85 | N_EVENTS ///< A dummy flag that marks the end of the list |
eencae | 5:3c9407e2fe55 | 86 | }; |
eencae | 5:3c9407e2fe55 | 87 | private: |
eencae | 5:3c9407e2fe55 | 88 | mbed::PwmOut *_led1; |
eencae | 5:3c9407e2fe55 | 89 | mbed::PwmOut *_led2; |
eencae | 5:3c9407e2fe55 | 90 | mbed::PwmOut *_led3; |
eencae | 5:3c9407e2fe55 | 91 | mbed::PwmOut *_led4; |
eencae | 5:3c9407e2fe55 | 92 | mbed::PwmOut *_led5; |
eencae | 5:3c9407e2fe55 | 93 | mbed::PwmOut *_led6; |
eencae | 5:3c9407e2fe55 | 94 | |
eencae | 5:3c9407e2fe55 | 95 | mbed::InterruptIn *_button_A; |
eencae | 5:3c9407e2fe55 | 96 | mbed::InterruptIn *_button_B; |
eencae | 5:3c9407e2fe55 | 97 | mbed::InterruptIn *_button_X; |
eencae | 5:3c9407e2fe55 | 98 | mbed::InterruptIn *_button_Y; |
eencae | 5:3c9407e2fe55 | 99 | mbed::InterruptIn *_button_L; |
eencae | 5:3c9407e2fe55 | 100 | mbed::InterruptIn *_button_R; |
eencae | 5:3c9407e2fe55 | 101 | mbed::InterruptIn *_button_back; |
eencae | 5:3c9407e2fe55 | 102 | mbed::InterruptIn *_button_start; |
eencae | 5:3c9407e2fe55 | 103 | mbed::InterruptIn *_button_joystick; |
eencae | 5:3c9407e2fe55 | 104 | |
eencae | 5:3c9407e2fe55 | 105 | mbed::AnalogIn *_vert; |
eencae | 5:3c9407e2fe55 | 106 | mbed::AnalogIn *_horiz; |
eencae | 5:3c9407e2fe55 | 107 | |
eencae | 5:3c9407e2fe55 | 108 | mbed::PwmOut *_buzzer; |
eencae | 5:3c9407e2fe55 | 109 | mbed::AnalogIn *_pot; |
eencae | 5:3c9407e2fe55 | 110 | |
eencae | 5:3c9407e2fe55 | 111 | mbed::Timeout *_timeout; |
eencae | 5:3c9407e2fe55 | 112 | |
eencae | 5:3c9407e2fe55 | 113 | std::bitset<N_EVENTS> _event_state; ///< A binary list of buttons that has been pressed |
eencae | 5:3c9407e2fe55 | 114 | |
eencae | 5:3c9407e2fe55 | 115 | // centred x,y values |
eencae | 5:3c9407e2fe55 | 116 | float _x0; |
eencae | 5:3c9407e2fe55 | 117 | float _y0; |
eencae | 5:3c9407e2fe55 | 118 | |
eencae | 5:3c9407e2fe55 | 119 | public: |
eencae | 5:3c9407e2fe55 | 120 | |
eencae | 5:3c9407e2fe55 | 121 | /** Constructor */ |
eencae | 5:3c9407e2fe55 | 122 | Gamepad(); |
eencae | 5:3c9407e2fe55 | 123 | |
eencae | 5:3c9407e2fe55 | 124 | /** Destructor */ |
eencae | 5:3c9407e2fe55 | 125 | ~Gamepad(); |
eencae | 5:3c9407e2fe55 | 126 | |
eencae | 5:3c9407e2fe55 | 127 | /** Initialise all peripherals and configure interrupts */ |
eencae | 5:3c9407e2fe55 | 128 | void init(); |
eencae | 5:3c9407e2fe55 | 129 | |
eencae | 5:3c9407e2fe55 | 130 | /** Turn all LEDs on */ |
eencae | 5:3c9407e2fe55 | 131 | void leds_on(); |
eencae | 5:3c9407e2fe55 | 132 | |
eencae | 5:3c9407e2fe55 | 133 | /** Turn all LEDs off */ |
eencae | 5:3c9407e2fe55 | 134 | void leds_off(); |
eencae | 5:3c9407e2fe55 | 135 | |
eencae | 5:3c9407e2fe55 | 136 | /** Set all LEDs to duty-cycle |
eencae | 5:3c9407e2fe55 | 137 | *@param value in range 0.0 to 1.0 |
eencae | 5:3c9407e2fe55 | 138 | */ |
eencae | 5:3c9407e2fe55 | 139 | void leds(float val) const; |
eencae | 5:3c9407e2fe55 | 140 | |
eencae | 5:3c9407e2fe55 | 141 | /** Set LED to duty-cycle |
eencae | 5:3c9407e2fe55 | 142 | *@param led number (0 to 5) |
eencae | 5:3c9407e2fe55 | 143 | *@param value in range 0.0 to 1.0 |
eencae | 5:3c9407e2fe55 | 144 | */ |
eencae | 5:3c9407e2fe55 | 145 | void led(int n,float val) const; |
eencae | 5:3c9407e2fe55 | 146 | |
eencae | 5:3c9407e2fe55 | 147 | /** Read potentiometer |
eencae | 5:3c9407e2fe55 | 148 | *@returns potentiometer value in range 0.0 to 1.0 |
eencae | 5:3c9407e2fe55 | 149 | */ |
eencae | 5:3c9407e2fe55 | 150 | float read_pot() const; |
eencae | 5:3c9407e2fe55 | 151 | |
eencae | 5:3c9407e2fe55 | 152 | /** Play tone on piezo |
eencae | 5:3c9407e2fe55 | 153 | * @param frequency in Hz |
eencae | 5:3c9407e2fe55 | 154 | * @param duration of tone in seconds |
eencae | 5:3c9407e2fe55 | 155 | */ |
eencae | 5:3c9407e2fe55 | 156 | void tone(float frequency, float duration); |
eencae | 5:3c9407e2fe55 | 157 | |
eencae | 5:3c9407e2fe55 | 158 | /** |
eencae | 5:3c9407e2fe55 | 159 | * @brief Check whether an event flag has been set and clear it |
eencae | 5:3c9407e2fe55 | 160 | * @param id[in] The ID of the event to test |
eencae | 5:3c9407e2fe55 | 161 | * @return true if the event occurred |
eencae | 5:3c9407e2fe55 | 162 | */ |
eencae | 5:3c9407e2fe55 | 163 | bool check_event(GamepadEvent const id); |
eencae | 5:3c9407e2fe55 | 164 | |
Jenny121 | 12:3952ba0683c7 | 165 | /** Get varanitude of joystick movement |
eencae | 5:3c9407e2fe55 | 166 | * @returns value in range 0.0 to 1.0 |
eencae | 5:3c9407e2fe55 | 167 | */ |
eencae | 5:3c9407e2fe55 | 168 | |
Jenny121 | 12:3952ba0683c7 | 169 | float get_vara(); |
eencae | 5:3c9407e2fe55 | 170 | |
eencae | 5:3c9407e2fe55 | 171 | /** Get angle of joystick movement |
eencae | 5:3c9407e2fe55 | 172 | * @returns value in range 0.0 to 359.9. 0.0 corresponds to N, 180.0 to S. -1.0 is central |
eencae | 5:3c9407e2fe55 | 173 | */ |
eencae | 5:3c9407e2fe55 | 174 | float get_angle(); |
eencae | 5:3c9407e2fe55 | 175 | |
eencae | 5:3c9407e2fe55 | 176 | /** Gets joystick direction |
eencae | 5:3c9407e2fe55 | 177 | * @returns an enum: CENTRE, N, NE, E, SE, S, SW, W, NW, |
eencae | 5:3c9407e2fe55 | 178 | */ |
eencae | 5:3c9407e2fe55 | 179 | Direction get_direction(); // N,NE,E,SE etc. |
eencae | 5:3c9407e2fe55 | 180 | |
eencae | 5:3c9407e2fe55 | 181 | /** Gets raw cartesian co-ordinates of joystick |
eencae | 5:3c9407e2fe55 | 182 | * @returns a struct with x,y members, each in the range 0.0 to 1.0 |
eencae | 5:3c9407e2fe55 | 183 | */ |
eencae | 5:3c9407e2fe55 | 184 | Vector2D get_coord(); // cartesian co-ordinates x,y |
eencae | 5:3c9407e2fe55 | 185 | |
eencae | 5:3c9407e2fe55 | 186 | /** Gets cartesian coordinates mapped to circular grid |
eencae | 5:3c9407e2fe55 | 187 | * @returns a struct with x,y members, each in the range 0.0 to 1.0 |
eencae | 5:3c9407e2fe55 | 188 | */ |
eencae | 5:3c9407e2fe55 | 189 | Vector2D get_mapped_coord(); // x,y mapped to circle |
eencae | 5:3c9407e2fe55 | 190 | |
eencae | 5:3c9407e2fe55 | 191 | /** Gets polar coordinates of the joystick |
Jenny121 | 12:3952ba0683c7 | 192 | * @returns a struct contains vara and angle |
eencae | 5:3c9407e2fe55 | 193 | */ |
Jenny121 | 12:3952ba0683c7 | 194 | Polar get_polar(); // vara and angle in struct form |
eencae | 5:3c9407e2fe55 | 195 | |
eencae | 5:3c9407e2fe55 | 196 | private: |
eencae | 5:3c9407e2fe55 | 197 | void init_buttons(); |
eencae | 5:3c9407e2fe55 | 198 | void tone_off(); |
eencae | 5:3c9407e2fe55 | 199 | |
eencae | 5:3c9407e2fe55 | 200 | void a_isr(); |
eencae | 5:3c9407e2fe55 | 201 | void b_isr(); |
eencae | 5:3c9407e2fe55 | 202 | void x_isr(); |
eencae | 5:3c9407e2fe55 | 203 | void y_isr(); |
eencae | 5:3c9407e2fe55 | 204 | void l_isr(); |
eencae | 5:3c9407e2fe55 | 205 | void r_isr(); |
eencae | 5:3c9407e2fe55 | 206 | void back_isr(); |
eencae | 5:3c9407e2fe55 | 207 | void start_isr(); |
eencae | 5:3c9407e2fe55 | 208 | void joy_isr(); |
eencae | 5:3c9407e2fe55 | 209 | }; |
eencae | 5:3c9407e2fe55 | 210 | |
eencae | 5:3c9407e2fe55 | 211 | #endif |