Merge branch 'js/distance-sensor' into 'main'

Add control library for distance sensor and display

See merge request capstone-group2/ui!13
This commit is contained in:
Johnathon Slightham
2026-03-04 07:55:35 -05:00
4 changed files with 49 additions and 12 deletions

View File

@@ -19,6 +19,12 @@ public class ControlLibrary : MonoBehaviour
[DllImport("libc_control")] [DllImport("libc_control")]
private static extern IntPtr get_configuration(out int module_id); // the data this points to will be invalidated when called again private static extern IntPtr get_configuration(out int module_id); // the data this points to will be invalidated when called again
[DllImport("libc_control")]
public static extern int send_string_control(int module_id, string s);
[DllImport("libc_control")]
public static extern double get_distance_control(int module_id);
[DllImport("libc_control")] [DllImport("libc_control")]
public static extern bool control_sentry_init( public static extern bool control_sentry_init(
string dsn, string dsn,

View File

@@ -1,4 +1,5 @@
using UnityEngine; using UnityEngine;
using System;
public class DisplayModule : ModuleBase public class DisplayModule : ModuleBase
{ {
@@ -17,5 +18,6 @@ public class DisplayModule : ModuleBase
// Replace with actual hardware communication logic // Replace with actual hardware communication logic
Debug.Log($"[DisplayModule] Sending display text: {text}"); Debug.Log($"[DisplayModule] Sending display text: {text}");
// Example: ControlLibrary.send_display_text(Int32.Parse(moduleID), text); // Example: ControlLibrary.send_display_text(Int32.Parse(moduleID), text);
ControlLibrary.send_string_control(Int32.Parse(moduleID), text);
} }
} }

View File

@@ -1,20 +1,48 @@
using System;
using UnityEngine;
public class DistanceSensorModule : ModuleBase public class DistanceSensorModule : ModuleBase
{ {
public bool objectDetected; // Keep a fixed-size array (no GC alloc every frame)
public float distanceMeters; public string[] infoLines = new string[2];
public string[] infoLines = new string[0];
public override string moduleType => "Distance"; public override string moduleType => "Distance";
public override string moduleName => "Distance Sensor Module"; public override string moduleName => "Distance Sensor Module";
// Optional: slow down polling to avoid spamming hardware
[SerializeField] float pollHz = 10f;
float nextPollTime;
void Update() void Update()
{ {
// Replace with your ControlLibrary call pattern // Always show something immediately
// var reading = ControlLibrary.get_distance(...) infoLines[0] = $"ModuleID raw: '{moduleID}'";
if (!objectDetected) // Poll at a fixed rate (avoids frame spam)
infoLines = new[] {"Object: Not detected" }; if (Time.unscaledTime < nextPollTime) return;
else nextPollTime = Time.unscaledTime + (1f / Mathf.Max(1f, pollHz));
infoLines = new[] { "Object: Detected", $"Distance: {distanceMeters:F2} m" };
if (!int.TryParse(moduleID, out var id))
{
infoLines[1] = "ERROR: moduleID is not an int";
return;
}
try
{
// Control library call
double distance = ControlLibrary.get_distance_control(id);
infoLines[1] = $"Distance: {distance:F0} mm";
}
catch (Exception e)
{
// SHOW THE PROBLEM ON SCREEN (works in release builds)
infoLines[1] = $"EXCEPTION: {e.GetType().Name}: {e.Message}";
// Also log to Player.log (works in builds)
Debug.LogException(e);
}
} }
public string[] GetInfoLines() => infoLines; public string[] GetInfoLines() => infoLines;

View File

@@ -77,9 +77,10 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: e4735e9a5ae30490bba1dc35e4603bce, type: 3} m_Script: {fileID: 11500000, guid: e4735e9a5ae30490bba1dc35e4603bce, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: '::' m_EditorClassIdentifier: '::'
objectDetected: 0 infoLines:
distanceMeters: 0 -
infoLines: [] -
pollHz: 10
--- !u!1 &5041994602013865196 --- !u!1 &5041994602013865196
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0