1、Ping Pong Buffer 原理分析

基本原理如上图所示,当设备有数据来时,先放入缓冲区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;
}

Ping Pong Buffer 双缓冲 C++代码学习相关推荐

  1. ping pong buffer

    Ping pong buffer is a buffer used in a transmission that contains two separate buffers, while one bu ...

  2. android双缓冲绘图技术分析

    转载请说明出处:http://www.jianshu.com/p/efc0bebfd22e 双缓冲.多缓冲技术在计算机科学中其实是一个广义的概念,不过其本质上的意思都是差不多的.今天我们就来讲一讲双缓 ...

  3. 【QT】QT从零入门教程(十三):QT画笔工具QPainter (双缓冲绘图)

      QPainter 用于执行绘图操作,其提供的 API 在 GUI 或 QImage.QOpenGLPaintDevice.QWidget 和QPaintDevice 显示图形(线.形状.渐变等). ...

  4. c语言双缓冲怎么用,C语言游戏编程:GDI怎么实现双缓冲绘图去掉闪烁

    在上篇文章中将我要用 C语言重新写一个俄罗斯方块 ,使用的是GDI的绘图模式(目前正在移植到DX上去,想添加一些更好友好的动画).数据与动画分离,动画的帧率保持在30左右.但是绘图的时候画面出现了强烈 ...

  5. 关于Surface的底层双缓冲机制学习

    双缓冲机制 问题的由来 CPU访问内存的速度要远远快于访问屏幕的速度.如果需要绘制大量复杂的图像时,每次都一个个从内存中读取图形然后绘制到屏幕就会造成多次地访问屏幕,从而导致效率很低.这就跟CPU和内 ...

  6. 双缓冲(Double Buffer)原理和使用【转】

    转自:http://blog.csdn.net/acs713/article/details/16359551 原文出自:http://blog.csdn.net/xiaohui_hubei/arti ...

  7. JAVA多线程双缓冲笔记_关于多线程学习的笔记

    task: implement Observer and Observable, read shared_ptr base 库 1. base/StringPiece.h Viewpoint 1. 判 ...

  8. STM32F407学习之DMA双缓冲模式HAL库实现

    本学期第一次培训要求用单片机实现1到40KHz的单步进变频正弦波输出,本菜鸡一开始用了通过修改定时器预分频系数和自动重装载值改变输出波表频率.修改定时器 + DDS两种方法,但实现的效果不太理想,不仅 ...

  9. 【学习总结】MFC(CImagelist)(重绘)(双缓冲)

    双缓冲实现按钮列表 本文通过介绍双缓冲与CImageList,重写CButton类,来实现按钮列表. 简单的效果图 双缓冲实现 双缓冲的技术原理: 在Windows中每一种设备都在内存中有一个设备描述 ...

最新文章

  1. U3D的Collider
  2. Android园区部队人脸识别源码门禁项目讲解
  3. c语言程序设计填空带答案,c语言程序设计填空题及答案复习用精编-20210414010859.docx-原创力文档...
  4. python!!!!惊了,这世上居然还有这么神奇的东西存在
  5. JAVA可不可以编写应用程序_编写一个java应用程序
  6. [容易]在二叉查找树中插入节点
  7. 阿里云发布聆听平台 全球招募300位MVP
  8. the server is not ready for publishing.Please check if the Publishing Tools on the server
  9. 常用SQL Server数据修复命令DBCC一览(转载)
  10. 授权普通非DBA用户可以有权限查看执行计划的方法
  11. Dapper.net 入门demo 轻量级ORM框架
  12. IDEA快捷键+使用小技巧
  13. 前端实现鼠标拖拽功能
  14. Python-png转换成jpg
  15. html5分辨率异常自动检测
  16. wordpress 菜单_如何在WordPress主题中添加菜单说明
  17. python 怎么样才有output_Python display.clear_output方法代码示例
  18. 德云一哥岳云鹏,准备录制河南和东方卫视节目,央视春晚还参加吗
  19. Oracle - 索引
  20. QT-局域网电脑远程桌面

热门文章

  1. 【比亚迪半导体】关联销售占比超过50%,关联销售毛利率是非关联销售的一倍
  2. 赌徒有10元,一次输赢1元,手头能到110元的概率
  3. 云服务器nginx站点主页路径更改方法
  4. 印度尼西亚通过加密货币期货交易规则
  5. java将长网址转换为短网址 用第三方接口 常用于生成二维码时需要
  6. python计算绩效工资编程_Python实战精选:计算销售提成
  7. 指定文件名无效或太长,请指定另一文件名
  8. 【王卓】数据结构与算法图(九)
  9. 【博学谷学习记录】超强总结,用心分享丨大数据超神之路(一):Scala基础语法副本
  10. 8.前端JavaScript之数据类型