-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsm.c
46 lines (40 loc) · 817 Bytes
/
fsm.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
/*
* fsm.c
*
* Created on: 1 de mar. de 2016
* Author: Administrador
*/
#include <stdlib.h>
#include "fsm.h"
fsm_t*
fsm_new (int state, fsm_trans_t* tt, void* user_data)
{
fsm_t* this = (fsm_t*) malloc (sizeof (fsm_t));
fsm_init (this, state, tt, user_data);
return this;
}
void
fsm_init (fsm_t* this, int state, fsm_trans_t* tt, void* user_data)
{
this->current_state = state;
this->tt = tt;
this->user_data = user_data;
}
void
fsm_destroy (fsm_t* this)
{
free(this);
}
void
fsm_fire (fsm_t* this)
{
fsm_trans_t* t;
for (t = this->tt; t->orig_state >= 0; ++t) {
if ((this->current_state == t->orig_state) && t->in(this)) {
this->current_state = t->dest_state;
if (t->out)
t->out(this);
break;
}
}
}