import csv import time from hil_lib import SCD30_HIL_Lib # Device paths from your environment EMULATOR = '/dev/i2c_emulator' TARGET = '/dev/esp_sensor_test' LOG_FILE = 'hil_validation_results.csv' # Initialize HIL hil = SCD30_HIL_Lib(EMULATOR, TARGET) print(f"--- SCD30 HIL Test Started ---") print(f"Logging to: {LOG_FILE}") with open(LOG_FILE, mode='w', newline='') as f: writer = csv.writer(f) writer.writerow(["Timestamp", "Simulated_CO2", "Target_Received_Msg", "Status"]) # Test cases: varying CO2 levels test_points = [400.0, 850.5, 1200.0, 2500.0, 5000.0] try: for co2 in test_points: print(f"\n[PHASE] Setting Simulated CO2 to {co2} ppm") # 1. Feed the bridge until the Target performs a successful Read # We loop briefly to wait for the Target's polling interval timeout = time.time() + 10 data_served = False while time.time() < timeout: if hil.process_bridge(co2, 24.0, 50.0): data_served = True break if not data_served: print("Error: Target didn't request data within 10s.") continue # 2. Capture what the Target actually saw # Wait for Target to print the result to Serial time.sleep(0.5) target_msg = hil.get_target_reading() # 3. Simple Validation status = "PASS" if str(int(co2)) in target_msg else "CHECK" print(f"[RESULT] Target Output: {target_msg}") writer.writerow([time.strftime("%H:%M:%S"), co2, target_msg, status]) f.flush() except KeyboardInterrupt: print("\nTest Aborted.") finally: hil.close() print("--- HIL Terminated ---")