view Joystick.h @ 1:fd012dcc048d

added isCentered, dvx, dvy
author ark
date 2017-01-29 13:59 +0100
parents f31cf3175189
children 395297edda26
line wrap: on
line source

#ifndef Joystick_h
#define Joystick_h

#include <Potentiometer.h>
#include <Button.h>


/**
    utility class for interpreting joystick values.
    combines two potentiometer and calibration information to provide a vector
    of user selected direction with 0,0 in the center and +x,+y in top-right.

    (c) 2017 arkadiusz@wobblyhead.net
*/
class Joystick {
    public: 
        // x-axis pot
        Potentiometer x;
        // y-axis pot
        Potentiometer y;
        // optional button
        Button b;

        // if calibrated, current vector, x component
        float vx;
        // if calibrated, current vector, y component
        float vy;
        // flag, if true joystick is considered centered/idle
        bool isCentered;
        // "digital" vector, x
        int dvx;
        // "digital" vector, y
        int dvy;

        // flag, if true joystick is calibrated.
        bool isCalibrated;

        // calibrated center of x axis
        int centerX;
        // calibrated center of y axis
        int centerY;

        /** 
         * center deadzone radius, if the joystick is not calibrated.
         *
         * positions within this distance from the center are considered in the
         * center.
         */
        const int CENTER_DEADZONE_UNCALIBRATED = 50;
        
        /** 
         * center deadzone radius, if the joystick is calibrated.
         * positions within this distance from the center are considered in the
         * center.
         */
        const int CENTER_DEADZONE_CALIBRATED = 5;

        /** 
         * center radius, for digital interpretation.
         */
        const int CENTER_RADIUS_DIGITAL = 100;

        /**
            create an uninitialised Joystick.
            setup() must be called after this to make it usable.
        */
        Joystick();

        /**
            setup joystick instance with flipped y axis
            
            @param {byte} pinX      input pin for x axis
            @param {byte} pinY      input pin for y axis
            @param {byte} pinB      input pin for button
        */
        void setup(byte pinX, byte pinY, byte pinB);

        /**
            setup joystick instance
            
            @param {byte} pinX      input pin for x axis
            @param {byte} pinY      input pin for y axis
            @param {byte} pinB      input pin for button
            @param {bool} flipY     flip y axis
        */
        void setup(byte pinX, byte pinY, byte pinB, bool flipY);

        /**
            check inputs.
            implicitely calls millis();
        */
        void update();

        /**
            check inputs, update values.
            if calibrated, calculate vector.
            @param {unsigned long} ms   current millis() value
        */
        void update(unsigned long ms);

    private: 
        bool flipY;

};

#endif