Skip to content

Commit

Permalink
Specify a preferred device
Browse files Browse the repository at this point in the history
When multiple devices are connected, let the user list the devices
and choose the preferred one.
  • Loading branch information
v3rm0n committed Feb 25, 2024
1 parent 9d70859 commit 1653907
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand Down Expand Up @@ -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) ==
Expand Down
30 changes: 28 additions & 2 deletions src/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 1653907

Please sign in to comment.