CMP EMBEDDED.COM

Login | Register     Welcome Guest ESC Boston  esc india  Call for Abstracts
 

Listing 3a Simple watch HSM: C implementation

#include "hsm.h"
typedef struct Watch Watch;
struct Watch {
Hsm super; /* superclass */
int tsec, tmin, thour, dday, dmonth;
State timekeeping, time, date;
State setting, hour, minute, day, month;
State *tkeepingHist;
};
enum WatchEvents {
MODE_EVT,
SET_EVT,
TICK_EVT
};
/*
timekeeping state handler */
Msg const *WatchTimekeepingHndlr(Msg const *msg) {
switch (MsgGetEvt(msg)) {
case START_EVT:
STATE_ENTER(me->tkeepingHist ? me->tkeepingHist :
&me->time);
return 0;
case SET_EVT:
STATE_TRAN(&me->setting);
printf("Watch::timekeeping-SET;");
return 0;
case TICK_EVT:
WatchTimeTick(me);
return 0
case EXIT_EVT:
me->tkeepingHist = STATE_CURR(me);
return 0;
} 
return msg;
}
/*... other state
handlerds ... */
/* Watch constructor */
void WatchCtor(Watch *me) {
HsmCtor((Hsm *)me, "Watch", (EvtHndlr)Watch_top);
StateCtor(&me->timekeeping, "timekeeping", 
&((Hsm *)me)->top, (EvtHndlr)Watch_timekeeping);
StateCtor(&me->time, "time", &me->timekeeping,
(EvtHndlr)Watch_time);
StateCtor(&me->date, "date", &me->timekeeping,
(EvtHndlr)Watch_date);
StateCtor(&me->setting, "setting", &((Hsm *)me)->top,
(EvtHndlr)Watch_setting);
StateCtor(&me->hour, "hour", &me->setting,
(EvtHndlr)Watch_hour);
Listing 3a, cont'd. Simple watch HSM: C implementation
StateCtor(&me->minute, "minute", &me->setting,
(EvtHndlr)Watch_minute);
StateCtor(&me->day, "day", &me->setting,
(EvtHndlr)Watch_day);
StateCtor(&me->month, "month", &me->setting,
(EvtHndlr)Watch_month);
me->tsec = me->tmin = me->thour = 0;
me->dday = me->dmonth = 1;
me->timekeepingHist =
NULL;
}
void main() { /* test harness */
Watch watch;
WatchCtor(&watch);
HsmOnStart((Hsm *)&watch);
for (;;) {
Msg *msg = getEvt(); /* block until event arrives */
HsmOnEvent((Hsm *)&watch, msg);
}
}

Listing 3b Simple watch HSM: C++ implementation

#include "hsm.h"
class Watch : public Hsm {
int tsec, tmin, thour, dday, dmonth;
timeTick(); /* process tick event in the 'time' state */
protected:
State timekeeping, time, date;
State setting, hour, minute, day, month;
State *tkeepingHist; /* to record history */
Msg const *topHndlr(Msg const *msg); 
Msg const *timekeepingHndlr(Msg const *msg); 
Msg const *settingHndlr(Msg const *msg); 
Msg const *hourHndlr(Msg const
*msg); 
/*... other state handler methods */
public:
Watch(); /* ctor for defining state hierarchy */
};
enum WatchEvents {
MODE_EVT,
SET_EVT,
TICK_EVT
};
/* timekeeping state handler */
Msg const *Watch::timekeepingHndlr(Msg const *msg) {
switch (msg->getEvt()) {
case START_EVT:
STATE_ENTER(tkeepingHist ? tkeepingHist : &time);
return 0;
case SET_EVT:
STATE_TRAN(&setting);
printf("Watch::timekeeping-SET;");
return
0;
case TICK_EVT:
timeTick();
return 0
case EXIT_EVT:
tkeepingHist = STATE_CURR();
return 0;
} 
return msg;
}
/*... other state handlerds ... */
/* Watch ctor */
Watch::Watch() 
: Hsm("Watch", (EvtHndlr)topHndlr),
timekeeping("timekeeping", &top,
(EvtHndlr)timekeepingHndlr),
time("time", &timekeeping, (EvtHndlr)timeHndlr),
date("date", &timekeeping, (EvtHndlr)dateHndlr),
setting("setting", &top,
(EvtHndlr)settingHndlr),
hour("hour", &setting, (EvtHndlr)hourHndlr),
minute("minute", &setting, (EvtHndlr)minuteHndlr),
day("day", &setting, (EvtHndlr)dayHndlr),
month("month", &setting, (EvtHndlr)monthHndlr)
{
tsec = tmin = thour = 0;
dday = dmonth = 1;
tkeepingHist = NULL;
}
/* test harness */
void main() {
Watch watch;
watch.onStart();
for (;;) {
Msg *msg = getEvt(); /* block until event arrives */
watch.onEvent(msg); 
}
}

Back

Embedded.com Career Center
Ready to take that job and shove it?
SEARCH JOBS

Browse all jobs

SPONSOR
RECENT JOB POSTINGS


 :