RSeries astromech firmware
PSRamBufferedPrintStream.h
Go to the documentation of this file.
1 #ifndef PSRamBufferedPrintStream_h
2 #define PSRamBufferedPrintStream_h
3 
4 #pragma once
5 
6 class RamBufferedPrintStream : public Print
7 {
8 public:
9  RamBufferedPrintStream(Stream& stream, size_t siz = 4096) :
10  fStream(stream),
11  fSize(siz)
12  {
13  fPtr = fBuffer = (uint8_t*)malloc(fSize);
14  fEnd = fPtr + fSize;
15  }
16 
17  void flush()
18  {
19  if (fPtr - fBuffer > 0)
20  {
21  fStream.write(fBuffer, fPtr - fBuffer);
22  fPtr = fBuffer;
23  }
24  }
25 
27  {
28  flush();
29  if (fBuffer != NULL)
30  free(fBuffer);
31  }
32 
33  virtual size_t write(uint8_t ch) override
34  {
35  return write(&ch, 1);
36  }
37 
38  virtual size_t write(const uint8_t *buffer, size_t size) override
39  {
40  if (fPtr + size >= fEnd)
41  {
42  flush();
43  }
44  if (fPtr + size < fEnd)
45  {
46  memcpy(fPtr, buffer, size);
47  fPtr += size;
48  }
49  else
50  {
51  fStream.write(buffer, size);
52  }
53  return size;
54  }
55 
56 private:
57  Stream& fStream;
58  uint8_t* fBuffer;
59  uint8_t* fEnd;
60  uint8_t* fPtr;
61  size_t fSize;
62 };
63 
64 class PSRamBufferedPrintStream : public Print
65 {
66 public:
67  PSRamBufferedPrintStream(Stream& stream, size_t siz = 64768) :
68  fStream(stream),
69  fSize(siz)
70  {
71  fPtr = fBuffer = (uint8_t*)ps_malloc(fSize);
72  fEnd = fPtr + fSize;
73  }
74 
75  void flush()
76  {
77  if (fPtr - fBuffer > 0)
78  {
79  fStream.write(fBuffer, fPtr - fBuffer);
80  fPtr = fBuffer;
81  }
82  }
83 
85  {
86  flush();
87  if (fBuffer != NULL)
88  free(fBuffer);
89  }
90 
91  virtual size_t write(uint8_t ch) override
92  {
93  return write(&ch, 1);
94  }
95 
96  virtual size_t write(const uint8_t *buffer, size_t size) override
97  {
98  if (fPtr + size >= fEnd)
99  {
100  flush();
101  }
102  if (fPtr + size < fEnd)
103  {
104  memcpy(fPtr, buffer, size);
105  fPtr += size;
106  }
107  else
108  {
109  fStream.write(buffer, size);
110  }
111  return size;
112  }
113 
114 private:
115  Stream& fStream;
116  uint8_t* fBuffer;
117  uint8_t* fEnd;
118  uint8_t* fPtr;
119  size_t fSize;
120 };
121 
122 #endif
RamBufferedPrintStream::write
virtual size_t write(const uint8_t *buffer, size_t size) override
Definition: PSRamBufferedPrintStream.h:38
PSRamBufferedPrintStream::write
virtual size_t write(uint8_t ch) override
Definition: PSRamBufferedPrintStream.h:91
RamBufferedPrintStream::RamBufferedPrintStream
RamBufferedPrintStream(Stream &stream, size_t siz=4096)
Definition: PSRamBufferedPrintStream.h:9
PSRamBufferedPrintStream
Definition: PSRamBufferedPrintStream.h:64
RamBufferedPrintStream::flush
void flush()
Definition: PSRamBufferedPrintStream.h:17
RamBufferedPrintStream
Definition: PSRamBufferedPrintStream.h:6
RamBufferedPrintStream::write
virtual size_t write(uint8_t ch) override
Definition: PSRamBufferedPrintStream.h:33
PSRamBufferedPrintStream::~PSRamBufferedPrintStream
virtual ~PSRamBufferedPrintStream()
Definition: PSRamBufferedPrintStream.h:84
PSRamBufferedPrintStream::write
virtual size_t write(const uint8_t *buffer, size_t size) override
Definition: PSRamBufferedPrintStream.h:96
PSRamBufferedPrintStream::flush
void flush()
Definition: PSRamBufferedPrintStream.h:75
RamBufferedPrintStream::~RamBufferedPrintStream
virtual ~RamBufferedPrintStream()
Definition: PSRamBufferedPrintStream.h:26
PSRamBufferedPrintStream::PSRamBufferedPrintStream
PSRamBufferedPrintStream(Stream &stream, size_t siz=64768)
Definition: PSRamBufferedPrintStream.h:67