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