Skip to content

Commit 50ae95c

Browse files
authored
Merge pull request #138 from v3rm0n/multi-device
Specify a preferred device
2 parents d39da7b + 1653907 commit 50ae95c

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,23 @@ Connect the M8 or Teensy (with headless firmware) to your computer and start the
8585

8686
If the stars are aligned correctly, you should see the M8 screen.
8787

88+
#### Choosing a preferred device
89+
90+
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
91+
92+
```
93+
./m8c --list
94+
95+
2024-02-25 18:39:27.806 m8c[99838:4295527] INFO: Found M8 device: /dev/cu.usbmodem124709801
96+
2024-02-25 18:39:27.807 m8c[99838:4295527] INFO: Found M8 device: /dev/cu.usbmodem121136001
97+
```
98+
99+
And you can specify the preferred device by using
100+
101+
```
102+
./m8c --dev /dev/cu.usbmodem124709801
103+
```
104+
88105
-----------
89106

90107
## Keyboard mappings

src/main.c

+14-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ void intHandler(int dummy) { run = QUIT; }
2828
void close_serial_port() { disconnect(); }
2929

3030
int main(int argc, char *argv[]) {
31+
32+
if(argc == 2 && strcmp(argv[1], "--list") == 0) {
33+
return list_devices();
34+
}
35+
36+
char *preferred_device = NULL;
37+
if (argc == 3 && strcmp(argv[1], "--dev") == 0) {
38+
preferred_device = argv[2];
39+
SDL_Log("Using preferred device %s.\n", preferred_device);
40+
}
41+
3142
// Initialize the config to defaults read in the params from the
3243
// configfile if present
3344
config_params_s conf = init_config();
@@ -66,7 +77,7 @@ int main(int argc, char *argv[]) {
6677
// First device detection to avoid SDL init if it isn't necessary. To be run
6778
// only if we shouldn't wait for M8 to be connected.
6879
if (conf.wait_for_device == 0) {
69-
if (init_serial(1) == 0) {
80+
if (init_serial(1, preferred_device) == 0) {
7081
SDL_free(serial_buf);
7182
return -1;
7283
}
@@ -86,7 +97,7 @@ int main(int argc, char *argv[]) {
8697
// main loop begin
8798
do {
8899
// try to init serial port
89-
int port_inited = init_serial(1);
100+
int port_inited = init_serial(1, preferred_device);
90101
// if port init was successful, try to enable and reset display
91102
if (port_inited == 1 && enable_and_reset_display(0) == 1) {
92103
// if audio routing is enabled, try to initialize audio devices
@@ -132,7 +143,7 @@ int main(int argc, char *argv[]) {
132143
// Poll for M8 device every second
133144
if (port_inited == 0 && (SDL_GetTicks() - ticks_poll_device > 1000)) {
134145
ticks_poll_device = SDL_GetTicks();
135-
if (run == WAIT_FOR_DEVICE && init_serial(0) == 1) {
146+
if (run == WAIT_FOR_DEVICE && init_serial(0, preferred_device) == 1) {
136147

137148
if (conf.audio_enabled == 1) {
138149
if (audio_init(conf.audio_buffer_size, conf.audio_device_name) ==

src/serial.c

+28-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,27 @@ static int detect_m8_serial_device(struct sp_port *m8_port) {
3535
return 0;
3636
}
3737

38+
int list_devices() {
39+
struct sp_port **port_list;
40+
enum sp_return result = sp_list_ports(&port_list);
41+
42+
if (result != SP_OK) {
43+
SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "sp_list_ports() failed!\n");
44+
abort();
45+
}
46+
47+
for (int i = 0; port_list[i] != NULL; i++) {
48+
struct sp_port *port = port_list[i];
49+
50+
if (detect_m8_serial_device(port)) {
51+
SDL_Log("Found M8 device: %s", sp_get_port_name(port));
52+
}
53+
}
54+
55+
sp_free_port_list(port_list);
56+
return 0;
57+
}
58+
3859
// Checks for connected devices and whether the specified device still exists
3960
int check_serial_port() {
4061

@@ -68,7 +89,7 @@ int check_serial_port() {
6889
return device_found;
6990
}
7091

71-
int init_serial(int verbose) {
92+
int init_serial(int verbose, char *preferred_device) {
7293
if (m8_port != NULL) {
7394
// Port is already initialized
7495
return 1;
@@ -95,8 +116,13 @@ int init_serial(int verbose) {
95116
struct sp_port *port = port_list[i];
96117

97118
if (detect_m8_serial_device(port)) {
98-
SDL_Log("Found M8 in %s.\n", sp_get_port_name(port));
119+
char *port_name = sp_get_port_name(port);
120+
SDL_Log("Found M8 in %s.\n", port_name);
99121
sp_copy_port(port, &m8_port);
122+
if (preferred_device != NULL && strcmp(preferred_device, port_name) == 0) {
123+
SDL_Log("Found preferred device, breaking");
124+
break;
125+
}
100126
}
101127
}
102128

src/serial.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ int init_serial_with_file_descriptor(int file_descriptor);
1313
#define serial_read_size 512
1414
#endif
1515

16-
int init_serial(int verbose);
16+
int init_serial(int verbose, char *preferred_device);
17+
int list_devices();
1718
int check_serial_port();
1819
int reset_display();
1920
int enable_and_reset_display();

0 commit comments

Comments
 (0)