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 {