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