try all detected devices when auto detecting Arduino; restore the end function's behavior which kills serial threads before quitting

This commit is contained in:
ohsqueezy 2023-10-13 14:58:26 -04:00
parent d1fe534c9f
commit 66043aa393
2 changed files with 28 additions and 14 deletions

38
NS.py
View File

@ -265,20 +265,33 @@ class NS(Game, Animation):
# If a list of serial ports was requested, print detected ports and exit.
if arguments.list_serial_ports:
for port in list_ports.comports():
print(f"Detected serial port: {port.device}")
if list_ports.comports():
for port in list_ports.comports():
print(f"Detected serial port: {port.device}")
else:
print("No serial ports detected")
exit()
# Open the port specified by the configuration or command line if it is found. If the specified port is not
# found, open the first found serial port. If no serial ports are found, raise an exception.
# found, iterate through the com ports, and try to open each. If no serial port can be opened, raise an
# exception.
requested_port = self.get_configuration("input", "arduino-port")
devices = [port.device for port in list_ports.comports()]
if requested_port in devices:
self.serial_reader = Serial(requested_port, timeout=.3)
elif devices:
self.serial_reader = Serial(devices[0], timeout=.3)
else:
raise SerialException("No serial port devices were detected. Use --no-serial for keyboard-only mode.")
if requested_port:
print(f"Could not connect with requested port {requested_port}. Searching for other ports.")
found = False
for device in devices:
try:
self.serial_reader = Serial(device, timeout=.3)
found = True
except SerialException:
print(f"Tried and failed to open connection with serial device {device}")
if not found:
raise SerialException("No usable serial port devices found. Use --no-serial for keyboard-only mode.")
print(f"Using serial device at port {self.serial_reader.port}")
self.serial_kill = False
self.serial_data = 0
self.reset_arduino()
@ -383,12 +396,6 @@ class NS(Game, Animation):
self.serial_reader.dtr = False
self.serial_reader.reset_input_buffer()
self.serial_reader.dtr = True
def end(self, evt):
if evt.type == QUIT or self.delegate.compare(evt, "quit"):
self.serial_kill = True
self.gpio_kill = True
Game.end(self, evt)
def apply_serial(self):
for ii, light in enumerate(self.platform.lights):
@ -558,6 +565,11 @@ class NS(Game, Animation):
"""
if event.type == QUIT or self.delegate.compare(event, "quit"):
if self.confirming_quit or not self.get_configuration("input", "confirm-quit"):
# Kill serial threads
self.serial_kill = True
self.gpio_kill = True
# If SHIFT is pressed, try permanently stopping the systemd service to get a console back in case this is running on
# Raspbian Lite
if pygame.key.get_mods() & pygame.KMOD_SHIFT:
@ -566,6 +578,8 @@ class NS(Game, Animation):
print("Killing with permanent stop sent to systemd scrapeboard service")
except:
print("No scrapeboard system service detected, so permanent quit either failed or was unnecessary")
# Call parent to complete quit
super().end(event)

4
config
View File

@ -93,8 +93,8 @@ level_2 = bgm/level_2.ogg, .65
end = bgm/end.ogg, .65
[pads]
ne_color = #E4B80C
nw_color = #FF88FF
sw_color = #2222FF
ne_color = #E4B80C
se_color = #FF2222
sw_color = #2222FF
center_y = 376