SerialBuffer.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * SerialBuffer.h
8  *
9  * @author 22 Aug 2018 - mikee47 <mike@sillyhouse.net>
10  *
11  ****/
12 
13 #pragma once
14 
15 #include <cstddef>
16 #include <cstdint>
17 #include <sming_attr.h>
18 
24 struct SerialBuffer {
25 public:
27  {
28  delete[] buffer;
29  }
30 
31  size_t getSize()
32  {
33  return size;
34  }
35 
39  __forceinline size_t available()
40  {
41  int ret = writePos - readPos;
42  if(ret < 0) {
43  ret += size;
44  }
45  return ret;
46  }
47 
51  __forceinline size_t getFreeSpace()
52  {
53  if(buffer == nullptr) {
54  return 0;
55  }
56  int ret = readPos - writePos - 1;
57  if(ret < 0) {
58  ret += size;
59  }
60  return ret;
61  }
62 
63  __forceinline bool isEmpty()
64  {
65  return (buffer == nullptr) || (writePos == readPos);
66  }
67 
68  __forceinline bool isFull()
69  {
70  return getFreeSpace() == 0;
71  }
72 
76  __forceinline int peekChar()
77  {
78  return isEmpty() ? -1 : buffer[readPos];
79  }
80 
81  /*
82  * Take a peek at the last character written into the buffer
83  */
84  __forceinline int peekLastChar()
85  {
86  return isEmpty() ? -1 : buffer[getPrevPos(writePos)];
87  }
88 
89  __forceinline int readChar()
90  {
91  if(isEmpty()) {
92  return -1;
93  }
94 
95  uint8_t c = buffer[readPos];
96  readPos = getNextPos(readPos);
97  return c;
98  }
99 
100  __forceinline size_t writeChar(uint8_t c)
101  {
102  size_t nextPos = getNextPos(writePos);
103  if(nextPos == readPos) {
104  return 0;
105  }
106 
107  buffer[writePos] = c;
108  writePos = nextPos;
109  return 1;
110  }
111 
116  int find(uint8_t c);
117 
118  // Must be called with interrupts disabled
119  size_t resize(size_t newSize);
120 
121  void clear()
122  {
123  readPos = writePos = 0;
124  }
125 
130  __forceinline size_t getReadData(void*& data)
131  {
132  data = buffer + readPos;
133  auto wp = writePos; // Guard against ISR changing value
134  return (wp < readPos) ? size - readPos : wp - readPos;
135  }
136 
141  __forceinline void skipRead(size_t length)
142  {
143  readPos += length;
144  if(readPos == size) {
145  readPos = 0;
146  }
147  }
148 
149 private:
151  __forceinline size_t getNextPos(size_t pos)
152  {
153  size_t n = pos + 1;
154  return (n == size) ? 0 : n;
155  }
156 
158  __forceinline size_t getPrevPos(size_t pos)
159  {
160  return (pos != 0 ? pos : size) - 1;
161  }
162 
163 private:
164  size_t size = 0;
165  size_t readPos = 0;
166  size_t writePos = 0;
167  char* buffer = nullptr;
168 };
FIFO buffer used for both receive and transmit data.
Definition: SerialBuffer.h:24
int find(uint8_t c)
find a character in the buffer
~SerialBuffer()
Definition: SerialBuffer.h:26
int readChar()
Definition: SerialBuffer.h:89
int peekLastChar()
Definition: SerialBuffer.h:84
size_t resize(size_t newSize)
void skipRead(size_t length)
Skip a number of chars starting at the given read position.
Definition: SerialBuffer.h:141
size_t getFreeSpace()
get number of bytes of space available in this buffer
Definition: SerialBuffer.h:51
size_t getSize()
Definition: SerialBuffer.h:31
int peekChar()
see if there's anything in the buffer
Definition: SerialBuffer.h:76
bool isFull()
Definition: SerialBuffer.h:68
size_t available()
get number of bytes stored in the buffer
Definition: SerialBuffer.h:39
size_t writeChar(uint8_t c)
Definition: SerialBuffer.h:100
size_t getReadData(void *&data)
Access data directly within buffer.
Definition: SerialBuffer.h:130
bool isEmpty()
Definition: SerialBuffer.h:63
void clear()
Definition: SerialBuffer.h:121