Ries Twisk / Mbed 2 deprecated JoyStick

Dependencies:   USBDevice mbed-rtos mbed

Fork of JoyStick by Ries Twisk

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SimpleButtonDecoder.cpp Source File

SimpleButtonDecoder.cpp

00001 #include "SimpleButtonDecoder.h"
00002 
00003 
00004 SimpleButtonDecoder::SimpleButtonDecoder(AnalogInFiltered *analogIn, int value, short deBounceRuns)
00005 {
00006     _analogIn = analogIn;
00007     _value = value;
00008     _isChanged = false;
00009     _deBounceRuns = deBounceRuns;
00010     _debounceCount = 1;
00011 
00012     _pulseUp=false;
00013     _status=false;
00014 }
00015 
00016 SimpleButtonDecoder::~SimpleButtonDecoder()
00017 {
00018 }
00019 
00020 void SimpleButtonDecoder::process()
00021 {
00022     _pulseUp=false;
00023     _pulseDown=false;
00024 
00025     // Detect if the analog in is within the value range + fuzzy factor
00026     if (abs((_analogIn->getData() + 32768) - _value) < _analogIn->getFuzzyFactor()) {
00027         // When the analog values iw within the fuzzy factor for XX debounce runs, consider this now a up flank to true state
00028         if (_debounceCount == _deBounceRuns) {
00029             _pulseUp=!_status;
00030             _status=true;
00031             _debounceCount++;
00032             return;
00033         }
00034         // If the _debounceCount is bigger then deboucneruns, consider this a on state (passed the up flanc)
00035         if (_debounceCount > _deBounceRuns) {
00036             return;
00037         }
00038         _debounceCount++;
00039     } else {
00040         // If debounce count is at 1, then consider this a down flanc back to false
00041         if (_debounceCount == 1) { 
00042             _debounceCount--;
00043             _pulseDown=_status;
00044             _status=false;
00045             return;
00046         }
00047         // if the debounce is below <1 then we have a stable state again.
00048         if (_debounceCount < 1) {
00049             return;
00050         }
00051         _debounceCount--;
00052     }
00053 }
00054 
00055 bool SimpleButtonDecoder::getStatus()
00056 {
00057     return _status;
00058 }
00059 
00060 bool SimpleButtonDecoder::getIsChanged()
00061 {
00062     return _pulseUp || _pulseDown;
00063 }
00064 
00065 bool SimpleButtonDecoder::getIsPressed()
00066 {
00067     return _pulseUp;
00068 }
00069 
00070 bool SimpleButtonDecoder::getIsReleased()
00071 {
00072     return _pulseUp;
00073 }