#include "fifo.h" /** * @brief RingBuff_Init * @param void * @return void * @note 初始化环形缓冲区 */ void RingBuff_Init(RingBuff_t *ringBuff) { //初始化相关信息 ringBuff->Head = 0; ringBuff->Tail = 0; ringBuff->Lenght = 0; } /** * @brief Write_RingBuff * @param u8 data * @return FLASE:环形缓冲区已满,写入失败;TRUE:写入成功 * @note 往环形缓冲区写入u8类型的数据 */ uint8_t Write_RingBuff(RingBuff_t *ringBuff,uint8_t data) { if(ringBuff->Lenght >= RINGBUFF_LEN) //判断缓冲区是否已满 { return FLASE; } ringBuff->Ring_Buff[ringBuff->Tail]=data; // ringBuff.Tail++; ringBuff->Tail = (ringBuff->Tail+1)%RINGBUFF_LEN;//防止越界非法访问 ringBuff->Lenght++; return OK; } /** * @brief Read_RingBuff * @param u8 *rData,用于保存读取的数据 * @return FLASE:环形缓冲区没有数据,读取失败;TRUE:读取成 * @note 从环形缓冲区读取一个u8类型的数据 */ uint8_t Read_RingBuff(RingBuff_t *ringBuff,uint8_t *rData) { if(ringBuff->Lenght == 0)//判断非空 { return FLASE; } *rData = ringBuff->Ring_Buff[ringBuff->Head];//先进先出FIFO,从缓冲区头出 // ringBuff.Head++; ringBuff->Head = (ringBuff->Head+1)%RINGBUFF_LEN;//防止越界非法访问 ringBuff->Lenght--; return OK; } /** * @brief Read_Length_RingBuff * @param RingBuff_t *ringBuff * @return * @note */ uint8_t Read_Length_RingBuff(RingBuff_t *ringBuff) { return ringBuff->Lenght; }