Skip to content

Make Ringbuffer size template-based #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 0 additions & 94 deletions cores/arduino/RingBuffer.cpp

This file was deleted.

113 changes: 102 additions & 11 deletions cores/arduino/RingBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifdef __cplusplus

#ifndef _RING_BUFFER_
#define _RING_BUFFER_

Expand All @@ -27,25 +29,114 @@
// location from which to read.
#define SERIAL_BUFFER_SIZE 64

class RingBuffer
template <int N>
class RingBufferN
{
public:
uint8_t _aucBuffer[SERIAL_BUFFER_SIZE] ;
uint8_t _aucBuffer[N] ;
int _iHead ;
int _iTail ;

public:
RingBuffer( void ) ;
RingBufferN( void ) ;
void store_char( uint8_t c ) ;
void clear();
int read_char();
int available();
int availableForStore();
int peek();
bool isFull();
void clear();
int read_char();
int available();
int availableForStore();
int peek();
bool isFull();

private:
int nextIndex(int index);
} ;
int nextIndex(int index);
};

typedef RingBufferN<SERIAL_BUFFER_SIZE> RingBuffer;


template <int N>
RingBufferN<N>::RingBufferN( void )
{
memset( _aucBuffer, 0, N ) ;
clear();
}

template <int N>
void RingBufferN<N>::store_char( uint8_t c )
{
int i = nextIndex(_iHead);

// 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 ;
}
}

template <int N>
void RingBufferN<N>::clear()
{
_iHead = 0;
_iTail = 0;
}

template <int N>
int RingBufferN<N>::read_char()
{
if(_iTail == _iHead)
return -1;

uint8_t value = _aucBuffer[_iTail];
_iTail = nextIndex(_iTail);

return value;
}

template <int N>
int RingBufferN<N>::available()
{
int delta = _iHead - _iTail;

if(delta < 0)
return N + delta;
else
return delta;
}

template <int N>
int RingBufferN<N>::availableForStore()
{
if (_iHead >= _iTail)
return N - 1 - _iHead + _iTail;
else
return _iTail - _iHead - 1;
}

template <int N>
int RingBufferN<N>::peek()
{
if(_iTail == _iHead)
return -1;

return _aucBuffer[_iTail];
}

template <int N>
int RingBufferN<N>::nextIndex(int index)
{
return (uint32_t)(index + 1) % N;
}

template <int N>
bool RingBufferN<N>::isFull()
{
return (nextIndex(_iHead) == _iTail);
}

#endif /* _RING_BUFFER_ */

#endif /* __cplusplus */
6 changes: 3 additions & 3 deletions cores/arduino/delay.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
#ifndef _DELAY_
#define _DELAY_

#include <stdint.h>
#include "variant.h"

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include "variant.h"

/**
* \brief Returns the number of milliseconds since the Arduino board began running the current program.
*
Expand Down
4 changes: 2 additions & 2 deletions libraries/Wire/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ class TwoWire : public Stream
bool transmissionBegun;

// RX Buffer
RingBuffer rxBuffer;
RingBufferN<256> rxBuffer;

//TX buffer
RingBuffer txBuffer;
RingBufferN<256> txBuffer;
uint8_t txAddress;

// Callback user functions
Expand Down