add LineFlip_buffer

This commit is contained in:
MReenen 2023-11-09 21:06:46 +01:00
parent 54b8a5a240
commit a9cc83427c

View File

@ -13,55 +13,93 @@ uint8_t rxBuffer_readPointer = 0;
class FlipFlop_Buffer class FlipFlop_Buffer
{ {
void *buffer_flip; private:
void *buffer_flop; void *_buffer_flip;
void *_buffer_flop;
size_t _size; size_t _size;
void *active_buffer; void *_active_buffer;
void *_active_buffer_end;
void *_write_head; void *_write_head;
public:
Buffer(size_t size) Buffer(size_t size)
{ {
this->_size = size; this->_size = size;
this->buffer_flip = maloc(size); this->_buffer_flip = maloc(size);
this->buffer_flop = maloc(size); this->_buffer_flop = maloc(size);
this->active_buffer = this->buffer_flip; this->_active_buffer = this->_buffer_flip;
this->_active_buffer_end = this->_buffer_flip + this->_size;
} }
~Buffer() ~Buffer()
{ {
free(this->buffer_flip); free(this->_buffer_flip);
free(this->buffer_flop); free(this->_buffer_flop);
} }
bool put(void *data, size_t len) bool put(void *data, size_t len)
{ {
bool sucsess; bool sucsess;
if (this->_write_head + len > this->buffer_flip + this->size) if (this->_write_head + len > this->_active_buffer_end)
{ {
memcpy(this->_write_head, data, this->_write_head - this->buffer_flip); memcpy(this->_write_head, data, this->_active_buffer_end - this->_write_head);
this->_write_head = this._active_buffer_end;
sucsess = false; sucsess = false;
} }
else else
{ {
memcpy(this->write_p, data, len); memcpy(this->write_p, data, len);
this->_write_head += len;
sucsess = true; sucsess = true;
} }
return sucsess; return sucsess;
} }
void* swich_buffer() void swich_buffer()
{ {
if (this->active_buffer == this->buffer_flip) if (this->_active_buffer == this->_buffer_flip)
{ {
this->active_buffer == this->buffer_flip; this->_active_buffer = this->_buffer_flop;
} }
else else
{ {
this->active_buffer == this->buffer_flop; this->_active_buffer == this->_buffer_flip;
} }
return this->active_buffer; this->_active_buffer_end = this._active_buffer + this._size;
return;
}
}
class LineFlip_Buffer : FlipFlop_Buffer
{
public:
bool overrite put(void *data, size_t len)
{
bool justSwiched = false;
bool sucsess = true;
for (int i = 0; i < len; i++)
{
if (&(data + i) == '\n' || &(data + i) == '\r')
{
if (!justSwiched)
{
this->swich_buffer();
justSwiched = true;
}
}
else
{
if(!FlipFlop_Buffer::put(data + i, 1))
{
sucsess = false;
}
justSwiched = false;
}
}
return sucsess
} }
} }