-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJoystick.h
77 lines (74 loc) · 1.9 KB
/
Joystick.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifndef JOYSTICK_H
#define JOYSTICK_H
#include "mbed.h"
// this value can be tuned to alter tolerance of joystick movement
#define TOL 0.1f
#define RAD2DEG 57.2957795131f
enum Direction {
CENTRE, // 0
u, // 1
r, // 2
d, // 3
l // 4
};
struct Vector2D {
float x;
float y;
};
struct Polar {
float mag;
float angle;
};
/** Joystick Class
@author Dr Craig A. Evans, University of Leeds
@brief Library for interfacing with analogue joystick
Example:
@code
#include "Joystick.h"
#include "mbed.h"
// y x button
Joystick joystick(PTB10,PTB11,PTC16);
int main() {
joystick.init();
while(1) {
Vector2D coord = joystick.get_coord();
printf("Coord = %f,%f\n",coord.x,coord.y);
Vector2D mapped_coord = joystick.get_mapped_coord();
printf("Mapped coord = %f,%f\n",mapped_coord.x,mapped_coord.y);
float mag = joystick.get_mag();
float angle = joystick.get_angle();
printf("Mag = %f Angle = %f\n",mag,angle);
Direction d = joystick.get_direction();
printf("Direction = %i\n",d);
if (joystick.button_pressed() ) {
printf("Button Pressed\n");
}
wait(0.5);
}
}
* @endcode
*/
class Joystick {
public:
// y-pot x-pot button
Joystick(PinName vertPin, PinName horizPin, PinName clickPin);
void init(); // needs to be called at start with joystick centred
float get_mag(); // polar
float get_angle(); // polar
Vector2D get_coord(); // cartesian co-ordinates x,y
Vector2D get_mapped_coord(); // x,y mapped to circle
Direction get_direction(); // N,NE,E,SE etc.
Polar get_polar(); // mag and angle in struct form
bool button_pressed(); // read button flag set in ISR when button pressed
bool paused;
private:
AnalogIn *vert;
AnalogIn *horiz;
InterruptIn *click;
volatile int _click_flag; // flag set in ISR
void click_isr(); // ISR on button press
// centred x,y values
float _x0;
float _y0;
};
#endif