-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathAtm_digital.cpp
84 lines (74 loc) · 2.87 KB
/
Atm_digital.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "Atm_digital.hpp"
Atm_digital& Atm_digital::begin( int pin, int debounce /* = 20 */, bool activeLow /* = false */, bool pullUp /* = false */ ) {
// clang-format off
const static state_t state_table[] PROGMEM = {
/* ON_ENTER ON_LOOP ON_EXIT EVT_TIMER EVT_HIGH EVT_LOW ELSE */
/* IDLE */ -1, -1, -1, -1, WAITH, -1, -1,
/* WAITH */ -1, -1, -1, VHIGH, -1, IDLE, -1,
/* VHIGH */ ENT_HIGH, -1, -1, -1, -1, WAITL, -1,
/* WAITL */ -1, -1, -1, VLOW, VHIGH, -1, -1,
/* VLOW */ ENT_LOW, -1, -1, -1, -1, -1, IDLE,
};
// clang-format on
Machine::begin( state_table, ELSE );
this->pin = pin;
this->activeLow = activeLow;
timer.set( debounce );
indicator = -1;
pinMode( pin, pullUp ? INPUT_PULLUP : INPUT );
return *this;
}
int Atm_digital::event( int id ) {
switch ( id ) {
case EVT_TIMER:
return timer.expired( this );
case EVT_HIGH:
return ( !digitalRead( pin ) != !activeLow ); // XOR
case EVT_LOW:
return !( !digitalRead( pin ) != !activeLow );
}
return 0;
}
void Atm_digital::action( int id ) {
switch ( id ) {
case ENT_HIGH:
connection[ON_CHANGE_TRUE].push( state() );
if ( indicator > -1 ) digitalWrite( indicator, !HIGH != !indicatorActiveLow );
return;
case ENT_LOW:
connection[ON_CHANGE_FALSE].push( state() );
if ( indicator > -1 ) digitalWrite( indicator, !LOW != !indicatorActiveLow );
return;
}
}
int Atm_digital::state( void ) {
return ( current == VHIGH || current == WAITL );
}
Atm_digital& Atm_digital::led( int led, bool activeLow /* = false */ ) {
indicator = led;
indicatorActiveLow = activeLow;
pinMode( indicator, OUTPUT );
return *this;
}
Atm_digital& Atm_digital::onChange( bool status, atm_cb_push_t callback, int idx /* = 0 */ ) {
connection[status ? ON_CHANGE_TRUE : ON_CHANGE_FALSE].set( callback, idx );
return *this;
}
Atm_digital& Atm_digital::onChange( bool status, Machine& machine, int event /* = 0 */ ) {
connection[status ? ON_CHANGE_TRUE : ON_CHANGE_FALSE].set( &machine, event );
return *this;
}
Atm_digital& Atm_digital::onChange( atm_cb_push_t callback, int idx /* = 0 */ ) {
connection[ON_CHANGE_FALSE].set( callback, idx );
connection[ON_CHANGE_TRUE].set( callback, idx );
return *this;
}
Atm_digital& Atm_digital::onChange( Machine& machine, int event /* = 0 */ ) {
connection[ON_CHANGE_FALSE].set( &machine, event );
connection[ON_CHANGE_TRUE].set( &machine, event );
return *this;
}
Atm_digital& Atm_digital::trace( Stream& stream ) {
setTrace( &stream, atm_serial_debug::trace, "DIGITAL\0EVT_TIMER\0EVT_HIGH\0EVT_LOW\0ELSE\0IDLE\0WAITH\0VHIGH\0WAITL\0VLOW" );
return *this;
}