forked from tinkerspy/Automaton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAtm_button.cpp
executable file
·173 lines (149 loc) · 5.51 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <Automaton.h>
#include "Atm_button.h"
// Add option for button press callback (for reading i2c buttons etc)
Atm_button & Atm_button::begin( int attached_pin )
{
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, -1,
/* WAIT */ -1, -1, -1, -1, PRESSED, -1, -1, -1, IDLE, -1, -1, -1,
/* PRESSED */ ACT_PRESS, -1, -1, -1, -1, REPEAT, -1, -1, RELEASE, -1, -1, -1,
/* REPEAT */ ACT_PRESS, -1, -1, -1, -1, -1, REPEAT, -1, RELEASE, -1, -1, -1,
/* RELEASE */ ACT_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 */ ACT_LSTART, -1, -1, -1, LPRESSED, -1, -1, -1, LIDLE, -1, -1, -1,
/* LPRESSED */ ACT_LCOUNT, -1, -1, -1, -1, LPRESSED, -1, -1, LRELEASE, WRELEASE, -1, -1,
/* LRELEASE */ ACT_LRELEASE, -1, ACT_WRELEASE, -1, -1, -1, -1, -1, -1, -1, -1, LIDLE,
/* WRELEASE */ ACT_LRELEASE, -1, ACT_WRELEASE, -1, -1, -1, -1, -1, LIDLE, -1, -1, -1,
/* AUTO */ ACT_AUTO, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, IDLE,
};
Machine::begin( state_table, ELSE );
pin = attached_pin;
counter_longpress.set( 0 );
timer_debounce.begin( this, DEBOUNCE );
timer_delay.begin( this, ATM_TIMER_OFF );
timer_repeat.begin( this, ATM_TIMER_OFF );
timer_auto.begin( this, ATM_TIMER_OFF );
pinMode( pin, INPUT_PULLUP );
return *this;
}
Atm_button & Atm_button::begin( int attached_pin, presscb_t press_callback )
{
begin( attached_pin );
callback = press_callback;
callback_idx = -1;
return *this;
}
Atm_button & Atm_button::onPress( Machine * machine, int msg )
{
client_machine = machine;
client_press = msg;
return *this;
}
Atm_button & Atm_button::onPress( Machine * machine, int msg_press, int msg_release )
{
client_machine = machine;
client_press = msg_press;
client_release = msg_release;
return *this;
}
Atm_button & Atm_button::onPress( presscb_t press_callback )
{
callback = press_callback;
callback_idx = -1;
return *this;
}
Atm_button & Atm_button::onPress( presscb_id_t press_callback, int idx )
{
callback_id = press_callback;
callback_idx = idx;
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, int speed ) {
timer_delay.set( delay );
timer_repeat.set( speed );
return *this;
}
Atm_button & Atm_button::repeat( void ) {
timer_delay.set( 500 );
timer_repeat.set( 50 );
return *this;
}
Atm_button & Atm_button::autoPress( int delay, int press ) {
_auto_press = press;
timer_auto.set( delay );
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();
case EVT_DELAY :
return timer_delay.expired();
case EVT_REPEAT :
return timer_repeat.expired();
case EVT_AUTO :
return timer_auto.expired();
case EVT_PRESS :
return !digitalRead( pin );
case EVT_RELEASE :
return digitalRead( pin );
case EVT_COUNTER :
return counter_longpress.expired();
}
return 0;
}
void Atm_button::cb( int press, int idx ) {
if ( callback )
(*callback)( press );
if ( callback_id )
(*callback_id)( press, idx );
}
void Atm_button::action( int id )
{
switch ( id ) {
case ACT_PRESS :
cb( 1, callback_idx );
if ( client_machine && client_press != -1 ) {
client_machine->msgWrite( client_press );
}
return;
case ACT_AUTO :
cb( _auto_press, callback_idx );
return;
case ACT_RELEASE :
cb( 0, callback_idx );
if ( client_machine && client_release != -1 ) {
client_machine->msgWrite( client_release );
}
return;
case ACT_LSTART :
counter_longpress.set( longpress_max );
return;
case ACT_LCOUNT :
counter_longpress.decrement();
cb( ( longpress_max - counter_longpress.value ) * -1, callback_idx );
return;
case ACT_LRELEASE :
cb( longpress_max - counter_longpress.value, callback_idx );
return;
case ACT_WRELEASE :
cb( 0, callback_idx );
return;
}
}