Mutila: Mouse's Utilities for Arduino
Oft-used utilities: debouncing buttons, averaging samples, and so on.
Public Member Functions | Static Public Attributes | Protected Attributes | List of all members
AbstractDebouncedButton Class Referenceabstract

#include <AbstractDebouncedButton.h>

Inheritance diagram for AbstractDebouncedButton:
Inheritance graph

Public Member Functions

 AbstractDebouncedButton ()
 
void begin (uint8_t threshold=DefaultThreshold, uint8_t delay=DefaultButtonDelay)
 
virtual void update ()=0
 
bool pushed (bool peek=false)
 
uint32_t tapped (bool peek=false)
 
bool held (uint16_t ms=DefaultHeldMs)
 
bool repeat (uint16_t initialMs=DefaultButtonRepeatInitialMs, uint16_t repeatMs=DefaultButtonRepeatMs)
 
void setState (bool newState)
 
- Public Member Functions inherited from AbstractButton
 AbstractButton ()
 
virtual void begin ()
 
virtual bool on ()=0
 

Static Public Attributes

static const uint8_t DefaultThreshold = 5
 
static const uint8_t DefaultButtonDelay = 5
 
static const uint16_t DefaultHeldMs = 800
 
static const uint16_t DefaultButtonRepeatInitialMs = 700
 
static const uint16_t DefaultButtonRepeatMs = 350
 

Protected Attributes

uint8_t _threshold
 
uint8_t _delay
 
uint32_t _lastUpdate
 
uint8_t _counter
 
bool _state
 
uint32_t _lastStateChange
 
bool _pushed
 
uint16_t _repeatCount
 
uint32_t _lastRepeat
 
uint32_t _lastOnDuration
 

Detailed Description

Timeslice-based button with debouncing.

AbstractDebouncedButton reduces / eliminates bouncing (multiple press events registing close together with only one physical press).

Definition at line 11 of file AbstractDebouncedButton.h.

Constructor & Destructor Documentation

◆ AbstractDebouncedButton()

AbstractDebouncedButton::AbstractDebouncedButton ( )

Constructor.

Definition at line 6 of file AbstractDebouncedButton.cpp.

Member Function Documentation

◆ begin()

void AbstractDebouncedButton::begin ( uint8_t  threshold = DefaultThreshold,
uint8_t  delay = DefaultButtonDelay 
)

Initialization.

Parameters
thresholdhow many tests of the pin must match for a state change.
delaynumber of ms between tests of pin state.

Should be called from setup(), or at any rate, before other members are called. This will set the pinMode. The minimum time it takes for button presses / released to register is threshold * delay.

Definition at line 10 of file AbstractDebouncedButton.cpp.

Here is the call graph for this function:

◆ held()

bool AbstractDebouncedButton::held ( uint16_t  ms = DefaultHeldMs)

Test if held on for extended period.

Parameters
mstime in ms the button has to have been on for to be considered held
Returns
true if the button is pushed and has been for longer than specified time.

Definition at line 34 of file AbstractDebouncedButton.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pushed()

bool AbstractDebouncedButton::pushed ( bool  peek = false)

Test if the button has been pushed since the last time it was off.

This function returns true once, as soon as the button is pushed - even if it has not been released yet. If the peek parameter is false then subsequent calls to pushed will not return true until the button has been released and pushed again. The idea is to allow frequent tests in a tight loop, and only get one positive result (unlike on(), which returns true any time the button is depressed).

Parameters
peekif true, do not reset the pushed state.
Returns
true if the button has been pushed and released since the last test.

Definition at line 20 of file AbstractDebouncedButton.cpp.

Here is the caller graph for this function:

◆ repeat()

bool AbstractDebouncedButton::repeat ( uint16_t  initialMs = DefaultButtonRepeatInitialMs,
uint16_t  repeatMs = DefaultButtonRepeatMs 
)

Get periodic press results when button is held.

Parameters
initialMstime between first press and first repeat in ms.
repeatMstime between subsequent releats.
Returns
true when pushed every so often...

Definition at line 39 of file AbstractDebouncedButton.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setState()

void AbstractDebouncedButton::setState ( bool  newState)

Explicitly set the state of the button.

Parameters
newStatethe new state to have.

Definition at line 58 of file AbstractDebouncedButton.cpp.

Here is the caller graph for this function:

◆ tapped()

uint32_t AbstractDebouncedButton::tapped ( bool  peek = false)

Test if the button has been pushed and released.

After returning a non-zero value (i.e. a tap was registered), the state will be reset. Only the last tap duration will be returned.

Parameters
peekif true, do not reset the pushed state.
Returns
length of last tap in ms if the button has been tapped, else 0.

Definition at line 27 of file AbstractDebouncedButton.cpp.

Here is the caller graph for this function:

◆ update()

virtual void AbstractDebouncedButton::update ( )
pure virtual

Allocate timeslice.

This method must be called frequently - usually from loop().

Implemented in DebouncedButton, DebouncedAnalogButton, and DebouncedDualButton.


The documentation for this class was generated from the following files: