shithub: m8c

Download patch

ref: 80bb5c09241908ed0bc7df4635981c9057f6fe07
parent: 4fb87c2464ef0a061ec884b132d6875bfd499cd5
author: Jonne Kokkonen <jonne.kokkonen@gmail.com>
date: Thu Jun 2 09:04:50 EDT 2022

fix glitchy device reinsert

--- a/fx_cube.c
+++ b/fx_cube.c
@@ -5,7 +5,7 @@
 #define target_height 240
 static SDL_Texture *texture_cube;
 static SDL_Texture *texture_text;
-static SDL_Texture *texture_gradient;
+//static SDL_Texture *texture_gradient;
 static SDL_Renderer *fx_renderer;
 static SDL_Color line_color;
 
@@ -64,7 +64,7 @@
       SDL_CreateTexture(fx_renderer, SDL_PIXELFORMAT_ARGB8888,
                         SDL_TEXTUREACCESS_TARGET, target_width, target_height);
 
-  SDL_Texture *og_target = SDL_GetRenderTarget(fx_renderer);                        
+  //SDL_Texture *og_target = SDL_GetRenderTarget(fx_renderer);                        
 
   SDL_SetRenderTarget(fx_renderer, texture_text);
 
@@ -73,7 +73,7 @@
 
   /* Create a texture for making a linear gradient. Scaling settings make the 2
      pixels into a gradient. This requires OpenGL or Direct3d though. */
-  SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
+  /* SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
   texture_gradient = SDL_CreateTexture(fx_renderer, SDL_PIXELFORMAT_ARGB8888,
                                        SDL_TEXTUREACCESS_TARGET, 1, 2);
   SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0");
@@ -85,7 +85,7 @@
   SDL_SetRenderDrawColor(fx_renderer, 0, 0, 0, SDL_ALPHA_TRANSPARENT);
   SDL_RenderDrawPoint(fx_renderer, 0, 0);
   SDL_SetRenderTarget(fx_renderer, og_target);
-
+  */
   // Initialize default nodes
   SDL_memcpy(nodes, default_nodes, sizeof(default_nodes));
 
@@ -93,13 +93,13 @@
   rotate_cube(M_PI / 4, SDL_atan(SDL_sqrt(2)));
 
   SDL_SetTextureBlendMode(texture_cube, SDL_BLENDMODE_BLEND);
-  SDL_SetTextureBlendMode(texture_gradient, SDL_BLENDMODE_BLEND);
+  //SDL_SetTextureBlendMode(texture_gradient, SDL_BLENDMODE_BLEND);
   SDL_SetTextureBlendMode(texture_text, SDL_BLENDMODE_BLEND);
 }
 
 void fx_cube_destroy() {
   SDL_DestroyTexture(texture_cube);
-  SDL_DestroyTexture(texture_gradient);
+  //SDL_DestroyTexture(texture_gradient);
   SDL_DestroyTexture(texture_text);
 }
 
@@ -126,7 +126,7 @@
     points[points_counter++] = (SDL_Point){p2[0] + center_x, p2[1] + center_y};
   }
 
-  SDL_RenderCopy(fx_renderer, texture_gradient, NULL, NULL);
+  //SDL_RenderCopy(fx_renderer, texture_gradient, NULL, NULL);
   SDL_RenderCopy(fx_renderer, texture_text, NULL, NULL);
   SDL_SetRenderDrawColor(fx_renderer, line_color.r, line_color.g, line_color.b,
                          line_color.a);
--- a/main.c
+++ b/main.c
@@ -1,6 +1,10 @@
 // Copyright 2021 Jonne Kokkonen
 // Released under the MIT licence, https://opensource.org/licenses/MIT
 
+/* Uncomment this line to enable debug messages or call make with `make
+   CFLAGS=-DDEBUG_MSG` */
+// #define DEBUG_MSG
+
 #include <SDL.h>
 #include <libserialport.h>
 #include <signal.h>
@@ -69,8 +73,6 @@
   if (initialize_sdl(conf.init_fullscreen, conf.init_use_gpu) == -1)
     run = QUIT;
 
-/* Uncomment this line to enable debug messages or call make with `make
-   CFLAGS=-DDEBUG_MSG` #define DEBUG_MSG */
 #ifdef DEBUG_MSG
   SDL_LogSetAllPriority(SDL_LOG_PRIORITY_DEBUG);
 #endif
@@ -78,8 +80,14 @@
   // main loop begin
   do {
     port = init_serial(1);
-    if (port != NULL && enable_and_reset_display(port)) {
-      run = RUN;
+    if (port != NULL) {
+      int result;
+      result = enable_and_reset_display(port);
+      if (result == 1) {
+        run = RUN;
+      } else {
+        run = QUIT;
+      }
     }
 
     // wait until device is connected
@@ -94,7 +102,7 @@
         // get current inputs
         input_msg_s input = get_input_msg(&conf);
         if (input.type == special && input.value == msg_quit) {
-          run = 0;
+          run = QUIT;
         }
 
         if (SDL_GetTicks() - ticks_update_screen > 16) {
@@ -104,16 +112,19 @@
         }
 
         // Poll for M8 device every second
-        if (!port && SDL_GetTicks() - ticks_poll_device > 1000) {
+        if (!port && (SDL_GetTicks() - ticks_poll_device > 1000)) {
           ticks_poll_device = SDL_GetTicks();
           port = init_serial(0);
           if (run == WAIT_FOR_DEVICE && port != NULL) {
+            int result = enable_and_reset_display(port);
+            SDL_Delay(100);
             // Device was found; enable display and proceed to the main loop
-            if (enable_and_reset_display(port)) {
+            if (result == 1) {
               run = RUN;
               screensaver_destroy();
             } else {
               run = QUIT;
+              screensaver_destroy();
             }
           }
         }
@@ -218,7 +229,7 @@
       render_screen();
       SDL_Delay(conf.idle_ms);
     }
-  } while (run);
+  } while (run > QUIT);
   // main loop end
 
   // exit, clean up
--- a/write.c
+++ b/write.c
@@ -1,6 +1,7 @@
 // Copyright 2021 Jonne Kokkonen
 // Released under the MIT licence, https://opensource.org/licenses/MIT
 
+#include "SDL_timer.h"
 #include <SDL_log.h>
 #include <libserialport.h>
 #include <stdint.h>
@@ -7,7 +8,7 @@
 #include <stdio.h>
 #include <unistd.h>
 
-int reset_display(struct sp_port *port){
+int reset_display(struct sp_port *port) {
   SDL_Log("Reset display\n");
   uint8_t buf[2];
   int result;
@@ -14,10 +15,11 @@
 
   buf[0] = 0x45;
   buf[1] = 0x52;
-    
+
   result = sp_blocking_write(port, buf, 2, 5);
   if (result != 2) {
-    SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error resetting M8 display, code %d", result);
+    SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error resetting M8 display, code %d",
+                 result);
     return 0;
   }
   return 1;
@@ -32,15 +34,17 @@
   buf[0] = 0x44;
   result = sp_blocking_write(port, buf, 1, 5);
   if (result != 1) {
-    SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error enabling M8 display, code %d", result);
+    SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error enabling M8 display, code %d",
+                 result);
     return 0;
   }
 
-  usleep(500);
-  if (!reset_display(port)){
+  SDL_Delay(5);
+  result = reset_display(port);
+  if (result == 1)
+    return 1;
+  else
     return 0;
-  }
-  return 1;
 }
 
 int disconnect(struct sp_port *port) {
@@ -51,7 +55,8 @@
 
   result = sp_blocking_write(port, buf, 1, 5);
   if (result != 1) {
-    SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error sending disconnect, code %d", result);
+    SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error sending disconnect, code %d",
+                 result);
     return -1;
   }
   return 1;
@@ -58,27 +63,28 @@
 }
 
 int send_msg_controller(struct sp_port *port, uint8_t input) {
-  char buf[2] = {'C',input};
+  char buf[2] = {'C', input};
   size_t nbytes = 2;
   int result;
   result = sp_blocking_write(port, buf, nbytes, 5);
   if (result != nbytes) {
-    SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error sending input, code %d", result);
+    SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error sending input, code %d",
+                 result);
     return -1;
   }
   return 1;
-
 }
 
 int send_msg_keyjazz(struct sp_port *port, uint8_t note, uint8_t velocity) {
   if (velocity > 0x7F)
     velocity = 0x7F;
-  char buf[3] = {'K',note,velocity};
+  char buf[3] = {'K', note, velocity};
   size_t nbytes = 3;
   int result;
-  result = sp_blocking_write(port, buf, nbytes,5);
+  result = sp_blocking_write(port, buf, nbytes, 5);
   if (result != nbytes) {
-    SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error sending keyjazz, code %d", result);
+    SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error sending keyjazz, code %d",
+                 result);
     return -1;
   }
 
--