-
Notifications
You must be signed in to change notification settings - Fork 0
/
statemachine.c
45 lines (38 loc) · 1.32 KB
/
statemachine.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
/***************************************************************************//**
* @file statemachine.c
*
* Simple finite state machine
*
*******************************************************************************/
#include <stdint.h>
#include <string.h>
#include "statemachine.h"
void StateMachine(STATE_MACHINE* const me, uint16_t ev) {
me->State(me, ev); // call actual state
if (me->StateNew != NULL) { // new state request
me->StateReturn = me->State; // remeber last state
me->State(me, EV_STATE_EXIT); // closing last state
me->State = me->StateNew; // switching state
me->StateNew = NULL;
me->State(me, EV_STATE_ENTER); // init new state
}
}
void SM_ST_StateDelayed(STATE_MACHINE* const me, uint16_t event) {
switch (event) {
case EV_TIMER_TICK: {
if (me->Timer_StateDelay) {
me->Timer_StateDelay--;
}else {
SM_SET_STATE(me->StateDelayed);
}
}break;
default: break;
}
}
void StateMachineInit(STATE_MACHINE* const me, SM_STATE_FUNC* const init_state) {
me->State = init_state;
me->StateNew = NULL;
me->StateDelayed = init_state;
me->StateReturn = init_state;
me->State(me, EV_STATE_ENTER); // enter into initial state (initial transition)
}