![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Li Ruofan 201199450
Dependencies: mbed Gamepad Joystick
Joystick/Joystick.h@3:cf9fead9c3f4, 2020-05-14 (annotated)
- Committer:
- DannyLee
- Date:
- Thu May 14 13:12:28 2020 +0000
- Revision:
- 3:cf9fead9c3f4
aaa
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DannyLee | 3:cf9fead9c3f4 | 1 | #ifndef JOYSTICK_H |
DannyLee | 3:cf9fead9c3f4 | 2 | #define JOYSTICK_H |
DannyLee | 3:cf9fead9c3f4 | 3 | |
DannyLee | 3:cf9fead9c3f4 | 4 | #include "mbed.h" |
DannyLee | 3:cf9fead9c3f4 | 5 | |
DannyLee | 3:cf9fead9c3f4 | 6 | // this value can be tuned to alter tolerance of joystick movement |
DannyLee | 3:cf9fead9c3f4 | 7 | #define TOL 0.1f |
DannyLee | 3:cf9fead9c3f4 | 8 | #define RAD2DEG 57.2957795131f |
DannyLee | 3:cf9fead9c3f4 | 9 | |
DannyLee | 3:cf9fead9c3f4 | 10 | enum Direction { |
DannyLee | 3:cf9fead9c3f4 | 11 | CENTRE, // 0 |
DannyLee | 3:cf9fead9c3f4 | 12 | N, // 1 |
DannyLee | 3:cf9fead9c3f4 | 13 | NE, // 2 |
DannyLee | 3:cf9fead9c3f4 | 14 | E, // 3 |
DannyLee | 3:cf9fead9c3f4 | 15 | SE, // 4 |
DannyLee | 3:cf9fead9c3f4 | 16 | S, // 5 |
DannyLee | 3:cf9fead9c3f4 | 17 | SW, // 6 |
DannyLee | 3:cf9fead9c3f4 | 18 | W, // 7 |
DannyLee | 3:cf9fead9c3f4 | 19 | NW // 8 |
DannyLee | 3:cf9fead9c3f4 | 20 | }; |
DannyLee | 3:cf9fead9c3f4 | 21 | |
DannyLee | 3:cf9fead9c3f4 | 22 | struct Vector2D { |
DannyLee | 3:cf9fead9c3f4 | 23 | float x; |
DannyLee | 3:cf9fead9c3f4 | 24 | float y; |
DannyLee | 3:cf9fead9c3f4 | 25 | }; |
DannyLee | 3:cf9fead9c3f4 | 26 | |
DannyLee | 3:cf9fead9c3f4 | 27 | struct Polar { |
DannyLee | 3:cf9fead9c3f4 | 28 | float mag; |
DannyLee | 3:cf9fead9c3f4 | 29 | float angle; |
DannyLee | 3:cf9fead9c3f4 | 30 | }; |
DannyLee | 3:cf9fead9c3f4 | 31 | |
DannyLee | 3:cf9fead9c3f4 | 32 | /** Joystick Class |
DannyLee | 3:cf9fead9c3f4 | 33 | @author Dr Craig A. Evans, University of Leeds |
DannyLee | 3:cf9fead9c3f4 | 34 | @brief Library for interfacing with analogue joystick |
DannyLee | 3:cf9fead9c3f4 | 35 | |
DannyLee | 3:cf9fead9c3f4 | 36 | Example: |
DannyLee | 3:cf9fead9c3f4 | 37 | |
DannyLee | 3:cf9fead9c3f4 | 38 | @code |
DannyLee | 3:cf9fead9c3f4 | 39 | |
DannyLee | 3:cf9fead9c3f4 | 40 | #include "mbed.h" |
DannyLee | 3:cf9fead9c3f4 | 41 | #include "Joystick.h" |
DannyLee | 3:cf9fead9c3f4 | 42 | |
DannyLee | 3:cf9fead9c3f4 | 43 | // y x button |
DannyLee | 3:cf9fead9c3f4 | 44 | Joystick joystick(PTB10,PTB11,PTC16); |
DannyLee | 3:cf9fead9c3f4 | 45 | |
DannyLee | 3:cf9fead9c3f4 | 46 | int main() { |
DannyLee | 3:cf9fead9c3f4 | 47 | |
DannyLee | 3:cf9fead9c3f4 | 48 | joystick.init(); |
DannyLee | 3:cf9fead9c3f4 | 49 | |
DannyLee | 3:cf9fead9c3f4 | 50 | while(1) { |
DannyLee | 3:cf9fead9c3f4 | 51 | |
DannyLee | 3:cf9fead9c3f4 | 52 | Vector2D coord = joystick.get_coord(); |
DannyLee | 3:cf9fead9c3f4 | 53 | printf("Coord = %f,%f\n",coord.x,coord.y); |
DannyLee | 3:cf9fead9c3f4 | 54 | |
DannyLee | 3:cf9fead9c3f4 | 55 | Vector2D mapped_coord = joystick.get_mapped_coord(); |
DannyLee | 3:cf9fead9c3f4 | 56 | printf("Mapped coord = %f,%f\n",mapped_coord.x,mapped_coord.y); |
DannyLee | 3:cf9fead9c3f4 | 57 | |
DannyLee | 3:cf9fead9c3f4 | 58 | float mag = joystick.get_mag(); |
DannyLee | 3:cf9fead9c3f4 | 59 | float angle = joystick.get_angle(); |
DannyLee | 3:cf9fead9c3f4 | 60 | printf("Mag = %f Angle = %f\n",mag,angle); |
DannyLee | 3:cf9fead9c3f4 | 61 | |
DannyLee | 3:cf9fead9c3f4 | 62 | Direction d = joystick.get_direction(); |
DannyLee | 3:cf9fead9c3f4 | 63 | printf("Direction = %i\n",d); |
DannyLee | 3:cf9fead9c3f4 | 64 | |
DannyLee | 3:cf9fead9c3f4 | 65 | if (joystick.button_pressed() ) { |
DannyLee | 3:cf9fead9c3f4 | 66 | printf("Button Pressed\n"); |
DannyLee | 3:cf9fead9c3f4 | 67 | } |
DannyLee | 3:cf9fead9c3f4 | 68 | |
DannyLee | 3:cf9fead9c3f4 | 69 | wait(0.5); |
DannyLee | 3:cf9fead9c3f4 | 70 | } |
DannyLee | 3:cf9fead9c3f4 | 71 | |
DannyLee | 3:cf9fead9c3f4 | 72 | |
DannyLee | 3:cf9fead9c3f4 | 73 | } |
DannyLee | 3:cf9fead9c3f4 | 74 | |
DannyLee | 3:cf9fead9c3f4 | 75 | * @endcode |
DannyLee | 3:cf9fead9c3f4 | 76 | */ |
DannyLee | 3:cf9fead9c3f4 | 77 | class Joystick |
DannyLee | 3:cf9fead9c3f4 | 78 | { |
DannyLee | 3:cf9fead9c3f4 | 79 | public: |
DannyLee | 3:cf9fead9c3f4 | 80 | |
DannyLee | 3:cf9fead9c3f4 | 81 | // y-pot x-pot button |
DannyLee | 3:cf9fead9c3f4 | 82 | Joystick(PinName vertPin,PinName horizPin,PinName clickPin); |
DannyLee | 3:cf9fead9c3f4 | 83 | |
DannyLee | 3:cf9fead9c3f4 | 84 | void init(); // needs to be called at start with joystick centred |
DannyLee | 3:cf9fead9c3f4 | 85 | float get_mag(); // polar |
DannyLee | 3:cf9fead9c3f4 | 86 | float get_angle(); // polar |
DannyLee | 3:cf9fead9c3f4 | 87 | Vector2D get_coord(); // cartesian co-ordinates x,y |
DannyLee | 3:cf9fead9c3f4 | 88 | Vector2D get_mapped_coord(); // x,y mapped to circle |
DannyLee | 3:cf9fead9c3f4 | 89 | Direction get_direction(); // N,NE,E,SE etc. |
DannyLee | 3:cf9fead9c3f4 | 90 | Polar get_polar(); // mag and angle in struct form |
DannyLee | 3:cf9fead9c3f4 | 91 | bool button_pressed(); // read button flag set in ISR when button pressed |
DannyLee | 3:cf9fead9c3f4 | 92 | |
DannyLee | 3:cf9fead9c3f4 | 93 | private: |
DannyLee | 3:cf9fead9c3f4 | 94 | |
DannyLee | 3:cf9fead9c3f4 | 95 | AnalogIn *vert; |
DannyLee | 3:cf9fead9c3f4 | 96 | AnalogIn *horiz; |
DannyLee | 3:cf9fead9c3f4 | 97 | InterruptIn *click; |
DannyLee | 3:cf9fead9c3f4 | 98 | |
DannyLee | 3:cf9fead9c3f4 | 99 | int _click_flag; // flag set in ISR |
DannyLee | 3:cf9fead9c3f4 | 100 | void click_isr(); // ISR on button press |
DannyLee | 3:cf9fead9c3f4 | 101 | |
DannyLee | 3:cf9fead9c3f4 | 102 | // centred x,y values |
DannyLee | 3:cf9fead9c3f4 | 103 | float _x0; |
DannyLee | 3:cf9fead9c3f4 | 104 | float _y0; |
DannyLee | 3:cf9fead9c3f4 | 105 | |
DannyLee | 3:cf9fead9c3f4 | 106 | }; |
DannyLee | 3:cf9fead9c3f4 | 107 | |
DannyLee | 3:cf9fead9c3f4 | 108 | #endif |