comparison Joystick.cpp @ 1:fd012dcc048d

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