Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: USBDevice mbed-rtos mbed
Fork of JoyStick by
SimpleButtonDecoder.cpp@5:a0bb17c379ce, 2016-06-22 (annotated)
- Committer:
- rvt
- Date:
- Wed Jun 22 12:50:16 2016 +0000
- Revision:
- 5:a0bb17c379ce
- Parent:
- 2:ae7a31a3c618
Latest
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| rvt | 2:ae7a31a3c618 | 1 | #include "SimpleButtonDecoder.h" |
| rvt | 2:ae7a31a3c618 | 2 | |
| rvt | 2:ae7a31a3c618 | 3 | |
| rvt | 2:ae7a31a3c618 | 4 | SimpleButtonDecoder::SimpleButtonDecoder(AnalogInFiltered *analogIn, int value, short deBounceRuns) |
| rvt | 2:ae7a31a3c618 | 5 | { |
| rvt | 2:ae7a31a3c618 | 6 | _analogIn = analogIn; |
| rvt | 2:ae7a31a3c618 | 7 | _value = value; |
| rvt | 2:ae7a31a3c618 | 8 | _isChanged = false; |
| rvt | 2:ae7a31a3c618 | 9 | _deBounceRuns = deBounceRuns; |
| rvt | 2:ae7a31a3c618 | 10 | _debounceCount = 1; |
| rvt | 2:ae7a31a3c618 | 11 | |
| rvt | 2:ae7a31a3c618 | 12 | _pulseUp=false; |
| rvt | 2:ae7a31a3c618 | 13 | _status=false; |
| rvt | 2:ae7a31a3c618 | 14 | } |
| rvt | 2:ae7a31a3c618 | 15 | |
| rvt | 2:ae7a31a3c618 | 16 | SimpleButtonDecoder::~SimpleButtonDecoder() |
| rvt | 2:ae7a31a3c618 | 17 | { |
| rvt | 2:ae7a31a3c618 | 18 | } |
| rvt | 2:ae7a31a3c618 | 19 | |
| rvt | 2:ae7a31a3c618 | 20 | void SimpleButtonDecoder::process() |
| rvt | 2:ae7a31a3c618 | 21 | { |
| rvt | 2:ae7a31a3c618 | 22 | _pulseUp=false; |
| rvt | 2:ae7a31a3c618 | 23 | _pulseDown=false; |
| rvt | 2:ae7a31a3c618 | 24 | |
| rvt | 2:ae7a31a3c618 | 25 | // Detect if the analog in is within the value range + fuzzy factor |
| rvt | 2:ae7a31a3c618 | 26 | if (abs((_analogIn->getData() + 32768) - _value) < _analogIn->getFuzzyFactor()) { |
| rvt | 2:ae7a31a3c618 | 27 | // When the analog values iw within the fuzzy factor for XX debounce runs, consider this now a up flank to true state |
| rvt | 2:ae7a31a3c618 | 28 | if (_debounceCount == _deBounceRuns) { |
| rvt | 2:ae7a31a3c618 | 29 | _pulseUp=!_status; |
| rvt | 2:ae7a31a3c618 | 30 | _status=true; |
| rvt | 2:ae7a31a3c618 | 31 | _debounceCount++; |
| rvt | 2:ae7a31a3c618 | 32 | return; |
| rvt | 2:ae7a31a3c618 | 33 | } |
| rvt | 2:ae7a31a3c618 | 34 | // If the _debounceCount is bigger then deboucneruns, consider this a on state (passed the up flanc) |
| rvt | 2:ae7a31a3c618 | 35 | if (_debounceCount > _deBounceRuns) { |
| rvt | 2:ae7a31a3c618 | 36 | return; |
| rvt | 2:ae7a31a3c618 | 37 | } |
| rvt | 2:ae7a31a3c618 | 38 | _debounceCount++; |
| rvt | 2:ae7a31a3c618 | 39 | } else { |
| rvt | 2:ae7a31a3c618 | 40 | // If debounce count is at 1, then consider this a down flanc back to false |
| rvt | 2:ae7a31a3c618 | 41 | if (_debounceCount == 1) { |
| rvt | 2:ae7a31a3c618 | 42 | _debounceCount--; |
| rvt | 2:ae7a31a3c618 | 43 | _pulseDown=_status; |
| rvt | 2:ae7a31a3c618 | 44 | _status=false; |
| rvt | 2:ae7a31a3c618 | 45 | return; |
| rvt | 2:ae7a31a3c618 | 46 | } |
| rvt | 2:ae7a31a3c618 | 47 | // if the debounce is below <1 then we have a stable state again. |
| rvt | 2:ae7a31a3c618 | 48 | if (_debounceCount < 1) { |
| rvt | 2:ae7a31a3c618 | 49 | return; |
| rvt | 2:ae7a31a3c618 | 50 | } |
| rvt | 2:ae7a31a3c618 | 51 | _debounceCount--; |
| rvt | 2:ae7a31a3c618 | 52 | } |
| rvt | 2:ae7a31a3c618 | 53 | } |
| rvt | 2:ae7a31a3c618 | 54 | |
| rvt | 2:ae7a31a3c618 | 55 | bool SimpleButtonDecoder::getStatus() |
| rvt | 2:ae7a31a3c618 | 56 | { |
| rvt | 2:ae7a31a3c618 | 57 | return _status; |
| rvt | 2:ae7a31a3c618 | 58 | } |
| rvt | 2:ae7a31a3c618 | 59 | |
| rvt | 2:ae7a31a3c618 | 60 | bool SimpleButtonDecoder::getIsChanged() |
| rvt | 2:ae7a31a3c618 | 61 | { |
| rvt | 2:ae7a31a3c618 | 62 | return _pulseUp || _pulseDown; |
| rvt | 2:ae7a31a3c618 | 63 | } |
| rvt | 2:ae7a31a3c618 | 64 | |
| rvt | 2:ae7a31a3c618 | 65 | bool SimpleButtonDecoder::getIsPressed() |
| rvt | 2:ae7a31a3c618 | 66 | { |
| rvt | 2:ae7a31a3c618 | 67 | return _pulseUp; |
| rvt | 2:ae7a31a3c618 | 68 | } |
| rvt | 2:ae7a31a3c618 | 69 | |
| rvt | 2:ae7a31a3c618 | 70 | bool SimpleButtonDecoder::getIsReleased() |
| rvt | 2:ae7a31a3c618 | 71 | { |
| rvt | 2:ae7a31a3c618 | 72 | return _pulseUp; |
| rvt | 2:ae7a31a3c618 | 73 | } |
