usrapp.h:
struct usrapp_t
{
struct vsfsm_msm_t msm;
struct vsfsm_t sm;
struct vsfsm_msm_entry_t trans_tbl[3];
};
extern struct usrapp_t usrapp;
void usrapp_srt_init(struct usrapp_t *app);
usrapp.c:
#include "vsf.h"
#include "usrapp.h"
enum
{
STATE_INIT = 0,
STATE_ON,
STATE_OFF,
};
#define USRAPP_EVT_ON1S VSFSM_EVT_USER
int usrapp_do_init(struct vsfsm_msm_t *msm);
int usrapp_do_on(struct vsfsm_msm_t *msm);
int usrapp_do_off(struct vsfsm_msm_t *msm);
struct usrapp_t usrapp =
{
.msm.entry_num = dimof(usrapp.trans_tbl),
.msm.trans_tbl = usrapp.trans_tbl,
.msm.user_data = &usrapp,
.msm.state = STATE_INIT,
.trans_tbl[0] = {STATE_INIT, VSFSM_EVT_INIT, NULL, usrapp_do_init},
.trans_tbl[1] = {STATE_ON, USRAPP_EVT_ON1S, NULL, usrapp_do_off},
.trans_tbl[2] = {STATE_OFF, USRAPP_EVT_ON1S, NULL, usrapp_do_on},
};
int usrapp_do_on(struct vsfsm_msm_t *msm)
{
vsfhal_gpio_set(1, 1 << 1);
return STATE_ON;
}
int usrapp_do_off(struct vsfsm_msm_t *msm)
{
vsfhal_gpio_clear(1, 1 << 1);
return STATE_OFF;
}
int usrapp_do_init(struct vsfsm_msm_t *msm)
{
vsfhal_gpio_init(1);
vsfhal_gpio_config_pin(1, 1, GPIO_OUTPP);
vsftimer_create(msm->sm, 1000, -1, USRAPP_EVT_ON1S);
return usrapp_do_off(msm);
}
void usrapp_srt_init(struct usrapp_t *app)
{
vsfsm_msm_init(&app->sm, &app->msm);
}
元状态机是选修,这个就自己看了。
网友评论