TDuino
Convenient functionality for Arduino
Public Member Functions
TButton Class Reference

Handles simple push buttons. More...

#include <TButton.h>

Public Member Functions

 TButton ()
 Default constructs for TButton. More...
 
virtual void attach (byte pin, byte mode=INPUT)
 Attaches the instance of TButton to a pin. More...
 
bool isPressed ()
 Detects if the button is pressed / down. More...
 
void setRepeat (unsigned int firstDelay, unsigned int followingDelay)
 Sets the repeat rate for the button. More...
 
void onPress (TPinInputCallback callback)
 Set the callback used for presses. More...
 
void onRelease (TPinInputCallback callback)
 Set the callback used for releases. More...
 
virtual void loop ()
 The buttons loop phase. More...
 
unsigned int getDebounce ()
 Get the debounce for the button. More...
 
void setDebounce (unsigned int debounce)
 Set the debounce for the button. More...
 

Additional Inherited Members

- Protected Member Functions inherited from TPinInput
virtual void falling ()
 Method called when the pins state is falling. More...
 
virtual void rising ()
 Method called when the pins state is rising. More...
 
 TPinInput ()
 The default contructor for the TPinInput class. More...
 
virtual ~TPinInput ()
 The destructor for the class. More...
 
int read (byte samples)
 Get the average value of multiple readings from the pin. More...
 
unsigned int getDebounce ()
 Get the amount of debounce used with events. More...
 
void setDebounce (unsigned int debounce)
 Sets the debounce value. More...
 
byte getDeviation ()
 Get the amount of deviation used with events. More...
 
void setDeviation (byte deviation, bool fixedSteps=false)
 Set the amount of deviation used to detect state change. More...
 
int getBufferedValue (byte sampleIndex=0)
 Get buffered value of the sample buffer. More...
 
byte getSamples ()
 Get the number of samples used with events. More...
 
void setSamples (byte samples, bool buffered=false)
 Set number of samples (reads) used with events. More...
 
void onFalling (TPinInputCallback callback)
 Assign a callback to handle falling pin state. More...
 
void onRising (TPinInputCallback callback)
 Assign a callback for rising pin state. More...
 
int read ()
 Read the value from the pin. More...
 
- Protected Member Functions inherited from TPin
bool isPinValid (const char *token=NULL)
 
bool isPwmValid (int &value, const char *token=NULL)
 
 TPin ()
 Default constructor for the TPin class. More...
 
void enable (byte on)
 Enable / disable a digital pin. More...
 
void flip ()
 Flip pin state. More...
 
void off ()
 Turn off the pin. More...
 
void on ()
 Turn on the pin. More...
 
void pwm (int value)
 Set the pin to a PWM value. More...
 
int read ()
 Read the value from the pin. More...
 
byte state ()
 Get the digital state of the pin. More...
 
bool isAnalog ()
 Checks if the pin is analog. More...
 
bool hasPwm ()
 Checkt if the pin has PWM capabilities. More...
 
byte getMode ()
 Get the pin's mode. More...
 
byte getPin ()
 Get the pin's number. More...
 
- Protected Member Functions inherited from TBase
virtual void defaults ()
 Method used to reset all class internal variables. More...
 
 TBase ()
 Constructor for class TBase. More...
 
virtual ~TBase ()
 Destructor for class TBase. More...
 
- Protected Attributes inherited from TPinInput
TPinInputCallback callbacks [2]
 Callbacks for onFalling and onRising.
 
byte deviation
 The amount of deviation required to trigger an event.
 
bool fixedDeviation
 If deviation is to be handled as fixed steps.
 
byte samples
 The number of samples to use for events.
 
int * sampleBuffer
 Buffer used for storing multiple samples.
 
int lastState
 The last known state of the pin. See loop().
 
unsigned int debounce
 The amount of debounce used with the pin.
 
unsigned long changeMillis
 The time of the last state change.
 
- Protected Attributes inherited from TPin
byte pin
 The pin assigned to this instance.
 
byte mode
 The mode used for the pin (INPUT / INPUT_PULLUP / OUTPUT). The two high order bits are used to store additional info about the pin.
 
- Protected Attributes inherited from TBase
unsigned long loopMillis
 The value of millis() to be used within loop()
 

Detailed Description

Handles simple push buttons.

This class is used to attach a callback event to a pin which has a push buttons attached to it. By default a TButton instance is initialized with a debounce of 20 milliseconds, you can change this with setDebounce().

See also
TButton::setDebounce()

Constructor & Destructor Documentation

◆ TButton()

TButton::TButton ( )

Default constructs for TButton.

Please see docs for the default constructor of TPin.

See also
TPin::TPin() attach()

Member Function Documentation

◆ attach()

void TButton::attach ( byte  pin,
byte mode  UNUSED_ATTR = INPUT 
)
virtual

Attaches the instance of TButton to a pin.

Parameters
pinSee TPin::attach()
modeSee TPin::attach()

The value of mode is ignored, always set to INPUT_PULLUP.

See also
TPin::attach()

Reimplemented from TPinInput.

◆ getDebounce()

unsigned int TPinInput::getDebounce

Get the debounce for the button.

See also
TPinInput::getDebounce()

◆ isPressed()

bool TButton::isPressed ( )

Detects if the button is pressed / down.

Returns
True is the button is pressed / down.

◆ loop()

void TButton::loop ( )
virtual

The buttons loop phase.

Extends the functionality of TPinInput with a repeat functionality.

See also
TPinInput::loop() setRepeat()

Reimplemented from TPinInput.

◆ onPress()

void TButton::onPress ( TPinInputCallback  callback)

Set the callback used for presses.

Parameters
callbackThe callback to be used for presses.

The callback method is invoked each time the button is pressed (the pin goes to LOW). If repeat is enabled with setRepeat() this is the callback which will be called for each repetition. The argument passed to the callback method is the pin number on which the button is attached and that pins state.

◆ onRelease()

void TButton::onRelease ( TPinInputCallback  callback)

Set the callback used for releases.

Parameters
callbackThe callback to be used for releases.

The callback method is invoked each time the button is released (the pin goes to HIGH).

See also
onPress()

◆ setDebounce()

void TPinInput::setDebounce

Set the debounce for the button.

See also
TPinInput::setDebounce()

◆ setRepeat()

void TButton::setRepeat ( unsigned int  firstDelay,
unsigned int  followingDelay 
)

Sets the repeat rate for the button.

Parameters
firstDelayDelay (in milliseconds) before the first repeat.
followingDelayDelay used for sub-sequent repeats.

If both arguments are set to zero (the default), no repeats will be generated.