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
516template <typename Type, uint16_t MaxElements>
52- class RingBuf
7+ class RingBufCPP
538{
549public:
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
91139protected:
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
99151Type _buf[MaxElements];
@@ -104,5 +156,4 @@ uint16_t _numElements;
104156
105157};
106158
107- #include " RingBuf.cpp"
108159#endif
0 commit comments