-
Notifications
You must be signed in to change notification settings - Fork 64
/
Atm_button.cpp
153 lines (138 loc) · 5.72 KB
/
Atm_button.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "Atm_button.hpp"
// Add option for button press callback (for reading i2c buttons etc)
Atm_button& Atm_button::begin( int attached_pin ) {
// clang-format off
const static state_t state_table[] PROGMEM = {
/* Standard Mode: press/repeat */
/* ON_ENTER ON_LOOP ON_EXIT EVT_LMODE EVT_TIMER EVT_DELAY EVT_REPEAT EVT_PRESS EVT_RELEASE EVT_COUNTER EVT_AUTO ELSE */
/* IDLE */ -1, -1, -1, LIDLE, -1, -1, -1, WAIT, -1, -1, AUTO_ST, -1,
/* WAIT */ -1, -1, -1, -1, PRESSED, -1, -1, -1, IDLE, -1, -1, -1,
/* PRESSED */ ENT_PRESS, -1, -1, -1, -1, REPEAT, -1, -1, RELEASE, -1, -1, -1,
/* REPEAT */ ENT_PRESS, -1, -1, -1, -1, -1, REPEAT, -1, RELEASE, -1, -1, -1,
/* RELEASE */ ENT_RELEASE, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, IDLE,
/* Long Press Mode: press/long press */
/* LIDLE */ -1, -1, -1, -1, -1, -1, -1, LWAIT, -1, -1, -1, -1,
/* LWAIT */ ENT_LSTART, -1, -1, -1, LPRESSED, -1, -1, -1, LIDLE, -1, -1, -1,
/* LPRESSED */ ENT_LCOUNT, -1, -1, -1, -1, LPRESSED, -1, -1, LRELEASE, WRELEASE, -1, -1,
/* LRELEASE */ ENT_LRELEASE, -1, EXT_WRELEASE, -1, -1, -1, -1, -1, -1, -1, -1, LIDLE,
/* WRELEASE */ ENT_LRELEASE, -1, EXT_WRELEASE, -1, -1, -1, -1, -1, LIDLE, -1, -1, -1,
/* AUTO_ST */ ENT_AUTO, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, IDLE,
};
// clang-format on
Machine::begin( state_table, ELSE );
pin = attached_pin;
counter_longpress.set( 0 );
timer_debounce.set( DEBOUNCE );
timer_delay.set( ATM_TIMER_OFF );
timer_repeat.set( ATM_TIMER_OFF );
timer_auto.set( ATM_TIMER_OFF );
pinMode( pin, INPUT_PULLUP );
return *this;
}
int Atm_button::event( int id ) {
switch ( id ) {
case EVT_LMODE:
return counter_longpress.value > 0;
case EVT_TIMER:
return timer_debounce.expired( this );
case EVT_DELAY:
return timer_delay.expired( this );
case EVT_REPEAT:
return timer_repeat.expired( this );
case EVT_AUTO:
return timer_auto.expired( this );
case EVT_PRESS:
return !digitalRead( pin );
case EVT_RELEASE:
return digitalRead( pin );
case EVT_COUNTER:
return counter_longpress.expired();
}
return 0;
}
void Atm_button::action( int id ) {
int press;
switch ( id ) {
case ENT_PRESS:
onpress.push( auto_press );
longpress[0].push( 1 );
return;
case ENT_AUTO:
onpress.push( 1 );
longpress[0].push( 1 );
return;
case ENT_RELEASE:
case EXT_WRELEASE:
onrelease.push( 0 );
return;
case ENT_LSTART:
counter_longpress.set( longpress_max );
return;
case ENT_LCOUNT:
counter_longpress.decrement();
press = ( longpress_max - counter_longpress.value );
if ( onpress.mode() == atm_connector::MODE_PUSHCB ) {
onpress.push( press * -1 );
}
return;
case ENT_LRELEASE:
press = ( longpress_max - counter_longpress.value );
onpress.push( press );
if ( press == 1 || press == 2 ) {
longpress[press-1].push( press );
}
return;
}
}
Atm_button& Atm_button::onPress( atm_cb_push_t callback, int idx /* = 0 */ ) {
onpress.set( callback, idx );
return *this;
}
Atm_button& Atm_button::onPress( Machine& machine, int event /* = 0 */ ) {
onpress.set( &machine, event );
return *this;
}
Atm_button& Atm_button::onPress( int id, atm_cb_push_t callback, int idx /* = 0 */ ) {
if ( id == 1 || id == 2 )
longpress[id-1].set( callback, idx );
return *this;
}
Atm_button& Atm_button::onPress( int id, Machine& machine, int event /* = 0 */ ) {
if ( id == 1 || id == 2 )
longpress[id-1].set( &machine, event );
return *this;
}
Atm_button& Atm_button::onRelease( atm_cb_push_t callback, int idx /* = 0 */ ) {
onrelease.set( callback, idx );
return *this;
}
Atm_button& Atm_button::onRelease( Machine& machine, int event /* = 0 */ ) {
onrelease.set( &machine, event );
return *this;
}
Atm_button& Atm_button::debounce( int delay ) {
timer_debounce.set( delay );
return *this;
}
Atm_button& Atm_button::longPress( int max, int delay ) {
longpress_max = max;
counter_longpress.set( longpress_max );
timer_delay.set( delay );
return *this;
}
Atm_button& Atm_button::repeat( int delay /* = 500 */, int speed /* = 50 */ ) {
timer_delay.set( delay );
timer_repeat.set( speed );
return *this;
}
Atm_button& Atm_button::autoPress( int delay, int press /* = 1 */ ) {
auto_press = press;
timer_auto.set( delay );
return *this;
}
Atm_button& Atm_button::trace( Stream& stream ) {
setTrace( &stream, atm_serial_debug::trace,
"BUTTON\0EVT_LMODE\0EVT_TIMER\0EVT_DELAY\0EVT_REPEAT\0EVT_PRESS\0EVT_RELEASE\0EVT_COUNTER\0EVT_"
"AUTO_ST\0ELSE\0IDLE\0WAIT\0PRESSED\0REPEAT\0RELEASE\0LIDLE\0LWAIT\0LPRESSED\0LRELEASE\0WRELEASE\0AUTO" );
return *this;
}