forked from nanovna-v2/NanoVNA2-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_fifo.cpp
35 lines (32 loc) · 832 Bytes
/
stream_fifo.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
#include "stream_fifo.hpp"
void StreamFIFO::input(const uint8_t* s, int len) {
uint32_t wrRPos = rpos;
uint32_t wrWPos = wpos;
int bufferSizeMask = bufferSize - 1;
__sync_synchronize();
uint32_t spaceLeft = (wrRPos - wrWPos - 1) & bufferSizeMask;
if(len > int(spaceLeft)) len = spaceLeft;
uint32_t target = (wrWPos + len) & bufferSizeMask;
for(uint32_t i = wrWPos; i != target; i = ((i+1) & bufferSizeMask)) {
buffer[i] = *s;
s++;
}
__sync_synchronize();
wpos = target;
}
bool StreamFIFO::drain() {
uint32_t rdRPos = rpos;
uint32_t rdWPos = wpos;
__sync_synchronize();
if(rdRPos == rdWPos)
return false;
if(rdWPos > rdRPos) {
output(buffer + rdRPos, rdWPos - rdRPos);
} else {
output(buffer + rdRPos, bufferSize - rdRPos);
if(rdWPos > 0)
output(buffer, rdWPos);
}
rpos = rdWPos;
return true;
}