From 6a71074b4b8998dac41022fa4619a54659732304 Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Mon, 30 Dec 2019 12:19:00 +0100 Subject: [PATCH] UDPProto: added "ack request" + response packets The ACK request packet contains a sequence number which is echoed back to the client in a simple UDP packet. By evaluating the difference between sent and received sequence number, the client can control how much data is buffered on the way to the ESP. --- include/UDPProto.h | 3 ++- src/UDPProto.cpp | 27 ++++++++++++++++----------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/include/UDPProto.h b/include/UDPProto.h index 855a800..14fbf18 100644 --- a/include/UDPProto.h +++ b/include/UDPProto.h @@ -11,7 +11,8 @@ public: SET_COLOUR = 0, FADE_COLOUR = 1, ADD_COLOUR = 2, - SET_FADESTEP = 3 + SET_FADESTEP = 3, + ACK_REQUEST = 255 }; UDPProto(Fader *fader); diff --git a/src/UDPProto.cpp b/src/UDPProto.cpp index 4b83156..a7ce39d 100644 --- a/src/UDPProto.cpp +++ b/src/UDPProto.cpp @@ -27,12 +27,7 @@ bool UDPProto::check(void) { int packetSize = m_udpServer.parsePacket(); if(packetSize) { - byte buf[1024]; - int len; - while((len = m_udpServer.read(buf, 1024)) == 1024) { - // do nothing - } - m_udpServer.endPacket(); + m_udpServer.flush(); return true; } else { return false; @@ -45,6 +40,8 @@ bool UDPProto::loop(void) byte action, strip, module, r, g, b, w; int len; + int32_t seq = -1; + int packetSize = m_udpServer.parsePacket(); if(packetSize) { @@ -87,16 +84,24 @@ bool UDPProto::loop(void) m_fader->set_fadestep(r); // red channel contains the fadestep in this case break; + case ACK_REQUEST: + seq = ((int32_t)r) << 8 | (int32_t)g; + default: break; } } // send a reply, to the IP address and port that sent us the packet we received - /* - ws2801UDP.beginPacket(ws2801UDP.remoteIP(), ws2801UDP.remotePort()); - ws2801UDP.write("OK\n"); - */ - m_udpServer.endPacket(); + if(seq >= 0) { + uint8_t data[2]; + data[0] = (seq >> 8) & 0xFF; + data[1] = seq & 0xFF; + m_udpServer.beginPacket(m_udpServer.remoteIP(), m_udpServer.remotePort()); + m_udpServer.write(data, 2); + m_udpServer.endPacket(); + } + + m_udpServer.flush(); return true; // processed a packet } else {