forked from DCC-EX/CommandStation-EX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircularBuffer.hpp
188 lines (160 loc) · 5.57 KB
/
CircularBuffer.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* © 2023 Thierry Paris / Locoduino
* All rights reserved.
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* It is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
*/
//-------------------------------------------------------------------
#ifndef __CircularBuffer_Hpp__
#define __CircularBuffer_Hpp__
//-------------------------------------------------------------------
#include <Arduino.h>
#include "DIAG.h"
//#define DCCPP_DEBUG_MODE
/** This is a thread-safe buffer of bytes, binary or not. Bytes are pushed on the top (its head), and got from the bottom of the
* buffer (its tail...).
*/
class CircularBuffer
{
private:
byte *buffer; // buffer itself
int size; // total size of this buffer
int head; // index of the first free byte in the buffer
int tail; // index of the next byte to get.
bool full; // if true, no more space !
int peakCount; // biggest occupancy size since the creation of the buffer.
#ifdef ARDUINO_ARCH_ESP32
SemaphoreHandle_t xSemaphore; // semaphore d'exclusion mutuelle
#endif
public:
/** Constructor.
@param inSize
*/
CircularBuffer(int inSize);
/** Initialize the list.
@param inMultiThread If the buffer is used in multi-thread environnement, initialize the semaphore before calling this begin().
*/
void begin(bool inMultiThread = false);
/** Close the usage of this buffer and free the allocated memory.
*/
void end();
/** Remove the full content of the buffer, ready for the next push.
*/
void clear();
/** Add one byte in the buffer.
@param inpData byte to add.
@return true if the byte have been pushed. false if the byte are lost due to max size reached...
*/
bool PushByte(byte inpData);
/** Add some bytes in the buffer.
@param inpData pointer to bytes to add.
@param inDataLength number of bytes to add.
@return true if all bytes have been pushed. false if one or more bytes are lost due to max size reached...
*/
bool PushBytes(byte* inpData, int inDataLength);
/** Get the next byte from the buffer.
@return first available byte, or 0.
*/
byte GetByte();
/** Get the next two bytes from the buffer, to form an integer.
@return integer created from two available bytes, or 0.
*/
int16_t GetInt16();
/** Get the next four bytes from the buffer, to form an integer.
@return integer created from two available bytes, or 0.
*/
int32_t GetInt32();
/** Get some bytes.
@param inpData buffer to fill.
@param inDataLength number of bytes to get.
@return true if all bytes have been get. false if there were not enough bytes in th buffer.
*/
bool GetBytes(byte* inpData, int inDataLength);
/** Get the next two bytes from the given buffer starting from the given position, to form an integer.
@param pBuffer buffer to scan.
@param inPos Position of the first useful byte.
@return integer created from two available bytes, or 0.
*/
static int16_t GetInt16(byte* pBuffer, int inPos);
/** Get the next four bytes from the given buffer starting from the given position, to form a 32 bits integer.
@param pBuffer buffer to scan.
@param inPos Position of the first useful byte.
@return integer created from four available bytes, or 0.
*/
static int32_t GetInt32(byte* pBuffer, int inPos);
/** Get some bytes from the given buffer starting from the given position.
@param pBuffer buffer to scan.
@param inPos Position of the first useful byte.
@param inpData buffer to fill.
@param inDataLength number of bytes to get.
@return true if all bytes have been get. false if there were not enough bytes in th buffer.
*/
static void GetBytes(byte *pBuffer, int inPos, byte* inpData, int inDataLength);
/** Count the number of bytes in the buffer.
@return number of bytes in the buffer.
*/
int GetCount();
/** Get the maximum size used by the buffer since the beggining of its usage.
@return maximum number of bytes in the buffer.
*/
int GetPeakCount() { return this->peakCount; }
/** Check if the buffer is empty or not.
*/
bool isEmpty() const
{
//if head and tail are equal, we are empty
return (!this->full && (this->head == this->tail));
}
/** Check if the buffer is full or not.
*/
bool isFull() const
{
//If tail is ahead the head by 1, we are full
return this->full;
}
/** Check if the begin has been called. If not, the buffer is not allocated and any usage will crash the system !
*/
bool CheckIfBeginDone();
#ifdef DCCPP_DEBUG_MODE
/** Unit test function
*/
static void Test();
/** Print the list of messages in the stack.
@remark Only available if DCCPP_DEBUG_MODE is defined.
*/
void printCircularBuffer();
#endif
void printStatus();
};
#ifdef ARDUINO_ARCH_ESP32
#define START_SEMAPHORE() \
{ \
byte semaphoreTaken = this->xSemaphore == NULL?1:0; \
if (this->xSemaphore != NULL) \
if (xSemaphoreTake(this->xSemaphore, (TickType_t)100) == pdTRUE) \
semaphoreTaken = 1; \
if (semaphoreTaken == 1) {
#define END_SEMAPHORE() \
} \
if (this->xSemaphore != NULL) \
xSemaphoreGive(this->xSemaphore); \
}
#define ABORT_SEMAPHORE() \
xSemaphoreGive(this->xSemaphore);
#else
#define START_SEMAPHORE()
#define END_SEMAPHORE()
#define ABORT_SEMAPHORE()
#endif
#endif