shithub: m8c

Download patch

ref: 4e40d5688ad175196878132e48b60e5fd93f7799
parent: b7ca6e794ffc102046aa76e101484d3a4ddb30aa
author: Jonne Kokkonen <jonne.kokkonen@gmail.com>
date: Fri Jun 21 14:21:44 EDT 2024

support variable sized rectangle commands

--- a/src/command.c
+++ b/src/command.c
@@ -13,7 +13,8 @@
 
 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 @@
 
   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;
--