MySensors Library & Examples
2.3.2-62-ge298769
drivers
TinyGSM
TinyGsmFifo.h
1
#ifndef TinyGsmFifo_h
2
#define TinyGsmFifo_h
3
4
template
<
class
T,
unsigned
N>
5
6
class
TinyGsmFifo
7
{
8
public
:
9
10
TinyGsmFifo
()
11
{
12
clear();
13
}
14
15
void
clear()
16
{
17
_r = 0;
18
_w = 0;
19
}
20
21
// writing thread/context API
22
//-------------------------------------------------------------
23
24
bool
writeable(
void
)
25
{
26
return
free() > 0;
27
}
28
29
int
free(
void
)
30
{
31
int
s = _r - _w;
32
if
(s <= 0) {
33
s += N;
34
}
35
return
s - 1;
36
}
37
38
bool
put(
const
T& c)
39
{
40
int
i = _w;
41
int
j = i;
42
i = _inc(i);
43
if
(i == _r) {
// !writeable()
44
return
false
;
45
}
46
_b[j] = c;
47
_w = i;
48
return
true
;
49
}
50
51
int
put(
const
T* p,
int
n,
bool
t =
false
)
52
{
53
int
c = n;
54
while
(c) {
55
int
f;
56
while
((f = free()) == 0) {
// wait for space
57
if
(!t) {
58
return
n - c;
// no more space and not blocking
59
}
60
/* nothing / just wait */
;
61
}
62
// check free space
63
if
(c < f) {
64
f = c;
65
}
66
int
w = _w;
67
int
m = N - w;
68
// check wrap
69
if
(f > m) {
70
f = m;
71
}
72
memcpy(&_b[w], p, f);
73
_w = _inc(w, f);
74
c -= f;
75
p += f;
76
}
77
return
n - c;
78
}
79
80
// reading thread/context API
81
// --------------------------------------------------------
82
83
bool
readable(
void
)
84
{
85
return
(_r != _w);
86
}
87
88
size_t
size(
void
)
89
{
90
int
s = _w - _r;
91
if
(s < 0) {
92
s += N;
93
}
94
return
s;
95
}
96
97
bool
get(T* p)
98
{
99
int
r = _r;
100
if
(r == _w) {
// !readable()
101
return
false
;
102
}
103
*p = _b[r];
104
_r = _inc(r);
105
return
true
;
106
}
107
108
int
get(T* p,
int
n,
bool
t =
false
)
109
{
110
int
c = n;
111
while
(c) {
112
int
f;
113
for
(;;) {
// wait for data
114
f = size();
115
if
(f) {
116
break
;
// free space
117
}
118
if
(!t) {
119
return
n - c;
// no space and not blocking
120
}
121
/* nothing / just wait */
;
122
}
123
// check available data
124
if
(c < f) {
125
f = c;
126
}
127
int
r = _r;
128
int
m = N - r;
129
// check wrap
130
if
(f > m) {
131
f = m;
132
}
133
memcpy(p, &_b[r], f);
134
_r = _inc(r, f);
135
c -= f;
136
p += f;
137
}
138
return
n - c;
139
}
140
141
private
:
142
int
_inc(
int
i,
int
n = 1)
143
{
144
return
(i + n) % N;
145
}
146
147
T _b[N];
148
int
_w;
149
int
_r;
150
};
151
152
#endif
TinyGsmFifo
Definition:
TinyGsmFifo.h:6
Copyright (C) 2013-2019 Sensnology AB. Generated by
doxygen
1.8.17