hello world: 基于PT协程的闪灯程序

usrapp.h:

struct usrapp_t
{
    struct vsfsm_pt_t pt;
    struct vsfsm_t sm;
    bool toggle;
};

extern struct usrapp_t usrapp;
void usrapp_srt_init(struct usrapp_t *app);

usrapp.c:

#include "vsf.h"
#include "usrapp.h"

vsf_err_t usrapp_led_thread(struct vsfsm_pt_t *pt, vsfsm_evt_t evt);
struct usrapp_t usrapp =
{
    .pt.thread                      = usrapp_led_thread,
    .pt.user_data                   = &usrapp,
};

#define USRAPP_EVT_ON1S                 VSFSM_EVT_USER
vsf_err_t usrapp_led_thread(struct vsfsm_pt_t *pt, vsfsm_evt_t evt)
{
    struct usrapp_t *app = (struct usrapp_t *)pt->user_data;

    vsfsm_pt_begin(pt);
    vsfhal_gpio_init(0);
    vsfhal_gpio_config_pin(0, 1, GPIO_OUTPP);
    vsftimer_create(pt->sm, 1000, -1, USRAPP_EVT_ON1S);
    while (1)
    {
        if (app->toggle)
            vsfhal_gpio_set(0, 1 << 1);
        else
            vsfhal_gpio_clear(0, 1 << 1);
        app->toggle = !app->toggle;
        vsfsm_pt_wfe(pt, USRAPP_EVT_ON1S);
    }
    vsfsm_pt_end(pt);
    return VSFERR_NONE;
}

void usrapp_srt_init(struct usrapp_t *app)
{
    vsfsm_pt_init(&app->sm, &app->pt);
}

这里,usrapp_led_thread中的app变量,在每次调用的时候,都会初始化。代码功能和之前的事件驱动的闪灯程序一样。

你可能感兴趣的:(hello world: 基于PT协程的闪灯程序)