typedef struct
{
void *buffer[2];
volatile uint8_t writeIndex;
volatile uint8_t readIndex;
volatile uint8_t readAvaliable[2];
} PingPongBuffer_t;
void *buffer[2] 无类型指针,指向不同数据类型的缓冲Buff
volatile uint8_t writeIndex 缓冲写索引
volatile uint8_t readIndex 缓冲读索引
相关函数实现volatile uint8_t readAvaliable[2] 可读标志位
void PingPongBuffer_Init(PingPongBuffer_t *ppbuf, void *buf0, void *buf1)
{
memset(ppbuf, 0, sizeof(PingPongBuffer_t));
ppbuf->buffer[0] = buf0;
ppbuf->buffer[1] = buf1;
}
bool PingPongBuffer_GetReadBuf(PingPongBuffer_t *ppbuf, void **pReadBuf)
{
if (ppbuf->readAvaliable[0])
{
ppbuf->readIndex = 0;
}
else if (ppbuf->readAvaliable[1])
{
ppbuf->readIndex = 1;
}
else
{
return false;
}
*pReadBuf = ppbuf->buffer[ppbuf->readIndex];
return true;
}
void PingPongBuffer_SetReadDone(PingPongBuffer_t *ppbuf)
{
ppbuf->readAvaliable[ppbuf->readIndex] = false;
}
void PingPongBuffer_GetWriteBuf(PingPongBuffer_t *ppbuf, void **pWriteBuf)
{
if (ppbuf->writeIndex == ppbuf->readIndex)
{
ppbuf->writeIndex = !ppbuf->readIndex;
}
*pWriteBuf = ppbuf->buffer[ppbuf->writeIndex];
}
void PingPongBuffer_SetWriteDone(PingPongBuffer_t *ppbuf)
{
ppbuf->readAvaliable[ppbuf->writeIndex] = true;
ppbuf->writeIndex = !ppbuf->writeIndex;
}



