COSA
An Object-Oriented Platform for Arduino Programming
Button Class Referenceabstract

#include <Button.hh>

Inheritance diagram for Button:
Inheritance graph
Collaboration diagram for Button:
Collaboration graph

Public Types

enum  Mode { ON_FALLING_MODE = 0, ON_RISING_MODE = 1, ON_CHANGE_MODE = 2 }
 
enum  Direction { MSB_FIRST = 0, LSB_FIRST = 1 }
 

Public Member Functions

 Button (Job::Scheduler *scheduler, Board::DigitalPin pin, Mode mode=ON_CHANGE_MODE)
 
virtual void on_change (uint8_t type)=0
 
Mode mode () const
 
uint8_t pin () const
 
bool is_set () const
 
bool is_high () const
 
bool is_on () const
 
bool is_clear () const
 
bool is_low () const
 
bool is_off () const
 
bool read () const
 
uint8_t read (OutputPin &clk, Direction order=MSB_FIRST) const
 
 operator bool () const
 
Pinoperator>> (uint8_t &var)
 
void period (uint32_t time)
 
uint32_t period () const
 
void reschedule ()
 
void expire_at (uint32_t time)
 
uint32_t expire_at () const
 
void expire_after (uint32_t time)
 
int32_t expire_after () const
 
uint32_t time () const
 
bool is_started () const
 
bool start ()
 
bool stop ()
 
virtual void on_expired ()
 
void detach ()
 
Linkagesucc () const
 
Linkagepred () const
 
void attach (Linkage *pred)
 

Static Public Member Functions

static void mode (Board::DigitalPin pin, Mode mode)
 
static Mode mode (Board::DigitalPin pin)
 
static uint8_t MASK (uint8_t pin)
 
static volatile uint8_t * PIN (uint8_t pin)
 
static volatile uint8_t * DDR (uint8_t pin)
 
static volatile uint8_t * PORT (uint8_t pin)
 
static bool read (Board::DigitalPin pin)
 

Protected Member Functions

virtual void run ()
 
volatile uint8_t * PIN () const
 
volatile uint8_t * DDR () const
 
volatile uint8_t * PORT () const
 
volatile uint8_t * PCIMR () const
 
virtual void on_event (uint8_t type, uint16_t value)
 

Protected Attributes

const Mode MODE
 
uint8_t m_state
 
volatile uint8_t *const m_sfr
 
const uint8_t m_mask
 
const uint8_t m_pin
 
uint32_t m_period
 
uint32_t m_expires
 
Schedulerm_scheduler
 
Linkagem_succ
 
Linkagem_pred
 

Static Protected Attributes

static const uint16_t SAMPLE_MS = 64
 

Detailed Description

Debounded Button; Sampled input pin (with internal pullup resistor). Uses a periodic function with timout of 64 ms for sampling and on change calls an event action. Subclass Button and implement the virtual on_change() method. Use the subclass for any state needed for the action function. Connect button/switch from pin to ground. Internal pull-up resistor is activated.

Circuit

(Dn)-----------------+
|
(\)
|
(GND)----------------+

Limitations

Button toggle faster than sample period may be missed. This is the case when connecting to a Rotary Encoder.

Also

The Button event handler requires the usage of an event dispatch or implementing the virtual member function Job::on_expired() to call run() and reschedule(). See Event.hh.

Definition at line 56 of file Button.hh.

Member Enumeration Documentation

enum Pin::Direction
inherited

Serialization directions; most or least significant bit first.

Enumerator
MSB_FIRST 
LSB_FIRST 

Definition at line 84 of file Pin.hh.

Button change detection modes; falling (high to low), rising (low to high) and change (falling or rising).

Enumerator
ON_FALLING_MODE 
ON_RISING_MODE 
ON_CHANGE_MODE 

Definition at line 62 of file Button.hh.

Constructor & Destructor Documentation

Button::Button ( Job::Scheduler scheduler,
Board::DigitalPin  pin,
Mode  mode = ON_CHANGE_MODE 
)
inline

Construct a button connected to the given pin and with the given change detection mode. The scheduler should allow periodic jobs with a time unit of milli-seconds (e.g. Watchdog::Scheduler).

Parameters
[in]schedulerfor periodic job.
[in]pinnumber.
[in]modechange detection mode.

Definition at line 77 of file Button.hh.

Member Function Documentation

void Linkage::attach ( Linkage pred)
inlineinherited

Attach given linkage as predecessor. Will check and detach if already attached.

Parameters
[in]predlinkage to attach.
Note
atomic

Definition at line 71 of file Linkage.hh.

static volatile uint8_t* Pin::DDR ( uint8_t  pin)
inlinestaticinherited

Return pointer to Data Direction Register.

Parameters
[in]pinnumber.
Returns
DDR register pointer.

Definition at line 64 of file Pin.hh.

volatile uint8_t* Pin::DDR ( ) const
inlineprotectedinherited

Return pointer to Data Direction Register.

Returns
DDR register pointer.

Definition at line 245 of file Pin.hh.

void Link::detach ( )
inlineinherited

Detach this link. Unlink from any list.

Definition at line 125 of file Linkage.hh.

void Job::expire_after ( uint32_t  time)
inlineinherited

Set expire time relative to latest expire time.

Parameters
[in]timeto add to latest expire time.

Definition at line 111 of file Job.hh.

int32_t Job::expire_after ( ) const
inlineinherited

Get time remaining before expired.

Returns
time.

Definition at line 129 of file Job.hh.

void Job::expire_at ( uint32_t  time)
inlineinherited

Set expire time. Absolute time in scheduler time unit.

Parameters
[in]timeto expire.

Definition at line 102 of file Job.hh.

uint32_t Job::expire_at ( ) const
inlineinherited

Get expire time.

Returns
time.

Definition at line 120 of file Job.hh.

bool Pin::is_clear ( ) const
inlineinherited

Return true(1) if the pin is clear otherwise false(0).

Returns
boolean.

Definition at line 142 of file Pin.hh.

bool Pin::is_high ( ) const
inlineinherited

Return true(1) if the pin is set otherwise false(0).

Returns
boolean.

Definition at line 122 of file Pin.hh.

bool Pin::is_low ( ) const
inlineinherited

Return true(1) if the pin is clear otherwise false(0).

Returns
boolean.

Definition at line 152 of file Pin.hh.

bool Pin::is_off ( ) const
inlineinherited

Return true(1) if the pin is clear otherwise false(0).

Returns
boolean.

Definition at line 162 of file Pin.hh.

bool Pin::is_on ( ) const
inlineinherited

Return true(1) if the pin is set otherwise false(0).

Returns
boolean.

Definition at line 132 of file Pin.hh.

bool Pin::is_set ( ) const
inlineinherited

Return true(1) if the pin is set otherwise false(0).

Returns
boolean.

Definition at line 112 of file Pin.hh.

bool Job::is_started ( ) const
inlineinherited

Return true(1) if the job is queued otherwise false(0).

Returns
bool.

Definition at line 149 of file Job.hh.

static uint8_t Pin::MASK ( uint8_t  pin)
inlinestaticinherited

Return bit mask for given Arduino pin number.

Parameters
[in]pinnumber.
Returns
pin bit mask.

Definition at line 42 of file Pin.hh.

Mode InputPin::mode ( ) const
inlineinherited

Get current input pin mode.

Returns
mode.

Definition at line 52 of file InputPin.hh.

static void InputPin::mode ( Board::DigitalPin  pin,
Mode  mode 
)
inlinestaticinherited

Set input pin to given mode.

Parameters
[in]pinnumber.
[in]modepin mode.
Note
atomic

Definition at line 64 of file InputPin.hh.

static Mode InputPin::mode ( Board::DigitalPin  pin)
inlinestaticinherited

Get input pin mode.

Parameters
[in]pinnumber.
Returns
mode.

Definition at line 80 of file InputPin.hh.

virtual void Button::on_change ( uint8_t  type)
pure virtual

The button change handler. Called when a change corresponding to the mode has been detected. Event types are; Event::FALLING_TYPE, Event::RISING_TYPE, and Event::CHANGE_TYPE. Sub-class must override this method.

Parameters
[in]typeevent type.
virtual void Periodic::on_event ( uint8_t  type,
uint16_t  value 
)
inlineprotectedvirtualinherited

Periodic event handler; dispatch the run() function on timeout events and reschedule the periodic job.

Parameters
[in]typethe type of event.
[in]valuethe event value.

Reimplemented from Job.

Definition at line 91 of file Periodic.hh.

virtual void Job::on_expired ( )
inlinevirtualinherited

Job member function that is called Scheduler::dispatch() when the job time has expired. This function is normally called from an interrupt service routine. The default implementation will push a timeout event with the job as target. The default event handler will call the job run() virtual member function. Override this function if the job should be executed during the interrupt service routine.

Definition at line 185 of file Job.hh.

Pin::operator bool ( ) const
inlineinherited

Read input pin and assign variable.

Parameters
[out]varto assign.
Returns
pin.

Definition at line 204 of file Pin.hh.

Pin& Pin::operator>> ( uint8_t &  var)
inlineinherited

Read input pin and assign variable.

Parameters
[out]varto assign.
Returns
pin.

Definition at line 215 of file Pin.hh.

volatile uint8_t* Pin::PCIMR ( ) const
inlineprotectedinherited

Return pin change interrupt mask register.

Returns
pin change mask register pointer.

Definition at line 263 of file Pin.hh.

void Periodic::period ( uint32_t  time)
inlineinherited

Set timeout period.

Parameters
[in]timeperiod of timeout.

Definition at line 57 of file Periodic.hh.

uint32_t Periodic::period ( ) const
inlineinherited

Get timeout period.

Returns
period of timeout.

Definition at line 67 of file Periodic.hh.

static volatile uint8_t* Pin::PIN ( uint8_t  pin)
inlinestaticinherited

Return pointer to PIN register.

Parameters
[in]pinnumber.
Returns
PIN register pointer.

Definition at line 53 of file Pin.hh.

uint8_t Pin::pin ( ) const
inlineinherited

Return Arduino pin number of abstract pin.

Returns
pin number.

Definition at line 103 of file Pin.hh.

volatile uint8_t* Pin::PIN ( ) const
inlineprotectedinherited

Return pointer to PIN register.

Returns
PIN register pointer.

Definition at line 236 of file Pin.hh.

static volatile uint8_t* Pin::PORT ( uint8_t  pin)
inlinestaticinherited

Return pointer to data PORT register.

Parameters
[in]pinnumber.
Returns
PORT register pointer.

Definition at line 75 of file Pin.hh.

volatile uint8_t* Pin::PORT ( ) const
inlineprotectedinherited

Return pointer to data PORT register.

Returns
PORT register pointer.

Definition at line 254 of file Pin.hh.

Linkage* Linkage::pred ( ) const
inlineinherited

Return predecessor in sequence.

Returns
predecessor linkage.

Definition at line 60 of file Linkage.hh.

bool Pin::read ( ) const
inlineinherited

Return true(1) if the pin is set otherwise false(0).

Returns
boolean.

Definition at line 172 of file Pin.hh.

uint8_t Pin::read ( OutputPin clk,
Direction  order = MSB_FIRST 
) const
inherited

Shift in a byte from the input pin using the given clock output pin. Shift in according to given direction.

Parameters
[in]clkoutput pin.
[in]orderbit first.
Returns
value.

Definition at line 25 of file Pin.cpp.

static bool Pin::read ( Board::DigitalPin  pin)
inlinestaticinherited

Use pin number directly to read value. Does not require an instance. Return true(1) if the pin is set otherwise false(0).

Parameters
[in]pinnumber.
Returns
boolean.

Definition at line 193 of file Pin.hh.

void Periodic::reschedule ( )
inlineinherited

Reschedule after a new period.

Definition at line 76 of file Periodic.hh.

virtual void Button::run ( )
inlineprotectedvirtual

Button periodic function. Called by job scheduler on timeout. Samples the attached pin and calls the pin change handler, on_change().

Reimplemented from Job.

Definition at line 112 of file Button.hh.

bool Job::start ( )
inlineinherited

Start the job. Returns true(1) if scheduled otherwise false(0).

Returns
bool.

Definition at line 159 of file Job.hh.

bool Job::stop ( )
inlineinherited

Stop the job. Returns true(1) if scheduled otherwise false(0).

Returns
bool.

Definition at line 169 of file Job.hh.

Linkage* Linkage::succ ( ) const
inlineinherited

Return successor in sequence.

Returns
successor linkage.

Definition at line 51 of file Linkage.hh.

uint32_t Job::time ( ) const
inlineinherited

Get current scheduler time. May be used to set relative expire time.

Returns
time.

Definition at line 139 of file Job.hh.

Member Data Documentation

uint32_t Job::m_expires
protectedinherited

Expire time. Scale (us, ms, s) depends on scheduler.

Definition at line 214 of file Job.hh.

const uint8_t Pin::m_mask
protectedinherited

Pin mask in port.

Definition at line 227 of file Pin.hh.

uint32_t Periodic::m_period
protectedinherited

Time period. Time unit is defined by the scheduler.

Definition at line 100 of file Periodic.hh.

const uint8_t Pin::m_pin
protectedinherited

Pin identity.

Definition at line 230 of file Pin.hh.

Linkage* Linkage::m_pred
protectedinherited

Definition at line 94 of file Linkage.hh.

Scheduler* Job::m_scheduler
protectedinherited

Job scheduler.

Definition at line 217 of file Job.hh.

volatile uint8_t* const Pin::m_sfr
protectedinherited

Special function register pointer.

Definition at line 224 of file Pin.hh.

uint8_t Button::m_state
protected

Current state.

Definition at line 104 of file Button.hh.

Linkage* Linkage::m_succ
protectedinherited

Double linked list pointers.

Definition at line 93 of file Linkage.hh.

const Mode Button::MODE
protected

Change detection mode.

Definition at line 101 of file Button.hh.

const uint16_t Button::SAMPLE_MS = 64
staticprotected

Button sampling period in milli-seconds.

Definition at line 98 of file Button.hh.


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