#!/usr/bin/env python3 """ MIPI TEST APPLICATION - MIPI_TEST.PY - ENTRY POINT OF APPLICATION VERSION: 0.1 AUTHOR: D. RICE 25/03/2026 © 2026 ARRIVE """ import vxi11 import time import sys import requests import threading url = f"http://192.168.45.8:5000/display" test_running = False # Global flag to control the background thread # --- Hardware Initialisation --- try: psu = vxi11.Instrument("192.168.45.3") scope = vxi11.Instrument("192.168.45.4") setup_del = 0.2 except Exception as e: print(f"ERROR: CANNOT CONNECT TO INSTRUMENTS: {e}") sys.exit(1) def set_display(state): """ Helper to handle HTTP PUT with error catching """ try: r = requests.put(url, json={"state": state}, timeout=2) return r.status_code == 200 except requests.exceptions.RequestException: return False def test_worker(interval): """ Background loop that toggles the display """ global test_running print(f"\n[TEST STARTED] TOGGLING EVERY {interval} SECONDS. USE MENU TO STOP") state = "on" while test_running: success = set_display(state) if not success: print(f"\n[TEST ERROR] FAILED TO REACH FLASK SERVER... STOPPING TEST...") test_running = False break # Toggle state for next iteration state = "off" if state == "on" else "on" # Sleep in small increments so we can exit the thread quickly for _ in range(int(interval * 10)): if not test_running: break time.sleep(0.1) def main_menu(): global test_running """Main Application Loop.""" while True: print("\n=== MIPI TEST CONTROL ===") print("1. RUN IDN CHECK (PSU & SCOPE)") print("2. CONFIGURE PSU (DEFAULT 24V/1.5A)") print("3. PSU OUTPUT ON/OFF (CH1)") print("4. START FLICKER TEST") print("5. STOP FLICKER TEST") print("6. EXIT") choice = input("\nSELECT OPTION (1-6): ") if choice == '1': print(f"PSU: {psu.ask('*IDN?')}") print(f"SCOPE: {scope.ask('*IDN?')}") elif choice == '2': psu.write('CH1:VOLT 24.0') psu.write('CH1:CURR 1.5') print("PSU CONFIGURED...") elif choice == '3': state = input("TYPE 'ON' OR 'OFF': ").upper() psu.write(f'OUTP CH1,{state}') elif choice == '4': if not test_running: try: sec = float(input("ENTER TOGGLE INTERVAL IN SECONDS: ")) test_running = True t = threading.Thread(target=test_worker, args=(sec,), daemon=True) t.start() except ValueError: print("INVALID TIME ENTERED.") else: print("TEST IS ALREADY RUNNING!") elif choice == '5': print("STOPPING TEST...") test_running = False elif choice == '6': test_running = False # Ensure thread stops psu.close() scope.close() break else: print("INVALID ENTRY. PLEASE CHOOSE 1-6") if __name__ == "__main__": # Optional: Run auto-config on startup main_menu()