diff --git a/README.md b/README.md index 707f590..a3a5e34 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,23 @@ Connect the M8 or Teensy (with headless firmware) to your computer and start the 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 diff --git a/src/main.c b/src/main.c index 656e270..abc9d83 100644 --- a/src/main.c +++ b/src/main.c @@ -28,6 +28,17 @@ void intHandler(int dummy) { run = QUIT; } 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 @@ int main(int argc, char *argv[]) { // 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 @@ int main(int argc, char *argv[]) { // 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 @@ int main(int argc, char *argv[]) { // 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) == diff --git a/src/serial.c b/src/serial.c index 445aa4a..5e783aa 100644 --- a/src/serial.c +++ b/src/serial.c @@ -35,6 +35,27 @@ static int detect_m8_serial_device(struct sp_port *m8_port) { 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 @@ int check_serial_port() { 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 @@ int init_serial(int verbose) { 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; + } } } diff --git a/src/serial.h b/src/serial.h index 1c79137..54ee22d 100644 --- a/src/serial.h +++ b/src/serial.h @@ -13,7 +13,8 @@ int init_serial_with_file_descriptor(int file_descriptor); #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();