-
Notifications
You must be signed in to change notification settings - Fork 9
/
tiny_fsm.h
61 lines (51 loc) · 1.47 KB
/
tiny_fsm.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
/*!
* @file
* @brief Simple implementation of a state machine that models functions as states.
*
* Signals are sent to the current state by invoking the state function with the
* signal and associated data.
*
* When the FSM transitions to a new state, the exit signal is sent to the current
* state, the current state is changed to the new state, then the entry signal is
* sent to the new state.
*/
#ifndef tiny_fsm_h
#define tiny_fsm_h
#include <stddef.h>
#include <stdint.h>
enum {
tiny_fsm_signal_entry,
tiny_fsm_signal_exit,
tiny_fsm_signal_user_start
};
typedef uint8_t tiny_fsm_signal_t;
struct tiny_fsm_t;
typedef void (*tiny_fsm_state_t)(
struct tiny_fsm_t* fsm,
tiny_fsm_signal_t signal,
const void* data);
typedef struct tiny_fsm_t {
tiny_fsm_state_t current;
} tiny_fsm_t;
/*!
* Initializes an FSM with the specified initial state. Sends the entry signal to the
* initial state.
*/
inline void tiny_fsm_init(tiny_fsm_t* self, tiny_fsm_state_t initial)
{
self->current = initial;
self->current(self, tiny_fsm_signal_entry, NULL);
}
/*!
* Sends a signal and optional signal data to the current state.
*/
inline void tiny_fsm_send_signal(tiny_fsm_t* self, tiny_fsm_signal_t signal, const void* data)
{
self->current(self, signal, data);
}
/*!
* Transitions the FSM to the target state. Sends exit to the current state, changes
* state, then sends entry to the target state.
*/
void tiny_fsm_transition(tiny_fsm_t* self, tiny_fsm_state_t target);
#endif