-
Notifications
You must be signed in to change notification settings - Fork 8
/
WiFiTermBuffer.cpp
70 lines (62 loc) · 1.32 KB
/
WiFiTermBuffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "WiFiTermBuffer.h"
WiFiTermBuffer::WiFiTermBuffer(size_t size)
{
_size = size;
_buffer = (char *) malloc(_size);
reset();
}
WiFiTermBuffer::~WiFiTermBuffer()
{
free(_buffer);
}
void WiFiTermBuffer::reset()
{
_ix_read = 0;
_ix_write = 0;
_available = 0;
_previous = 0;
}
size_t WiFiTermBuffer::write(uint8_t c)
{
_buffer[_ix_write++] = c;
if (_ix_write == _size) _ix_write = 0;
if (_available < _size) _available++;
if (_available + _previous > _size) _previous = _size - _available;
_ix_read = (_ix_write >= _available) ? 0 : _size;
_ix_read += _ix_write;
_ix_read -= _available;
return 1;
}
int WiFiTermBuffer::read()
{
if (_available == 0) return -1;
char c = _buffer[_ix_read++];
if (_ix_read == _size) _ix_read = 0;
_available--;
_previous++;
return c;
}
void WiFiTermBuffer::flush()
{
_ix_read = _ix_write;
_previous += _available;
_available = 0;
}
int WiFiTermBuffer::peek() const
{
if (_available == 0) return -1;
char c = _buffer[_ix_read];
return c;
}
void WiFiTermBuffer::peekPrevious(char *to) const
{
size_t len = _previous;
size_t ix = (_ix_read >= _previous) ? 0 : _size;
ix += _ix_read;
ix -= _previous;
while (len--)
{
*to++ = _buffer[ix++];
if (ix == _size) ix = 0;
}
}