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 
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 - Line-aware circular buffer for CSV data streaming
void clear()
Clears all data from buffer and resets line counts.
size_t getStartLineCount() const
Gets the current start line number (oldest available line).
size_t addLine(const char *line)
Adds a complete line to the buffer (must include \r terminator).
virtual int peek() override
Reads one byte from current read range without advancing position.
size_t getUsedBytes() const
Gets number of bytes currently used in buffer.
void resetForRead()
Resets read position to beginning of all available data.
size_t getBufferSize() const
Gets the configured buffer size.
size_t getEndLineCount() const
Gets the current end line number (newest line + 1).
size_t getLineCount() const
Gets total number of lines currently in buffer.
~pfodCircularLineBuffer()
Destructor - Cleans up buffer memory.
virtual size_t write(uint8_t c) override
Write single byte to buffer.
size_t getAvailableBytes() const
Gets number of bytes available in buffer.
size_t addLine(const String &line)
Adds a complete line to the buffer (must include \r terminator).
pfodCircularLineBuffer(size_t bufferSize=4096)
Constructor - Initializes empty circular buffer with specified size.
virtual int available() override
Returns number of bytes available to read from current read range.
void debugBufferRange(Print *outPtr)
Prints debug information about buffer state and positions.
virtual size_t write(const uint8_t *buffer, size_t size) override
Write buffer.
void getRange(size_t fromLineCount, BufferRange &range)
Gets buffer range starting from specified line number.
virtual int read() override
Reads one byte from current read range and advances position.
bool isEmpty() const
Checks if buffer is empty.
void setReadRange(const BufferRange &range)
Sets the current read position to a specific buffer range.
void getAllRange(BufferRange &range)
Gets buffer range for all available data.
pfodCircularLineBuffer.h