SafeString  4.1.44
SafeString is a safe, robust and debuggable replacement for string processing in Arduino
BufferedOutput.h
Go to the documentation of this file.
1 #ifndef BufferedOutput_h
2 #define BufferedOutput_h
3 #ifdef __cplusplus
4 
14 
36 #include <Print.h>
37 #include <Printable.h>
38 #include "SafeString.h" // for Output and #define SSTRING_DEBUG and stream support
39 
40 // handle namespace arduino
42 
43 #define createBufferedOutput(name, size, ...) uint8_t name ## _OUTPUT_BUFFER[(size)+4]; BufferedOutput name(sizeof(name ## _OUTPUT_BUFFER),name ## _OUTPUT_BUFFER, __VA_ARGS__ ); // add 4 for dropMark
44 
46 
80 class BufferedOutput : public Stream {
81  public:
82 
98  BufferedOutput(size_t _bufferSize, uint8_t *_buf, BufferedOutputMode mode, bool allOrNothing = true);
99 
105  void connect(HardwareSerial& _serial);
106 
107 
114  void connect(Stream& _stream, const uint32_t baudRate=0);
115 
124  void nextByteOut();
125 
133  virtual size_t write(uint8_t);
134  virtual size_t write(const uint8_t *buf, size_t size);
135  virtual int available();
136  virtual int read();
137  virtual int peek();
143  virtual void flush();
144 
150  virtual int availableForWrite();
155  size_t getSize();
156 
168  int clearSpace(size_t len);
169 
179  void protect();
185  void clear(); // clears outgoing (write) buffer, even if protected.
186 
187 
195  size_t terminateLastLine(); // adds a newline if one not already there
196 
197  private:
198  int internalAvailableForWrite();
199  int internalStreamAvailableForWrite(); // returns 0 if no availableForWrite else connection.availableForWrite()-1 to allow for ESP blocking on 1
200  void writeDropMark();
201  size_t bytesToBeSent(); // bytes in this buffer to be sent, // this ignores any data in the HardwareSerial buffer
202  BufferedOutputMode mode; // = 0;
203  bool allOrNothing; // = true current setting reset to allOrNothingSetting after each write(buf,size)
204  bool allOrNothingSetting; // = true as passed in to constructor
205  uint8_t defaultBuffer[8]; // if buffer passed in too small or NULL
206  unsigned long us_perByte; // == 1000000 / (baudRate/10) == 10000000 / baudRate
207  Stream* streamPtr;
208  HardwareSerial* serialPtr; // non-null if HardwareSerial and availableForWrite returns non zero
209  uint32_t baudRate;
210  unsigned long sendTimerStart;
211  bool waitForEmpty;
212  Print* debugOut; // only used if #define DEBUG uncomment in BufferedOutput.cpp
213  int txBufferSize; // serial tx buffer, if any OR set to zero to only use ringBuffer
214  bool dropMarkWritten;
215  uint8_t lastCharWritten; // check for \n
216  volatile bool inNextByteOut; // per-instance lock to prevent recursive calls to nextByteOut()
217 
218  // ringBuffer methods
223  void rb_init(uint8_t* _buf, size_t _size);
224  void rb_clear();
225  bool rb_clearSpace(size_t len); //returns true if some output dropped, clears space in outgoing (write) buffer, by removing last bytes written
226  // from Stream
227  inline int rb_available() {
228  return rb_buffer_count;
229  }
230  int rb_peek();
231  int rb_read();
232  size_t rb_write(uint8_t b); // does not block, drops bytes if buffer full
233  size_t rb_write(const uint8_t *buffer, size_t size); // does not block, drops bytes if buffer full
234  int rb_availableForWrite(); // { return (bufSize - buffer_count); }
235  size_t rb_getSize(); // size of ring buffer
236  bool rb_lastBufferedByteProtect();
237  void rb_dump(Stream* streamPtr);
238 
239  uint8_t* rb_buf;
240  uint16_t rb_bufSize;
241  uint16_t rb_buffer_head;
242  uint16_t rb_buffer_tail;
243  uint16_t rb_buffer_count;
244  uint16_t rb_wrapBufferIdx(uint16_t idx);
245  void rb_internalWrite(uint8_t b);
246 };
247 
248 #include "SafeStringNameSpaceEnd.h"
249 
250 #endif // __cplusplus
251 #endif // BufferedOutput_h
BufferedOutputMode
@ DROP_IF_FULL
@ DROP_UNTIL_EMPTY
@ BLOCK_IF_FULL
To create a BufferedOutput use the macro createBufferedOutput see the detailed description.
size_t terminateLastLine()
size_t terminateLastLine()
void protect()
void protect()
virtual int available()
virtual int availableForWrite()
int availableForWrite()
size_t getSize()
size_t getSize();
void connect(Stream &_stream, const uint32_t baudRate=0)
void connect(Stream& _stream, const uint32_t baudRate); // the stream to write to and how fast to wri...
int clearSpace(size_t len)
int clearSpace(size_t len);
virtual int read()
BufferedOutput(size_t _bufferSize, uint8_t *_buf, BufferedOutputMode mode, bool allOrNothing=true)
use createBufferedOutput(name, size, mode); instead BufferedOutput(size_t _bufferSize,...
virtual int peek()
virtual void flush()
void flush()
virtual size_t write(const uint8_t *buf, size_t size)
void connect(HardwareSerial &_serial)
void connect(HardwareSerial& _serial); // the output to write to, can also read from
void clear()
void clear()
virtual size_t write(uint8_t)
write(uint8_t b)
void nextByteOut()
void nextByteOut()