Skip to content

Commit 503a9b3

Browse files
facchinmsandeepmistry
authored andcommitted
Make RingBuffer class template-based
This commit prepares to enlarge the ringbuffer for specific needs (at compile time). Also, it fixes a spurious include in delay.h
1 parent d58b840 commit 503a9b3

File tree

3 files changed

+105
-108
lines changed

3 files changed

+105
-108
lines changed

Diff for: cores/arduino/RingBuffer.cpp

-94
This file was deleted.

Diff for: cores/arduino/RingBuffer.h

+102-11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1717
*/
1818

19+
#ifdef __cplusplus
20+
1921
#ifndef _RING_BUFFER_
2022
#define _RING_BUFFER_
2123

@@ -27,25 +29,114 @@
2729
// location from which to read.
2830
#define SERIAL_BUFFER_SIZE 64
2931

30-
class RingBuffer
32+
template <int N>
33+
class RingBufferN
3134
{
3235
public:
33-
uint8_t _aucBuffer[SERIAL_BUFFER_SIZE] ;
36+
uint8_t _aucBuffer[N] ;
3437
int _iHead ;
3538
int _iTail ;
3639

3740
public:
38-
RingBuffer( void ) ;
41+
RingBufferN( void ) ;
3942
void store_char( uint8_t c ) ;
40-
void clear();
41-
int read_char();
42-
int available();
43-
int availableForStore();
44-
int peek();
45-
bool isFull();
43+
void clear();
44+
int read_char();
45+
int available();
46+
int availableForStore();
47+
int peek();
48+
bool isFull();
4649

4750
private:
48-
int nextIndex(int index);
49-
} ;
51+
int nextIndex(int index);
52+
};
53+
54+
typedef RingBufferN<SERIAL_BUFFER_SIZE> RingBuffer;
55+
56+
57+
template <int N>
58+
RingBufferN<N>::RingBufferN( void )
59+
{
60+
memset( _aucBuffer, 0, N ) ;
61+
clear();
62+
}
63+
64+
template <int N>
65+
void RingBufferN<N>::store_char( uint8_t c )
66+
{
67+
int i = nextIndex(_iHead);
68+
69+
// if we should be storing the received character into the location
70+
// just before the tail (meaning that the head would advance to the
71+
// current location of the tail), we're about to overflow the buffer
72+
// and so we don't write the character or advance the head.
73+
if ( i != _iTail )
74+
{
75+
_aucBuffer[_iHead] = c ;
76+
_iHead = i ;
77+
}
78+
}
79+
80+
template <int N>
81+
void RingBufferN<N>::clear()
82+
{
83+
_iHead = 0;
84+
_iTail = 0;
85+
}
86+
87+
template <int N>
88+
int RingBufferN<N>::read_char()
89+
{
90+
if(_iTail == _iHead)
91+
return -1;
92+
93+
uint8_t value = _aucBuffer[_iTail];
94+
_iTail = nextIndex(_iTail);
95+
96+
return value;
97+
}
98+
99+
template <int N>
100+
int RingBufferN<N>::available()
101+
{
102+
int delta = _iHead - _iTail;
103+
104+
if(delta < 0)
105+
return N + delta;
106+
else
107+
return delta;
108+
}
109+
110+
template <int N>
111+
int RingBufferN<N>::availableForStore()
112+
{
113+
if (_iHead >= _iTail)
114+
return N - 1 - _iHead + _iTail;
115+
else
116+
return _iTail - _iHead - 1;
117+
}
118+
119+
template <int N>
120+
int RingBufferN<N>::peek()
121+
{
122+
if(_iTail == _iHead)
123+
return -1;
124+
125+
return _aucBuffer[_iTail];
126+
}
127+
128+
template <int N>
129+
int RingBufferN<N>::nextIndex(int index)
130+
{
131+
return (uint32_t)(index + 1) % N;
132+
}
133+
134+
template <int N>
135+
bool RingBufferN<N>::isFull()
136+
{
137+
return (nextIndex(_iHead) == _iTail);
138+
}
50139

51140
#endif /* _RING_BUFFER_ */
141+
142+
#endif /* __cplusplus */

Diff for: cores/arduino/delay.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
#ifndef _DELAY_
2020
#define _DELAY_
2121

22+
#include <stdint.h>
23+
#include "variant.h"
24+
2225
#ifdef __cplusplus
2326
extern "C" {
2427
#endif
2528

26-
#include <stdint.h>
27-
#include "variant.h"
28-
2929
/**
3030
* \brief Returns the number of milliseconds since the Arduino board began running the current program.
3131
*

0 commit comments

Comments
 (0)