M
Mark
Hello,
I was reading
http://www.netrino.com/Embedded-Systems/How-To/State-Machines-Event-Driven-Systems
and later on in the article the author provides simple implementation of
finite state machine; the implementation is indeed very simple and clever,
yet I can't precisely understand some parts. Here is the implementation:
typedef short Signal;
typedef struct Event Event;
typedef struct Fsm Fsm;
typedef void (*State)(Fsm *, Event const *);
struct Event
{
Signal sig;
};
struct Fsm
{
State state__;
};
#define FsmCtor_(me_, init_) ((me_)->state__ = (State)(init_))
#define FsmInit(me_, e_) (*(me_)->state__)((me_), (e_))
#define FsmDispatch(me_, e_) (*(me_)->state__)((me_), (e_))
#define FsmTran_(me_, targ_) ((me_)->state__ = (State)(targ_))
My questions will be:
1. Not quite obvious the reason to define 'Signal' as an alias to short.
What's the profit ?
2. Is it necessary to define Event as a structure?
3. IMO would be better for readibility to have inline functions instead of
macros?
4. To me the more obvious solution would be something like:
struct fsm {
int event;
int state;
int (* fsm_callback)(int eve, int st);
};
/* state table */
struct fsm fsm_table[] = {
{ STATE_INIT, EV_1, &f1 },
{ STATE_UP, EV_2, &f2 },
{ 0, 0, NULL }
};
PS. Read the article again, but failed to grasp the benefits, perhaps
someone may point out. Thanks.
I was reading
http://www.netrino.com/Embedded-Systems/How-To/State-Machines-Event-Driven-Systems
and later on in the article the author provides simple implementation of
finite state machine; the implementation is indeed very simple and clever,
yet I can't precisely understand some parts. Here is the implementation:
typedef short Signal;
typedef struct Event Event;
typedef struct Fsm Fsm;
typedef void (*State)(Fsm *, Event const *);
struct Event
{
Signal sig;
};
struct Fsm
{
State state__;
};
#define FsmCtor_(me_, init_) ((me_)->state__ = (State)(init_))
#define FsmInit(me_, e_) (*(me_)->state__)((me_), (e_))
#define FsmDispatch(me_, e_) (*(me_)->state__)((me_), (e_))
#define FsmTran_(me_, targ_) ((me_)->state__ = (State)(targ_))
My questions will be:
1. Not quite obvious the reason to define 'Signal' as an alias to short.
What's the profit ?
2. Is it necessary to define Event as a structure?
3. IMO would be better for readibility to have inline functions instead of
macros?
4. To me the more obvious solution would be something like:
struct fsm {
int event;
int state;
int (* fsm_callback)(int eve, int st);
};
/* state table */
struct fsm fsm_table[] = {
{ STATE_INIT, EV_1, &f1 },
{ STATE_UP, EV_2, &f2 },
{ 0, 0, NULL }
};
PS. Read the article again, but failed to grasp the benefits, perhaps
someone may point out. Thanks.