Skip to content

Commit e3e219d

Browse files
committed
improved support for bigger animations
1 parent 458e76c commit e3e219d

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

src/input/tcp.cpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,25 +127,46 @@ void TcpInput::disconnect(void *arg, AsyncClient *client) {
127127
* the queue handler
128128
*/
129129
void TcpInput::queue(void *parameter) {
130-
//TODO: if there are multiple packets (like for a GIF), get all packets and split them into 210 byte pieces. currently only the first packet gets split, which breaks bigger GIFs
130+
size_t previousSize = 0;
131+
uint8_t previousBuffer[210];
131132

132133
while (true) {
133134
data_packet_t dataPacket;
134135
if (xQueueReceive(parsePacketQueue, &dataPacket, (TickType_t)10) == pdPASS) {
135136
size_t off = 0;
136137
size_t maxImage = 274;
137138
size_t maxAnimation = 210;
138-
size_t len = dataPacket.size;
139+
size_t len = previousSize + dataPacket.size;
139140
uint8_t *buffer = dataPacket.data;
140141

141142
while (len > 0) {
142-
size_t max = buffer[0] == 0x01 && buffer[3] == 0x49 ? maxAnimation : maxImage;
143+
// prepare packet
144+
uint8_t *packet = previousSize > 0 ? previousBuffer : buffer;
145+
146+
// split packet into corresponding max size, if necessary
147+
size_t max = packet[0] == 0x01 && packet[3] == 0x49 ? maxAnimation : maxImage;
143148
size_t use = len > max ? max : len;
144-
TcpInput::parse(buffer, use);
149+
150+
// copy rest of the current buffer into previous buffer
151+
if (previousSize > 0) {
152+
memcpy(previousBuffer, buffer, use - previousSize);
153+
}
154+
155+
// parse and process packet
156+
TcpInput::parse(packet, use);
145157

146-
off += use;
147-
buffer += use;
158+
// move pointer and reduce open length
159+
off += use - previousSize;
160+
buffer += use - previousSize;
148161
len = dataPacket.size - off;
162+
previousSize = 0;
163+
164+
// copy rest into separate buffer instead of sending incomplete data, if there is another message in the queue
165+
if (len <= max && uxQueueMessagesWaiting(parsePacketQueue) > 0) {
166+
previousSize = len;
167+
memcpy(previousBuffer, buffer, len);
168+
break;
169+
}
149170
}
150171
}
151172
}

0 commit comments

Comments
 (0)