Zhang Xin yu
/
ZhangXinyu201090208
zhangxinyu01text
Gamepad/Gamepad.h@12:3952ba0683c7, 2019-05-06 (annotated)
- Committer:
- Jenny121
- Date:
- Mon May 06 06:09:02 2019 +0000
- Revision:
- 12:3952ba0683c7
- Parent:
- 5:3c9407e2fe55
- Child:
- 13:29336cfc67b9
zhang xinyu
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 | 12:3952ba0683c7 | 44 | /** Vector 2D struct |
Jenny121 | 12:3952ba0683c7 | 45 | * @ param float x and y for value */ |
eencae | 5:3c9407e2fe55 | 46 | struct Vector2D { |
Jenny121 | 12:3952ba0683c7 | 47 | float x; |
Jenny121 | 12:3952ba0683c7 | 48 | float y; |
eencae | 5:3c9407e2fe55 | 49 | }; |
eencae | 5:3c9407e2fe55 | 50 | |
Jenny121 | 12:3952ba0683c7 | 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 | */ |
eencae | 5:3c9407e2fe55 | 56 | struct Polar { |
Jenny121 | 12:3952ba0683c7 | 57 | float vara; |
Jenny121 | 12:3952ba0683c7 | 58 | float angle; |
eencae | 5:3c9407e2fe55 | 59 | }; |
eencae | 5:3c9407e2fe55 | 60 | |
eencae | 5:3c9407e2fe55 | 61 | /** Gamepad Class |
Jenny121 | 12:3952ba0683c7 | 62 | @brief Library of the gamepad |
Jenny121 | 12:3952ba0683c7 | 63 | @author zhang xinyu |
Jenny121 | 12:3952ba0683c7 | 64 | @date May 2019 |
eencae | 5:3c9407e2fe55 | 65 | */ |
eencae | 5:3c9407e2fe55 | 66 | class Gamepad |
eencae | 5:3c9407e2fe55 | 67 | { |
eencae | 5:3c9407e2fe55 | 68 | public: |
eencae | 5:3c9407e2fe55 | 69 | /** Gamepad events |
eencae | 5:3c9407e2fe55 | 70 | * @brief List of events that can be registered on the gamepad |
Jenny121 | 12:3952ba0683c7 | 71 | * @ param defined the button in the pad |
eencae | 5:3c9407e2fe55 | 72 | */ |
eencae | 5:3c9407e2fe55 | 73 | enum GamepadEvent { |
eencae | 5:3c9407e2fe55 | 74 | A_PRESSED, ///< Button A has been pressed |
eencae | 5:3c9407e2fe55 | 75 | B_PRESSED, ///< Button B has been pressed |
eencae | 5:3c9407e2fe55 | 76 | X_PRESSED, ///< Button X has been pressed |
eencae | 5:3c9407e2fe55 | 77 | Y_PRESSED, ///< Button Y has been pressed |
eencae | 5:3c9407e2fe55 | 78 | L_PRESSED, ///< Button L has been pressed |
eencae | 5:3c9407e2fe55 | 79 | R_PRESSED, ///< Button R has been pressed |
eencae | 5:3c9407e2fe55 | 80 | BACK_PRESSED, ///< Button "Back" has been pressed |
eencae | 5:3c9407e2fe55 | 81 | START_PRESSED, ///< Button "Start" has been pressed |
eencae | 5:3c9407e2fe55 | 82 | JOY_PRESSED, ///< Joystick button has been pressed |
eencae | 5:3c9407e2fe55 | 83 | N_EVENTS ///< A dummy flag that marks the end of the list |
eencae | 5:3c9407e2fe55 | 84 | }; |
eencae | 5:3c9407e2fe55 | 85 | private: |
eencae | 5:3c9407e2fe55 | 86 | mbed::PwmOut *_led1; |
eencae | 5:3c9407e2fe55 | 87 | mbed::PwmOut *_led2; |
eencae | 5:3c9407e2fe55 | 88 | mbed::PwmOut *_led3; |
eencae | 5:3c9407e2fe55 | 89 | mbed::PwmOut *_led4; |
eencae | 5:3c9407e2fe55 | 90 | mbed::PwmOut *_led5; |
eencae | 5:3c9407e2fe55 | 91 | mbed::PwmOut *_led6; |
eencae | 5:3c9407e2fe55 | 92 | |
eencae | 5:3c9407e2fe55 | 93 | mbed::InterruptIn *_button_A; |
eencae | 5:3c9407e2fe55 | 94 | mbed::InterruptIn *_button_B; |
eencae | 5:3c9407e2fe55 | 95 | mbed::InterruptIn *_button_X; |
eencae | 5:3c9407e2fe55 | 96 | mbed::InterruptIn *_button_Y; |
eencae | 5:3c9407e2fe55 | 97 | mbed::InterruptIn *_button_L; |
eencae | 5:3c9407e2fe55 | 98 | mbed::InterruptIn *_button_R; |
eencae | 5:3c9407e2fe55 | 99 | mbed::InterruptIn *_button_back; |
eencae | 5:3c9407e2fe55 | 100 | mbed::InterruptIn *_button_start; |
eencae | 5:3c9407e2fe55 | 101 | mbed::InterruptIn *_button_joystick; |
eencae | 5:3c9407e2fe55 | 102 | |
eencae | 5:3c9407e2fe55 | 103 | mbed::AnalogIn *_vert; |
eencae | 5:3c9407e2fe55 | 104 | mbed::AnalogIn *_horiz; |
eencae | 5:3c9407e2fe55 | 105 | |
eencae | 5:3c9407e2fe55 | 106 | mbed::PwmOut *_buzzer; |
eencae | 5:3c9407e2fe55 | 107 | mbed::AnalogIn *_pot; |
eencae | 5:3c9407e2fe55 | 108 | |
eencae | 5:3c9407e2fe55 | 109 | mbed::Timeout *_timeout; |
eencae | 5:3c9407e2fe55 | 110 | |
eencae | 5:3c9407e2fe55 | 111 | std::bitset<N_EVENTS> _event_state; ///< A binary list of buttons that has been pressed |
eencae | 5:3c9407e2fe55 | 112 | |
eencae | 5:3c9407e2fe55 | 113 | // centred x,y values |
eencae | 5:3c9407e2fe55 | 114 | float _x0; |
eencae | 5:3c9407e2fe55 | 115 | float _y0; |
eencae | 5:3c9407e2fe55 | 116 | |
eencae | 5:3c9407e2fe55 | 117 | public: |
eencae | 5:3c9407e2fe55 | 118 | |
eencae | 5:3c9407e2fe55 | 119 | /** Constructor */ |
eencae | 5:3c9407e2fe55 | 120 | Gamepad(); |
eencae | 5:3c9407e2fe55 | 121 | |
eencae | 5:3c9407e2fe55 | 122 | /** Destructor */ |
eencae | 5:3c9407e2fe55 | 123 | ~Gamepad(); |
eencae | 5:3c9407e2fe55 | 124 | |
eencae | 5:3c9407e2fe55 | 125 | /** Initialise all peripherals and configure interrupts */ |
eencae | 5:3c9407e2fe55 | 126 | void init(); |
eencae | 5:3c9407e2fe55 | 127 | |
eencae | 5:3c9407e2fe55 | 128 | /** Turn all LEDs on */ |
eencae | 5:3c9407e2fe55 | 129 | void leds_on(); |
eencae | 5:3c9407e2fe55 | 130 | |
eencae | 5:3c9407e2fe55 | 131 | /** Turn all LEDs off */ |
eencae | 5:3c9407e2fe55 | 132 | void leds_off(); |
eencae | 5:3c9407e2fe55 | 133 | |
eencae | 5:3c9407e2fe55 | 134 | /** Set all LEDs to duty-cycle |
eencae | 5:3c9407e2fe55 | 135 | *@param value in range 0.0 to 1.0 |
eencae | 5:3c9407e2fe55 | 136 | */ |
eencae | 5:3c9407e2fe55 | 137 | void leds(float val) const; |
eencae | 5:3c9407e2fe55 | 138 | |
eencae | 5:3c9407e2fe55 | 139 | /** Set LED to duty-cycle |
eencae | 5:3c9407e2fe55 | 140 | *@param led number (0 to 5) |
eencae | 5:3c9407e2fe55 | 141 | *@param value in range 0.0 to 1.0 |
eencae | 5:3c9407e2fe55 | 142 | */ |
eencae | 5:3c9407e2fe55 | 143 | void led(int n,float val) const; |
eencae | 5:3c9407e2fe55 | 144 | |
eencae | 5:3c9407e2fe55 | 145 | /** Read potentiometer |
eencae | 5:3c9407e2fe55 | 146 | *@returns potentiometer value in range 0.0 to 1.0 |
eencae | 5:3c9407e2fe55 | 147 | */ |
eencae | 5:3c9407e2fe55 | 148 | float read_pot() const; |
eencae | 5:3c9407e2fe55 | 149 | |
eencae | 5:3c9407e2fe55 | 150 | /** Play tone on piezo |
eencae | 5:3c9407e2fe55 | 151 | * @param frequency in Hz |
eencae | 5:3c9407e2fe55 | 152 | * @param duration of tone in seconds |
eencae | 5:3c9407e2fe55 | 153 | */ |
eencae | 5:3c9407e2fe55 | 154 | void tone(float frequency, float duration); |
eencae | 5:3c9407e2fe55 | 155 | |
eencae | 5:3c9407e2fe55 | 156 | /** |
eencae | 5:3c9407e2fe55 | 157 | * @brief Check whether an event flag has been set and clear it |
eencae | 5:3c9407e2fe55 | 158 | * @param id[in] The ID of the event to test |
eencae | 5:3c9407e2fe55 | 159 | * @return true if the event occurred |
eencae | 5:3c9407e2fe55 | 160 | */ |
eencae | 5:3c9407e2fe55 | 161 | bool check_event(GamepadEvent const id); |
eencae | 5:3c9407e2fe55 | 162 | |
Jenny121 | 12:3952ba0683c7 | 163 | /** Get varanitude of joystick movement |
eencae | 5:3c9407e2fe55 | 164 | * @returns value in range 0.0 to 1.0 |
eencae | 5:3c9407e2fe55 | 165 | */ |
eencae | 5:3c9407e2fe55 | 166 | |
Jenny121 | 12:3952ba0683c7 | 167 | float get_vara(); |
eencae | 5:3c9407e2fe55 | 168 | |
eencae | 5:3c9407e2fe55 | 169 | /** Get angle of joystick movement |
eencae | 5:3c9407e2fe55 | 170 | * @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 | 171 | */ |
eencae | 5:3c9407e2fe55 | 172 | float get_angle(); |
eencae | 5:3c9407e2fe55 | 173 | |
eencae | 5:3c9407e2fe55 | 174 | /** Gets joystick direction |
eencae | 5:3c9407e2fe55 | 175 | * @returns an enum: CENTRE, N, NE, E, SE, S, SW, W, NW, |
eencae | 5:3c9407e2fe55 | 176 | */ |
eencae | 5:3c9407e2fe55 | 177 | Direction get_direction(); // N,NE,E,SE etc. |
eencae | 5:3c9407e2fe55 | 178 | |
eencae | 5:3c9407e2fe55 | 179 | /** Gets raw cartesian co-ordinates of joystick |
eencae | 5:3c9407e2fe55 | 180 | * @returns a struct with x,y members, each in the range 0.0 to 1.0 |
eencae | 5:3c9407e2fe55 | 181 | */ |
eencae | 5:3c9407e2fe55 | 182 | Vector2D get_coord(); // cartesian co-ordinates x,y |
eencae | 5:3c9407e2fe55 | 183 | |
eencae | 5:3c9407e2fe55 | 184 | /** Gets cartesian coordinates mapped to circular grid |
eencae | 5:3c9407e2fe55 | 185 | * @returns a struct with x,y members, each in the range 0.0 to 1.0 |
eencae | 5:3c9407e2fe55 | 186 | */ |
eencae | 5:3c9407e2fe55 | 187 | Vector2D get_mapped_coord(); // x,y mapped to circle |
eencae | 5:3c9407e2fe55 | 188 | |
eencae | 5:3c9407e2fe55 | 189 | /** Gets polar coordinates of the joystick |
Jenny121 | 12:3952ba0683c7 | 190 | * @returns a struct contains vara and angle |
eencae | 5:3c9407e2fe55 | 191 | */ |
Jenny121 | 12:3952ba0683c7 | 192 | Polar get_polar(); // vara and angle in struct form |
eencae | 5:3c9407e2fe55 | 193 | |
eencae | 5:3c9407e2fe55 | 194 | private: |
eencae | 5:3c9407e2fe55 | 195 | void init_buttons(); |
eencae | 5:3c9407e2fe55 | 196 | void tone_off(); |
eencae | 5:3c9407e2fe55 | 197 | |
eencae | 5:3c9407e2fe55 | 198 | void a_isr(); |
eencae | 5:3c9407e2fe55 | 199 | void b_isr(); |
eencae | 5:3c9407e2fe55 | 200 | void x_isr(); |
eencae | 5:3c9407e2fe55 | 201 | void y_isr(); |
eencae | 5:3c9407e2fe55 | 202 | void l_isr(); |
eencae | 5:3c9407e2fe55 | 203 | void r_isr(); |
eencae | 5:3c9407e2fe55 | 204 | void back_isr(); |
eencae | 5:3c9407e2fe55 | 205 | void start_isr(); |
eencae | 5:3c9407e2fe55 | 206 | void joy_isr(); |
eencae | 5:3c9407e2fe55 | 207 | }; |
eencae | 5:3c9407e2fe55 | 208 | |
eencae | 5:3c9407e2fe55 | 209 | #endif |