SafeString  4.1.27
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 
36 #include <Print.h>
37 #include <Printable.h>
38 
39 #include "SafeString.h" // for SSTRING_DEBUG and SafeString::Output and stream support
40 
41 // handle namespace arduino
43 
44 #define createBufferedInput(name, size) uint8_t name ## _INPUT_BUFFER[(size)]; BufferedInput name(sizeof(name ## _INPUT_BUFFER),name ## _INPUT_BUFFER);
45 
46 
61 class BufferedInput : public Stream {
62  public:
70  BufferedInput(size_t _bufferSize, uint8_t *_buf);
71 
76  void connect(Stream& _stream);
77 
78  void nextByteIn();
79  virtual size_t write(uint8_t);
80  virtual size_t write(const uint8_t *buf, size_t size);
81  virtual int available();
82  virtual int read();
83  virtual int peek();
84  virtual void flush();
85  virtual int availableForWrite();
86  size_t getSize(); // returns buffer size
87 
88  // Counts when number of chars dropped due to full inputBuffer
89  // count is reset to zero at the end of this call
92 
93  private:
94  Stream* streamPtr;
95  int maxAvail;
96  int bufUsed;
97  uint8_t defaultBuffer[8]; // if buffer passed in too small or NULL
98 
99  // ringBuffer methods
104  void rb_init(uint8_t* _buf, size_t _size);
105  // from Stream
106  inline int rb_available() {
107  return rb_buffer_count;
108  }
109  int rb_peek();
110  int rb_read();
111  size_t rb_write(uint8_t b); // does not block, drops bytes if buffer full
112  size_t rb_write(const uint8_t *buffer, size_t size); // does not block, drops bytes if buffer full
113  int rb_availableForWrite(); // { return (bufSize - buffer_count); }
114  size_t rb_getSize(); // size of ring buffer
115  void rb_clear();
116  void rb_dump(Stream* streamPtr);
117  uint8_t* rb_buf;
118  uint16_t rb_bufSize;
119  uint16_t rb_buffer_head;
120  uint16_t rb_buffer_tail;
121  uint16_t rb_buffer_count;
122  uint16_t rb_wrapBufferIdx(uint16_t idx);
123  void rb_internalWrite(uint8_t b);
124 };
125 
126 #include "SafeStringNameSpaceEnd.h"
127 
128 #endif // __cplusplus
129 #endif // BufferedInput_h
To create a BufferedInput use the macro createBufferedInput see the detailed description.
Definition: BufferedInput.h:61
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 createBufferedOutput(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()