444444444444
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 2D struct 00045 * @ param float x and y for value */ 00046 struct Vector2D { 00047 float x; 00048 float y; 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 struct Polar { 00057 float vara; 00058 float angle; 00059 }; 00060 00061 /** Gamepad Class 00062 @brief Library of the gamepad 00063 @author zhang xinyu 00064 @date May 2019 00065 */ 00066 class Gamepad 00067 { 00068 public: 00069 /** Gamepad events 00070 * @brief List of events that can be registered on the gamepad 00071 * @ param defined the button in the pad 00072 */ 00073 enum GamepadEvent { 00074 A_PRESSED, ///< Button A has been pressed 00075 B_PRESSED, ///< Button B has been pressed 00076 X_PRESSED, ///< Button X has been pressed 00077 Y_PRESSED, ///< Button Y has been pressed 00078 L_PRESSED, ///< Button L has been pressed 00079 R_PRESSED, ///< Button R has been pressed 00080 BACK_PRESSED, ///< Button "Back" has been pressed 00081 START_PRESSED, ///< Button "Start" has been pressed 00082 JOY_PRESSED, ///< Joystick button has been pressed 00083 N_EVENTS ///< A dummy flag that marks the end of the list 00084 }; 00085 private: 00086 mbed::PwmOut *_led1; 00087 mbed::PwmOut *_led2; 00088 mbed::PwmOut *_led3; 00089 mbed::PwmOut *_led4; 00090 mbed::PwmOut *_led5; 00091 mbed::PwmOut *_led6; 00092 00093 mbed::InterruptIn *_button_A; 00094 mbed::InterruptIn *_button_B; 00095 mbed::InterruptIn *_button_X; 00096 mbed::InterruptIn *_button_Y; 00097 mbed::InterruptIn *_button_L; 00098 mbed::InterruptIn *_button_R; 00099 mbed::InterruptIn *_button_back; 00100 mbed::InterruptIn *_button_start; 00101 mbed::InterruptIn *_button_joystick; 00102 00103 mbed::AnalogIn *_vert; 00104 mbed::AnalogIn *_horiz; 00105 00106 mbed::PwmOut *_buzzer; 00107 mbed::AnalogIn *_pot; 00108 00109 mbed::Timeout *_timeout; 00110 00111 std::bitset<N_EVENTS> _event_state; ///< A binary list of buttons that has been pressed 00112 00113 // centred x,y values 00114 float _x0; 00115 float _y0; 00116 00117 public: 00118 00119 /** Constructor */ 00120 Gamepad(); 00121 00122 /** Destructor */ 00123 ~Gamepad(); 00124 00125 /** Initialise all peripherals and configure interrupts */ 00126 void init(); 00127 00128 /** Turn all LEDs on */ 00129 void leds_on(); 00130 00131 /** Turn all LEDs off */ 00132 void leds_off(); 00133 00134 /** Set all LEDs to duty-cycle 00135 *@param value in range 0.0 to 1.0 00136 */ 00137 void leds(float val) const; 00138 00139 /** Set LED to duty-cycle 00140 *@param led number (0 to 5) 00141 *@param value in range 0.0 to 1.0 00142 */ 00143 void led(int n,float val) const; 00144 00145 /** Read potentiometer 00146 *@returns potentiometer value in range 0.0 to 1.0 00147 */ 00148 float read_pot() const; 00149 00150 /** Play tone on piezo 00151 * @param frequency in Hz 00152 * @param duration of tone in seconds 00153 */ 00154 void tone(float frequency, float duration); 00155 00156 /** 00157 * @brief Check whether an event flag has been set and clear it 00158 * @param id[in] The ID of the event to test 00159 * @return true if the event occurred 00160 */ 00161 bool check_event(GamepadEvent const id); 00162 00163 /** Get varanitude of joystick movement 00164 * @returns value in range 0.0 to 1.0 00165 */ 00166 00167 float get_vara(); 00168 00169 /** Get angle of joystick movement 00170 * @returns value in range 0.0 to 359.9. 0.0 corresponds to N, 180.0 to S. -1.0 is central 00171 */ 00172 float get_angle(); 00173 00174 /** Gets joystick direction 00175 * @returns an enum: CENTRE, N, NE, E, SE, S, SW, W, NW, 00176 */ 00177 Direction get_direction(); // N,NE,E,SE etc. 00178 00179 /** Gets raw cartesian co-ordinates of joystick 00180 * @returns a struct with x,y members, each in the range 0.0 to 1.0 00181 */ 00182 Vector2D get_coord(); // cartesian co-ordinates x,y 00183 00184 /** Gets cartesian coordinates mapped to circular grid 00185 * @returns a struct with x,y members, each in the range 0.0 to 1.0 00186 */ 00187 Vector2D get_mapped_coord(); // x,y mapped to circle 00188 00189 /** Gets polar coordinates of the joystick 00190 * @returns a struct contains vara and angle 00191 */ 00192 Polar get_polar(); // vara and angle in struct form 00193 00194 private: 00195 void init_buttons(); 00196 void tone_off(); 00197 00198 void a_isr(); 00199 void b_isr(); 00200 void x_isr(); 00201 void y_isr(); 00202 void l_isr(); 00203 void r_isr(); 00204 void back_isr(); 00205 void start_isr(); 00206 void joy_isr(); 00207 }; 00208 00209 #endif
Generated on Sat Jul 16 2022 19:26:21 by
1.7.2