From 8dbefd9847baa356960dbe740920fe7dc4a550c3 Mon Sep 17 00:00:00 2001 From: zhanglinjing Date: Thu, 11 Apr 2024 13:57:27 +0200 Subject: [PATCH] fix RingBuffer initialization --- cores/RingBuffer.cpp | 37 ++++++++++++++++--------------------- cores/RingBuffer.h | 10 ++++------ 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/cores/RingBuffer.cpp b/cores/RingBuffer.cpp index 95143404..d0cf1da0 100644 --- a/cores/RingBuffer.cpp +++ b/cores/RingBuffer.cpp @@ -20,38 +20,33 @@ // @Project Includes //**************************************************************************** #include "RingBuffer.h" - +#include //**************************************************************************** // @Local Functions //**************************************************************************** // Constructors //////////////////////////////////////////////////////////////// -RingBuffer::RingBuffer( int bufferSize ) +RingBuffer::RingBuffer( void ) { - _bufferSize = bufferSize; - _aucBuffer = new uint8_t[_bufferSize]; - _iHead = 0 ; - _iTail = 0 ; + memset( (void *)_aucBuffer, 0, SERIAL_BUFFER_SIZE ) ; + _iHead=0 ; + _iTail=0 ; } -// Public Methods ////////////////////////////////////////////////////////////// - void RingBuffer::store_char( uint8_t c ) { - int i = _iHead + 1; - if( i >= _bufferSize ) - i = 0; - - // if we should be storing the received character into the location - // just before the tail (meaning that the head would advance to the - // current location of the tail), we're about to overflow the buffer - // and so we don't write the character or advance the head. - if ( i != _iTail ) - { - _aucBuffer[_iHead] = c ; - _iHead = i ; - } + int i = (uint32_t)(_iHead + 1) % SERIAL_BUFFER_SIZE ; + + // if we should be storing the received character into the location + // just before the tail (meaning that the head would advance to the + // current location of the tail), we're about to overflow the buffer + // and so we don't write the character or advance the head. + if ( i != _iTail ) + { + _aucBuffer[_iHead] = c ; + _iHead = i ; + } } //**************************************************************************** diff --git a/cores/RingBuffer.h b/cores/RingBuffer.h index 8b0f0bd1..01959ec9 100644 --- a/cores/RingBuffer.h +++ b/cores/RingBuffer.h @@ -29,15 +29,13 @@ class RingBuffer { -public: - volatile uint8_t *_aucBuffer; - // volatile uint8_t _aucBuffer[SERIAL_BUFFER_SIZE] ; + public: + volatile uint8_t _aucBuffer[SERIAL_BUFFER_SIZE] ; volatile int _iHead ; volatile int _iTail ; - volatile int _bufferSize ; -public: - RingBuffer( int bufferSize = SERIAL_BUFFER_SIZE) ; + public: + RingBuffer( void ) ; void store_char( uint8_t c ) ; } ;