-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathstate_machine-1.c
71 lines (60 loc) · 1.21 KB
/
state_machine-1.c
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
#include <stm32f4_discovery.h>
#include <os.h>
enum
{
EventALL = hsmALL,
EventStop = hsmStop,
EventExit = hsmExit,
EventEntry = hsmEntry,
EventInit = hsmInit,
EventSwitch = hsmUser,
EventTick,
};
OS_TSK(dispatcher, NULL);
OS_HSM(blinker, 1);
OS_HSM_STATE(StateOff, NULL);
OS_HSM_STATE(StateOn, NULL);
void StateOffHandler(hsm_t *hsm, unsigned event)
{
(void) hsm;
switch (event)
{
case EventInit:
LEDs = 0;
break;
default:
assert(false); // system shouldn't get here
}
}
void StateOnHandler(hsm_t *hsm, unsigned event)
{
(void) hsm;
switch (event)
{
case EventTick:
LED_Tick();
break;
default:
assert(false); // system shouldn't get here
}
}
hsm_action_t tab[] =
{
HSM_ACTION_INIT(StateOff, EventInit, NULL, StateOffHandler),
HSM_ACTION_INIT(StateOff, EventSwitch, StateOn, NULL),
HSM_ACTION_INIT(StateOn, EventSwitch, StateOff, NULL),
HSM_ACTION_INIT(StateOn, EventTick, NULL, StateOnHandler),
};
#define tabsize (int)(sizeof(tab)/sizeof(tab[0]))
int main()
{
LED_Init();
for (int i = 0; i < tabsize; i++) hsm_link(&tab[i]);
hsm_start(blinker, dispatcher, StateOff);
hsm_send(blinker, EventSwitch);
for (;;)
{
tsk_delay(SEC);
hsm_send(blinker, EventTick);
}
}