b_mod_select

数据结构

typedef struct
{
    uint8_t fds[(SELECT_FDS_NUMBER + 7) / 8];
} bFdSet_t;

接口介绍

#define bFD_ZERO(pfds)                           \
    do                                           \
    {                                            \
        if ((pfds) != NULL)                      \
        {                                        \
            memset((pfds), 0, sizeof(bFdSet_t)); \
        }                                        \
    } while (0)

#define bFD_SET(fd, pfds)                               \
    do                                                  \
    {                                                   \
        if ((fd) < SELECT_FDS_NUMBER && (pfds) != NULL) \
        {                                               \
            (pfds)->fds[(fd) / 8] |= 0x1 << ((fd) % 8); \
        }                                               \
    } while (0)

#define bFD_ISSET(fd, pfds) ((pfds)->fds[(fd) / 8] & (0x1 << ((fd) % 8)))

#define B_PT_SELECT(pt, maxfdp, readfds, writefds, errorfds, timeout, result) \
    PT_WAIT_UNTIL(pt, (result = bSelect(maxfdp, readfds, writefds, errorfds)) > 0, timeout)

// bSelect是非阻塞的,如果要做到阻塞的效果,建议开启pt,并使用B_PT_SELECT
int bSelect(int maxfdp, bFdSet_t *readfds, bFdSet_t *writefds, bFdSet_t *errorfds);

使用例子

static PT_THREAD(gsensor_task(struct pt *pt))
{
    int             count = 0;
    bGsensor3Axis_t Gsensor[32];
    static int      fd = -1;
    static bFdSet_t fdset;
    PT_BEGIN(pt);
    fd = bOpen(bLIS3DH, BCORE_FLAG_RW);
    if (fd < 0)
    {
        return 0;
    }
    while (1)
    {
        bFD_ZERO(&fdset);
        bFD_SET(fd, &fdset);
        B_PT_SELECT(pt, fd + 1, &fdset, NULL, NULL, 2000, count);
        b_log("select:%d\r\n", count);
        if (count > 0)
        {
            if (bFD_ISSET(fd, &fdset))
            {
                bRead(fd, (uint8_t *)&Gsensor, sizeof(Gsensor));
                for (int i = 0; i < 32; i++)
                {
                    b_log("x:%d y:%d z:%d\r\n", Gsensor[i].x_mg, Gsensor[i].y_mg, Gsensor[i].z_mg);
                }
            }
        }
    }
    PT_END(pt);
}