|
0
|
1 #ifndef Joystick_h
|
|
|
2 #define Joystick_h
|
|
|
3
|
|
|
4 #include <Potentiometer.h>
|
|
|
5 #include <Button.h>
|
|
|
6
|
|
4
|
7 /**
|
|
|
8 * center deadzone radius, if the joystick is not calibrated.
|
|
|
9 * raw pot value (0..1023)
|
|
|
10 */
|
|
|
11 #define CENTER_DEADZONE_UNCALIBRATED 100
|
|
|
12
|
|
|
13 /**
|
|
|
14 * center deadzone radius, if the joystick is calibrated.
|
|
|
15 * raw pot value (0..1023)
|
|
|
16 */
|
|
|
17 #define CENTER_DEADZONE_CALIBRATED 15
|
|
|
18
|
|
|
19 /**
|
|
|
20 * center radius, for digital interpretation. logical (-100..100) value
|
|
|
21 */
|
|
|
22 #define CENTER_RADIUS_DIGITAL 20
|
|
|
23
|
|
0
|
24
|
|
|
25 /**
|
|
|
26 utility class for interpreting joystick values.
|
|
|
27 combines two potentiometer and calibration information to provide a vector
|
|
|
28 of user selected direction with 0,0 in the center and +x,+y in top-right.
|
|
|
29
|
|
|
30 (c) 2017 arkadiusz@wobblyhead.net
|
|
|
31 */
|
|
|
32 class Joystick {
|
|
|
33 public:
|
|
|
34 // x-axis pot
|
|
|
35 Potentiometer x;
|
|
|
36 // y-axis pot
|
|
|
37 Potentiometer y;
|
|
|
38 // optional button
|
|
|
39 Button b;
|
|
|
40
|
|
|
41 // if calibrated, current vector, x component
|
|
|
42 float vx;
|
|
|
43 // if calibrated, current vector, y component
|
|
|
44 float vy;
|
|
1
|
45 // flag, if true joystick is considered centered/idle
|
|
|
46 bool isCentered;
|
|
|
47 // "digital" vector, x
|
|
|
48 int dvx;
|
|
|
49 // "digital" vector, y
|
|
|
50 int dvy;
|
|
0
|
51
|
|
|
52 // flag, if true joystick is calibrated.
|
|
|
53 bool isCalibrated;
|
|
|
54
|
|
|
55 // calibrated center of x axis
|
|
|
56 int centerX;
|
|
|
57 // calibrated center of y axis
|
|
|
58 int centerY;
|
|
|
59
|
|
|
60 /**
|
|
|
61 create an uninitialised Joystick.
|
|
|
62 setup() must be called after this to make it usable.
|
|
|
63 */
|
|
|
64 Joystick();
|
|
|
65
|
|
|
66 /**
|
|
|
67 setup joystick instance with flipped y axis
|
|
|
68
|
|
|
69 @param {byte} pinX input pin for x axis
|
|
|
70 @param {byte} pinY input pin for y axis
|
|
|
71 @param {byte} pinB input pin for button
|
|
|
72 */
|
|
|
73 void setup(byte pinX, byte pinY, byte pinB);
|
|
|
74
|
|
|
75 /**
|
|
|
76 setup joystick instance
|
|
|
77
|
|
|
78 @param {byte} pinX input pin for x axis
|
|
|
79 @param {byte} pinY input pin for y axis
|
|
|
80 @param {byte} pinB input pin for button
|
|
|
81 @param {bool} flipY flip y axis
|
|
|
82 */
|
|
|
83 void setup(byte pinX, byte pinY, byte pinB, bool flipY);
|
|
|
84
|
|
|
85 /**
|
|
|
86 check inputs.
|
|
|
87 implicitely calls millis();
|
|
|
88 */
|
|
|
89 void update();
|
|
|
90
|
|
|
91 /**
|
|
|
92 check inputs, update values.
|
|
|
93 if calibrated, calculate vector.
|
|
|
94 @param {unsigned long} ms current millis() value
|
|
|
95 */
|
|
|
96 void update(unsigned long ms);
|
|
|
97
|
|
|
98 private:
|
|
|
99 bool flipY;
|
|
|
100
|
|
|
101 };
|
|
|
102
|
|
|
103 #endif
|