diff Joystick.cpp @ 1:fd012dcc048d

added isCentered, dvx, dvy
author ark
date 2017-01-29 13:59 +0100
parents f31cf3175189
children d423ea399401
line wrap: on
line diff
--- a/Joystick.cpp	Thu Jan 26 00:48:42 2017 +0100
+++ b/Joystick.cpp	Sun Jan 29 13:59:21 2017 +0100
@@ -6,6 +6,7 @@
 
 #include "Joystick.h"
 
+#define DEBUG
 
 /**
     see Joystick.h for description.
@@ -21,7 +22,7 @@
     @param {byte} pinB      input pin for button
 */
 void Joystick::setup(byte pinX, byte pinY, byte pinB) {
-    this->setup(pinX, pinY, pinB, true);
+    setup(pinX, pinY, pinB, true);
 }
 
 
@@ -34,59 +35,65 @@
     @param {bool} flipY     mirror y axis output
 */
 void Joystick::setup(byte pinX, byte pinY, byte pinB, bool flipY) {
-    this->x.setup(pinX);
-    this->y.setup(pinY);
-    this->b.setup(pinB, true);
-    this->centerX = 512;
-    this->centerY = 512;
+    x.setup(pinX);
+    y.setup(pinY);
+    b.setup(pinB, true);
+    centerX = 512;
+    centerY = 512;
     this->flipY = flipY;
 }
 
 
 void Joystick::update() {
-    this->update(millis());
+    update(millis());
 }
 
 
 void Joystick::update(unsigned long ms) {
-    this->x.update(ms);
-    this->y.update(ms);
-    this->b.update(ms);
-    float vx = 0;
-    float vy = 0;
+    x.update(ms);
+    y.update(ms);
+    b.update(ms);
     // if not calibrated yet, and stick is stopped within the center deadzone,
     // auto-calibrate to that point
-    if(!this->isCalibrated) {
-        bool inDeadzone = abs(512-this->x.value) < Joystick::CENTER_DEADZONE &&
-            abs(512-this->y.value) < Joystick::CENTER_DEADZONE;
-        if(inDeadzone) {
-            if(this->x.isStopped && this->y.isStopped) {
-                this->isCalibrated = true;
-                this->centerX = this->x.value;
-                this->centerY = this->y.value;
+    if(!isCalibrated) {
+        isCentered = abs(512-x.value) < CENTER_DEADZONE_UNCALIBRATED &&
+            abs(512-y.value) < CENTER_DEADZONE_UNCALIBRATED;
+        if(isCentered) {
+            if(x.isStopped && y.isStopped) {
+                isCalibrated = true;
+                centerX = x.value;
+                centerY = y.value;
                 #ifdef DEBUG
-                Serial.print("Joystick calibrated, center=");
-                Serial.print(this->centerX);
+                Serial.print(ms);
+                Serial.print(": Joystick calibrated, center=");
+                Serial.print(centerX);
                 Serial.print(",");
-                Serial.println(this->centerY);
+                Serial.println(centerY);
                 #endif
             }
             else return;
         }
     }
-    vx = this->x.value - this->centerX;
-    if(vx < 0)
-        this->vx = vx*100/this->centerX;
-    else
-        this->vx = vx*100/(1023-this->centerX);
-    vy = this->y.value - this->centerY;
-    if(vy < 0)
-        this->vy = vy*100/this->centerY;
-    else
-        this->vy = vy*100/(1023-this->centerY);
-    // flip vy to cartesian logic
+    // raw values deltas from (calibrated or assumed) center
+    float dx = x.value-centerX;
+    float dy = y.value-centerY;
+    // if calibrated, check here if centered
+    if(isCalibrated) 
+        isCentered = abs(dx) < CENTER_DEADZONE_CALIBRATED &&
+            abs(dy) < CENTER_DEADZONE_CALIBRATED;
+    // calculate vector mapping 0..centerN..1023 to -100..0..100
+    if(dx < 0) vx = dx*100/centerX;
+    else vx = dx*100/(1023-centerX);
+    if(dy < 0) vy = dy*100/centerY;
+    else vy = dy*100/(1023-centerY);
+    // flip vy to cartesian logic if configured
     if(this->flipY)
-        this->vy = -this->vy;
+        vy = -vy;
+    // add digital vector
+    dvx = vx < -CENTER_RADIUS_DIGITAL ? -100 :
+        vx > CENTER_RADIUS_DIGITAL ? 100 : 0;
+    dvy = vy < -CENTER_RADIUS_DIGITAL ? -100 :
+        vy > CENTER_RADIUS_DIGITAL ? 100 : 0;
 }