shithub: m8c

Download patch

ref: 079034a7c8395f3c59bc6a00d3072d9f478337ce
parent: 6c6b32b7c6a8a4b8e99984dd1e7edbb3a21deae0
author: Bloop Click <103341015+bloopclick@users.noreply.github.com>
date: Mon May 23 18:11:06 EDT 2022

Improve cpu usage

- Makes nonblocking calls to serial read
- Moves main thread wait to an SDL_Delay() call
- Adds an INI parameter to control SDL_Delay() argument/ thread sleep time

This is not a perfect solution but lower end and battery powered systems may
get a lot of mileage out of it. On higher-end systems with large thread wakeup
periods, there may be an impact to framerate, limiting it below 60.

--- a/config.c
+++ b/config.c
@@ -23,6 +23,7 @@
 
   c.init_fullscreen = 0;  // default fullscreen state at load
   c.init_use_gpu = 1;     // default to use hardware acceleration
+  c.idle_ms = 10;         // default to high performance
 
   c.key_up = SDL_SCANCODE_UP;
   c.key_left = SDL_SCANCODE_LEFT;
@@ -78,6 +79,7 @@
           conf->init_fullscreen ? "true" : "false");
   sprintf(ini_values[initPointer++], "use_gpu=%s\n",
           conf->init_use_gpu ? "true" : "false");
+  sprintf(ini_values[initPointer++], "idle_ms=%d\n", conf->idle_ms);
   sprintf(ini_values[initPointer++], "[keyboard]\n");
   sprintf(ini_values[initPointer++], "key_up=%d\n", conf->key_up);
   sprintf(ini_values[initPointer++], "key_left=%d\n", conf->key_left);
@@ -164,6 +166,7 @@
 void read_graphics_config(ini_t *ini, config_params_s *conf) {
   const char *param_fs = ini_get(ini, "graphics", "fullscreen");
   const char *param_gpu = ini_get(ini, "graphics", "use_gpu");
+  const char *idle_ms = ini_get(ini, "graphics", "idle_ms");
 
   if (strcmpci(param_fs, "true") == 0) {
     conf->init_fullscreen = 1;
@@ -176,6 +179,9 @@
     } else
       conf->init_use_gpu = 0;
   }
+
+  if (idle_ms)
+    conf->idle_ms = SDL_atoi(idle_ms);
 
 }
 
--- a/config.h
+++ b/config.h
@@ -10,6 +10,7 @@
   char *filename;
   int init_fullscreen;
   int init_use_gpu;
+  int idle_ms;
 
   int key_up;
   int key_left;
--- a/main.c
+++ b/main.c
@@ -35,6 +35,8 @@
   // TODO: take cli parameter to override default configfile location
   read_config(&conf);
 
+  SDL_Log("idle_ms=%d", conf.idle_ms);
+
   // allocate memory for serial buffer
   uint8_t *serial_buf = malloc(serial_read_size);
 
@@ -115,7 +117,8 @@
 
     while (1) {
       // read serial port
-      int bytes_read = sp_blocking_read(port, serial_buf, serial_read_size, 1);
+      //int bytes_read = sp_blocking_read(port, serial_buf, serial_read_size, 1);
+      int bytes_read = sp_nonblocking_read(port, serial_buf, serial_read_size);
       if (bytes_read < 0) {
         SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, "Error %d reading serial. \n",
                         (int)bytes_read);
@@ -140,6 +143,7 @@
       }
     }
     render_screen();
+    SDL_Delay(conf.idle_ms);
   }
 
   // exit, clean up
--- a/render.c
+++ b/render.c
@@ -203,7 +203,7 @@
 }
 
 void render_screen() {
-  if (dirty && (SDL_GetTicks() - ticks > 14)) {
+  if (dirty) {
     dirty = 0;
     ticks = SDL_GetTicks();
     SDL_SetRenderTarget(rend, NULL);
--