shithub: m8c

Download patch

ref: 165390774a1e06279ae10ac088302d2b25063d61
parent: 9d70859ec2d75be67e2e80b04fb1f5b85b371737
author: Maido Käära <maido@producement.com>
date: Sun Feb 25 13:46:28 EST 2024

Specify a preferred device

When multiple devices are connected, let the user list the devices
and choose the preferred one.

--- a/README.md
+++ b/README.md
@@ -85,6 +85,23 @@
 
 If the stars are aligned correctly, you should see the M8 screen.
 
+#### Choosing a preferred device
+
+When you have multiple M8 devices connected and you want to choose a specific one or launch m8c multiple times, you can get the list of devices by running
+
+```
+./m8c --list
+
+2024-02-25 18:39:27.806 m8c[99838:4295527] INFO: Found M8 device: /dev/cu.usbmodem124709801
+2024-02-25 18:39:27.807 m8c[99838:4295527] INFO: Found M8 device: /dev/cu.usbmodem121136001
+```
+
+And you can specify the preferred device by using
+
+```
+./m8c --dev /dev/cu.usbmodem124709801
+```
+
 -----------
 
 ## Keyboard mappings
--- a/src/main.c
+++ b/src/main.c
@@ -28,6 +28,17 @@
 void close_serial_port() { disconnect(); }
 
 int main(int argc, char *argv[]) {
+
+  if(argc == 2 && strcmp(argv[1], "--list") == 0) {
+    return list_devices();
+  }
+
+  char *preferred_device = NULL;
+  if (argc == 3 && strcmp(argv[1], "--dev") == 0) {
+    preferred_device = argv[2];
+    SDL_Log("Using preferred device %s.\n", preferred_device);
+  }
+
   // Initialize the config to defaults read in the params from the
   // configfile if present
   config_params_s conf = init_config();
@@ -66,7 +77,7 @@
   // First device detection to avoid SDL init if it isn't necessary. To be run
   // only if we shouldn't wait for M8 to be connected.
   if (conf.wait_for_device == 0) {
-    if (init_serial(1) == 0) {
+    if (init_serial(1, preferred_device) == 0) {
       SDL_free(serial_buf);
       return -1;
     }
@@ -86,7 +97,7 @@
   // main loop begin
   do {
     // try to init serial port
-    int port_inited = init_serial(1);
+    int port_inited = init_serial(1, preferred_device);
     // if port init was successful, try to enable and reset display
     if (port_inited == 1 && enable_and_reset_display(0) == 1) {
       // if audio routing is enabled, try to initialize audio devices
@@ -132,7 +143,7 @@
         // Poll for M8 device every second
         if (port_inited == 0 && (SDL_GetTicks() - ticks_poll_device > 1000)) {
           ticks_poll_device = SDL_GetTicks();
-          if (run == WAIT_FOR_DEVICE && init_serial(0) == 1) {
+          if (run == WAIT_FOR_DEVICE && init_serial(0, preferred_device) == 1) {
 
             if (conf.audio_enabled == 1) {
               if (audio_init(conf.audio_buffer_size, conf.audio_device_name) ==
--- a/src/serial.c
+++ b/src/serial.c
@@ -35,6 +35,27 @@
   return 0;
 }
 
+int list_devices() {
+  struct sp_port **port_list;
+  enum sp_return result = sp_list_ports(&port_list);
+
+  if (result != SP_OK) {
+    SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "sp_list_ports() failed!\n");
+    abort();
+  }
+
+  for (int i = 0; port_list[i] != NULL; i++) {
+    struct sp_port *port = port_list[i];
+
+    if (detect_m8_serial_device(port)) {
+      SDL_Log("Found M8 device: %s", sp_get_port_name(port));
+    }
+  }
+
+  sp_free_port_list(port_list);
+  return 0;
+}
+
 // Checks for connected devices and whether the specified device still exists
 int check_serial_port() {
 
@@ -68,7 +89,7 @@
   return device_found;
 }
 
-int init_serial(int verbose) {
+int init_serial(int verbose, char *preferred_device) {
   if (m8_port != NULL) {
     // Port is already initialized
     return 1;
@@ -95,8 +116,13 @@
     struct sp_port *port = port_list[i];
 
     if (detect_m8_serial_device(port)) {
-      SDL_Log("Found M8 in %s.\n", sp_get_port_name(port));
+      char *port_name = sp_get_port_name(port);
+      SDL_Log("Found M8 in %s.\n", port_name);
       sp_copy_port(port, &m8_port);
+      if (preferred_device != NULL && strcmp(preferred_device, port_name) == 0) {
+        SDL_Log("Found preferred device, breaking");
+        break;
+      }
     }
   }
 
--- a/src/serial.h
+++ b/src/serial.h
@@ -13,7 +13,8 @@
 #define serial_read_size 512
 #endif
 
-int init_serial(int verbose);
+int init_serial(int verbose, char *preferred_device);
+int list_devices();
 int check_serial_port();
 int reset_display();
 int enable_and_reset_display();
--