pfodParser 5.0.1 The pfodParser library is handles commands sent from the Android pfodApp, pfodApp supports WiFi, BLE, Bluetooth and SMS connections

pfodCircularLineBuffer.h
Go to the documentation of this file.
1
15 #ifndef PFOD_CIRCULAR_LINE_BUFFER_H
16 #define PFOD_CIRCULAR_LINE_BUFFER_H
17
18 #include <Arduino.h>
19
24 struct BufferRange {
25 size_t startPos; // Starting byte position in buffer
26 size_t endPos; // Ending byte position in buffer (exclusive)
27 size_t startLine; // Line number at start position
28 size_t endLine; // Line number at end position
29 };
30
40 class pfodCircularLineBuffer : public Stream {
41 public:
49 pfodCircularLineBuffer(size_t bufferSize = 4096);
50
54 ~pfodCircularLineBuffer();
55
64 size_t addLine(const char* line);
65
74 size_t addLine(const String& line);
75
84 void getRange(size_t fromLineCount, BufferRange& range);
85
91 void getAllRange(BufferRange& range);
92
99 void setReadRange(const BufferRange& range);
100
106 void resetForRead();
107
113 size_t getStartLineCount() const { return startLineCount; }
114
120 size_t getEndLineCount() const { return endLineCount; }
121
127 size_t getLineCount() const { return endLineCount - startLineCount; }
128
134 size_t getBufferSize() const { return bufferSize; }
135
141 size_t getUsedBytes() const;
142
148 size_t getAvailableBytes() const { return bufferSize - getUsedBytes(); }
149
155 bool isEmpty() const { return head == tail; }
156
160 void clear();
161
168 void debugBufferRange(Print* outPtr);
169
170 // Stream interface implementation (read operations)
171
177 virtual int available() override;
178
184 virtual int peek() override;
185
191 virtual int read() override;
192
193 // Print interface implementation (write operations for adding data)
194
202 virtual size_t write(uint8_t c) override;
203
211 virtual size_t write(const uint8_t *buffer, size_t size) override;
212
213 private:
214 size_t bufferSize; // Configured buffer size
215 uint8_t* buffer; // Circular buffer storage
216 size_t head; // Write position (next byte to write)
217 size_t tail; // Read position (first byte to read)
218 size_t startLineCount; // Line number at tail position
219 size_t endLineCount; // Line number at head position (next line to write)
220
221 // Current read range for Stream interface
222 size_t readPos; // Current read position within readRange
223 size_t readEndPos; // End position of current read range
224 size_t lineByteCount;
225 uint8_t prevByte;
226
227
236 bool findNextLine(size_t fromPos, size_t& lineEnd);
237
238
246 size_t bytesInRange(size_t start, size_t end) const;
247 };
248
249 #endif // PFOD_CIRCULAR_LINE_BUFFER_H
pfodCircularLineBuffer
pfodCircularLineBuffer - Line-aware circular buffer for CSV data streaming
Definition: pfodCircularLineBuffer.h:40
pfodCircularLineBuffer::clear
void clear()
Clears all data from buffer and resets line counts.
pfodCircularLineBuffer::getStartLineCount
size_t getStartLineCount() const
Gets the current start line number (oldest available line).
Definition: pfodCircularLineBuffer.h:113
pfodCircularLineBuffer::addLine
size_t addLine(const char *line)
Adds a complete line to the buffer (must include \r terminator).
pfodCircularLineBuffer::peek
virtual int peek() override
Reads one byte from current read range without advancing position.
pfodCircularLineBuffer::getUsedBytes
size_t getUsedBytes() const
Gets number of bytes currently used in buffer.
pfodCircularLineBuffer::resetForRead
void resetForRead()
Resets read position to beginning of all available data.
pfodCircularLineBuffer::getBufferSize
size_t getBufferSize() const
Gets the configured buffer size.
Definition: pfodCircularLineBuffer.h:134
pfodCircularLineBuffer::getEndLineCount
size_t getEndLineCount() const
Gets the current end line number (newest line + 1).
Definition: pfodCircularLineBuffer.h:120
pfodCircularLineBuffer::getLineCount
size_t getLineCount() const
Gets total number of lines currently in buffer.
Definition: pfodCircularLineBuffer.h:127
pfodCircularLineBuffer::~pfodCircularLineBuffer
~pfodCircularLineBuffer()
Destructor - Cleans up buffer memory.
pfodCircularLineBuffer::write
virtual size_t write(uint8_t c) override
Write single byte to buffer.
pfodCircularLineBuffer::getAvailableBytes
size_t getAvailableBytes() const
Gets number of bytes available in buffer.
Definition: pfodCircularLineBuffer.h:148
pfodCircularLineBuffer::addLine
size_t addLine(const String &line)
Adds a complete line to the buffer (must include \r terminator).
pfodCircularLineBuffer::pfodCircularLineBuffer
pfodCircularLineBuffer(size_t bufferSize=4096)
Constructor - Initializes empty circular buffer with specified size.
pfodCircularLineBuffer::available
virtual int available() override
Returns number of bytes available to read from current read range.
pfodCircularLineBuffer::debugBufferRange
void debugBufferRange(Print *outPtr)
Prints debug information about buffer state and positions.
pfodCircularLineBuffer::write
virtual size_t write(const uint8_t *buffer, size_t size) override
Write buffer.
pfodCircularLineBuffer::getRange
void getRange(size_t fromLineCount, BufferRange &range)
Gets buffer range starting from specified line number.
pfodCircularLineBuffer::read
virtual int read() override
Reads one byte from current read range and advances position.
pfodCircularLineBuffer::isEmpty
bool isEmpty() const
Checks if buffer is empty.
Definition: pfodCircularLineBuffer.h:155
pfodCircularLineBuffer::setReadRange
void setReadRange(const BufferRange &range)
Sets the current read position to a specific buffer range.
pfodCircularLineBuffer::getAllRange
void getAllRange(BufferRange &range)
Gets buffer range for all available data.
BufferRange
pfodCircularLineBuffer.h
Definition: pfodCircularLineBuffer.h:24
BufferRange::startLine
size_t startLine
Definition: pfodCircularLineBuffer.h:27
BufferRange::startPos
size_t startPos
Definition: pfodCircularLineBuffer.h:25
BufferRange::endPos
size_t endPos
Definition: pfodCircularLineBuffer.h:26
BufferRange::endLine
size_t endLine
Definition: pfodCircularLineBuffer.h:28