|
0
|
1 #if defined(ARDUINO) && ARDUINO >= 100
|
|
|
2 #include "Arduino.h"
|
|
|
3 #else
|
|
|
4 #include "WProgram.h"
|
|
|
5 #endif
|
|
|
6
|
|
|
7 #include "Joystick.h"
|
|
|
8
|
|
1
|
9 #define DEBUG
|
|
0
|
10
|
|
|
11 /**
|
|
|
12 see Joystick.h for description.
|
|
|
13 */
|
|
|
14
|
|
|
15 Joystick::Joystick() {}
|
|
|
16
|
|
|
17 /**
|
|
|
18 setup joystick instance
|
|
|
19
|
|
|
20 @param {byte} pinX input pin for x axis
|
|
|
21 @param {byte} pinY input pin for y axis
|
|
|
22 @param {byte} pinB input pin for button
|
|
|
23 */
|
|
|
24 void Joystick::setup(byte pinX, byte pinY, byte pinB) {
|
|
1
|
25 setup(pinX, pinY, pinB, true);
|
|
0
|
26 }
|
|
|
27
|
|
|
28
|
|
|
29 /**
|
|
|
30 setup joystick instance
|
|
|
31
|
|
|
32 @param {byte} pinX input pin for x axis
|
|
|
33 @param {byte} pinY input pin for y axis
|
|
|
34 @param {byte} pinB input pin for button
|
|
|
35 @param {bool} flipY mirror y axis output
|
|
|
36 */
|
|
|
37 void Joystick::setup(byte pinX, byte pinY, byte pinB, bool flipY) {
|
|
1
|
38 x.setup(pinX);
|
|
|
39 y.setup(pinY);
|
|
|
40 b.setup(pinB, true);
|
|
|
41 centerX = 512;
|
|
|
42 centerY = 512;
|
|
0
|
43 this->flipY = flipY;
|
|
|
44 }
|
|
|
45
|
|
|
46
|
|
|
47 void Joystick::update() {
|
|
1
|
48 update(millis());
|
|
0
|
49 }
|
|
|
50
|
|
|
51
|
|
|
52 void Joystick::update(unsigned long ms) {
|
|
1
|
53 x.update(ms);
|
|
|
54 y.update(ms);
|
|
|
55 b.update(ms);
|
|
0
|
56 // if not calibrated yet, and stick is stopped within the center deadzone,
|
|
|
57 // auto-calibrate to that point
|
|
1
|
58 if(!isCalibrated) {
|
|
|
59 isCentered = abs(512-x.value) < CENTER_DEADZONE_UNCALIBRATED &&
|
|
|
60 abs(512-y.value) < CENTER_DEADZONE_UNCALIBRATED;
|
|
|
61 if(isCentered) {
|
|
|
62 if(x.isStopped && y.isStopped) {
|
|
|
63 isCalibrated = true;
|
|
|
64 centerX = x.value;
|
|
|
65 centerY = y.value;
|
|
0
|
66 #ifdef DEBUG
|
|
1
|
67 Serial.print(ms);
|
|
|
68 Serial.print(": Joystick calibrated, center=");
|
|
|
69 Serial.print(centerX);
|
|
0
|
70 Serial.print(",");
|
|
1
|
71 Serial.println(centerY);
|
|
0
|
72 #endif
|
|
|
73 }
|
|
|
74 else return;
|
|
|
75 }
|
|
|
76 }
|
|
1
|
77 // raw values deltas from (calibrated or assumed) center
|
|
|
78 float dx = x.value-centerX;
|
|
|
79 float dy = y.value-centerY;
|
|
|
80 // if calibrated, check here if centered
|
|
|
81 if(isCalibrated)
|
|
|
82 isCentered = abs(dx) < CENTER_DEADZONE_CALIBRATED &&
|
|
|
83 abs(dy) < CENTER_DEADZONE_CALIBRATED;
|
|
3
|
84 // if centered, reduce vector to 0,0
|
|
|
85 if(isCentered) {
|
|
|
86 vx = 0.0;
|
|
|
87 vy = 0.0;
|
|
|
88 }
|
|
|
89 else {
|
|
|
90 // calculate vector mapping 0..centerN..1023 to -100..0..100
|
|
|
91 if(dx < 0) vx = dx*100/centerX;
|
|
|
92 else vx = dx*100/(1023-centerX);
|
|
|
93 if(dy < 0) vy = dy*100/centerY;
|
|
|
94 else vy = dy*100/(1023-centerY);
|
|
|
95 // flip vy to cartesian logic if configured
|
|
|
96 if(this->flipY)
|
|
|
97 vy = -vy;
|
|
|
98 }
|
|
1
|
99 // add digital vector
|
|
|
100 dvx = vx < -CENTER_RADIUS_DIGITAL ? -100 :
|
|
|
101 vx > CENTER_RADIUS_DIGITAL ? 100 : 0;
|
|
|
102 dvy = vy < -CENTER_RADIUS_DIGITAL ? -100 :
|
|
|
103 vy > CENTER_RADIUS_DIGITAL ? 100 : 0;
|
|
0
|
104 }
|
|
|
105
|
|
|
106
|
|
|
107
|