37 uint16_t SharedBuffer::head = 0;
38 uint16_t SharedBuffer::usedSpace = 0;
52 uint16_t SharedBuffer::writeAt(uint16_t index, uint16_t len,
const byte* data)
55 if (len <= SHARED_BUFFER_CAPACITY - index)
59 if (index == SHARED_BUFFER_CAPACITY)
67 uint16_t size_1 = SHARED_BUFFER_CAPACITY - index;
70 uint16_t size_2 = len - size_1;
81 uint16_t SharedBuffer::append(uint16_t len,
const byte* data)
83 if (len == 0)
return 0;
85 uint16_t bytes_to_write = min(len, SHARED_BUFFER_CAPACITY - usedSpace);
87 head = writeAt(head,bytes_to_write,data);
89 usedSpace += bytes_to_write;
91 return bytes_to_write;
94 uint16_t SharedBuffer::readAt(uint16_t index, uint16_t len, byte* data)
97 if (len <= SHARED_BUFFER_CAPACITY - index)
101 if (index == SHARED_BUFFER_CAPACITY) index = 0;
106 uint16_t size_1 = SHARED_BUFFER_CAPACITY - index;
109 uint16_t size_2 = len - size_1;
128 int16_t availableSpace = SHARED_BUFFER_CAPACITY - usedSpace -
sizeof(BufferHeader);
129 if (availableSpace < 0)
132 len = min(len, availableSpace);
141 header.nextIndex = 0xFFFF;
144 append(
sizeof(header), (byte*)&header);
148 if (lastWritten != 0xFFFF)
149 writeAt(lastWritten,
sizeof(h), (byte*)&h);
169 readAt(nextRead,
sizeof(header), (byte*) &header);
171 nextRead = header.nextIndex;
174 lastWritten = 0xFFFF;
183 uint16_t d = (s->nextRead < head) ? head - s->nextRead : SHARED_BUFFER_CAPACITY - s->nextRead + head;
190 ACTRACE(
"release(). head=%d, usedSpace=%d", head,usedSpace);
191 return header.length;
218 uint16_t txPtr = TXSTART_INIT_DATA + dstOffset;
219 bool startOdd = txPtr & 1;
221 uint16_t n = nextRead;
222 while (n!=0xFFFF && count>0)
225 readAt(n,
sizeof(header), (byte*)&header);
226 if ((txPtr + header.length > TXSTOP_INIT + 1))
229 uint16_t src = SHARED_BUFFER_INIT + n +
sizeof(header);
231 if (src >= SHARED_BUFFER_INIT + SHARED_BUFFER_CAPACITY)
232 src -= SHARED_BUFFER_CAPACITY;
234 uint16_t len = header.length;
235 bool odd = startOdd ^ (txPtr & 1);
237 if (src + len > SHARED_BUFFER_INIT + SHARED_BUFFER_CAPACITY)
239 len = SHARED_BUFFER_INIT + SHARED_BUFFER_CAPACITY - src;
242 len = header.length - len;
243 src = SHARED_BUFFER_INIT;
251 n = header.nextIndex;
255 return txPtr - (TXSTART_INIT_DATA + dstOffset);
265 return nextRead == 0xFFFF;
uint16_t release()
Releases one fragment of data
static void moveMem(uint16_t dest, uint16_t src, uint16_t len)
static uint16_t calc(uint16_t len, const uint8_t *data)
Calculates the checksum of the specified buffer in memory
void remove(ListItem *item)
void flush()
Releases all data in this buffer, freeing up space.
static uint16_t add(uint16_t a, uint16_t b)
Adds two checksums, taking carry into account.
uint16_t write(uint16_t len, const byte *data)
Writes a fragment to the shared buffer
ACROSS_MODULE("SharedBuffer")
Implements a "shared" circular buffer using spare ENC28J60 memory.
bool isEmpty()
Determines whether this buffer is empty
static void readBuf(uint16_t src, uint16_t len, byte *data)
static void writeBuf(uint16_t dst, uint16_t len, const byte *data)
uint16_t fillTxBuffer(uint16_t dstOffset, uint16_t &checksum, uint16_t count=0xFFFF)
Copies up to count fragments to the transmit buffer via DMA, concatenating them starting at given off...