Ping Pong Buffer 双缓冲 C++代码学习

1、Ping Pong Buffer 原理分析

Ping Pong Buffer 双缓冲 C++代码学习_第1张图片

基本原理如上图所示,当设备有数据来时,先放入缓冲区1

在这里插入图片描述

然后将缓冲区1的数据放入缓冲区2,这时缓冲区1可接收下次数据。工作区可从缓冲区2拿数据

2、C++代码实现

相关结构体创建

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] 可读标志位

相关函数实现

/**
   * @brief  Ping-pong buffer initialization
   * @param  ppbuf: Pointer to the ping-pong buffer structure
   * @param  buf0:  Pointer to the first buffer
   * @param  buf1:  Pointer to the second buffer
   * @retval None
   */
void PingPongBuffer_Init(PingPongBuffer_t *ppbuf, void *buf0, void *buf1)
{
    memset(ppbuf, 0, sizeof(PingPongBuffer_t));
    ppbuf->buffer[0] = buf0;
    ppbuf->buffer[1] = buf1;
}

/**
  * @brief  Get a readable buffer
  * @param  ppbuf:     Pointer to the ping-pong buffer structure
  * @param  pReadBuf:  Pointer to the pointer to the buffer to be read
  * @retval Returns true if there is a buffer to be read
  */
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;
}

/**
  * @brief  Notify buffer read completion
  * @param  ppbuf: Pointer to the ping-pong buffer structure
  * @retval None
  */
void PingPongBuffer_SetReadDone(PingPongBuffer_t *ppbuf)
{
    ppbuf->readAvaliable[ppbuf->readIndex] = false;
}

/**
  * @brief  Get writable buffer
  * @param  ppbuf:      Pointer to the ping-pong buffer structure
  * @param  pWriteBuf:  Pointer to the pointer to the buffer to be wriye
  * @retval None
  */
void PingPongBuffer_GetWriteBuf(PingPongBuffer_t *ppbuf, void **pWriteBuf)
{
    if (ppbuf->writeIndex == ppbuf->readIndex)
    {
        ppbuf->writeIndex = !ppbuf->readIndex;
    }
    *pWriteBuf = ppbuf->buffer[ppbuf->writeIndex];
}

/**
  * @brief  Notify buffer write completion
  * @param  ppbuf: Pointer to the ping-pong buffer structure
  * @retval None
  */
void PingPongBuffer_SetWriteDone(PingPongBuffer_t *ppbuf)
{
    ppbuf->readAvaliable[ppbuf->writeIndex] = true;
    ppbuf->writeIndex = !ppbuf->writeIndex;
}

你可能感兴趣的:(#,C/C++,c++,开发语言)