本帖最后由 liang7143 于 2011-9-20 17:40 编辑
建立一个FIFO其实 也是很简单的
比如,先定义一个缓冲区
#define MAX_FIFO_DEPTH 8
typedef struct _MESSAGE_FIFO
{
BYTE IndexHead;
BYTE Count;
KEY_EVENT Buf[MAX_FIFO_DEPTH];
} MESSAGE_FIFO;
MESSAGE_FIFO MsgFifos;
写缓冲区
BOOL
MessageSend(
BYTE event
)
{
MESSAGE_FIFO* pMsgFifo = &MsgFifos;
if(pMsgFifo->Count >= MAX_FIFO_DEPTH) //fifo full
{
return FALSE;
}
pMsgFifo->Buf[(pMsgFifo->IndexHead + (pMsgFifo->Count++)) % MAX_FIFO_DEPTH] = event;
return TRUE;
}
从缓冲区中读取数据
//fifo empty return MSG_NONE, else return message
BYTE MessageGet(
BYTE MsgFifoIndex
)
{
BYTE event = MSG_NONE;
MESSAGE_FIFO* pMsgFifo = &MsgFifos;
if(pMsgFifo->Count) //fifo is not empty
{
event = pMsgFifo->Buf[pMsgFifo->IndexHead];
pMsgFifo->IndexHead = (pMsgFifo->IndexHead + 1) % MAX_FIFO_DEPTH;
pMsgFifo->Count--;
}
return event;
}
|