From 126f4b5677f95d932a520dfa73bedf8a3087681e Mon Sep 17 00:00:00 2001 From: irrit-im Date: Mon, 30 Dec 2024 14:17:13 +0200 Subject: [PATCH 1/3] now working but still --- port_map.py | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 port_map.py diff --git a/port_map.py b/port_map.py new file mode 100644 index 0000000..7f02ff5 --- /dev/null +++ b/port_map.py @@ -0,0 +1,106 @@ +from pybricks.hubs import PrimeHub +from pybricks.parameters import Port +from pybricks.pupdevices import ColorSensor, Motor, ForceSensor, UltrasonicSensor +from pybricks.tools import wait +from pybricks.parameters import Button + +hub = PrimeHub() + + +def map_ports() -> dict[Port, (ColorSensor | Motor | ForceSensor | UltrasonicSensor)]: + available_device_types = (ColorSensor, Motor, ForceSensor, UltrasonicSensor) + + ports = (Port.A, Port.B, Port.C, Port.D, Port.E, Port.F) + port_devices = { + Port.A: None, + Port.B: None, + Port.C: None, + Port.D: None, + Port.E: None, + Port.F: None, + } + + for port in ports: + for device_type in available_device_types: + try: + port_devices[port] = device_type(port) + break + except OSError: + ... + + return port_devices + + +def get_device_name( + device: ColorSensor | Motor | ForceSensor | UltrasonicSensor, +) -> str: + device_type = type(device) + return str(device_type).replace("", "") + + +def generate_code_difinition(device, port) -> str: + device_name: str = get_device_name(device) + print(f"{device_name.lower()} = {device_name}({port})") + + +def print_hardware_difinitions(port_devices: dict) -> None: + for port, device in port_devices.items(): + device_name: str = get_device_name(device) + print(f"{device_name.lower()} = {device_name}({port})") + + +def signal_color_sensor(sensor: ColorSensor) -> None: + sensor.lights.off() + wait(150) + sensor.lights.on(100) + wait(150) + + +def signal_motor(motor: Motor) -> None: + motor.run_time(100, 100, wait=False) + + +def indicate_port_on_matrix(port: Port) -> None: + port_pixels = { + Port.A: (0, 0), + Port.B: (0, 4), + Port.C: (2, 0), + Port.D: (2, 4), + Port.E: (4, 0), + Port.F: (4, 4), + } + hub.display.off() + x, y = port_pixels[port] + hub.display.pixel(x, y) + + +def signal_devices(port_devices: dict) -> None: + indicator = 0 # TODO: rename + + while True: + if Button.RIGHT in hub.buttons.pressed(): + indocator = (indicator + 1) % 6 + if Button.LEFT in hub.buttons.pressed(): + indocator = (indicator - 1) % 6 + + port, device = port_devices.items()[indicator] + indicate_port_on_matrix(port=port) + device_name: str = get_device_name(device) + if device_name == "ColorSensor": + signal_color_sensor(device) + elif device_name == "Motor": + signal_motor(device) + else: + wait(100) + + +device_ports = map_ports() +print_hardware_difinitions(device_ports) +signal_devices(device_ports) +print("done") +# = (Port.C) +# = (Port.F) +# = (Port.A) +# = (Port.D) +# = (Port.B) +# = (Port.E) From ad5e331134e9d5ce43aab8e79e59492011b5c079 Mon Sep 17 00:00:00 2001 From: irrit-im Date: Mon, 30 Dec 2024 14:53:46 +0200 Subject: [PATCH 2/3] very ugly code but it works --- port_map.py | 127 +++++++++++++++++++++++++++------------------------- 1 file changed, 67 insertions(+), 60 deletions(-) diff --git a/port_map.py b/port_map.py index 7f02ff5..a8c9c0a 100644 --- a/port_map.py +++ b/port_map.py @@ -4,31 +4,33 @@ from pybricks.tools import wait from pybricks.parameters import Button +PORTS = (Port.A, Port.B, Port.C, Port.D, Port.E, Port.F) +PORT_PIXELS = { + Port.A: (0, 0), + Port.B: (0, 4), + Port.C: (2, 0), + Port.D: (2, 4), + Port.E: (4, 0), + Port.F: (4, 4), +} + hub = PrimeHub() -def map_ports() -> dict[Port, (ColorSensor | Motor | ForceSensor | UltrasonicSensor)]: +def map_ports() -> list[ColorSensor | Motor | ForceSensor | UltrasonicSensor]: available_device_types = (ColorSensor, Motor, ForceSensor, UltrasonicSensor) - ports = (Port.A, Port.B, Port.C, Port.D, Port.E, Port.F) - port_devices = { - Port.A: None, - Port.B: None, - Port.C: None, - Port.D: None, - Port.E: None, - Port.F: None, - } - - for port in ports: + devices = [] + + for port in PORTS: for device_type in available_device_types: try: - port_devices[port] = device_type(port) + devices.append(device_type(port)) break except OSError: ... - return port_devices + return devices def get_device_name( @@ -38,69 +40,74 @@ def get_device_name( return str(device_type).replace("", "") -def generate_code_difinition(device, port) -> str: +def generate_code_difinition( + device: ColorSensor | Motor | ForceSensor | UltrasonicSensor, port: Port +) -> str: device_name: str = get_device_name(device) print(f"{device_name.lower()} = {device_name}({port})") -def print_hardware_difinitions(port_devices: dict) -> None: - for port, device in port_devices.items(): +def print_hardware_difinitions( + devices: list[ColorSensor | Motor | ForceSensor | UltrasonicSensor], +) -> None: + for i in range(6): + port = PORTS[i] + device = devices[i] device_name: str = get_device_name(device) print(f"{device_name.lower()} = {device_name}({port})") -def signal_color_sensor(sensor: ColorSensor) -> None: - sensor.lights.off() - wait(150) - sensor.lights.on(100) - wait(150) - - -def signal_motor(motor: Motor) -> None: - motor.run_time(100, 100, wait=False) +def indicate_device( + device: ColorSensor | Motor | ForceSensor | UltrasonicSensor, +) -> None: + if type(device) == ColorSensor: + device.lights.on(100) + elif type(device) == Motor: + device.dc(100) def indicate_port_on_matrix(port: Port) -> None: - port_pixels = { - Port.A: (0, 0), - Port.B: (0, 4), - Port.C: (2, 0), - Port.D: (2, 4), - Port.E: (4, 0), - Port.F: (4, 4), - } hub.display.off() - x, y = port_pixels[port] + x, y = PORT_PIXELS[port] hub.display.pixel(x, y) -def signal_devices(port_devices: dict) -> None: - indicator = 0 # TODO: rename +def stop_device(device: ColorSensor | Motor | ForceSensor | UltrasonicSensor) -> None: + if type(device) == ColorSensor: + device.lights.off() + elif type(device) == Motor: + device.stop() + + +def signal_devices( + devices: list[ColorSensor | Motor | ForceSensor | UltrasonicSensor], +) -> None: + indicator = 0 while True: + running_device = devices[indicator] + indicate_port_on_matrix(port=PORTS[indicator]) + indicate_device(running_device) + if Button.RIGHT in hub.buttons.pressed(): - indocator = (indicator + 1) % 6 + stop_device(running_device) + indicator = (indicator + 1) % 6 + wait(200) if Button.LEFT in hub.buttons.pressed(): - indocator = (indicator - 1) % 6 + stop_device(running_device) + indicator = (indicator - 1) % 6 + wait(200) - port, device = port_devices.items()[indicator] - indicate_port_on_matrix(port=port) - device_name: str = get_device_name(device) - if device_name == "ColorSensor": - signal_color_sensor(device) - elif device_name == "Motor": - signal_motor(device) - else: - wait(100) - - -device_ports = map_ports() -print_hardware_difinitions(device_ports) -signal_devices(device_ports) -print("done") -# = (Port.C) -# = (Port.F) -# = (Port.A) -# = (Port.D) -# = (Port.B) -# = (Port.E) + wait(20) + + +def main() -> None: + devices = map_ports() + print_hardware_difinitions(devices) + for device in devices: + if type(device) == ColorSensor: + device.lights.off() + signal_devices(devices) + + +main() From 17fd3cbd85d4d6c14956fd9312c8cde2bf4c4413 Mon Sep 17 00:00:00 2001 From: irrit-im Date: Thu, 2 Jan 2025 14:16:07 +0200 Subject: [PATCH 3/3] fixed errors --- port_map.py | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/port_map.py b/port_map.py index a8c9c0a..523e4e5 100644 --- a/port_map.py +++ b/port_map.py @@ -21,11 +21,12 @@ def map_ports() -> list[ColorSensor | Motor | ForceSensor | UltrasonicSensor]: available_device_types = (ColorSensor, Motor, ForceSensor, UltrasonicSensor) devices = [] - - for port in PORTS: + for port_index in range(6): + port = PORTS[port_index] + devices.append(None) for device_type in available_device_types: try: - devices.append(device_type(port)) + devices[port_index] = device_type(port) break except OSError: ... @@ -50,20 +51,22 @@ def generate_code_difinition( def print_hardware_difinitions( devices: list[ColorSensor | Motor | ForceSensor | UltrasonicSensor], ) -> None: - for i in range(6): - port = PORTS[i] - device = devices[i] - device_name: str = get_device_name(device) - print(f"{device_name.lower()} = {device_name}({port})") + for port_index in range(6): + port = PORTS[port_index] + device = devices[port_index] + if device: + device_name: str = get_device_name(device) + print(f"{device_name.lower()} = {device_name}({port})") def indicate_device( device: ColorSensor | Motor | ForceSensor | UltrasonicSensor, ) -> None: - if type(device) == ColorSensor: - device.lights.on(100) - elif type(device) == Motor: - device.dc(100) + if device: + if type(device) == ColorSensor: + device.lights.on(100) + elif type(device) == Motor: + device.dc(100) def indicate_port_on_matrix(port: Port) -> None: @@ -73,10 +76,11 @@ def indicate_port_on_matrix(port: Port) -> None: def stop_device(device: ColorSensor | Motor | ForceSensor | UltrasonicSensor) -> None: - if type(device) == ColorSensor: - device.lights.off() - elif type(device) == Motor: - device.stop() + if device: + if type(device) == ColorSensor: + device.lights.off() + elif type(device) == Motor: + device.stop() def signal_devices(