forked from tinkerspy/Automaton
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtm_timer.cpp
More file actions
executable file
·82 lines (73 loc) · 1.78 KB
/
Atm_timer.cpp
File metadata and controls
executable file
·82 lines (73 loc) · 1.78 KB
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
#include <Automaton.h>
#include "Atm_timer.h"
Atm_timer & Atm_timer::begin( void )
{
const static state_t state_table[] PROGMEM = {
/* ON_ENTER ON_LOOP ON_EXIT EVT_TIMER EVT_COUNTER EVT_OFF EVT_ON ELSE */
/* IDLE */ -1, ATM_SLEEP, -1, -1, -1, -1, WAIT, -1,
/* WAIT */ -1, -1, -1, TRIGGER, -1, IDLE, -1, -1,
/* TRIGGER */ ACT_TRIG, -1, -1, -1, IDLE, IDLE, -1, WAIT,
};
Machine::begin( state_table, ELSE );
Machine::msgQueue( messages, MSG_END );
timer.begin( this, ATM_TIMER_OFF );
counter.set( 1 );
state( WAIT );
return *this;
}
Atm_timer & Atm_timer::onTimer( Machine * machine, uint8_t msg )
{
client_machine = machine;
client_msg = msg;
return *this;
}
Atm_timer & Atm_timer::onTimer( timer_cb_t timer_callback )
{
callback = timer_callback;
return *this;
}
Atm_timer & Atm_timer::interval( int v )
{
timer.set( v );
state( WAIT );
return *this;
}
Atm_timer & Atm_timer::repeat( int v )
{
counter.set( v );
state( WAIT );
return *this;
}
Atm_timer & Atm_timer::id( int v )
{
timer_id = v;
return *this;
}
int Atm_timer::event( int id )
{
switch ( id ) {
case EVT_COUNTER :
return counter.expired();
case EVT_TIMER :
return timer.expired();
case EVT_OFF :
return msgRead( MSG_OFF );
case EVT_ON :
return msgRead( MSG_ON );
}
return 0;
}
void Atm_timer::action( int id )
{
switch ( id ) {
case ACT_TRIG :
counter.decrement();
if ( callback ) {
(*callback)( timer_id );
}
if ( client_machine ) {
client_machine->msgWrite( client_msg );
}
return;
}
}