shithub: m8c

Download patch

ref: 7f74d42da1d0defad619ee88d40596ba2493238a
parent: a2a575b829eb5a0326995cacfbbef1bc8b53d31e
author: Jonne Kokkonen <jonne.kokkonen@gmail.com>
date: Mon Aug 19 18:19:54 EDT 2024

Clean up unreachable code and redundant else keywords

--- a/src/audio.c
+++ b/src/audio.c
@@ -40,48 +40,47 @@
   if (devcount_in < 1) {
     SDL_Log("No audio capture devices, SDL Error: %s", SDL_GetError());
     return 0;
-  } else {
-    for (i = 0; i < devcount_in; i++) {
-      // Check if input device exists before doing anything else
-      SDL_LogDebug(SDL_LOG_CATEGORY_AUDIO, "%s", SDL_GetAudioDeviceName(i, SDL_TRUE));
-      if (SDL_strstr(SDL_GetAudioDeviceName(i, SDL_TRUE), "M8") != NULL) {
-        SDL_Log("M8 Audio Input device found: %s", SDL_GetAudioDeviceName(i, SDL_TRUE));
-        m8_device_id = i;
-      }
+  }
+  for (i = 0; i < devcount_in; i++) {
+    // Check if input device exists before doing anything else
+    SDL_LogDebug(SDL_LOG_CATEGORY_AUDIO, "%s", SDL_GetAudioDeviceName(i, SDL_TRUE));
+    if (SDL_strstr(SDL_GetAudioDeviceName(i, SDL_TRUE), "M8") != NULL) {
+      SDL_Log("M8 Audio Input device found: %s", SDL_GetAudioDeviceName(i, SDL_TRUE));
+      m8_device_id = i;
     }
-    if (m8_device_id == -1) {
-      // forget about it
-      SDL_Log("Cannot find M8 audio input device");
-      return 0;
-    }
+  }
+  if (m8_device_id == -1) {
+    // forget about it
+    SDL_Log("Cannot find M8 audio input device");
+    return 0;
+  }
 
-    SDL_AudioSpec want_in, have_in, want_out, have_out;
+  SDL_AudioSpec want_in, have_in, want_out, have_out;
 
-    // Open output device first to avoid possible Directsound errors
-    SDL_zero(want_out);
-    want_out.freq = 44100;
-    want_out.format = AUDIO_S16;
-    want_out.channels = 2;
-    want_out.samples = audio_buffer_size;
-    devid_out = SDL_OpenAudioDevice(output_device_name, 0, &want_out, &have_out,
-                                    SDL_AUDIO_ALLOW_ANY_CHANGE);
-    if (devid_out == 0) {
-      SDL_Log("Failed to open output: %s", SDL_GetError());
-      return 0;
-    }
+  // Open output device first to avoid possible Directsound errors
+  SDL_zero(want_out);
+  want_out.freq = 44100;
+  want_out.format = AUDIO_S16;
+  want_out.channels = 2;
+  want_out.samples = audio_buffer_size;
+  devid_out =
+      SDL_OpenAudioDevice(output_device_name, 0, &want_out, &have_out, SDL_AUDIO_ALLOW_ANY_CHANGE);
+  if (devid_out == 0) {
+    SDL_Log("Failed to open output: %s", SDL_GetError());
+    return 0;
+  }
 
-    SDL_zero(want_in);
-    want_in.freq = 44100;
-    want_in.format = AUDIO_S16;
-    want_in.channels = 2;
-    want_in.samples = audio_buffer_size;
-    want_in.callback = audio_cb_in;
-    devid_in = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(m8_device_id, SDL_TRUE), SDL_TRUE,
-                                   &want_in, &have_in, SDL_AUDIO_ALLOW_ANY_CHANGE);
-    if (devid_in == 0) {
-      SDL_Log("Failed to open M8 audio device, SDL Error: %s", SDL_GetError());
-      return 0;
-    }
+  SDL_zero(want_in);
+  want_in.freq = 44100;
+  want_in.format = AUDIO_S16;
+  want_in.channels = 2;
+  want_in.samples = audio_buffer_size;
+  want_in.callback = audio_cb_in;
+  devid_in = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(m8_device_id, SDL_TRUE), SDL_TRUE, &want_in,
+                                 &have_in, SDL_AUDIO_ALLOW_ANY_CHANGE);
+  if (devid_in == 0) {
+    SDL_Log("Failed to open M8 audio device, SDL Error: %s", SDL_GetError());
+    return 0;
   }
 
   // Start audio processing
--- a/src/command.c
+++ b/src/command.c
@@ -42,8 +42,7 @@
 
   switch (recv_buf[0]) {
 
-  case draw_rectangle_command:
-
+  case draw_rectangle_command: {
     if (size < draw_rectangle_command_min_datalength ||
         size > draw_rectangle_command_max_datalength) {
       SDL_LogError(SDL_LOG_CATEGORY_ERROR,
@@ -52,52 +51,47 @@
                    size);
       dump_packet(size, recv_buf);
       return 0;
-      break;
-    } else {
+    }
+    /* 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 */
 
-      /* 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;
 
-      static struct draw_rectangle_command rectcmd;
+    rectcmd.pos.x = decodeInt16(recv_buf, 1);
+    rectcmd.pos.y = decodeInt16(recv_buf, 3);
 
-      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;
+    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;
     }
 
-    break;
+    draw_rectangle(&rectcmd);
+    return 1;
+  }
 
-  case draw_character_command:
-
+  case draw_character_command: {
     if (size != draw_character_command_datalength) {
       SDL_LogError(SDL_LOG_CATEGORY_ERROR,
                    "Invalid draw character packet: expected length %d, got %d",
@@ -104,22 +98,17 @@
                    draw_character_command_datalength, size);
       dump_packet(size, recv_buf);
       return 0;
-      break;
-    } else {
-
-      struct draw_character_command charcmd = {
-          recv_buf[1],                                          // char
-          {decodeInt16(recv_buf, 2), decodeInt16(recv_buf, 4)}, // position x/y
-          {recv_buf[6], recv_buf[7], recv_buf[8]},              // foreground r/g/b
-          {recv_buf[9], recv_buf[10], recv_buf[11]}};           // background r/g/b
-      draw_character(&charcmd);
-      return 1;
     }
+    struct draw_character_command charcmd = {
+        recv_buf[1],                                          // char
+        {decodeInt16(recv_buf, 2), decodeInt16(recv_buf, 4)}, // position x/y
+        {recv_buf[6], recv_buf[7], recv_buf[8]},              // foreground r/g/b
+        {recv_buf[9], recv_buf[10], recv_buf[11]}};           // background r/g/b
+    draw_character(&charcmd);
+    return 1;
+  }
 
-    break;
-
-  case draw_oscilloscope_waveform_command:
-
+  case draw_oscilloscope_waveform_command: {
     if (size < draw_oscilloscope_waveform_command_mindatalength ||
         size > draw_oscilloscope_waveform_command_maxdatalength) {
       SDL_LogError(SDL_LOG_CATEGORY_ERROR,
@@ -128,22 +117,18 @@
                    draw_oscilloscope_waveform_command_maxdatalength, size);
       dump_packet(size, recv_buf);
       return 0;
-      break;
-    } else {
+    }
+    struct draw_oscilloscope_waveform_command osccmd;
 
-      struct draw_oscilloscope_waveform_command osccmd;
+    osccmd.color = (struct color){recv_buf[1], recv_buf[2], recv_buf[3]}; // color r/g/b
+    memcpy(osccmd.waveform, &recv_buf[4], size - 4);
 
-      osccmd.color = (struct color){recv_buf[1], recv_buf[2], recv_buf[3]}; // color r/g/b
-      memcpy(osccmd.waveform, &recv_buf[4], size - 4);
+    osccmd.waveform_size = size - 4;
 
-      osccmd.waveform_size = size - 4;
+    draw_waveform(&osccmd);
+    return 1;
+  }
 
-      draw_waveform(&osccmd);
-      return 1;
-    }
-
-    break;
-
   case joypad_keypressedstate_command: {
     if (size != joypad_keypressedstate_command_datalength) {
       SDL_LogError(SDL_LOG_CATEGORY_ERROR,
@@ -152,12 +137,10 @@
                    joypad_keypressedstate_command_datalength, size);
       dump_packet(size, recv_buf);
       return 0;
-      break;
     }
 
     // nothing is done with joypad key pressed packets for now
     return 1;
-    break;
   }
 
   case system_info_command: {
@@ -188,14 +171,12 @@
     set_font_mode(recv_buf[5]);
 
     return 1;
-    break;
   }
 
   default:
-    SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Invalid packet\n");
+    SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Invalid packet");
     dump_packet(size, recv_buf);
     return 0;
-    break;
   }
   return 1;
 }
--- a/src/main.c
+++ b/src/main.c
@@ -234,7 +234,8 @@
           SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, "Error %d reading serial. \n", (int)bytes_read);
           run = QUIT;
           break;
-        } else if (bytes_read > 0) {
+        }
+        if (bytes_read > 0) {
           // input from device: reset the zero byte counter and create a
           // pointer to the serial buffer
           zerobyte_packets = 0;
@@ -261,17 +262,16 @@
             if (check_serial_port()) {
               // the device is still there, carry on
               break;
-            } else {
-              port_inited = 0;
-              run = WAIT_FOR_DEVICE;
-              close_serial_port();
-              if (conf.audio_enabled == 1) {
-                audio_destroy();
-              }
-              /* we'll make one more loop to see if the device is still there
-               * but just sending zero bytes. if it doesn't get detected when
-               * resetting the port, it will disconnect */
             }
+            port_inited = 0;
+            run = WAIT_FOR_DEVICE;
+            close_serial_port();
+            if (conf.audio_enabled == 1) {
+              audio_destroy();
+            }
+            /* we'll make one more loop to see if the device is still there
+             * but just sending zero bytes. if it doesn't get detected when
+             * resetting the port, it will disconnect */
           }
           break;
         }
--- a/src/ringbuffer.c
+++ b/src/ringbuffer.c
@@ -23,35 +23,33 @@
 uint32_t ring_buffer_push(RingBuffer *rb, const uint8_t *data, uint32_t length) {
   if (ring_buffer_full(rb)) {
     return -1; // buffer full, push fails
+  }
+  uint32_t space1 = rb->max_size - rb->tail;
+  uint32_t n = (length <= rb->max_size - rb->size) ? length : (rb->max_size - rb->size);
+  if (n <= space1) {
+    SDL_memcpy(rb->buffer + rb->tail, data, n);
   } else {
-    uint32_t space1 = rb->max_size - rb->tail;
-    uint32_t n = (length <= rb->max_size - rb->size) ? length : (rb->max_size - rb->size);
-    if (n <= space1) {
-      SDL_memcpy(rb->buffer + rb->tail, data, n);
-    } else {
-      SDL_memcpy(rb->buffer + rb->tail, data, space1);
-      SDL_memcpy(rb->buffer, data + space1, n - space1);
-    }
-    rb->tail = (rb->tail + n) % rb->max_size;
-    rb->size += n;
-    return n; // push successful, returns number of bytes pushed
+    SDL_memcpy(rb->buffer + rb->tail, data, space1);
+    SDL_memcpy(rb->buffer, data + space1, n - space1);
   }
+  rb->tail = (rb->tail + n) % rb->max_size;
+  rb->size += n;
+  return n; // push successful, returns number of bytes pushed
 }
 
 uint32_t ring_buffer_pop(RingBuffer *rb, uint8_t *data, uint32_t length) {
   if (ring_buffer_empty(rb)) {
     return -1; // buffer empty, pop fails
+  }
+  uint32_t space1 = rb->max_size - rb->head;
+  uint32_t n = (length <= rb->size) ? length : rb->size;
+  if (n <= space1) {
+    SDL_memcpy(data, rb->buffer + rb->head, n);
   } else {
-    uint32_t space1 = rb->max_size - rb->head;
-    uint32_t n = (length <= rb->size) ? length : rb->size;
-    if (n <= space1) {
-      SDL_memcpy(data, rb->buffer + rb->head, n);
-    } else {
-      SDL_memcpy(data, rb->buffer + rb->head, space1);
-      SDL_memcpy(data + space1, rb->buffer, n - space1);
-    }
-    rb->head = (rb->head + n) % rb->max_size;
-    rb->size -= n;
-    return n; // pop successful, returns number of bytes popped
+    SDL_memcpy(data, rb->buffer + rb->head, space1);
+    SDL_memcpy(data + space1, rb->buffer, n - space1);
   }
+  rb->head = (rb->head + n) % rb->max_size;
+  rb->size -= n;
+  return n; // pop successful, returns number of bytes popped
 }
--