from __future__ import annotations import argparse import asyncio from .hub import DuploColor, DuploSound, DuploTrainHub MENU = """ === Test DUPLO Hub #5 === 1) Avancer 2) Reculer 3) Stop 4) Jouer une musique 5) Changer couleur lumière 7) Éteindre la lumière 6) Demo rapide q) Quitter """ SOUND_MENU = """ Sons disponibles: - brake - station_departure - water_refill - horn - steam """ SOUND_BY_NAME: dict[str, DuploSound] = { "brake": DuploSound.BRAKE, "station_departure": DuploSound.STATION_DEPARTURE, "water_refill": DuploSound.WATER_REFILL, "horn": DuploSound.HORN, "steam": DuploSound.STEAM, } COLOR_MENU = """ Couleurs disponibles: - black - pink - purple - blue - lightblue - cyan - green - yellow - orange - red - white """ COLOR_BY_NAME: dict[str, DuploColor] = { "black": DuploColor.BLACK, "pink": DuploColor.PINK, "purple": DuploColor.PURPLE, "blue": DuploColor.BLUE, "lightblue": DuploColor.LIGHTBLUE, "cyan": DuploColor.CYAN, "green": DuploColor.GREEN, "yellow": DuploColor.YELLOW, "orange": DuploColor.ORANGE, "red": DuploColor.RED, "white": DuploColor.WHITE, } async def run_test(address: str | None, speed: int) -> None: hub = DuploTrainHub(address=address) await hub.connect() print("Connecté au hub DUPLO.") try: while True: print(MENU) choice = input("Choix: ").strip().lower() if choice == "1": await hub.forward(speed) print(f"Avance à {abs(speed)}%") elif choice == "2": await hub.backward(speed) print(f"Recule à {abs(speed)}%") elif choice == "3": await hub.stop() print("Stop") elif choice == "4": print(SOUND_MENU) sound_name = input("Son: ").strip().lower() sound = SOUND_BY_NAME.get(sound_name) if sound is None: print("Son invalide") continue await hub.play_sound(sound) print(f"Musique jouée ({sound_name.upper()})") elif choice == "5": print(COLOR_MENU) color_name = input("Couleur: ").strip().lower() color = COLOR_BY_NAME.get(color_name) if color is None: print("Couleur invalide") continue await hub.change_light_color(color) print(f"Couleur lumière changée ({color_name.upper()})") elif choice == "7": await hub.turn_off_light() print("Lumière éteinte") elif choice == "6": await hub.demo() print("Demo terminée") elif choice == "q": break else: print("Choix invalide") finally: await hub.stop() await hub.disconnect() print("Déconnecté") def main() -> None: parser = argparse.ArgumentParser(description="Test manuel du hub DUPLO Train #5") parser.add_argument("--address", help="Adresse BLE du hub (optionnel)") parser.add_argument("--speed", type=int, default=50, help="Vitesse par défaut 0..100") args = parser.parse_args() asyncio.run(run_test(address=args.address, speed=max(0, min(100, args.speed)))) if __name__ == "__main__": main()