Skip to content

Commit fef1922

Browse files
committed
control.py: Support windows and listing devices
Signed-off-by: Daniel Schaefer <dhs@frame.work>
1 parent 67ae06a commit fef1922

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

control.py

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212

1313
# Need to install
1414
import serial
15+
from serial.tools import list_ports
1516

1617
# Optional dependencies:
1718
# from PIL import Image
1819
# import PySimpleGUI as sg
1920

2021
FWK_MAGIC = [0x32, 0xAC]
21-
22+
FWK_VID = 0x32AC
23+
LED_MATRIX_PID = 0x20
24+
INPUTMODULE_PIDS = [LED_MATRIX_PID]
2225

2326
class CommandVals(IntEnum):
2427
Brightness = 0x00
@@ -133,6 +136,7 @@ class GameControlVal(IntEnum):
133136

134137
def main():
135138
parser = argparse.ArgumentParser()
139+
parser.add_argument("-l", "--list", help="List all compatible devices", action="store_true")
136140
parser.add_argument("--bootloader", help="Jump to the bootloader to flash new firmware",
137141
action="store_true")
138142
parser.add_argument('--sleep', help='Simulate the host going to sleep or waking up',
@@ -190,8 +194,7 @@ def main():
190194
"--get-color", help="Get RGB color (C1 Minimal Input Module)", action="store_true")
191195
parser.add_argument("-v", "--version",
192196
help="Get device version", action="store_true")
193-
parser.add_argument("--serial-dev", help="Change the serial dev. Probably /dev/ttyACM0 on Linux, COM0 on Windows",
194-
default='/dev/ttyACM0')
197+
parser.add_argument("--serial-dev", help="Change the serial dev. Probably /dev/ttyACM0 on Linux, COM0 on Windows")
195198

196199
parser.add_argument(
197200
"--disp-str", help="Display a string on the LCD Display", type=str)
@@ -214,9 +217,31 @@ def main():
214217

215218
args = parser.parse_args()
216219

217-
if args.serial_dev is not None:
218-
global SERIAL_DEV
220+
global SERIAL_DEV
221+
ports = find_devs()
222+
223+
if args.list:
224+
print_devs(ports)
225+
sys.exit(0)
226+
227+
if not ports:
228+
print("No device found")
229+
sys.exit(1)
230+
231+
if len(ports) == 1:
232+
SERIAL_DEV = ports[0].device
233+
elif args.serial_dev is not None:
219234
SERIAL_DEV = args.serial_dev
235+
elif len(ports) >= 1:
236+
print("More than 1 compatible device found. Please choose with --serial-dev ...")
237+
print("Example on Windows: --serial-dev COM3")
238+
print("Example on Linux: --serial-dev /dev/ttyACM0")
239+
print_devs(ports)
240+
sys.exit(1)
241+
242+
if SERIAL_DEV is None:
243+
print("No device selected")
244+
sys.exit(1)
220245

221246
if args.bootloader:
222247
bootloader()
@@ -312,6 +337,17 @@ def main():
312337
parser.print_help(sys.stderr)
313338
sys.exit(1)
314339

340+
def find_devs():
341+
ports = list_ports.comports() # Same result as python -m serial.tools.list_ports
342+
return [port for port in ports if port.vid == 0x32AC and port.pid in INPUTMODULE_PIDS]
343+
344+
def print_devs(ports):
345+
for port in ports:
346+
print(f"{port.device}")
347+
print(f" VID: 0x{port.vid:04X}")
348+
print(f" PID: 0x{port.pid:04X}")
349+
print(f" SN: {port.serial_number}")
350+
print(f" Product: {port.product}")
315351

316352
def bootloader():
317353
"""Reboot into the bootloader to flash new firmware"""
@@ -949,8 +985,11 @@ def send_serial(s, command):
949985

950986
def gui():
951987
import PySimpleGUI as sg
988+
global SERIAL_DEV
952989

953990
layout = [
991+
[sg.Text("Device:"), sg.Text(SERIAL_DEV)],
992+
954993
[sg.Text("Bootloader")],
955994
[sg.Button("Bootloader")],
956995

0 commit comments

Comments
 (0)