大学生
最后登录1970-1-1
在线时间 小时
注册时间2015-5-8
|
发表于 2020-1-19 11:40:29
|
显示全部楼层
[mw_shl_code=c,true]
uint8_t GPS_ring_buffer[RING_MAXLEN];
ring_buf_struct ring_struct;
ring_buf_struct_ptr GPS_ring;
void ringBufferInit(void)
{
GPS_ring = &ring_struct;
GPS_ring->size = RING_MAXLEN;
GPS_ring->buffer = GPS_ring_buffer;
memset(GPS_ring->buffer , 0x00 , GPS_ring->size);
GPS_ring->rptr = 0x00;
GPS_ring->wptr = 0x00;
}
BOOL ringBufferEmpty(ring_buf_struct *ring)
{
if (ring->rptr == ring->wptr)
return (TRUE);
return (FALSE);
}
BOOL ringBufferFull(ring_buf_struct *ring)
{
if (ring->rptr == ((ring->wptr + 1) % ring->size))
return (TRUE);
return (FALSE);
}
BOOL WriteCharTo_ringBuffer(ring_buf_struct *ring , uint8_t ch)
{
if (ringBufferFull(ring))
return (FALSE);
ring->buffer[ring->wptr] = ch;
ring->wptr++;
ring->wptr = ring->wptr % ring->size;
return (TRUE);
}
BOOL ReadCharFrom_ringBuffer(ring_buf_struct *ring , uint8_t *ch)
{
if (ringBufferEmpty(ring))
return (FALSE);
*ch = ring->buffer[ring->rptr];
ring->rptr++;
ring->rptr = ring->rptr % ring->size;
return (TRUE);
}
uint16_t ringBufferFilllength(ring_buf_struct *ring)
{
return ((ring->size + ring->wptr - ring->rptr) % ring->size);
}
uint16_t ringBufferFreelength(ring_buf_struct *ring)
{
return (ring->size - ringBufferFilllength(ring) - 1);
}
//环形缓冲区切记:
//(1)、不一定wptr一定大于rptr
//(2)、不一定rptr一定大于wptr
BOOL WriteBufferTo_ringBuffer(ring_buf_struct *ring , uint8_t *buffer , uint16_t n)
{
uint16_t i;
if (ringBufferFull(ring))
return (FALSE);
if (ringBufferFreelength(ring) < n)
return (FALSE);
i = MIN(n , ring->size - ring->wptr % ring->size); //从ring->wptr开始到缓冲区结尾的空间
memcpy(ring->buffer + (ring->wptr % ring->size) , buffer , i);
if (n > i)
memcpy(ring->buffer , buffer + i , n - i); //从缓冲区开始(0)到ring->rptr之前的n-i个有效空间
ring->wptr = (ring->wptr + n) % ring->size;
return (TRUE);
}
//环形缓冲区切记:
//(1)、不一定wptr一定大于rptr
//(2)、不一定rptr一定大于wptr
///////////////////////////////////////////////////////////////////////////////////////////////
//1.当rptr = 1 , wptr = 0 ,如果环形缓冲区空间=23(RING_MAXLEN=23)字节,则读出22个字节后,则rptr = 0 , wptr = 0(读出的22个字节正确)
//2.当rptr = 2 , wptr = 1 ,如果环形缓冲区空间=23(RING_MAXLEN=23)字节,则读出22个字节后,则rptr = 1 , wptr = 1(读出的22个字节正确)
//3.当rptr = 3 , wptr = 2 ,如果环形缓冲区空间=23(RING_MAXLEN=23)字节,则读出22个字节后,则rptr = 2 , wptr = 2(读出的22个字节正确)
//4.当rptr = 4 , wptr = 3 ,如果环形缓冲区空间=23(RING_MAXLEN=23)字节,则读出22个字节后,则rptr = 3 , wptr = 3(读出的22个字节正确)
//5.当rptr = 5 , wptr = 4 ,如果环形缓冲区空间=23(RING_MAXLEN=23)字节,则读出22个字节后,则rptr = 4 , wptr = 4(读出的22个字节正确)
//6.当rptr =10 , wptr = 9 ,如果环形缓冲区空间=23(RING_MAXLEN=23)字节,则读出22个字节后,则rptr = 9 , wptr = 9(读出的22个字节正确)
//7.当rptr =11 , wptr =10 ,如果环形缓冲区空间=23(RING_MAXLEN=23)字节,则读出22个字节后,则rptr =10 , wptr =10(读出的22个字节正确)
//8.当rptr =12 , wptr =11 ,如果环形缓冲区空间=23(RING_MAXLEN=23)字节,则读出22个字节后,则rptr =11 , wptr =11(读出的22个字节正确)
//9.当rptr =13 , wptr =12 ,如果环形缓冲区空间=23(RING_MAXLEN=23)字节,则读出22个字节后,则rptr =12 , wptr =12(读出的22个字节正确)
//10.当rptr=21 , wptr =20 ,如果环形缓冲区空间=23(RING_MAXLEN=23)字节,则读出22个字节后,则rptr =20 , wptr =20(读出的22个字节正确)
//11.当rptr=22 , wptr =21 ,如果环形缓冲区空间=23(RING_MAXLEN=23)字节,则读出22个字节后,则rptr =21 , wptr =21(读出的22个字节正确)
//12.当rptr= 5 , wptr = 4 ,如果环形缓冲区空间=23(RING_MAXLEN=23)字节,则读出20个字节后,则rptr = 2 , wptr = 4(读出的20个字节正确)(读数据时,折绕正确)
//13.当rptr=10 , wptr = 8 ,如果环形缓冲区空间=23(RING_MAXLEN=23)字节,则读出16个字节后,则rptr = 3 , wptr = 8(读出的16个字节正确)(读数据时,折绕正确)
//14.当rptr= 3 , wptr = 2 ,如果环形缓冲区空间=23(RING_MAXLEN=23)字节,则读出22个字节后,则rptr = 2 , wptr = 2(读出的22个字节正确)(读数据时,折绕正确)
///////////////////////////////////////////////////////////////////////////////////////////////
uint16_t ReadBufferFrom_ringBuffer(ring_buf_struct *ring , uint8_t *buffer , uint16_t n)
{
uint16_t i;
uint16_t len;
len = MIN(n , ringBufferFilllength(ring));
i = MIN(len , ring->size - ring->rptr % ring->size );
memcpy(buffer , ring->buffer + (ring->rptr % ring->size) , i);
if (len > i)
memcpy(buffer + i, ring->buffer , len - i);
ring->rptr = (ring->rptr + len) % ring->size;
return (len);
}
[/mw_shl_code]
|
|