Two axis analog (2x potmeter) joystick driver.

Revision:
5:48ba213dfbb3
Parent:
4:f2f560cb71fe
--- a/Joystick.h	Tue Feb 14 06:49:11 2017 +0000
+++ b/Joystick.h	Thu Mar 16 12:35:54 2017 +0000
@@ -50,7 +50,7 @@
 #ifndef JOYSTICK_ADC_MAX
 #define JOYSTICK_ADC_MAX 0xFFFF
 #endif
-#define JOYSTICK_CENTER_DEADZONE JOYSTICK_ADC_MAX / 50
+#define JOYSTICK_CENTER_DEADZONE JOYSTICK_ADC_MAX * _centerDeadzone
 
 struct JoystickValue
 {
@@ -73,7 +73,7 @@
 class Joystick
 {
 public:
-    Joystick(PinName pinX, PinName pinY, float processInterval = 0, void(*onChange)(JoystickValue value, JoystickValue prevValue) = NULL)
+    Joystick(PinName pinX, PinName pinY, float processInterval = 0, float centerDeadzone = 0.03f, void(*onChange)(JoystickValue value, JoystickValue prevValue) = NULL)
     : _xIn(pinX)
     , _yIn(pinY)
     , _center(JOYSTICK_ADC_MAX / 2)
@@ -81,6 +81,7 @@
     , _calibrated(false)
     , _calibrationCounter(0)
     , _range(JOYSTICK_ADC_MAX / 2)
+    , _centerDeadzone(centerDeadzone)
     , _swapXY(false)
     , _flipX(false)
     , _flipY(false)
@@ -90,12 +91,15 @@
     {
         setAutoProcessInterval(processInterval);
     };
+
     void process(void);
+
     void setAutoProcessInterval(float processInterval)
     {
         _ticker.detach();
         if (processInterval != 0) _ticker.attach(Callback<void()>(this, &Joystick::process), processInterval);
     };
+
     void calibrate()
     {
         _center.x = JOYSTICK_ADC_MAX / 2;
@@ -103,70 +107,98 @@
         _calibrationCounter = 0;
         _calibrated = false;
     };
+
+    inline float getCenterDeadzone() const
+    {
+        return _centerDeadzone;
+    };
+
+    inline void setCenterDeadzone(float centerDeadzone)
+    {
+        if (centerDeadzone < 0 || centerDeadzone > 1) return;
+        _centerDeadzone = centerDeadzone;
+    };
+
     inline bool isCalibrated() const
     {
         return _calibrated;
     };
+
     inline bool isLocked() const
     {
         return _locked;
     };
+
     inline void setLocked(bool locked)
     {
         _locked = locked;
     };
+
     inline bool isSwapXY() const
     {
         return _swapXY;
     };
+
     inline void setSwapXY(bool swapXY)
     {
         _swapXY = swapXY;
     };
+
     inline bool isFlipX() const
     {
         return _flipX;
     };
+
     inline void setFlipX(bool flipX)
     {
         _flipX = flipX;
     };
+
     inline bool isFlipY() const
     {
         return _flipY;
     };
+
     inline void setFlipY(bool flipY)
     {
         _flipY = flipY;
     };
+
     inline int getRange() const
     {
         return _range;
     };
+
     inline void setRange(int range)
     {
         _range = range;
     };
+
     inline int getDelta() const
     {
         return _delta;
     };
+
     inline void setDelta(int delta)
     {
         _delta = delta;
     };
+
     inline int getX() const
     {
         return _joyValue.x;
     };
+
     inline int getY() const
     {
         return _joyValue.y;
     };
+
     inline JoystickValue getCurrentValue() const
     {
         return _joyValue;
     };
+
 private:
     AnalogIn _xIn;
     AnalogIn _yIn;
@@ -175,6 +207,7 @@
     bool _calibrated;
     uint16_t _calibrationCounter;
     int _range;
+    float _centerDeadzone;
     bool _swapXY;
     bool _flipX;
     bool _flipY;