From 4e40d5688ad175196878132e48b60e5fd93f7799 Mon Sep 17 00:00:00 2001 From: Jonne Kokkonen Date: Fri, 21 Jun 2024 18:21:44 +0300 Subject: [PATCH 1/2] support variable sized rectangle commands --- src/command.c | 49 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/src/command.c b/src/command.c index 6ac37bd..48b0ed8 100644 --- a/src/command.c +++ b/src/command.c @@ -13,7 +13,8 @@ static uint16_t decodeInt16(uint8_t *data, uint8_t start) { enum m8_command_bytes { draw_rectangle_command = 0xFE, - draw_rectangle_command_datalength = 12, + draw_rectangle_command_min_datalength = 5, + draw_rectangle_command_max_datalength = 12, draw_character_command = 0xFD, draw_character_command_datalength = 12, draw_oscilloscope_waveform_command = 0xFC, @@ -43,19 +44,51 @@ int process_command(uint8_t *data, uint32_t size) { case draw_rectangle_command: - if (size != draw_rectangle_command_datalength) { + if (size < draw_rectangle_command_min_datalength || + size > draw_rectangle_command_max_datalength) { SDL_LogError(SDL_LOG_CATEGORY_ERROR, - "Invalid draw rectangle packet: expected length %d, got %d", - draw_rectangle_command_datalength, size); + "Invalid draw rectangle packet: expected between %d and %d, got %d", + draw_rectangle_command_min_datalength, draw_rectangle_command_max_datalength, + size); dump_packet(size, recv_buf); return 0; break; } else { - struct draw_rectangle_command rectcmd = { - {decodeInt16(recv_buf, 1), decodeInt16(recv_buf, 3)}, // position x/y - {decodeInt16(recv_buf, 5), decodeInt16(recv_buf, 7)}, // size w/h - {recv_buf[9], recv_buf[10], recv_buf[11]}}; // color r/g/b + /* Support variable sized rectangle commands + If colors are omitted, the last drawn color should be used + If size is omitted, the size should be 1x1 pixels + So basically the command can be 5, 8, 9 or 12 bytes long */ + + static struct draw_rectangle_command rectcmd; + + rectcmd.pos.x = decodeInt16(recv_buf, 1); + rectcmd.pos.y = decodeInt16(recv_buf, 3); + + switch (size) { + case 5: + rectcmd.size.width = 1; + rectcmd.size.height = 1; + break; + case 8: + rectcmd.size.width = 1; + rectcmd.size.height = 1; + rectcmd.color.r = recv_buf[5]; + rectcmd.color.g = recv_buf[6]; + rectcmd.color.b = recv_buf[7]; + break; + case 9: + rectcmd.size.width = decodeInt16(recv_buf, 5); + rectcmd.size.height = decodeInt16(recv_buf, 7); + break; + default: + rectcmd.size.width = decodeInt16(recv_buf, 5); + rectcmd.size.height = decodeInt16(recv_buf, 7); + rectcmd.color.r = recv_buf[9]; + rectcmd.color.g = recv_buf[10]; + rectcmd.color.b = recv_buf[11]; + break; + } draw_rectangle(&rectcmd); return 1; From b45bb6f75d8159e6cbf517f16f50b2a7342dfd65 Mon Sep 17 00:00:00 2001 From: Jonne Kokkonen Date: Fri, 21 Jun 2024 18:23:41 +0300 Subject: [PATCH 2/2] fix a typo in rectangle command error msg --- src/command.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/command.c b/src/command.c index 48b0ed8..4aafec5 100644 --- a/src/command.c +++ b/src/command.c @@ -47,7 +47,7 @@ int process_command(uint8_t *data, uint32_t size) { if (size < draw_rectangle_command_min_datalength || size > draw_rectangle_command_max_datalength) { SDL_LogError(SDL_LOG_CATEGORY_ERROR, - "Invalid draw rectangle packet: expected between %d and %d, got %d", + "Invalid draw rectangle packet: expected length between %d and %d, got %d", draw_rectangle_command_min_datalength, draw_rectangle_command_max_datalength, size); dump_packet(size, recv_buf);