|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import logging |
| 5 | +import threading |
| 6 | +import time |
| 7 | + |
| 8 | +from openlifu.io.LIFUInterface import LIFUInterface |
| 9 | + |
| 10 | +# set PYTHONPATH=%cd%\src;%PYTHONPATH% |
| 11 | +# python notebooks/test_async.py |
| 12 | + |
| 13 | +# Setup logging |
| 14 | +logging.basicConfig(level=logging.INFO) |
| 15 | + |
| 16 | +interface = None |
| 17 | + |
| 18 | +# Callbacks |
| 19 | +def on_connect(descriptor, port): |
| 20 | + print(f"🔌 CONNECTED: {descriptor} on port {port}") |
| 21 | + |
| 22 | +def on_disconnect(descriptor, port): |
| 23 | + print(f"❌ DISCONNECTED: {descriptor} from port {port}") |
| 24 | + |
| 25 | +def on_data_received(descriptor, packet): |
| 26 | + print(f"📦 DATA [{descriptor}]: {packet}") |
| 27 | + |
| 28 | +def monitor_interface(): |
| 29 | + """Run the device monitor loop in a separate thread using asyncio.""" |
| 30 | + asyncio.run(interface.start_monitoring(interval=1)) |
| 31 | + |
| 32 | +def rebind_tx_callbacks(): |
| 33 | + """Bind callbacks to the TX UART, if present.""" |
| 34 | + if interface.txdevice and interface.txdevice.uart: |
| 35 | + interface.txdevice.uart.signal_connect.connect(on_connect) |
| 36 | + interface.txdevice.uart.signal_disconnect.connect(on_disconnect) |
| 37 | + interface.txdevice.uart.signal_data_received.connect(on_data_received) |
| 38 | + |
| 39 | +def run_menu(): |
| 40 | + while True: |
| 41 | + print("\n--- LIFU MENU ---") |
| 42 | + print("1. Turn ON 12V") |
| 43 | + print("2. Turn OFF 12V") |
| 44 | + print("3. Ping TX") |
| 45 | + print("4. Show Connection Status") |
| 46 | + print("5. Exit") |
| 47 | + choice = input("Enter choice: ").strip() |
| 48 | + |
| 49 | + tx_connected, hv_connected = interface.is_device_connected() |
| 50 | + |
| 51 | + if choice == "1": |
| 52 | + if hv_connected: |
| 53 | + print("⚡ Sending 12V ON...") |
| 54 | + interface.hvcontroller.turn_12v_on() |
| 55 | + time.sleep(2.0) |
| 56 | + print("🔄 Reinitializing TX...") |
| 57 | + rebind_tx_callbacks() |
| 58 | + else: |
| 59 | + print("⚠️ HV not connected.") |
| 60 | + |
| 61 | + elif choice == "2": |
| 62 | + if hv_connected: |
| 63 | + print("🛑 Sending 12V OFF...") |
| 64 | + interface.hvcontroller.turn_12v_off() |
| 65 | + else: |
| 66 | + print("⚠️ HV not connected.") |
| 67 | + |
| 68 | + elif choice == "3": |
| 69 | + if tx_connected: |
| 70 | + print("📡 Sending PING to TX...") |
| 71 | + resp = interface.txdevice.ping() |
| 72 | + if resp: |
| 73 | + print("✅ TX responded to PING.") |
| 74 | + else: |
| 75 | + print("❌ No response or error.") |
| 76 | + else: |
| 77 | + print("⚠️ TX not connected.") |
| 78 | + |
| 79 | + elif choice == "4": |
| 80 | + print("Status:") |
| 81 | + print(f" TX: {'✅ Connected' if tx_connected else '❌ Not connected'}") |
| 82 | + print(f" HV: {'✅ Connected' if hv_connected else '❌ Not connected'}") |
| 83 | + |
| 84 | + elif choice == "5": |
| 85 | + print("Exiting...") |
| 86 | + interface.stop_monitoring() |
| 87 | + break |
| 88 | + else: |
| 89 | + print("Invalid choice.") |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + interface = LIFUInterface(HV_test_mode=False, run_async=False) |
| 93 | + |
| 94 | + # Bind callbacks for HV and (initially connected) TX |
| 95 | + if interface.hvcontroller.uart: |
| 96 | + interface.hvcontroller.uart.signal_connect.connect(on_connect) |
| 97 | + interface.hvcontroller.uart.signal_disconnect.connect(on_disconnect) |
| 98 | + interface.hvcontroller.uart.signal_data_received.connect(on_data_received) |
| 99 | + |
| 100 | + rebind_tx_callbacks() |
| 101 | + |
| 102 | + print("🔍 Starting LIFU monitoring...") |
| 103 | + monitor_thread = threading.Thread(target=monitor_interface, daemon=True) |
| 104 | + monitor_thread.start() |
| 105 | + |
| 106 | + try: |
| 107 | + run_menu() |
| 108 | + except KeyboardInterrupt: |
| 109 | + print("\n🛑 Stopped by user.") |
| 110 | + interface.stop_monitoring() |
0 commit comments