typedef struct { struct HORN *Buffer; //缓冲区 unsigned int pw; //写地址 unsigned int pr; //读地址 }RingBuffer_t,*pRingBuffer_t; static RingBuffer_t RingBuffer; /** @brief 写一个字节 @param wdata:写入的一个字节数据 @retval 1:环形缓冲区满 0:环形缓冲区未满 **/ uint8_t RingBuffer_Write_One_Byte(pRingBuffer_t pRingBuffer,const struct HORN wdata) { if((pRingBuffer->pw + 1)%BUFF_SIZE==pRingBuffer->pr)//环形缓冲区满 { return 1; }else //环形缓冲区未满 { pRingBuffer->Buffer[pRingBuffer->pw] = wdata; pRingBuffer->pw=(pRingBuffer->pw+1)%BUFF_SIZE; return 0; } } /** @brief 读一个字节数据 @param rdata:保存读出的数据 @retval 1:已读出全部数据 0:仍有未读出的数据 **/ uint8_t RingBuffer_Read_One_Byte(pRingBuffer_t pRingBuffer,struct HORN *rdata) { if(pRingBuffer->pr==pRingBuffer->pw)//已读出全部数据 { return 1; }else//仍有未读出的数据 { *rdata=pRingBuffer->Buffer[pRingBuffer->pr]; pRingBuffer->pr=(pRingBuffer->pr+1)%BUFF_SIZE; return 0; } } //环形缓冲区buff初始化 void ring_buff_init(void) { RingBuffer.Buffer = horn_data; RingBuffer.pw = 0; RingBuffer.pr = 0; } //环形缓冲区添加数据 void ring_buff_add_data(unsigned char voide,unsigned char vol) { struct HORN wdata; wdata.voide = voide; wdata.vol = vol; RingBuffer_Write_One_Byte(&RingBuffer,wdata); }
标签:pr,pw,pRingBuffer,环形,wdata,RingBuffer,缓冲区 From: https://www.cnblogs.com/-yjx-/p/17452467.html