SafeString  4.1.27
SafeString is a safe, robust and debuggable replacement for string processing in Arduino
SafeStringStream.h
Go to the documentation of this file.
1 #ifndef SAFE_STRING_STREAM_H
2 #define SAFE_STRING_STREAM_H
3 /*
4  SafeStringStream.h a Stream wrapper for a SafeString
5  by Matthew Ford
6  (c)2020 Forward Computing and Control Pty. Ltd.
7  This code is not warranted to be fit for any purpose. You may only use it at your own risk.
8  This code may be freely used for both private and commercial use.
9  Provide this copyright is maintained.
10 **/
11 #ifdef __cplusplus
12 #include <Arduino.h>
13 #include "SafeString.h"
14 
15 // handle namespace arduino
17 
18 
28 class SafeStringStream : public Stream {
29  public:
30 
34  explicit SafeStringStream(); // nothing to send yet
35 
44 
45  // Use this constructor to set an rxBuffer to use insted of the internal 8 byte buffer
46  // the sf set here can be replaced in the begin( ) call
47 
56  explicit SafeStringStream(SafeString &sf, SafeString &sfRxBuffer);
57 
58 
59 
64  void begin(const uint32_t baudRate = 0); // start to release at this baud rate, 0 means infinite baudRate //uint32_t
65 
71  void begin(SafeString &sf, const uint32_t baudRate = 0); // start to release sf contents at this baud rate, 0 means infinite baudRate
72  // this begin replaces any previous sf with the sf passed here.
73 
74 
80  size_t write(uint8_t b);
81 
85  int available();
86 
89  int read();
90 
93  int peek();
94 
97  void flush(); // for ESP32 etc
98 
102 
103  // number of chars dropped due to SafeStringStream Rx buffer overflow
104  // count is reset to zero at the end of this call
105 
109  size_t RxBufferOverflow();
110 
111  private:
112  SafeStringStream(const SafeStringStream& other);
113  void init();
114  unsigned long us_perByte; // == 1000000 / (baudRate/10) == 10000000 / baudRate
115  uint32_t baudRate;
116  unsigned long releaseNextByte();
117  unsigned long sendTimerStart;
118  char Rx_BUFFER[9]; // 8char + null
119  SafeString* sfRxBufferPtr;
120  size_t missedCharsCount;
121  protected:
123 };
124 
125 #include "SafeStringNameSpaceEnd.h"
126 
127 #endif // __cplusplus
128 #endif // SAFE_STRING_STREAM_H
To create SafeStrings use one of the four (4) macros createSafeString or cSF, createSafeStringFromCha...
Definition: SafeString.h:307
The SafeStringStream class allows you to test your code with data read from a given SafeString as tho...
int available()
How many bytes are currently available to be read.
SafeStringStream()
SafeStringStream empty constructor, nothing to read yet.
SafeStringStream(SafeString &sf, SafeString &sfRxBuffer)
SafeStringStream constructor with data to be read from this stream and an RX buffer to be used.
SafeString * sfPtr
size_t RxBufferOverflow()
How may bytes have been dropped due to this stream's RX buffer being full.
void flush()
flush any buffered writes, does nothing here except release next byte at current baud rate.
int availableForWrite()
How may bytes can be written to this stream before its underlying SafeString is full.
void begin(const uint32_t baudRate=0)
Enable SafeStringStream to start delivering data when read() called.
SafeStringStream(SafeString &sf)
SafeStringStream constructor with data to be read from this stream.
void begin(SafeString &sf, const uint32_t baudRate=0)
Enable SafeStringStream to start delivering data from this SafeString when read() called.
int peek()
Peek at the next byte, returns -1 if none available.
size_t write(uint8_t b)
Write a byte to this stream, the data is appended to the SafeString that is supplying data for this s...
int read()
Read the next byte, returns -1 if none available.