SafeString  4.1.40
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 
78 class BufferedOutput : public Stream {
79  public:
80 
96  BufferedOutput(size_t _bufferSize, uint8_t *_buf, BufferedOutputMode mode, bool allOrNothing = true);
97 
103  void connect(HardwareSerial& _serial);
104 
105 
112  void connect(Stream& _stream, const uint32_t baudRate=0);
113 
122  void nextByteOut();
123 
131  virtual size_t write(uint8_t);
132  virtual size_t write(const uint8_t *buf, size_t size);
133  virtual int available();
134  virtual int read();
135  virtual int peek();
141  virtual void flush();
142 
148  virtual int availableForWrite();
153  size_t getSize();
154 
166  int clearSpace(size_t len);
167 
177  void protect();
183  void clear(); // clears outgoing (write) buffer, even if protected.
184 
185 
193  size_t terminateLastLine(); // adds a newline if one not already there
194 
195  private:
196  int internalAvailableForWrite();
197  int internalStreamAvailableForWrite(); // returns 0 if no availableForWrite else connection.availableForWrite()-1 to allow for ESP blocking on 1
198  void writeDropMark();
199  size_t bytesToBeSent(); // bytes in this buffer to be sent, // this ignores any data in the HardwareSerial buffer
200  BufferedOutputMode mode; // = 0;
201  bool allOrNothing; // = true current setting reset to allOrNothingSetting after each write(buf,size)
202  bool allOrNothingSetting; // = true as passed in to constructor
203  uint8_t defaultBuffer[8]; // if buffer passed in too small or NULL
204  unsigned long us_perByte; // == 1000000 / (baudRate/10) == 10000000 / baudRate
205  Stream* streamPtr;
206  HardwareSerial* serialPtr; // non-null if HardwareSerial and availableForWrite returns non zero
207  uint32_t baudRate;
208  unsigned long sendTimerStart;
209  bool waitForEmpty;
210  Print* debugOut; // only used if #define DEBUG uncomment in BufferedOutput.cpp
211  int txBufferSize; // serial tx buffer, if any OR set to zero to only use ringBuffer
212  bool dropMarkWritten;
213  uint8_t lastCharWritten; // check for \n
214 
215  // ringBuffer methods
220  void rb_init(uint8_t* _buf, size_t _size);
221  void rb_clear();
222  bool rb_clearSpace(size_t len); //returns true if some output dropped, clears space in outgoing (write) buffer, by removing last bytes written
223  // from Stream
224  inline int rb_available() {
225  return rb_buffer_count;
226  }
227  int rb_peek();
228  int rb_read();
229  size_t rb_write(uint8_t b); // does not block, drops bytes if buffer full
230  size_t rb_write(const uint8_t *buffer, size_t size); // does not block, drops bytes if buffer full
231  int rb_availableForWrite(); // { return (bufSize - buffer_count); }
232  size_t rb_getSize(); // size of ring buffer
233  bool rb_lastBufferedByteProtect();
234  void rb_dump(Stream* streamPtr);
235 
236  uint8_t* rb_buf;
237  uint16_t rb_bufSize;
238  uint16_t rb_buffer_head;
239  uint16_t rb_buffer_tail;
240  uint16_t rb_buffer_count;
241  uint16_t rb_wrapBufferIdx(uint16_t idx);
242  void rb_internalWrite(uint8_t b);
243 };
244 
245 #include "SafeStringNameSpaceEnd.h"
246 
247 #endif // __cplusplus
248 #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()