SafeString  4.1.27
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:
96  BufferedOutput(size_t _bufferSize, uint8_t *_buf, BufferedOutputMode = BLOCK_IF_FULL, bool allOrNothing = true);
97 
103  void connect(HardwareSerial& _serial);
104 
105 
112  void connect(Stream& _stream, const uint32_t baudRate=0);
113 
114  void nextByteOut();
115  virtual size_t write(uint8_t);
116  virtual size_t write(const uint8_t *buf, size_t size);
117  virtual int available();
118  virtual int read();
119  virtual int peek();
120  virtual void flush(); // this blocks until write buffer empty
121  virtual int availableForWrite();
122  size_t getSize(); // returns buffer size + any hardwareSerial buffer size found on connect
123  int clearSpace(size_t len); // clears space in outgoing (write) buffer, by removing last bytes written, Serial Tx buffer is NOT changed, returns available space in buffer + Tx
124  void protect(); // prevents current buffer contects from being cleared by clearSpace(), but clear() will still clear the whole buffer
125  void clear(); // clears outgoing (write) buffer, even if protected.
126  size_t terminateLastLine(); // adds a newline if one not already there
127 
128  private:
129  int internalAvailableForWrite();
130  int internalStreamAvailableForWrite(); // returns 0 if no availableForWrite else connection.availableForWrite()-1 to allow for ESP blocking on 1
131  void writeDropMark();
132  size_t bytesToBeSent(); // bytes in this buffer to be sent, // this ignores any data in the HardwareSerial buffer
133  BufferedOutputMode mode; // = 0;
134  bool allOrNothing; // = true current setting reset to allOrNothingSetting after each write(buf,size)
135  bool allOrNothingSetting; // = true as passed in to constructor
136  uint8_t defaultBuffer[8]; // if buffer passed in too small or NULL
137  unsigned long us_perByte; // == 1000000 / (baudRate/10) == 10000000 / baudRate
138  Stream* streamPtr;
139  HardwareSerial* serialPtr; // non-null if HardwareSerial and availableForWrite returns non zero
140  uint32_t baudRate;
141  unsigned long sendTimerStart;
142  bool waitForEmpty;
143  Print* debugOut; // only used if #define DEBUG uncomment in BufferedOutput.cpp
144  int txBufferSize; // serial tx buffer, if any OR set to zero to only use ringBuffer
145  bool dropMarkWritten;
146  uint8_t lastCharWritten; // check for \n
147 
148  // ringBuffer methods
153  void rb_init(uint8_t* _buf, size_t _size);
154  void rb_clear();
155  bool rb_clearSpace(size_t len); //returns true if some output dropped, clears space in outgoing (write) buffer, by removing last bytes written
156  // from Stream
157  inline int rb_available() {
158  return rb_buffer_count;
159  }
160  int rb_peek();
161  int rb_read();
162  size_t rb_write(uint8_t b); // does not block, drops bytes if buffer full
163  size_t rb_write(const uint8_t *buffer, size_t size); // does not block, drops bytes if buffer full
164  int rb_availableForWrite(); // { return (bufSize - buffer_count); }
165  size_t rb_getSize(); // size of ring buffer
166  bool rb_lastBufferedByteProtect();
167  void rb_dump(Stream* streamPtr);
168 
169  uint8_t* rb_buf;
170  uint16_t rb_bufSize;
171  uint16_t rb_buffer_head;
172  uint16_t rb_buffer_tail;
173  uint16_t rb_buffer_count;
174  uint16_t rb_wrapBufferIdx(uint16_t idx);
175  void rb_internalWrite(uint8_t b);
176 };
177 
178 #include "SafeStringNameSpaceEnd.h"
179 
180 #endif // __cplusplus
181 #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()
BufferedOutput(size_t _bufferSize, uint8_t *_buf, BufferedOutputMode=BLOCK_IF_FULL, bool allOrNothing=true)
use createBufferedOutput(name, size, mode); instead BufferedOutput(size_t _bufferSize,...
virtual int available()
virtual int availableForWrite()
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)
virtual int read()
virtual int peek()
virtual 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 serial – the Har...
virtual size_t write(uint8_t)
void nextByteOut()