zhangxinyu01text
Embed:
(wiki syntax)
Show/hide line numbers
Gamepad.h
00001 #ifndef GAMEPAD_H 00002 #define GAMEPAD_H 00003 00004 #include <bitset> 00005 00006 // Forward declaration of the classes that we use from the mbed library 00007 // This avoids the need for us to include the huge mbed.h header inside our 00008 // own library API 00009 00010 /** Gamepad Class 00011 * @brief with the para and elemets of LCD pad i sLCD 00012 * @author Zhang Xinyu 00013 * @school EE of SWJTU &leeds joint school 00014 * @date MAY 2019 00015 */ 00016 00017 namespace mbed 00018 { 00019 class AnalogIn; 00020 class InterruptIn; 00021 class PwmOut; 00022 class Timeout; 00023 } 00024 00025 #define TOL 0.1f 00026 #define RAD2DEG 57.2957795131f 00027 00028 /** Enum for direction 00029 * @ details defined the direction of joysticks 00030 * @ param pushed defined the CENTRE N,NE,E,SE,S,SW,W,NW acording to agnles 00031 */ 00032 enum Direction { 00033 CENTRE, 00034 N, 00035 NE, 00036 E, 00037 SE, 00038 S, 00039 SW, 00040 W, 00041 NW 00042 }; 00043 00044 /** vector struct */ 00045 struct Vector2D { 00046 float x; /** < char for x value position> */ 00047 float y; /** < char for y value position> */ 00048 00049 }; 00050 00051 /** Polar coordinate Struct 00052 * @ param the vara magnitude 00053 * @ param float for angle (in degrees) 00054 * @ details the vara and angle are use by other function 00055 */ 00056 00057 /** vector struct */ 00058 struct Polar { 00059 float vara; /** < char for vara magnatic value of joystick> */ 00060 float angle; /** < char for angle by joysticks accoring to LCD> */ 00061 }; 00062 00063 /** Gamepad Class 00064 @brief Library of the gamepad 00065 @author zhang xinyu 00066 @date May 2019 00067 */ 00068 class Gamepad 00069 { 00070 public: 00071 /** Gamepad events 00072 * @brief List of events that can be registered on the gamepad 00073 * @ param defined the button in the pad 00074 */ 00075 enum GamepadEvent { 00076 A_PRESSED, ///< Button A has been pressed 00077 B_PRESSED, ///< Button B has been pressed 00078 X_PRESSED, ///< Button X has been pressed 00079 Y_PRESSED, ///< Button Y has been pressed 00080 L_PRESSED, ///< Button L has been pressed 00081 R_PRESSED, ///< Button R has been pressed 00082 BACK_PRESSED, ///< Button "Back" has been pressed 00083 START_PRESSED, ///< Button "Start" has been pressed 00084 JOY_PRESSED, ///< Joystick button has been pressed 00085 N_EVENTS ///< A dummy flag that marks the end of the list 00086 }; 00087 private: 00088 mbed::PwmOut *_led1; 00089 mbed::PwmOut *_led2; 00090 mbed::PwmOut *_led3; 00091 mbed::PwmOut *_led4; 00092 mbed::PwmOut *_led5; 00093 mbed::PwmOut *_led6; 00094 00095 mbed::InterruptIn *_button_A; 00096 mbed::InterruptIn *_button_B; 00097 mbed::InterruptIn *_button_X; 00098 mbed::InterruptIn *_button_Y; 00099 mbed::InterruptIn *_button_L; 00100 mbed::InterruptIn *_button_R; 00101 mbed::InterruptIn *_button_back; 00102 mbed::InterruptIn *_button_start; 00103 mbed::InterruptIn *_button_joystick; 00104 00105 mbed::AnalogIn *_vert; 00106 mbed::AnalogIn *_horiz; 00107 00108 mbed::PwmOut *_buzzer; 00109 mbed::AnalogIn *_pot; 00110 00111 mbed::Timeout *_timeout; 00112 00113 std::bitset<N_EVENTS> _event_state; ///< A binary list of buttons that has been pressed 00114 00115 // centred x,y values 00116 float _x0; 00117 float _y0; 00118 00119 public: 00120 00121 /** Constructor */ 00122 Gamepad(); 00123 00124 /** Destructor */ 00125 ~Gamepad(); 00126 00127 /** Initialise all peripherals and configure interrupts */ 00128 void init(); 00129 00130 /** Turn all LEDs on */ 00131 void leds_on(); 00132 00133 /** Turn all LEDs off */ 00134 void leds_off(); 00135 00136 /** Set all LEDs to duty-cycle 00137 *@param value in range 0.0 to 1.0 00138 */ 00139 void leds(float val) const; 00140 00141 /** Set LED to duty-cycle 00142 *@param led number (0 to 5) 00143 *@param value in range 0.0 to 1.0 00144 */ 00145 void led(int n,float val) const; 00146 00147 /** Read potentiometer 00148 *@returns potentiometer value in range 0.0 to 1.0 00149 */ 00150 float read_pot() const; 00151 00152 /** Play tone on piezo 00153 * @param frequency in Hz 00154 * @param duration of tone in seconds 00155 */ 00156 void tone(float frequency, float duration); 00157 00158 /** 00159 * @brief Check whether an event flag has been set and clear it 00160 * @param id[in] The ID of the event to test 00161 * @return true if the event occurred 00162 */ 00163 bool check_event(GamepadEvent const id); 00164 00165 /** Get varanitude of joystick movement 00166 * @returns value in range 0.0 to 1.0 00167 */ 00168 00169 float get_vara(); 00170 00171 /** Get angle of joystick movement 00172 * @returns value in range 0.0 to 359.9. 0.0 corresponds to N, 180.0 to S. -1.0 is central 00173 */ 00174 float get_angle(); 00175 00176 /** Gets joystick direction 00177 * @returns an enum: CENTRE, N, NE, E, SE, S, SW, W, NW, 00178 */ 00179 Direction get_direction(); // N,NE,E,SE etc. 00180 00181 /** Gets raw cartesian co-ordinates of joystick 00182 * @returns a struct with x,y members, each in the range 0.0 to 1.0 00183 */ 00184 Vector2D get_coord(); // cartesian co-ordinates x,y 00185 00186 /** Gets cartesian coordinates mapped to circular grid 00187 * @returns a struct with x,y members, each in the range 0.0 to 1.0 00188 */ 00189 Vector2D get_mapped_coord(); // x,y mapped to circle 00190 00191 /** Gets polar coordinates of the joystick 00192 * @returns a struct contains vara and angle 00193 */ 00194 Polar get_polar(); // vara and angle in struct form 00195 00196 private: 00197 void init_buttons(); 00198 void tone_off(); 00199 00200 void a_isr(); 00201 void b_isr(); 00202 void x_isr(); 00203 void y_isr(); 00204 void l_isr(); 00205 void r_isr(); 00206 void back_isr(); 00207 void start_isr(); 00208 void joy_isr(); 00209 }; 00210 00211 #endif
Generated on Mon Feb 20 2023 02:55:49 by
1.7.2