Files
ui/Assets/DistanceSensorModule.cs
2026-03-08 21:39:00 -04:00

44 lines
1.2 KiB
C#

using System;
using UnityEngine;
public class DistanceSensorModule : ModuleBase
{
// Keep a fixed-size array (no GC alloc every frame)
public string[] infoLines = new string[2];
public override string moduleType => "Distance";
public override string moduleName => "Distance Sensor Module";
// Optional: slow down polling to avoid spamming hardware
[SerializeField] float pollHz = 10f;
float nextPollTime;
void Update()
{
// Always show something immediately
infoLines[0] = $"ModuleID raw: '{moduleID}'";
// Poll at a fixed rate (avoids frame spam)
if (Time.unscaledTime < nextPollTime) return;
nextPollTime = Time.unscaledTime + (1f / Mathf.Max(1f, pollHz));
if (!int.TryParse(moduleID, out var id))
{
infoLines[1] = "ERROR: moduleID is not an int";
return;
}
try
{
double distance = ControlLibrary.get_distance_control(id);
infoLines[1] = $"Distance: {distance:F0} mm";
}
catch (Exception e)
{
infoLines[1] = $"EXCEPTION: {e.GetType().Name}: {e.Message}";
Debug.LogException(e);
}
}
public string[] GetInfoLines() => infoLines;
}