-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimebomb_sm.h
65 lines (53 loc) · 2.03 KB
/
timebomb_sm.h
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
#ifndef _TIMEBOMB_SM_H
#define _TIMEBOMB_SM_H
#include <assert.h>
#include <statemap.h>
struct TimeBomb;
struct timebombContext;
struct TimeBombState {
void (*Entry)(struct timebombContext *const fsm);
void (*Exit)(struct timebombContext *const fsm);
void (*BUTTON)(struct timebombContext *const fsm, Event const *const e);
void (*TIMEOUT)(struct timebombContext *const fsm, Event const *const e);
void (*Default)(struct timebombContext *const fsm);
STATE_MEMBERS
};
extern const struct TimeBombState TimeBombMap_wait4button;
extern const struct TimeBombState TimeBombMap_blink;
extern const struct TimeBombState TimeBombMap_pause;
extern const struct TimeBombState TimeBombMap_boom;
struct timebombContext {
struct TimeBomb *_owner;
FSM_MEMBERS(TimeBomb)
};
#ifdef NO_TIMEBOMB_SM_MACRO
extern void timebombContext_Init(struct timebombContext *const fsm, struct TimeBomb *const owner);
extern void timebombContext_EnterStartState(struct timebombContext *const fsm);
extern void timebombContext_BUTTON(struct timebombContext *const fsm, Event const *const e);
extern void timebombContext_TIMEOUT(struct timebombContext *const fsm, Event const *const e);
#else
#define timebombContext_Init(fsm, owner) \
FSM_INIT((fsm), &TimeBombMap_wait4button); \
(fsm)->_owner = (owner)
#define ENTRY_STATE(state) \
if ((state)->Entry != NULL) {
(state)->Entry(fsm); \
}
#define EXIT_STATE(state) \
if ((state)->Exit != NULL) {
(state)->Exit(fsm); \
}
#define timebombContext_EnterStartState(fsm) \
ENTRY_STATE(getState(fsm))
#define timebombContext_BUTTON(fsm, e) \
assert(getState(fsm) != NULL); \
setTransition((fsm), "BUTTON"); \
getState(fsm)->BUTTON((fsm), (e)); \
setTransition((fsm), NULL)
#define timebombContext_TIMEOUT(fsm, e) \
assert(getState(fsm) != NULL); \
setTransition((fsm), "TIMEOUT"); \
getState(fsm)->TIMEOUT((fsm), (e)); \
setTransition((fsm), NULL)
#endif
#endif