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