add LineFlip_buffer

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

View File

@ -13,56 +13,94 @@ uint8_t rxBuffer_readPointer = 0;
class FlipFlop_Buffer
{
void *buffer_flip;
void *buffer_flop;
size_t _size;
private:
void *_buffer_flip;
void *_buffer_flop;
size_t _size;
void *active_buffer;
void *_active_buffer;
void *_active_buffer_end;
void *_write_head;
void *_write_head;
Buffer(size_t size)
{
this->_size = size;
this->buffer_flip = maloc(size);
this->buffer_flop = maloc(size);
public:
this->active_buffer = this->buffer_flip;
}
~Buffer()
{
free(this->buffer_flip);
free(this->buffer_flop);
}
bool put(void *data, size_t len)
{
bool sucsess;
if (this->_write_head + len > this->buffer_flip + this->size)
Buffer(size_t size)
{
memcpy(this->_write_head, data, this->_write_head - this->buffer_flip);
sucsess = false;
}
else
{
memcpy(this->write_p, data, len);
sucsess = true;
}
return sucsess;
}
this->_size = size;
this->_buffer_flip = maloc(size);
this->_buffer_flop = maloc(size);
void* swich_buffer()
{
if (this->active_buffer == this->buffer_flip)
{
this->active_buffer == this->buffer_flip;
this->_active_buffer = this->_buffer_flip;
this->_active_buffer_end = this->_buffer_flip + this->_size;
}
else
~Buffer()
{
this->active_buffer == this->buffer_flop;
free(this->_buffer_flip);
free(this->_buffer_flop);
}
bool put(void *data, size_t len)
{
bool sucsess;
if (this->_write_head + len > this->_active_buffer_end)
{
memcpy(this->_write_head, data, this->_active_buffer_end - this->_write_head);
this->_write_head = this._active_buffer_end;
sucsess = false;
}
else
{
memcpy(this->write_p, data, len);
this->_write_head += len;
sucsess = true;
}
return sucsess;
}
void swich_buffer()
{
if (this->_active_buffer == this->_buffer_flip)
{
this->_active_buffer = this->_buffer_flop;
}
else
{
this->_active_buffer == this->_buffer_flip;
}
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
}
return this->active_buffer;
}
}
void setup() {