HID Joystick - For use with X-Plane or other programs that can read HID JoySticks

Dependencies:   USBDevice mbed-rtos mbed

Fork of JoyStick by Ries Twisk

This is a simple Joystick HID that I use for xplane and a home build yoke + paddels, see this forum with the look and feel of it : http://forums.x-plane.org/index.php?showtopic=70041

The analog input are filtered with a LowPass IIR filter and the digital input's will be derived from the analog input and de-bounced.

The analog values are read at a 1Khz interval and to ensure we don't push the USB stack to much at a maximum rate of 20 updates/sec HID data is send over USB only if any values where changed. The JoyStick will send 16Bit analog values as opposite of 8 bit values that are normally used to increase accuracy of the whole system. This is well noticeable within x-plane!

The JoyStick uses the JoyStick copied from Wim Huiskamp and modified to suite my needs and the MBED RTOS libraries for reading analog inputs, sending debug data over USB and sending HID data, 3 threads in total.

SimpleButtonDecoder.cpp

Committer:
rvt
Date:
2016-06-22
Revision:
5:a0bb17c379ce
Parent:
2:ae7a31a3c618

File content as of revision 5:a0bb17c379ce:

#include "SimpleButtonDecoder.h"


SimpleButtonDecoder::SimpleButtonDecoder(AnalogInFiltered *analogIn, int value, short deBounceRuns)
{
    _analogIn = analogIn;
    _value = value;
    _isChanged = false;
    _deBounceRuns = deBounceRuns;
    _debounceCount = 1;

    _pulseUp=false;
    _status=false;
}

SimpleButtonDecoder::~SimpleButtonDecoder()
{
}

void SimpleButtonDecoder::process()
{
    _pulseUp=false;
    _pulseDown=false;

    // Detect if the analog in is within the value range + fuzzy factor
    if (abs((_analogIn->getData() + 32768) - _value) < _analogIn->getFuzzyFactor()) {
        // When the analog values iw within the fuzzy factor for XX debounce runs, consider this now a up flank to true state
        if (_debounceCount == _deBounceRuns) {
            _pulseUp=!_status;
            _status=true;
            _debounceCount++;
            return;
        }
        // If the _debounceCount is bigger then deboucneruns, consider this a on state (passed the up flanc)
        if (_debounceCount > _deBounceRuns) {
            return;
        }
        _debounceCount++;
    } else {
        // If debounce count is at 1, then consider this a down flanc back to false
        if (_debounceCount == 1) { 
            _debounceCount--;
            _pulseDown=_status;
            _status=false;
            return;
        }
        // if the debounce is below <1 then we have a stable state again.
        if (_debounceCount < 1) {
            return;
        }
        _debounceCount--;
    }
}

bool SimpleButtonDecoder::getStatus()
{
    return _status;
}

bool SimpleButtonDecoder::getIsChanged()
{
    return _pulseUp || _pulseDown;
}

bool SimpleButtonDecoder::getIsPressed()
{
    return _pulseUp;
}

bool SimpleButtonDecoder::getIsReleased()
{
    return _pulseUp;
}