SafeString  4.1.45
SafeString is a safe, robust and debuggable replacement for string processing in Arduino
BufferedInput.h
Go to the documentation of this file.
1 #ifndef BufferedInput_h
2 #define BufferedInput_h
3 #ifdef __cplusplus
4 
14 
39 #include <Print.h>
40 #include <Printable.h>
41 
42 #include "SafeString.h" // for SSTRING_DEBUG and SafeString::Output and stream support
43 
44 // handle namespace arduino
46 
47 #define createBufferedInput(name, size) uint8_t name ## _INPUT_BUFFER[(size)]; BufferedInput name(sizeof(name ## _INPUT_BUFFER),name ## _INPUT_BUFFER);
48 
49 
64 class BufferedInput : public Stream {
65  public:
73  BufferedInput(size_t _bufferSize, uint8_t *_buf);
74 
79  void connect(Stream& _stream);
80 
81  void nextByteIn();
82  virtual size_t write(uint8_t);
83  virtual size_t write(const uint8_t *buf, size_t size);
84  virtual int available();
85  virtual int read();
86  virtual int peek();
87  virtual void flush();
88  virtual int availableForWrite();
89  size_t getSize(); // returns buffer size
90 
91  // These two are HIGH WATER MARKS, not drop counts. BufferedInput does not count dropped chars;
92  // nextByteIn() only ever moves as many bytes as currently fit, it never discards any.
93  // Each value is reset to zero by the call that reads it, so each reading covers the interval
94  // since the previous call.
95  int maxStreamAvailable(); // the largest available() seen on the connected Stream, ie how close the
96  // hardware RX buffer came to overflowing. If this approaches the RX buffer size, call nextByteIn() more often
97  int maxBufferUsed(); // the largest fill level seen in this buffer. If this reaches the buffer
98  // size then chars ARE being lost at the Stream, increase the size passed to createBufferedInput( )
99 
100  private:
101  Stream* streamPtr;
102  int maxAvail;
103  int bufUsed;
104  uint8_t defaultBuffer[8]; // if buffer passed in too small or NULL
105 
106  // ringBuffer methods
111  void rb_init(uint8_t* _buf, size_t _size);
112  // from Stream
113  inline int rb_available() {
114  return rb_buffer_count;
115  }
116  int rb_peek();
117  int rb_read();
118  size_t rb_write(uint8_t b); // does not block, drops bytes if buffer full
119  size_t rb_write(const uint8_t *buffer, size_t size); // does not block, drops bytes if buffer full
120  int rb_availableForWrite(); // { return (bufSize - buffer_count); }
121  size_t rb_getSize(); // size of ring buffer
122  void rb_clear();
123  void rb_dump(Stream* streamPtr);
124  uint8_t* rb_buf;
125  uint16_t rb_bufSize;
126  uint16_t rb_buffer_head;
127  uint16_t rb_buffer_tail;
128  uint16_t rb_buffer_count;
129  uint16_t rb_wrapBufferIdx(uint16_t idx);
130  void rb_internalWrite(uint8_t b);
131 };
132 
133 #include "SafeStringNameSpaceEnd.h"
134 
135 #endif // __cplusplus
136 #endif // BufferedInput_h
To create a BufferedInput use the macro createBufferedInput see the detailed description.
Definition: BufferedInput.h:64
virtual size_t write(uint8_t)
int maxStreamAvailable()
virtual void flush()
void connect(Stream &_stream)
void connect(Stream& _stream); // the stream to read from, can also write to stream – the stream to b...
virtual size_t write(const uint8_t *buf, size_t size)
BufferedInput(size_t _bufferSize, uint8_t *_buf)
use createBufferedInput(name, size); instead BufferedInput(size_t _bufferSize, uint8_t *_buf);
virtual int read()
virtual int peek()
int maxBufferUsed()
virtual int availableForWrite()
size_t getSize()
virtual int available()
void nextByteIn()