shithub: m8c

Download patch

ref: 454a4ade946208957b1e3adc8920bd02829d65b8
parent: de00892f333cd27b2a42b53266d9940365bc7024
author: Jason Porritt <jason.porritt@atomicobject.com>
date: Wed May 18 17:15:09 EDT 2022

Read all available serial data each loop.

--- a/main.c
+++ b/main.c
@@ -106,26 +106,35 @@
       }
     }
 
-    // read serial port
-    int bytes_read = sp_blocking_read(port, serial_buf, serial_read_size, 3);
-    if (bytes_read < 0) {
-      SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, "Error %d reading serial. \n",
-                      (int)bytes_read);
-      run = 0;
-    }
-    if (bytes_read > 0) {
-      for (int i = 0; i < bytes_read; i++) {
-        uint8_t rx = serial_buf[i];
-        // process the incoming bytes into commands and draw them
-        int n = slip_read_byte(&slip, rx);
-        if (n != SLIP_NO_ERROR) {
-          if (n == SLIP_ERROR_INVALID_PACKET) {
-            reset_display(port);
-          } else {
-            SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SLIP error %d\n", n);
+
+    int total_received_bytes = 0;
+    while (1) {
+      // read serial port
+      int bytes_read = sp_blocking_read(port, serial_buf, serial_read_size, 1);
+      if (bytes_read < 0) {
+        SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, "Error %d reading serial. \n",
+                        (int)bytes_read);
+        run = 0;
+        break;
+      } else if (bytes_read > 0) {
+        total_received_bytes += bytes_read;
+        for (int i = 0; i < bytes_read; i++) {
+          uint8_t rx = serial_buf[i];
+          // process the incoming bytes into commands and draw them
+          int n = slip_read_byte(&slip, rx);
+          if (n != SLIP_NO_ERROR) {
+            if (n == SLIP_ERROR_INVALID_PACKET) {
+              reset_display(port);
+            } else {
+              SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SLIP error %d\n", n);
+            }
           }
         }
+      } else {
+        break;
       }
+    }
+    if (total_received_bytes > 0) {
       render_screen();
     }
   }
--- a/render.c
+++ b/render.c
@@ -13,7 +13,9 @@
 SDL_Renderer *rend;
 SDL_Texture *maintexture;
 SDL_Color background_color = (SDL_Color){0, 0, 0, 0};
+static uint32_t ticks;
 
+
 static uint32_t ticks_fps;
 static int fps;
 uint8_t fullscreen = 0;
@@ -20,6 +22,7 @@
 
 // Initializes SDL and creates a renderer and required surfaces
 int initialize_sdl(int init_fullscreen, int init_use_gpu) {
+  ticks = SDL_GetTicks();
 
   const int window_width = 640;  // SDL window width
   const int window_height = 480; // SDL window height
@@ -175,7 +178,8 @@
 }
 
 void render_screen() {
-
+  if (SDL_GetTicks() - ticks > 14) {
+    ticks = SDL_GetTicks();
     SDL_SetRenderTarget(rend, NULL);
     SDL_SetRenderDrawColor(rend, 0, 0, 0, 0);
     SDL_RenderClear(rend);
@@ -190,4 +194,5 @@
       SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "%.1f fps\n", (float)fps / 5);
       fps = 0;
     }
+  }
 }
--