Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
SerialBuffer.h
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/anakod/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 #ifndef _SMING_CORE_DATA_BUFFER_SERIAL_BUFFER_H_
14 #define _SMING_CORE_DATA_BUFFER_SERIAL_BUFFER_H_
15 
16 #include <user_config.h>
17 
23 struct SerialBuffer {
24 public:
25  ~SerialBuffer()
26  {
27  delete buffer;
28  }
29 
30  size_t getSize()
31  {
32  return size;
33  }
34 
38  __forceinline size_t available()
39  {
40  int ret = writePos - readPos;
41  if(ret < 0) {
42  ret += size;
43  }
44  return ret;
45  }
46 
50  __forceinline size_t getFreeSpace()
51  {
52  if(buffer == nullptr) {
53  return 0;
54  }
55  int ret = readPos - writePos - 1;
56  if(ret < 0) {
57  ret += size;
58  }
59  return ret;
60  }
61 
62  __forceinline bool isEmpty()
63  {
64  return (buffer == nullptr) || (writePos == readPos);
65  }
66 
67  __forceinline bool isFull()
68  {
69  return getFreeSpace() == 0;
70  }
71 
75  __forceinline int peekChar()
76  {
77  return isEmpty() ? -1 : buffer[readPos];
78  }
79 
80  /*
81  * Take a peek at the last character written into the buffer
82  */
83  __forceinline int peekLastChar()
84  {
85  return isEmpty() ? -1 : buffer[getPrevPos(writePos)];
86  }
87 
88  __forceinline int readChar()
89  {
90  if(isEmpty()) {
91  return -1;
92  }
93 
94  uint8_t c = buffer[readPos];
95  readPos = getNextPos(readPos);
96  return c;
97  }
98 
99  __forceinline size_t writeChar(uint8_t c)
100  {
101  size_t nextPos = getNextPos(writePos);
102  if(nextPos == readPos) {
103  return 0;
104  }
105 
106  buffer[writePos] = c;
107  writePos = nextPos;
108  return 1;
109  }
110 
115  int find(uint8_t c);
116 
117  // Must be called with interrupts disabled
118  size_t resize(size_t newSize);
119 
120  void clear()
121  {
122  readPos = writePos = 0;
123  }
124 
129  __forceinline size_t getReadData(void*& data)
130  {
131  data = buffer + readPos;
132  auto wp = writePos; // Guard against ISR changing value
133  return (wp < readPos) ? size - readPos : wp - readPos;
134  }
135 
140  __forceinline void skipRead(size_t length)
141  {
142  readPos += length;
143  if(readPos == size) {
144  readPos = 0;
145  }
146  }
147 
148 private:
150  __forceinline size_t getNextPos(size_t pos)
151  {
152  size_t n = pos + 1;
153  return (n == size) ? 0 : n;
154  }
155 
157  __forceinline size_t getPrevPos(size_t pos)
158  {
159  return (pos != 0 ? pos : size) - 1;
160  }
161 
162 private:
163  size_t size = 0;
164  size_t readPos = 0;
165  size_t writePos = 0;
166  char* buffer = nullptr;
167 };
168 
169 #endif // _SMING_CORE_DATA_BUFFER_SERIAL_BUFFER_H_
int find(uint8_t c)
find a character in the buffer
__forceinline int peekChar()
see if there&#39;s anything in the buffer
Definition: SerialBuffer.h:75
__forceinline size_t available()
get number of bytes stored in the buffer
Definition: SerialBuffer.h:38
FIFO buffer used for both receive and transmit data.
Definition: SerialBuffer.h:23
__forceinline void skipRead(size_t length)
Skip a number of chars starting at the given read position.
Definition: SerialBuffer.h:140
__forceinline size_t getReadData(void *&data)
Access data directly within buffer.
Definition: SerialBuffer.h:129
__forceinline size_t getFreeSpace()
get number of bytes of space available in this buffer
Definition: SerialBuffer.h:50