Skip to content

Commit 5fcb0b0

Browse files
committed
Renamed to RingBufCPP, fixed returns inside atomic macros
1 parent 273cf1a commit 5fcb0b0

File tree

9 files changed

+202
-167
lines changed

9 files changed

+202
-167
lines changed

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 D. Aaron Wisner
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Feel free to improve this library. Fork it, make your changes, then submit a pul
4242
### Constructor
4343

4444
```c++
45-
RingBuf<typename Type, uint16_t MaxElements>();
45+
RingBufCPP<typename Type, uint16_t MaxElements>();
4646
```
4747

4848
Creates a new RingBuf object that can buffer up to MaxElements of type Type.

RingBuf.cpp

Lines changed: 0 additions & 107 deletions
This file was deleted.

RingBuf.h

Lines changed: 109 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,151 @@
1-
#ifndef EM_RINGBUF_FIFO_CPP_H
2-
#define EM_RINGBUF_FIFO_CPP_H
1+
#ifndef EM_RINGBUF_CPP_H
2+
#define EM_RINGBUF_CPP_H
33

4-
// TODO fix this
5-
#define NULL (void *)(0)
6-
7-
#ifdef ARDUINO
8-
#include <Arduino.h>
9-
#else
10-
#include <stdint.h>
11-
#endif
12-
13-
#ifdef ARDUINO
14-
15-
#if defined(ARDUINO_ARCH_AVR)
16-
#include <util/atomic.h>
17-
#define RB_ATOMIC_START ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
18-
#define RB_ATOMIC_END }
19-
20-
21-
#elif defined(ARDUINO_ARCH_ESP8266)
22-
#ifndef __STRINGIFY
23-
#define __STRINGIFY(a) #a
24-
#endif
25-
26-
#ifndef xt_rsil
27-
#define xt_rsil(level) (__extension__({uint32_t state; __asm__ __volatile__("rsil %0," __STRINGIFY(level) : "=a" (state)); state;}))
28-
#endif
29-
30-
#ifndef xt_wsr_ps
31-
#define xt_wsr_ps(state) __asm__ __volatile__("wsr %0,ps; isync" :: "a" (state) : "memory")
32-
#endif
33-
34-
#define RB_ATOMIC_START do { uint32_t _savedIS = xt_rsil(15) ;
35-
#define RB_ATOMIC_END xt_wsr_ps(_savedIS); } while(0);
36-
37-
#else
38-
#define RB_ATOMIC_START {
39-
#define RB_ATOMIC_END }
40-
#warning “This library only fully supports AVR and ESP8266 Boards.”
41-
#warning "Operations on the buffer in ISRs are not safe!"
42-
#endif
43-
44-
#else
45-
#define RB_ATOMIC_START {
46-
#define RB_ATOMIC_END }
47-
#warning "Operations on the buffer in ISRs are not safe!"
48-
#warning "Impliment RB_ATOMIC_START and RB_ATOMIC_END macros for safe ISR operation!"
49-
#endif
4+
#include "RingBufHelpers.h"
505

516
template <typename Type, uint16_t MaxElements>
52-
class RingBuf
7+
class RingBufCPP
538
{
549
public:
5510

56-
RingBuf();
11+
RingBufCPP()
12+
{
13+
RB_ATOMIC_START
14+
{
15+
_numElements = 0;
16+
17+
_head = 0;
18+
}
19+
RB_ATOMIC_END
20+
}
5721

5822
/**
5923
* Add element obj to the buffer
6024
* Return: true on success
6125
*/
62-
bool add(Type &obj);
26+
bool add(Type &obj)
27+
{
28+
bool ret = false;
29+
RB_ATOMIC_START
30+
{
31+
if (!isFull()) {
32+
_buf[_head] = obj;
33+
_head = (_head + 1)%MaxElements;
34+
_numElements++;
35+
36+
ret = true;
37+
}
38+
}
39+
RB_ATOMIC_END
40+
41+
return ret;
42+
}
43+
6344

6445
/**
6546
* Remove last element from buffer, and copy it to dest
6647
* Return: true on success
6748
*/
68-
bool pull(Type *dest);
49+
bool pull(Type *dest)
50+
{
51+
bool ret = false;
52+
uint16_t tail;
53+
54+
RB_ATOMIC_START
55+
{
56+
if (!isEmpty()) {
57+
tail = getTail();
58+
*dest = _buf[tail];
59+
_numElements--;
60+
61+
ret = true;
62+
}
63+
}
64+
RB_ATOMIC_END
65+
66+
return ret;
67+
}
68+
6969

7070
/**
7171
* Peek at num'th element in the buffer
7272
* Return: a pointer to the num'th element
7373
*/
74-
Type *peek(uint16_t num);
74+
Type peek(uint16_t num)
75+
{
76+
Type *ret = NULL;
77+
78+
RB_ATOMIC_START
79+
{
80+
if (num < _numElements) //make sure not out of bounds
81+
ret = &_buf[(getTail() + num)%MaxElements];
82+
}
83+
RB_ATOMIC_END
84+
85+
return ret;
86+
}
87+
7588

7689
/**
7790
* Return: true if buffer is full
7891
*/
79-
bool isFull();
92+
bool isFull()
93+
{
94+
bool ret;
95+
96+
RB_ATOMIC_START
97+
{
98+
ret = _numElements >= MaxElements;
99+
}
100+
RB_ATOMIC_END
101+
102+
return ret;
103+
}
104+
80105

81106
/**
82107
* Return: number of elements in buffer
83108
*/
84-
uint16_t numElements();
109+
uint16_t numElements()
110+
{
111+
uint16_t ret;
112+
113+
RB_ATOMIC_START
114+
{
115+
ret = _numElements;
116+
}
117+
RB_ATOMIC_END
118+
119+
return ret;
120+
}
121+
85122

86123
/**
87124
* Return: true if buffer is empty
88125
*/
89-
bool isEmpty();
126+
bool isEmpty()
127+
{
128+
bool ret;
129+
130+
RB_ATOMIC_START
131+
{
132+
ret = !_numElements;
133+
}
134+
RB_ATOMIC_END
135+
136+
return ret;
137+
}
90138

91139
protected:
92140
/**
93141
* Calculates the index in the array of the oldest element
94142
* Return: index in array of element
95143
*/
96-
uint16_t getTail();
144+
uint16_t getTail()
145+
{
146+
return (_head + (MaxElements - _numElements))%MaxElements;
147+
}
148+
97149

98150
// underlying array
99151
Type _buf[MaxElements];
@@ -104,5 +156,4 @@ uint16_t _numElements;
104156

105157
};
106158

107-
#include "RingBuf.cpp"
108159
#endif

0 commit comments

Comments
 (0)