rework esp code en update py code
This commit is contained in:
@@ -8,100 +8,13 @@
|
||||
WiFiUDP UDP;
|
||||
#define RX_BUFFER_SIZE 255
|
||||
char rxBuffer[RX_BUFFER_SIZE];
|
||||
uint8_t rxBuffer_writePointer = 0;
|
||||
char lineBuffer[RX_BUFFER_SIZE];
|
||||
uint8_t rxBuffer_readPointer = 0;
|
||||
uint8_t rxBuffer_lineStart = 0;
|
||||
uint8_t rxBuffer_lineEnd = 0;
|
||||
bool unProcessedLineAvailable = false;
|
||||
|
||||
class FlipFlop_Buffer
|
||||
{
|
||||
private:
|
||||
void *_buffer_flip;
|
||||
void *_buffer_flop;
|
||||
size_t _size;
|
||||
|
||||
void *_active_buffer;
|
||||
void *_active_buffer_end;
|
||||
|
||||
void *_write_head;
|
||||
|
||||
public:
|
||||
|
||||
Buffer(size_t size)
|
||||
{
|
||||
this->_size = size;
|
||||
this->_buffer_flip = maloc(size);
|
||||
this->_buffer_flop = maloc(size);
|
||||
|
||||
this->_active_buffer = this->_buffer_flip;
|
||||
this->_active_buffer_end = this->_buffer_flip + this->_size;
|
||||
}
|
||||
~Buffer()
|
||||
{
|
||||
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
|
||||
}
|
||||
}
|
||||
bool rxBuffer_overflow = false;
|
||||
|
||||
void setup() {
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
||||
@@ -114,18 +27,35 @@ void setup() {
|
||||
UDP.begin(UDP_PORT);
|
||||
}
|
||||
|
||||
void processLine()
|
||||
{
|
||||
rxBuffer[rxBuffer_lineEnd] = 0;
|
||||
// what are the commands
|
||||
}
|
||||
|
||||
int readNextLine()
|
||||
{
|
||||
while (rxBuffer_readPointer < RX_BUFFER_SIZE)
|
||||
{
|
||||
if (rxBuffer[rxBuffer_readPointer] == '\n')
|
||||
{
|
||||
rxBuffer_lineEnd = rxBuffer_readPointer;
|
||||
processLine();
|
||||
}
|
||||
|
||||
rxBuffer_readPointer++;
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
int packetSize = UDP.parsePacket();
|
||||
if (packetSize) {
|
||||
int len = UDP.read(&rxBuffer[rxBuffer_writePointer], RX_BUFFER_SIZE-rxBuffer_writePointer);
|
||||
if (rxBuffer_writePointer >= RX_BUFFER_SIZE)
|
||||
{
|
||||
rxBuffer_writePointer -= RX_BUFFER_SIZE;
|
||||
}
|
||||
}
|
||||
int len = UDP.read(&rxBuffer[0], RX_BUFFER_SIZE);
|
||||
|
||||
if (rxBuffer_writePointer != rxBuffer_readPointer)
|
||||
{
|
||||
rxBuffer_readPointer = 0;
|
||||
rxBuffer_lineStart = 0;
|
||||
rxBuffer_lineEnd = 0;
|
||||
|
||||
readNextLine();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user