From 6006d4daf30184186c881b1b55f03d5b39fbd1c8 Mon Sep 17 00:00:00 2001 From: freezy Date: Mon, 30 Nov 2026 17:33:10 +0100 Subject: [PATCH 1/3] outputs: light the toy a port names in the port test A port whose LedWiz equivalent output names a toy, such as an LED strip, was refused by the port test. The test now lights that toy for its duration instead, through the same output gate. The new vpe_dof_test_toy (ABI 9) finds the toy as vpe_dof_describe reports it and, on DOF's main thread, sets a layer of its own (1000000) above the ones effects use: a strip or another matrix gets white at a quarter (64 per channel) on every LED, so a whole strip doesn't overload its supply, an RGB toy white, and a toy with one output the value. Clearing leaves the layer transparent, since a strip with no layer stops sending. A toy the test can't light returns VPE_DOF_NOT_SUPPORTED, and the toy's type and name come back for the message. --- .../DofInstanceTests.cs | 53 ++++++++- VisualPinball.Engine.DOF.Test/TestConfig.cs | 18 ++- .../Runtime/Player/DofHardware.cs | 64 +++++++++-- .../Tests/DofPluginTests.cs | 50 +++++++- VisualPinball.Engine.DOF/DofException.cs | 2 + VisualPinball.Engine.DOF/DofInstance.cs | 33 +++++- VisualPinball.Engine.DOF/Native/DofNative.cs | 5 +- native/include/vpe_dof.h | 20 +++- native/src/vpe_dof.cpp | 42 +++++++ native/src/vpe_dof_toys.h | 108 ++++++++++++++++++ native/tests/internal.cpp | 90 +++++++++++++++ 11 files changed, 465 insertions(+), 20 deletions(-) create mode 100644 native/src/vpe_dof_toys.h diff --git a/VisualPinball.Engine.DOF.Test/DofInstanceTests.cs b/VisualPinball.Engine.DOF.Test/DofInstanceTests.cs index 051ccc6..0ef2029 100644 --- a/VisualPinball.Engine.DOF.Test/DofInstanceTests.cs +++ b/VisualPinball.Engine.DOF.Test/DofInstanceTests.cs @@ -171,7 +171,7 @@ private void BoundedCleanup(DofInstance dof) [Test] public void ReportsAbiAndLibdofVersion() { - Assert.That(DofLibrary.NativeAbiVersion, Is.EqualTo(8)); + Assert.That(DofLibrary.NativeAbiVersion, Is.EqualTo(9)); Assert.That(DofLibrary.LibdofVersion, Does.Match(@"^\d+\.\d+\.\d+")); } @@ -474,6 +474,57 @@ public void PortTestGoesThroughTheGate() dof.Finish(); } + [Test] + public void PortTestLightsTheToyAPortNames() + { + var unit = Unit("ArtNetA"); + var strip = Unit("ArtNetStrip"); + strip.Ports = false; + // port 9 names an LED strip of 4 x 2 LEDs on a controller of its own, port 10 the equivalent itself, which no test lights + using var config = new TestConfig().GlobalConfig() + .Cabinet(8, new Dictionary { [9] = "Matrix1", [10] = "LedWizEquivalent 1" }, TestConfig.LedStrip("Matrix1", strip, 4, 2), unit, strip); + using var dof = DofInstance.Create(config.Root); + // as the port test runs DOF: dark, without connect tests and without a table + dof.SetOutputGate(false); + dof.SetConnectTests(false); + dof.Init(null, null); + var cabinet = dof.Describe(); + Assert.That(cabinet.Devices.Single().KindOf(9), Is.EqualTo(DofPortKind.Toy)); + Assert.That(cabinet.Toys.Select(t => (t.Type, t.Name)), Does.Contain(("LedStrip", "Matrix1"))); + + Assert.That(dof.TestToy(1, 9, 255, out var toy), Is.EqualTo(DofStatus.Ok)); + Assert.That((toy.Type, toy.Name), Is.EqualTo(("LedStrip", "Matrix1"))); + // DOF's main thread runs the test before a closing requested after it + WaitSettled(dof, dof.SetOutputGate(false)); + ExpectFrameAfter(strip.Universe, 0, "a frame of the strip while closed"); + var opening = _capture.Mark(strip.Universe); + Assert.That(FramesBetween(strip.Universe, 0, opening).All(f => f.All(v => v == 0)), Is.True, "the strip stays dark while the gate is closed"); + + Assert.That(WaitSettled(dof, dof.SetOutputGate(true)).Written, Is.GreaterThanOrEqualTo(1)); + var lit = ExpectIndex(strip.Universe, f => f.Take(24).All(v => v == 64), "every LED of the strip in dim white", opening); + Assert.That(_capture.Frames(strip.Universe)[lit].Skip(24).All(v => v == 0), Is.True, "nothing past the strip's LEDs"); + Assert.That(_capture.Frames(unit.Universe).All(f => f.Take(8).All(v => v == 0)), Is.True, "no controller output"); + + WaitSettled(dof, dof.SetOutputGate(false)); + ExpectIndex(strip.Universe, f => f.All(v => v == 0), "the strip dark once the gate closed", lit + 1); + Assert.That(dof.TestToy(1, 9, 0, out _), Is.EqualTo(DofStatus.Ok)); + // cleared before the gate opens again: the strip shows nothing + WaitSettled(dof, dof.SetOutputGate(true)); + var cleared = _capture.Mark(strip.Universe); + ExpectFrameAfter(strip.Universe, cleared, "a frame of the strip after it was cleared"); + Assert.That(FramesBetween(strip.Universe, cleared).All(f => f.All(v => v == 0)), Is.True, "a cleared strip is dark"); + + Assert.That(dof.TestToy(1, 10, 255, out toy), Is.EqualTo(DofStatus.NotSupported), "a toy a test can't light"); + Assert.That((toy.Type, toy.Name), Is.EqualTo(("LedWizEquivalent", "LedWizEquivalent 1"))); + Assert.That(dof.TestToy(1, 1, 255, out toy), Is.EqualTo(DofStatus.InvalidArgument), "a port that drives a controller output"); + Assert.That(toy.Name, Is.Null); + Assert.That(dof.TestToy(1, 99, 255, out _), Is.EqualTo(DofStatus.InvalidArgument), "unknown port"); + Assert.That(dof.TestToy(2, 9, 255, out _), Is.EqualTo(DofStatus.InvalidArgument), "unknown device"); + Assert.That(dof.TestOutput(1, 9, 255), Is.EqualTo(DofStatus.InvalidArgument), "the output test doesn't set a toy"); + dof.Finish(); + Assert.That(dof.TestToy(1, 9, 255, out _), Is.EqualTo(DofStatus.InvalidState), "after finish"); + } + [Test] public void OnlyOneInstanceAtATime() { diff --git a/VisualPinball.Engine.DOF.Test/TestConfig.cs b/VisualPinball.Engine.DOF.Test/TestConfig.cs index 2728c1c..c923f11 100644 --- a/VisualPinball.Engine.DOF.Test/TestConfig.cs +++ b/VisualPinball.Engine.DOF.Test/TestConfig.cs @@ -6,6 +6,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Text; namespace VisualPinball.Engine.DOF.Test @@ -16,6 +17,8 @@ public sealed class ArtNetUnit public string Name; public int Universe; public string Address = "127.0.0.1"; + /// Whether ports of the LedWiz equivalent drive its channels; not for one only a toy uses, such as an LED strip. + public bool Ports = true; } /// @@ -64,7 +67,10 @@ public TestConfig GlobalConfig(string tableConfigPattern = null, string logPatte /// public TestConfig Cabinet(int perUnit, params ArtNetUnit[] units) => Cabinet(perUnit, null, units); - public TestConfig Cabinet(int perUnit, IDictionary extraPorts, params ArtNetUnit[] units) + public TestConfig Cabinet(int perUnit, IDictionary extraPorts, params ArtNetUnit[] units) => Cabinet(perUnit, extraPorts, null, units); + + /// More toys, as cabinet XML elements (), after the equivalent. + public TestConfig Cabinet(int perUnit, IDictionary extraPorts, string extraToys, params ArtNetUnit[] units) { var xml = new StringBuilder(); xml.AppendLine(""); @@ -82,7 +88,7 @@ public TestConfig Cabinet(int perUnit, IDictionary extraPorts, para xml.AppendLine(" 1"); xml.AppendLine(" "); var port = 1; - foreach (var unit in units) { + foreach (var unit in units.Where(u => u.Ports)) { for (var channel = 1; channel <= perUnit; channel++, port++) { xml.AppendLine($" {unit.Name}.{channel:00}{port}"); } @@ -94,11 +100,19 @@ public TestConfig Cabinet(int perUnit, IDictionary extraPorts, para } xml.AppendLine(" "); xml.AppendLine(" "); + if (extraToys != null) { + xml.AppendLine(extraToys); + } xml.AppendLine(" "); xml.AppendLine(""); return Write("Cabinet.xml", xml.ToString()); } + /// An LED strip of a cabinet, in RGB order, whose LEDs are the first channels of an Art-Net controller. + public static string LedStrip(string name, ArtNetUnit unit, int width, int height) + => $" {name}{width}{height}1" + + $"RGB{unit.Name}"; + /// An ini file (directoutputconfig.ini for device 1, directoutputconfigN.ini otherwise) with table rows. public TestConfig Ini(int device, params string[] rows) => Write(device == 1 ? "directoutputconfig.ini" : $"directoutputconfig{device}.ini", IniText(rows)); diff --git a/VisualPinball.Engine.DOF.Unity/Runtime/Player/DofHardware.cs b/VisualPinball.Engine.DOF.Unity/Runtime/Player/DofHardware.cs index c3f7a02..9786559 100644 --- a/VisualPinball.Engine.DOF.Unity/Runtime/Player/DofHardware.cs +++ b/VisualPinball.Engine.DOF.Unity/Runtime/Player/DofHardware.cs @@ -113,7 +113,7 @@ internal static bool TryParse(string id, out int device, out int port) internal static string Id(int device, int port) => device + ":" + port; /// - /// Opens DOF dark and waits until every unit confirmed it, sets the port, opens the gate for + /// Opens DOF dark and waits until every unit confirmed it, sets the port (or lights the toy it names), opens the gate for /// and closes it again before the port is turned off and DOF finishes. Runs on the /// calling thread and blocks; call it from a worker. /// @@ -140,9 +140,9 @@ internal static (bool Success, string Message) Pulse(DofConfigStore store, Plugi return (false, $"{Units(dark.Units, "DOF")} did not confirm within {confirmTimeout.TotalSeconds:0.#} s that {Their(dark.Units)} outputs " + "are off, so nothing was turned on: a controller that is still connecting could run its own test."); } + var devices = Devices(native); if (dark.State.Written == 0) { // most likely the board of the device isn't there; DOF says so before the gate would open - var devices = Devices(native); if (devices != null && !devices.Any(d => d.Number == device)) { return (false, NotSettable(devices, dark.HasCabinet, device, port, output)); } @@ -150,11 +150,17 @@ internal static (bool Success, string Message) Pulse(DofConfigStore store, Plugi ? $"No controller can send the pulse: {Units(dark.Units, "every controller")} failed, so nothing was turned on." : "No controller took part, so nothing was turned on."); } - var status = native.TestOutput(device, port, 255); + // A port that names a toy, such as an LED strip, lights the toy instead of a controller output. + var isToy = devices != null && devices.Any(d => d.Number == device && d.KindOf(port) == DofPortKind.Toy); + DofToy toy = default; + var status = isToy ? native.TestToy(device, port, 255, out toy) : native.TestOutput(device, port, 255); if (status != DofStatus.Ok) { + if (status == DofStatus.NotSupported) { + return (false, $"On device {device}, {What(port, output)} names {ToyName(toy)}, which a test can't light."); + } return (false, status == DofStatus.InvalidArgument - ? NotSettable(Devices(native), dark.HasCabinet, device, port, output) - : $"DOF could not set output {output} of device {device} ({status})."); + ? NotSettable(devices, dark.HasCabinet, device, port, output) + : $"DOF could not {(isToy ? "light the toy on" : "set")} output {output} of device {device} ({status})."); } if (cancellation.IsCancellationRequested) { return (false, Cancelled); @@ -164,13 +170,26 @@ internal static (bool Success, string Message) Pulse(DofConfigStore store, Plugi var cancelled = cancellation.WaitHandle.WaitOne(duration); native.SetOutputGate(false); var open = watch.Elapsed; - native.TestOutput(device, port, 0); + if (isToy) { + native.TestToy(device, port, 0, out _); + } else { + native.TestOutput(device, port, 0); + } // The units that wrote the opening. Which of them drives the tested port is not known, so this counts the // controllers that wrote the open gate, not that the port got the pulse; and a write can land after the count. var wrote = opening >= 0 ? native.GetGateState(opening).Written : 0; var failed = dark.State.Failed > 0 ? $" {Units(dark.Units, "A controller")} failed and took no part." : string.Empty; // The times are the gate's, as requested: a controller writes the opening a little later and, when the // gate closes early, maybe not at all. + if (isToy) { + var lit = $"{ToyName(toy)} on output {output} of device {device}"; + if (cancelled) { + return (false, $"Cancelled after {open.TotalSeconds:0.##} s: {lit} was to be lit for {duration.TotalSeconds:0.##} s.{failed}"); + } + return wrote > 0 + ? (true, $"Lit {lit} for {duration.TotalSeconds:0.##} s: {Controllers(wrote)} acknowledged the gate opening.{failed}") + : (false, $"Requested to light {lit} for {duration.TotalSeconds:0.##} s, but no controller confirmed writing it.{failed}"); + } if (cancelled) { return (false, $"Cancelled after {open.TotalSeconds:0.##} s: the pulse to output {output} of device {device} was requested for " + $"{duration.TotalSeconds:0.##} s.{failed}"); @@ -184,6 +203,30 @@ internal static (bool Success, string Message) Pulse(DofConfigStore store, Plugi private const string Cancelled = "Cancelled; nothing was turned on."; + /// A toy for a message, by its kind and name: "LED strip Matrix1". + internal static string ToyName(DofToy toy) + { + string kind; + switch (toy.Type) { + case "LedStrip": kind = "LED strip"; break; + case "RGBLed": kind = "RGB LED"; break; + case "RGBAToy": kind = "RGB toy"; break; + case "RGBAToyGroup": kind = "group of RGB toys"; break; + case "Lamp": kind = "lamp"; break; + case "Motor": kind = "motor"; break; + case "GearMotor": kind = "gear motor"; break; + case "Shaker": kind = "shaker"; break; + case "AnalogAlphaToyGroup": kind = "group of toys"; break; + case null: + case "": + case "AnalogAlphaToy": kind = "toy"; break; + default: kind = toy.Type; break; + } + return string.IsNullOrEmpty(toy.Name) ? kind : $"{kind} {toy.Name}"; + } + + private static string What(int port, int output) => output == port ? $"output {output}" : $"port {port} (wired to output {output})"; + private static IReadOnlyList Devices(IDofTestNative native) { try { @@ -194,14 +237,14 @@ private static IReadOnlyList Devices(IDofTestNative native) } /// - /// Why DOF can't set a port: it has no such device, the device has fewer ports, or the port drives a toy or nothing. + /// Why DOF can't set a port: it has no such device, the device has fewer ports, or the port drives nothing. /// /// The devices DOF set up; null if unknown. /// Whether the configuration has a cabinet file, which sets up the devices. /// The output the port drives, as the page shows it. internal static string NotSettable(IReadOnlyList devices, bool hasCabinet, int device, int port, int output) { - var what = output == port ? $"output {output}" : $"port {port} (wired to output {output})"; + var what = What(port, output); if (devices == null) { return $"Device {device} has no {what} DOF can set."; } @@ -227,7 +270,7 @@ internal static string NotSettable(IReadOnlyList devices, bool h return $"Device {device} goes up to {(output == port ? "output" : "port")} {ports.Value.Count}, so there is no {what}. A game can't fire it either."; } return ports.Value.KindOf(port) == DofPortKind.Toy - ? $"On device {device}, {what} names a toy, such as an LED strip, which a test can't turn on directly." + ? $"On device {device}, {what} names a toy DOF can't find." : $"On device {device}, {what} is connected to no controller output. A game can't fire it either."; } @@ -433,6 +476,8 @@ internal interface IDofTestNative : IDisposable DofGateState GetGateState(long generation); IReadOnlyList GetGatePending(long generation); DofStatus TestOutput(int device, int port, int value); + /// Lights the toy a port names (), and clears it with 0. + DofStatus TestToy(int device, int port, int value, out DofToy toy); void Finish(); } @@ -450,6 +495,7 @@ internal sealed class DofTestInstance : IDofTestNative public DofGateState GetGateState(long generation) => _instance.GetGateState(generation); public IReadOnlyList GetGatePending(long generation) => _instance.GetGatePending(generation); public DofStatus TestOutput(int device, int port, int value) => _instance.TestOutput(device, port, value); + public DofStatus TestToy(int device, int port, int value, out DofToy toy) => _instance.TestToy(device, port, value, out toy); public void Finish() => _instance.Finish(); public void Dispose() => _instance.Dispose(); } diff --git a/VisualPinball.Engine.DOF.Unity/Tests/DofPluginTests.cs b/VisualPinball.Engine.DOF.Unity/Tests/DofPluginTests.cs index bcd3b9b..1e90929 100644 --- a/VisualPinball.Engine.DOF.Unity/Tests/DofPluginTests.cs +++ b/VisualPinball.Engine.DOF.Unity/Tests/DofPluginTests.cs @@ -292,7 +292,9 @@ public void SaysWhyATestCantSetAPort() host.Factory.PulseDuration = TimeSpan.FromMilliseconds(20); host.Import(371, "vpetest,S1", null, "\"Rom\",\"Flasher\""); Assert.That(Pulse(host, "1:1").Success, Is.True); - Assert.That(Pulse(host, "1:6").Message, Is.EqualTo("On device 1, output 6 names a toy, such as an LED strip, which a test can't turn on directly.")); + native.Toy = new DofToy("LedWizEquivalent", "LedWizEquivalent 1"); + native.ToyStatus = DofStatus.NotSupported; + Assert.That(Pulse(host, "1:6").Message, Is.EqualTo("On device 1, output 6 names LedWizEquivalent LedWizEquivalent 1, which a test can't light.")); Assert.That(Pulse(host, "1:7").Message, Is.EqualTo("On device 1, output 7 is connected to no controller output. A game can't fire it either.")); Assert.That(Pulse(host, "1:9").Message, Is.EqualTo("Device 1 goes up to output 7, so there is no output 9. A game can't fire it either.")); native.Ports = new DofDevicePorts[0]; @@ -315,6 +317,37 @@ public void SaysWhyATestCantSetAPort() Assert.That(DofPortTest.NotSettable(new[] { new DofDevicePorts(51, "ooooooo+") }, false, 51, 8, 8), Is.EqualTo("Device 51 has no output 8 DOF can set."), "nor past what it describes"); } + [Test] + public void LightsTheToyAPortNames() + { + var native = new RecordingTestNative { Ports = new[] { new DofDevicePorts(1, "ooooot-") } }; + var host = Host(); + host.Factory.TestNativeFactory = _ => native; + host.Factory.PulseDuration = TimeSpan.FromMilliseconds(20); + host.Import(372, "vpetest,S1", null, "\"Rom\",\"Flasher\""); + var result = Pulse(host, "1:6"); + Assert.That(result.Success, Is.True, result.Message); + Assert.That(native.Calls, Is.EqualTo(new[] { "gate:closed", "connect-tests:off", "init", "gate:closed", "confirmed", "toy 1:6=255", "gate:open", "gate:closed", "toy 1:6=0", "finish", "dispose" }), + "the toy is lit and cleared where an output would be set, with the same gate"); + Assert.That(result.Message, Is.EqualTo("Lit LED strip Matrix1 on output 6 of device 1 for 0.02 s: 1 controller acknowledged the gate opening.")); + + native.Calls.Clear(); + Assert.That(Pulse(host, "1:1").Success, Is.True); + Assert.That(native.Calls, Does.Contain("1:1=255").And.Not.Contain("toy 1:1=255"), "a port that drives an output still sets it"); + + native.SendsOpening = false; + result = Pulse(host, "1:6"); + Assert.That(result.Success, Is.False); + Assert.That(result.Message, Is.EqualTo("Requested to light LED strip Matrix1 on output 6 of device 1 for 0.02 s, but no controller confirmed writing it.")); + native.SendsOpening = true; + + Assert.That(DofPortTest.ToyName(new DofToy("RGBLed", "Flasher")), Is.EqualTo("RGB LED Flasher")); + Assert.That(DofPortTest.ToyName(new DofToy("Shaker", "")), Is.EqualTo("shaker")); + Assert.That(DofPortTest.ToyName(new DofToy("SomeToy", "Topper")), Is.EqualTo("SomeToy Topper"), "a type without a name of its own"); + Assert.That(DofPortTest.ToyName(default), Is.EqualTo("toy")); + AssertDofIsFree("released after the test"); + } + [Test] public void TheOutputMapShowsRgbToysAsTheirChannels() { @@ -1071,6 +1104,21 @@ public DofGateState GetGateState(long generation) } public IReadOnlyList GetGatePending(long generation) => (ConfirmAfter >= 0 && _polls > ConfirmAfter ? new string[0] : Units).Concat(FailedUnits).ToList(); + /// The toy a port that names one names, and what lighting it returns. + public DofToy Toy = new DofToy("LedStrip", "Matrix1"); + public DofStatus ToyStatus = DofStatus.Ok; + public DofStatus TestToy(int device, int port, int value, out DofToy toy) + { + Calls.Add($"toy {device}:{port}={value}"); + toy = default; + foreach (var d in Ports ?? new DofDevicePorts[0]) { + if (d.Number == device && d.KindOf(port) == DofPortKind.Toy) { + toy = Toy; + return ToyStatus; + } + } + return DofStatus.InvalidArgument; + } public DofStatus TestOutput(int device, int port, int value) { Calls.Add($"{device}:{port}={value}"); diff --git a/VisualPinball.Engine.DOF/DofException.cs b/VisualPinball.Engine.DOF/DofException.cs index 1bef08a..13e29cc 100644 --- a/VisualPinball.Engine.DOF/DofException.cs +++ b/VisualPinball.Engine.DOF/DofException.cs @@ -16,6 +16,8 @@ public enum DofStatus InvalidState = 3, /// Another DOF instance exists in this process. Busy = 4, + /// The port names a toy that a port test can't light, such as a LedWiz equivalent. + NotSupported = 5, } /// A failed VpeDof call, with libdof's message where it gave one. diff --git a/VisualPinball.Engine.DOF/DofInstance.cs b/VisualPinball.Engine.DOF/DofInstance.cs index 8e6a5d9..90bc777 100644 --- a/VisualPinball.Engine.DOF/DofInstance.cs +++ b/VisualPinball.Engine.DOF/DofInstance.cs @@ -91,7 +91,7 @@ public enum DofPortKind None, /// An output of a controller, which sets. Output, - /// A toy, such as an LED strip, which only its effects set. + /// A toy, such as an LED strip, which lights. Toy, } @@ -442,7 +442,8 @@ public IReadOnlyList GetGatePending(long generation) /// /// /// if the device or port does not exist or the port names a toy instead of - /// a controller output; before init or after finish. + /// a controller output ( lights that); before init or after + /// finish. /// public DofStatus TestOutput(int device, int port, int value) { @@ -454,6 +455,34 @@ public DofStatus TestOutput(int device, int port, int value) } } + /// + /// Lights the toy a port of an ini device names instead of a controller output, such as an LED strip, through the + /// output gate: (0 to 255) on a layer of its own above the effects' layers, and 0 clears it. + /// An LED strip or another matrix gets white at a quarter of the value on every LED, so a whole strip doesn't + /// overload its supply; an RGB toy gets white at the value, and a toy with one output the value. + /// + /// The toy the port names, as lists it; default if it names none. + /// + /// if the device or port does not exist or the port names no toy; + /// for a toy a test can't light; before + /// init or after finish. + /// + public DofStatus TestToy(int device, int port, int value, out DofToy toy) + { + var buffer = new byte[ErrorSize]; + int status; + EnterOrThrow(); + try { + status = DofNative.TestToy(_dof, device, port, value, buffer, buffer.Length); + } finally { + Leave(); + } + var text = Utf8.DecodeBuffer(buffer); + var separator = text.IndexOf('|'); + toy = separator < 0 ? default : new DofToy(text.Substring(0, separator), text.Substring(separator + 1)); + return (DofStatus)status; + } + /// The output controllers and toys, after . public DofCabinetDescription Describe() { diff --git a/VisualPinball.Engine.DOF/Native/DofNative.cs b/VisualPinball.Engine.DOF/Native/DofNative.cs index eb2aff2..cd6c5de 100644 --- a/VisualPinball.Engine.DOF/Native/DofNative.cs +++ b/VisualPinball.Engine.DOF/Native/DofNative.cs @@ -15,7 +15,7 @@ namespace VisualPinball.Engine.DOF.Native internal static class DofNative { public const string Library = "VpeDof"; - public const int AbiVersion = 8; + public const int AbiVersion = 9; [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate void LogCallback(int level, IntPtr message, IntPtr user); @@ -77,6 +77,9 @@ internal static class DofNative [DllImport(Library, CallingConvention = CallingConvention.Cdecl, EntryPoint = "vpe_dof_test_output")] public static extern int TestOutput(IntPtr dof, int device, int port, int value); + [DllImport(Library, CallingConvention = CallingConvention.Cdecl, EntryPoint = "vpe_dof_test_toy")] + public static extern int TestToy(IntPtr dof, int device, int port, int value, byte[] toy, int toySize); + [DllImport(Library, CallingConvention = CallingConvention.Cdecl, EntryPoint = "vpe_dof_describe")] public static extern int Describe(IntPtr dof, StringCallback callback, IntPtr user); diff --git a/native/include/vpe_dof.h b/native/include/vpe_dof.h index 34b0c18..810ae19 100644 --- a/native/include/vpe_dof.h +++ b/native/include/vpe_dof.h @@ -36,7 +36,7 @@ extern "C" { #endif /// Version of this ABI. Bumped on every incompatible change; the managed binding refuses a library with another one. -#define VPE_DOF_ABI_VERSION 8 +#define VPE_DOF_ABI_VERSION 9 enum vpe_dof_status { @@ -45,6 +45,7 @@ enum vpe_dof_status VPE_DOF_INVALID_ARGUMENT = 2, VPE_DOF_INVALID_STATE = 3, VPE_DOF_BUSY = 4, + VPE_DOF_NOT_SUPPORTED = 5, }; enum vpe_dof_log_level @@ -147,9 +148,19 @@ VPE_DOF_API int32_t VPE_DOF_CALL vpe_dof_gate_pending(vpe_dof* dof, int64_t gene /// Sets one port of an ini device (the N of directoutputconfigN.ini; 1 for directoutputconfig.ini) to a value, as the /// port test does. Runs on DOF's main thread and goes through the output gate. VPE_DOF_INVALID_ARGUMENT if the device -/// or port does not exist, or the port names a toy (an LED strip or matrix) instead of a controller output. +/// or port does not exist, or the port names a toy (an LED strip or matrix) instead of a controller output: see +/// vpe_dof_test_toy. VPE_DOF_API int32_t VPE_DOF_CALL vpe_dof_test_output(vpe_dof* dof, int32_t device, int32_t port, int32_t value); +/// Lights the toy a port of an ini device names instead of a controller output, such as an LED strip, as the port test +/// does: `value` (0 to 255) on a layer of its own above the layers of effects, and 0 clears it. A matrix toy, such as an +/// LED strip, gets white at a quarter of `value` on every LED, so a whole strip does not overload its supply; an RGB toy +/// gets white at `value` and a toy with one output `value`. Runs on DOF's main thread, whose cabinet update sends it, and +/// goes through the output gate. `toy`, if given, gets "|" of the toy the port names, as vpe_dof_describe +/// lists it, and is empty for another port. VPE_DOF_INVALID_ARGUMENT if the device or port does not exist or the port +/// names no toy; VPE_DOF_NOT_SUPPORTED for a toy the test can't light, such as a LedWiz equivalent. +VPE_DOF_API int32_t VPE_DOF_CALL vpe_dof_test_toy(vpe_dof* dof, int32_t device, int32_t port, int32_t value, char* toy, int32_t toy_size); + /// Routes the ports of an ini device (the N of directoutputconfigN.ini) to other outputs of its LedWiz equivalent, for /// cabling that differs from the configuration: port i + 1 drives the output port targets[i] drives in the cabinet, its /// own for 0 or i + 1, and nothing for a negative target. A port whose target the equivalent has no output for drives @@ -161,8 +172,9 @@ VPE_DOF_API int32_t VPE_DOF_CALL vpe_dof_set_output_map(vpe_dof* dof, int32_t de /// After init: lists "controller|||" and "toy||" lines describing the cabinet, and a /// "device||" line for each LedWiz equivalent, which vpe_dof_test_output sets: a character per port from 1, -/// 'o' for one that drives a controller output, 't' for one that names a toy, such as an LED strip, and '-' for one that -/// drives nothing; up to port 4096, followed by a '+' if the device has ports beyond. +/// 'o' for one that drives a controller output, 't' for one that names a toy, such as an LED strip, which +/// vpe_dof_test_toy lights, and '-' for one that drives nothing; up to port 4096, followed by a '+' if the device has +/// ports beyond. VPE_DOF_API int32_t VPE_DOF_CALL vpe_dof_describe(vpe_dof* dof, vpe_dof_string_fn callback, void* user); /// Lists the table rows of the ini files libdof finds under `base_path` for `table_file` as "|" lines, diff --git a/native/src/vpe_dof.cpp b/native/src/vpe_dof.cpp index 4b6d506..f813839 100644 --- a/native/src/vpe_dof.cpp +++ b/native/src/vpe_dof.cpp @@ -8,6 +8,7 @@ #include "vpe_dof.h" #include "vpe_dof_text.h" +#include "vpe_dof_toys.h" #include #include @@ -754,6 +755,47 @@ VPE_DOF_API int32_t VPE_DOF_CALL vpe_dof_test_output(vpe_dof* dof, int32_t devic } } +VPE_DOF_API int32_t VPE_DOF_CALL vpe_dof_test_toy(vpe_dof* dof, int32_t device, int32_t port, int32_t value, char* toy, int32_t toy_size) +{ + WriteError(toy, toy_size, ""); + if (!IsRunning(dof)) + return VPE_DOF_INVALID_STATE; + try + { + DOF::Cabinet* cabinet = dof->pinball->GetCabinet(); + DOF::ToyList* toys = cabinet ? cabinet->GetToys() : nullptr; + if (!toys) + return VPE_DOF_INVALID_ARGUMENT; + // The toy vpe_dof_describe reports as 't': on the first equivalent of the number, the first output of the port, and + // the first toy of its name. The cabinet's toys and their outputs are fixed after init. + const DOF::LedWizEquivalentOutput* output = nullptr; + for (DOF::IToy* other : *toys) + { + auto* lwe = dynamic_cast(other); + if (lwe && lwe->GetLedWizNumber() == device) + { + output = lwe->GetOutputs().FindByNumber(port); + break; + } + } + // a port that drives a controller output is set with vpe_dof_test_output + if (!output || output->GetOutput() || output->GetOutputName().empty()) + return VPE_DOF_INVALID_ARGUMENT; + DOF::IToy* target = toys->FindByName(output->GetOutputName()); + if (!target) + return VPE_DOF_INVALID_ARGUMENT; + WriteError(toy, toy_size, (Sanitize(TypeName(*target)) + "|" + Sanitize(target->GetName())).c_str()); + if (vpe_dof_toys::KindOf(target) == vpe_dof_toys::Kind::None) + return VPE_DOF_NOT_SUPPORTED; + const int v = std::clamp(value, 0, 255); + return dof->pinball->Post([target, v]() { vpe_dof_toys::Light(target, v); }) ? VPE_DOF_OK : VPE_DOF_INVALID_STATE; + } + catch (...) + { + return VPE_DOF_ERROR; + } +} + VPE_DOF_API int32_t VPE_DOF_CALL vpe_dof_set_output_map(vpe_dof* dof, int32_t device, const int32_t* targets, int32_t count) { if (!dof || device < 1 || device > 999 || count < 0 || count > 4096 || (count > 0 && !targets)) diff --git a/native/src/vpe_dof_toys.h b/native/src/vpe_dof_toys.h new file mode 100644 index 0000000..209566b --- /dev/null +++ b/native/src/vpe_dof_toys.h @@ -0,0 +1,108 @@ +// Visual Pinball Engine +// Copyright (C) 2026 freezy and VPE Team +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#pragma once + +#include + +#include "cab/toys/IAnalogAlphaToy.h" +#include "cab/toys/IMatrixToy.h" +#include "cab/toys/IRGBAToy.h" +#include "cab/toys/IToy.h" +#include "cab/toys/IToyUpdatable.h" +#include "cab/toys/virtual/AnalogAlphaToyGroup.h" +#include "cab/toys/virtual/RGBAToyGroup.h" +#include "general/analog/AnalogAlpha.h" +#include "general/color/RGBAColor.h" + +// How the port test lights a toy that a port of a LedWiz equivalent names, such as an LED strip, where other ports name a +// controller output: on a layer of its own, which the toy blends over the layers of effects as it blends theirs. +namespace vpe_dof_toys +{ + +// The layer the test sets. A toy blends its layers from the lowest number up, so this one is on top. Effects take their +// layer from the table config, where layers are numbered in the tens, and a port test runs without a table anyway. It +// leaves room for the offset a toy group adds to a layer when it passes it on to its toys. +constexpr int TestLayer = 1000000; + +// What each colour channel of a matrix toy, such as an LED strip, gets for the full test value: a quarter. A whole strip +// at full white draws the most current it can, some 60 mA per LED, which can overload a supply sized for what effects +// light at once; a quarter is still plain to see. The strip's own brightness and fading curve apply on top. +constexpr int MatrixTestLevel = 64; + +enum class Kind +{ + None, + // an LED strip or a group of RGB toys: dim white over the whole matrix + ColorMatrix, + // a group of toys with one output each: the value on each + ValueMatrix, + // an RGB toy, such as an RGB LED: white + Color, + // a toy with one output, such as a lamp, a motor or a shaker: the value + Value, +}; + +inline Kind KindOf(DOF::IToy* toy) +{ + if (dynamic_cast*>(toy)) + return Kind::ColorMatrix; + if (dynamic_cast*>(toy)) + return Kind::ValueMatrix; + if (dynamic_cast(toy)) + return Kind::Color; + if (dynamic_cast(toy)) + return Kind::Value; + return Kind::None; +} + +template void Fill(DOF::IMatrixToy* matrix, const Element& value) +{ + const int width = matrix->GetWidth(); + const int height = matrix->GetHeight(); + for (int y = 0; y < height; y++) + { + for (int x = 0; x < width; x++) + matrix->SetElement(TestLayer, x, y, value); + } +} + +// Sets the toy's test layer for `value` (0 to 255), or clears it with 0, as KindOf says. Run on DOF's main thread: its +// cabinet update then sends the toy's outputs to the controllers. Returns false for a toy the test can't light. +// A cleared layer stays, transparent: a strip with no layer sends nothing, so it would keep what the test lit. +inline bool Light(DOF::IToy* toy, int value) +{ + const int v = std::clamp(value, 0, 255); + switch (KindOf(toy)) + { + case Kind::ColorMatrix: + { + const int level = (v * MatrixTestLevel + 127) / 255; + Fill(dynamic_cast*>(toy), v > 0 ? DOF::RGBAColor(level, level, level, 255) : DOF::RGBAColor()); + break; + } + case Kind::ValueMatrix: + Fill(dynamic_cast*>(toy), v > 0 ? DOF::AnalogAlpha(v, 255) : DOF::AnalogAlpha()); + break; + case Kind::Color: + dynamic_cast(toy)->GetLayers().GetOrCreateLayer(TestLayer, 1)[0] = v > 0 ? DOF::RGBAColor(v, v, v, 255) : DOF::RGBAColor(); + break; + case Kind::Value: + dynamic_cast(toy)->GetLayers().GetOrCreateLayer(TestLayer, 1)[0] = v > 0 ? DOF::AnalogAlpha(v, 255) : DOF::AnalogAlpha(); + break; + default: + return false; + } + // A group passes its layers on to its toys when it updates, which can come after their own update in the cabinet's: + // passed on now, they send it with this one. + if (dynamic_cast(toy) || dynamic_cast(toy)) + { + if (auto* group = dynamic_cast(toy)) + group->UpdateToy(); + } + return true; +} + +} diff --git a/native/tests/internal.cpp b/native/tests/internal.cpp index 7ce1899..c369b4a 100644 --- a/native/tests/internal.cpp +++ b/native/tests/internal.cpp @@ -35,6 +35,10 @@ #include "cab/schedules/ScheduledSettings.h" #include "cab/toys/IMatrixToy.h" #include "cab/toys/ToyList.h" +#include "cab/toys/hardware/LedStrip.h" +#include "cab/toys/layer/AnalogAlphaToy.h" +#include "cab/toys/layer/RGBAToy.h" +#include "cab/toys/lwequivalent/LedWizEquivalent.h" #include "DOF/Config.h" #include "fx/EffectBase.h" #include "fx/EffectList.h" @@ -64,6 +68,7 @@ #endif #include "../src/vpe_dof_text.h" +#include "../src/vpe_dof_toys.h" using namespace DOF; @@ -207,6 +212,90 @@ void MatrixEffectWhoseToyFailsIsFinished() table.Finish(); } +bool IsColor(const RGBAColor& color, int red, int green, int blue, int alpha) +{ + return color.GetRed() == red && color.GetGreen() == green && color.GetBlue() == blue && color.GetAlpha() == alpha; +} + +// The port test lights a toy on a layer above the ones of effects, and clearing it gives back what they set. +void PortTestLightsEveryKindOfToy() +{ + using vpe_dof_toys::TestLayer; + const int dim = vpe_dof_toys::MatrixTestLevel; + + RGBAToy rgb; + Output red, green, blue; + rgb.SetRedOutput(&red); + rgb.SetGreenOutput(&green); + rgb.SetBlueOutput(&blue); + rgb.GetLayers().GetOrCreateLayer(1, 1)[0] = RGBAColor(255, 0, 0, 255); + CHECK(vpe_dof_toys::Light(&rgb, 255), "an RGB toy can be lit"); + rgb.UpdateOutputs(); + CHECK(red.GetOutput() == 255 && green.GetOutput() == 255 && blue.GetOutput() == 255, "an RGB toy shows white over an effect"); + vpe_dof_toys::Light(&rgb, 0); + rgb.UpdateOutputs(); + CHECK(red.GetOutput() == 255 && green.GetOutput() == 0 && blue.GetOutput() == 0, "a cleared RGB toy shows the effect again"); + + AnalogAlphaToy lamp; + Output output; + lamp.SetOutput(&output); + CHECK(vpe_dof_toys::Light(&lamp, 200), "a toy with one output can be lit"); + lamp.UpdateOutputs(); + CHECK(output.GetOutput() == 200, "a toy with one output gets the value"); + vpe_dof_toys::Light(&lamp, 0); + lamp.UpdateOutputs(); + CHECK(output.GetOutput() == 0, "a cleared toy with one output is off"); + + LedStrip strip; + strip.SetWidth(3); + strip.SetHeight(2); + CHECK(vpe_dof_toys::Light(&strip, 255), "an LED strip can be lit"); + bool dimWhite = true; + for (int y = 0; y < 2; y++) + { + for (int x = 0; x < 3; x++) + dimWhite &= IsColor(strip.GetElement(TestLayer, x, y), dim, dim, dim, 255); + } + CHECK(dimWhite, "every LED of a strip gets dim white"); + vpe_dof_toys::Light(&strip, 0); + bool clear = strip.GetLayers().GetLayer(TestLayer) != nullptr; + for (int y = 0; y < 2; y++) + { + for (int x = 0; x < 3; x++) + clear &= strip.GetElement(TestLayer, x, y).GetAlpha() == 0; + } + // a strip without a layer sends nothing, so it would stay lit + CHECK(clear, "a cleared strip keeps its layer, transparent"); + + // Groups pass the layer on to their toys at once, whichever updates first. + Pinball pinball; + ToyList* toys = pinball.GetCabinet()->GetToys(); + RGBAToy* first = new RGBAToy(); + first->SetName("first"); + toys->AddToy(first); + AnalogAlphaToy* second = new AnalogAlphaToy(); + second->SetName("second"); + toys->AddToy(second); + RGBAToyGroup* colors = new RGBAToyGroup(); + colors->SetName("colors"); + colors->SetToyNames({ { "first" } }); + toys->AddToy(colors); + colors->Init(pinball.GetCabinet()); + AnalogAlphaToyGroup* values = new AnalogAlphaToyGroup(); + values->SetName("values"); + values->SetToyNames({ { "second" } }); + toys->AddToy(values); + values->Init(pinball.GetCabinet()); + CHECK(vpe_dof_toys::Light(colors, 255) && vpe_dof_toys::Light(values, 255), "groups can be lit"); + RGBAColor* firstLayer = first->GetLayers().GetLayer(TestLayer, 1); + AnalogAlpha* secondLayer = second->GetLayers().GetLayer(TestLayer, 1); + CHECK(firstLayer && IsColor(firstLayer[0], dim, dim, dim, 255), "a group of RGB toys passes dim white on"); + CHECK(secondLayer && secondLayer[0].GetValue() == 255 && secondLayer[0].GetAlpha() == 255, "a group of toys with one output passes the value on"); + + LedWizEquivalent lwe; + CHECK(vpe_dof_toys::KindOf(&lwe) == vpe_dof_toys::Kind::None && !vpe_dof_toys::Light(&lwe, 255), "a LedWiz equivalent can't be lit"); +} + // The names GetPending returns are the units GetState counts as pending or failed. bool NamesAgree(OutputGate& gate, int64_t generation) { @@ -1971,6 +2060,7 @@ int main() StartWatchdog(timeout && std::atoi(timeout) > 0 ? std::atoi(timeout) : 120); EffectsThatNeverStartedAreNotFinished(); MatrixEffectWhoseToyFailsIsFinished(); + PortTestLightsEveryKindOfToy(); UntouchedUnitsAreCountedApart(); LatchOvertakesAQueuedOpening(); OpeningReservedBeforeALatchIsSuperseded(); From 2ce530cb24d20a010fcba95f1c1764ace5c33d3f Mon Sep 17 00:00:00 2001 From: freezy Date: Mon, 30 Nov 2026 17:48:44 +0100 Subject: [PATCH 2/3] outputs: refuse the toy test on a game and on port 0, word its result as requested The toy test lit a strip from a cabinet entry without a port number (port 0) and the output test set one numbered 0 or below, though the description lists neither: both now refuse ports below 1. It also ran on an instance initialized with a table or ROM, whose effects could use the test's layer, which clearing would wipe: it now returns VPE_DOF_INVALID_STATE there, as the port test always runs DOF without a table. The success message said the toy was lit when any controller acknowledged the gate opening, also when the toy's own controller failed: it now says the light was requested, as the output test does, and that delivery to the toy is unconfirmed. A strip with an inverted fading curve sends 191 for the test's dim white and 255 once cleared: that curve is for LEDs that light at low values, and effects go through it the same way, so it stays; a test and the docs say so. --- .../DofInstanceTests.cs | 54 +++++++++++++++++-- VisualPinball.Engine.DOF.Test/TestConfig.cs | 6 ++- .../Runtime/Player/DofHardware.cs | 6 ++- .../Tests/DofPluginTests.cs | 11 +++- VisualPinball.Engine.DOF/DofInstance.cs | 15 ++++-- native/include/vpe_dof.h | 14 +++-- native/src/vpe_dof.cpp | 13 ++++- native/src/vpe_dof_toys.h | 5 +- 8 files changed, 103 insertions(+), 21 deletions(-) diff --git a/VisualPinball.Engine.DOF.Test/DofInstanceTests.cs b/VisualPinball.Engine.DOF.Test/DofInstanceTests.cs index 0ef2029..ab61f52 100644 --- a/VisualPinball.Engine.DOF.Test/DofInstanceTests.cs +++ b/VisualPinball.Engine.DOF.Test/DofInstanceTests.cs @@ -480,9 +480,10 @@ public void PortTestLightsTheToyAPortNames() var unit = Unit("ArtNetA"); var strip = Unit("ArtNetStrip"); strip.Ports = false; - // port 9 names an LED strip of 4 x 2 LEDs on a controller of its own, port 10 the equivalent itself, which no test lights - using var config = new TestConfig().GlobalConfig() - .Cabinet(8, new Dictionary { [9] = "Matrix1", [10] = "LedWizEquivalent 1" }, TestConfig.LedStrip("Matrix1", strip, 4, 2), unit, strip); + // Port 9 names an LED strip of 4 x 2 LEDs on a controller of its own, port 10 the equivalent itself, which no test + // lights. Entries numbered 0, as one without a number is, and -1 are no ports, though they name the strip and an output. + var ports = new Dictionary { [9] = "Matrix1", [10] = "LedWizEquivalent 1", [0] = "Matrix1", [-1] = "ArtNetA.03" }; + using var config = new TestConfig().GlobalConfig().Cabinet(8, ports, TestConfig.LedStrip("Matrix1", strip, 4, 2), unit, strip); using var dof = DofInstance.Create(config.Root); // as the port test runs DOF: dark, without connect tests and without a table dof.SetOutputGate(false); @@ -521,10 +522,57 @@ public void PortTestLightsTheToyAPortNames() Assert.That(dof.TestToy(1, 99, 255, out _), Is.EqualTo(DofStatus.InvalidArgument), "unknown port"); Assert.That(dof.TestToy(2, 9, 255, out _), Is.EqualTo(DofStatus.InvalidArgument), "unknown device"); Assert.That(dof.TestOutput(1, 9, 255), Is.EqualTo(DofStatus.InvalidArgument), "the output test doesn't set a toy"); + Assert.That(dof.TestToy(1, 0, 255, out toy), Is.EqualTo(DofStatus.InvalidArgument), "port 0"); + Assert.That(toy.Name, Is.Null); + Assert.That(dof.TestOutput(1, 0, 255), Is.EqualTo(DofStatus.InvalidArgument), "port 0 of the output test"); + Assert.That(dof.TestOutput(1, -1, 255), Is.EqualTo(DofStatus.InvalidArgument), "a negative port of the output test"); + Assert.That(dof.Describe().Devices.Single().Count, Is.EqualTo(10), "the description starts at port 1"); dof.Finish(); Assert.That(dof.TestToy(1, 9, 255, out _), Is.EqualTo(DofStatus.InvalidState), "after finish"); } + [Test] + public void PortTestKeepsTheStripsFadingCurve() + { + // An inverted curve is for LEDs that light at low values: the dim white of the test is 191 there, and off is 255. + var strip = Unit("ArtNetStrip"); + strip.Ports = false; + using var config = new TestConfig().GlobalConfig() + .Cabinet(8, new Dictionary { [1] = "Matrix1" }, TestConfig.LedStrip("Matrix1", strip, 2, 1, "InvertedLinear"), strip); + using var dof = DofInstance.Create(config.Root); + dof.SetOutputGate(false); + dof.SetConnectTests(false); + dof.Init(null, null); + Assert.That(dof.TestToy(1, 1, 255, out _), Is.EqualTo(DofStatus.Ok)); + var opening = _capture.Mark(strip.Universe); + WaitSettled(dof, dof.SetOutputGate(true)); + var lit = ExpectIndex(strip.Universe, f => f.Take(6).All(v => v == 191), "the strip's dim white through the inverted curve", opening); + + WaitSettled(dof, dof.SetOutputGate(false)); + Assert.That(dof.TestToy(1, 1, 0, out _), Is.EqualTo(DofStatus.Ok)); + WaitSettled(dof, dof.SetOutputGate(true)); + var cleared = _capture.Mark(strip.Universe); + ExpectFrameAfter(strip.Universe, Math.Max(cleared, lit + 1), "a frame of the strip after it was cleared"); + Assert.That(FramesBetween(strip.Universe, cleared).All(f => f.Take(6).All(v => v == 255)), Is.True, "cleared, the strip sends the curve's off"); + dof.Finish(); + } + + [Test] + public void ToyTestRefusesAGame() + { + // A table's effects could use the test's layer: a game instance takes no toy test. + var unit = Unit("ArtNetA"); + var strip = Unit("ArtNetStrip"); + strip.Ports = false; + using var config = new TestConfig().GlobalConfig() + .Cabinet(8, new Dictionary { [9] = "Matrix1" }, TestConfig.LedStrip("Matrix1", strip, 4, 2), unit, strip).Ini(1, "vpetest,S1"); + using var dof = Start(config); + Assert.That(dof.Describe().Devices.Single().KindOf(9), Is.EqualTo(DofPortKind.Toy)); + Assert.That(dof.TestToy(1, 9, 255, out var toy), Is.EqualTo(DofStatus.InvalidState)); + Assert.That(toy.Name, Is.Null); + dof.Finish(); + } + [Test] public void OnlyOneInstanceAtATime() { diff --git a/VisualPinball.Engine.DOF.Test/TestConfig.cs b/VisualPinball.Engine.DOF.Test/TestConfig.cs index c923f11..2f22531 100644 --- a/VisualPinball.Engine.DOF.Test/TestConfig.cs +++ b/VisualPinball.Engine.DOF.Test/TestConfig.cs @@ -109,9 +109,11 @@ public TestConfig Cabinet(int perUnit, IDictionary extraPorts, stri } /// An LED strip of a cabinet, in RGB order, whose LEDs are the first channels of an Art-Net controller. - public static string LedStrip(string name, ArtNetUnit unit, int width, int height) + /// Its fading curve, such as InvertedLinear; libdof's default (Linear) if null. + public static string LedStrip(string name, ArtNetUnit unit, int width, int height, string fadingCurve = null) => $" {name}{width}{height}1" - + $"RGB{unit.Name}"; + + $"RGB{unit.Name}" + + (fadingCurve == null ? "" : $"{fadingCurve}") + ""; /// An ini file (directoutputconfig.ini for device 1, directoutputconfigN.ini otherwise) with table rows. public TestConfig Ini(int device, params string[] rows) => Write(device == 1 ? "directoutputconfig.ini" : $"directoutputconfig{device}.ini", IniText(rows)); diff --git a/VisualPinball.Engine.DOF.Unity/Runtime/Player/DofHardware.cs b/VisualPinball.Engine.DOF.Unity/Runtime/Player/DofHardware.cs index 9786559..427acad 100644 --- a/VisualPinball.Engine.DOF.Unity/Runtime/Player/DofHardware.cs +++ b/VisualPinball.Engine.DOF.Unity/Runtime/Player/DofHardware.cs @@ -186,9 +186,11 @@ internal static (bool Success, string Message) Pulse(DofConfigStore store, Plugi if (cancelled) { return (false, $"Cancelled after {open.TotalSeconds:0.##} s: {lit} was to be lit for {duration.TotalSeconds:0.##} s.{failed}"); } + // as for a port: which controller drives the toy is not known, so this counts every one that wrote the opening + var requestedLight = $"Requested to light {lit} for {duration.TotalSeconds:0.##} s"; return wrote > 0 - ? (true, $"Lit {lit} for {duration.TotalSeconds:0.##} s: {Controllers(wrote)} acknowledged the gate opening.{failed}") - : (false, $"Requested to light {lit} for {duration.TotalSeconds:0.##} s, but no controller confirmed writing it.{failed}"); + ? (true, $"{requestedLight}: {Controllers(wrote)} acknowledged the gate opening. Delivery to the toy is unconfirmed.{failed}") + : (false, $"{requestedLight}, but no controller confirmed writing it.{failed}"); } if (cancelled) { return (false, $"Cancelled after {open.TotalSeconds:0.##} s: the pulse to output {output} of device {device} was requested for " diff --git a/VisualPinball.Engine.DOF.Unity/Tests/DofPluginTests.cs b/VisualPinball.Engine.DOF.Unity/Tests/DofPluginTests.cs index 1e90929..1f98a42 100644 --- a/VisualPinball.Engine.DOF.Unity/Tests/DofPluginTests.cs +++ b/VisualPinball.Engine.DOF.Unity/Tests/DofPluginTests.cs @@ -329,7 +329,16 @@ public void LightsTheToyAPortNames() Assert.That(result.Success, Is.True, result.Message); Assert.That(native.Calls, Is.EqualTo(new[] { "gate:closed", "connect-tests:off", "init", "gate:closed", "confirmed", "toy 1:6=255", "gate:open", "gate:closed", "toy 1:6=0", "finish", "dispose" }), "the toy is lit and cleared where an output would be set, with the same gate"); - Assert.That(result.Message, Is.EqualTo("Lit LED strip Matrix1 on output 6 of device 1 for 0.02 s: 1 controller acknowledged the gate opening.")); + Assert.That(result.Message, Is.EqualTo("Requested to light LED strip Matrix1 on output 6 of device 1 for 0.02 s: 1 controller acknowledged the gate opening. " + + "Delivery to the toy is unconfirmed.")); + + // another controller's acknowledgement doesn't say the toy's controller took part + native.Units = new[] { "ArtNet 1" }; + native.FailedUnits = new[] { "Teensy" }; + result = Pulse(host, "1:6"); + Assert.That(result.Message, Is.EqualTo("Requested to light LED strip Matrix1 on output 6 of device 1 for 0.02 s: 1 controller acknowledged the gate opening. " + + "Delivery to the toy is unconfirmed. Teensy failed and took no part.")); + native.FailedUnits = new string[0]; native.Calls.Clear(); Assert.That(Pulse(host, "1:1").Success, Is.True); diff --git a/VisualPinball.Engine.DOF/DofInstance.cs b/VisualPinball.Engine.DOF/DofInstance.cs index 90bc777..cd8f68d 100644 --- a/VisualPinball.Engine.DOF/DofInstance.cs +++ b/VisualPinball.Engine.DOF/DofInstance.cs @@ -441,8 +441,8 @@ public IReadOnlyList GetGatePending(long generation) /// value, through the output gate. /// /// - /// if the device or port does not exist or the port names a toy instead of - /// a controller output ( lights that); before init or after + /// if the device or port (from 1) does not exist or the port names a toy + /// instead of a controller output ( lights that); before init or after /// finish. /// public DofStatus TestOutput(int device, int port, int value) @@ -459,13 +459,18 @@ public DofStatus TestOutput(int device, int port, int value) /// Lights the toy a port of an ini device names instead of a controller output, such as an LED strip, through the /// output gate: (0 to 255) on a layer of its own above the effects' layers, and 0 clears it. /// An LED strip or another matrix gets white at a quarter of the value on every LED, so a whole strip doesn't - /// overload its supply; an RGB toy gets white at the value, and a toy with one output the value. + /// overload its supply; an RGB toy gets white at the value, and a toy with one output the value. The strip's fading + /// curve maps it as it maps what effects set: an inverted one sends 191 for it and 255 for off. /// + /// + /// Only for an instance initialized without a table and ROM, as the port test runs DOF: a table's effects could use + /// the test's layer, which clearing it would wipe. + /// /// The toy the port names, as lists it; default if it names none. /// - /// if the device or port does not exist or the port names no toy; + /// if the device or port (from 1) does not exist or the port names no toy; /// for a toy a test can't light; before - /// init or after finish. + /// init, after finish, or on an instance initialized with a table or ROM. /// public DofStatus TestToy(int device, int port, int value, out DofToy toy) { diff --git a/native/include/vpe_dof.h b/native/include/vpe_dof.h index 810ae19..1897695 100644 --- a/native/include/vpe_dof.h +++ b/native/include/vpe_dof.h @@ -148,17 +148,21 @@ VPE_DOF_API int32_t VPE_DOF_CALL vpe_dof_gate_pending(vpe_dof* dof, int64_t gene /// Sets one port of an ini device (the N of directoutputconfigN.ini; 1 for directoutputconfig.ini) to a value, as the /// port test does. Runs on DOF's main thread and goes through the output gate. VPE_DOF_INVALID_ARGUMENT if the device -/// or port does not exist, or the port names a toy (an LED strip or matrix) instead of a controller output: see +/// or port (from 1) does not exist, or the port names a toy (an LED strip or matrix) instead of a controller output: see /// vpe_dof_test_toy. VPE_DOF_API int32_t VPE_DOF_CALL vpe_dof_test_output(vpe_dof* dof, int32_t device, int32_t port, int32_t value); /// Lights the toy a port of an ini device names instead of a controller output, such as an LED strip, as the port test /// does: `value` (0 to 255) on a layer of its own above the layers of effects, and 0 clears it. A matrix toy, such as an /// LED strip, gets white at a quarter of `value` on every LED, so a whole strip does not overload its supply; an RGB toy -/// gets white at `value` and a toy with one output `value`. Runs on DOF's main thread, whose cabinet update sends it, and -/// goes through the output gate. `toy`, if given, gets "|" of the toy the port names, as vpe_dof_describe -/// lists it, and is empty for another port. VPE_DOF_INVALID_ARGUMENT if the device or port does not exist or the port -/// names no toy; VPE_DOF_NOT_SUPPORTED for a toy the test can't light, such as a LedWiz equivalent. +/// gets white at `value` and a toy with one output `value`. The strip's fading curve maps it as it maps what effects set: +/// an inverted curve, for LEDs that light at low values, sends 191 for it and 255 for off. Runs on DOF's main thread, +/// whose cabinet update sends it, and goes through the output gate. Only for an instance initialized without a table +/// and ROM, as the port test runs it: a table's effects could use the test's layer. `toy`, if given, gets +/// "|" of the toy the port names, as vpe_dof_describe lists it, and is empty for another port. +/// VPE_DOF_INVALID_ARGUMENT if the device or port (from 1) does not exist or the port names no toy; +/// VPE_DOF_NOT_SUPPORTED for a toy the test can't light, such as a LedWiz equivalent; VPE_DOF_INVALID_STATE before +/// init, after finish, or with a table. VPE_DOF_API int32_t VPE_DOF_CALL vpe_dof_test_toy(vpe_dof* dof, int32_t device, int32_t port, int32_t value, char* toy, int32_t toy_size); /// Routes the ports of an ini device (the N of directoutputconfigN.ini) to other outputs of its LedWiz equivalent, for diff --git a/native/src/vpe_dof.cpp b/native/src/vpe_dof.cpp index f813839..02b2b1b 100644 --- a/native/src/vpe_dof.cpp +++ b/native/src/vpe_dof.cpp @@ -80,6 +80,8 @@ struct vpe_dof std::string basePath; // The host's routing of ini device ports (vpe_dof_set_output_map), applied when init sets up the cabinet. std::map> outputMaps; + // Whether init was given a table or ROM, whose effects can use any layer of a toy (vpe_dof_test_toy). + bool hasTable = false; std::atomic state { State::Created }; std::mutex lifecycle; }; @@ -463,6 +465,7 @@ VPE_DOF_API int32_t VPE_DOF_CALL vpe_dof_init(vpe_dof* dof, const char* table_fi // Before the table config makes its toys from the cabinet's outputs, and only once the cabinet is set up. const auto maps = dof->outputMaps; dof->pinball->SetCabinetReady([maps](DOF::Cabinet* cabinet) { ApplyOutputMaps(maps, cabinet); }); + dof->hasTable = (table_file && *table_file) || (rom_name && *rom_name); dof->pinball->Setup(globalConfig, table_file ? table_file : "", rom_name ? rom_name : ""); dof->pinball->Init(); dof->state = State::Running; @@ -727,6 +730,9 @@ VPE_DOF_API int32_t VPE_DOF_CALL vpe_dof_test_output(vpe_dof* dof, int32_t devic { if (!IsRunning(dof)) return VPE_DOF_INVALID_STATE; + // as vpe_dof_describe: a cabinet entry without a number has port 0, which is no port + if (port < 1) + return VPE_DOF_INVALID_ARGUMENT; try { DOF::Cabinet* cabinet = dof->pinball->GetCabinet(); @@ -758,8 +764,13 @@ VPE_DOF_API int32_t VPE_DOF_CALL vpe_dof_test_output(vpe_dof* dof, int32_t devic VPE_DOF_API int32_t VPE_DOF_CALL vpe_dof_test_toy(vpe_dof* dof, int32_t device, int32_t port, int32_t value, char* toy, int32_t toy_size) { WriteError(toy, toy_size, ""); - if (!IsRunning(dof)) + // Only without a table: an effect could use the test's layer, which clearing would wipe. Set before the state + // changed to running, so it is read after that. + if (!IsRunning(dof) || dof->hasTable) return VPE_DOF_INVALID_STATE; + // as vpe_dof_describe: a cabinet entry without a number has port 0, which is no port + if (port < 1) + return VPE_DOF_INVALID_ARGUMENT; try { DOF::Cabinet* cabinet = dof->pinball->GetCabinet(); diff --git a/native/src/vpe_dof_toys.h b/native/src/vpe_dof_toys.h index 209566b..ca53e38 100644 --- a/native/src/vpe_dof_toys.h +++ b/native/src/vpe_dof_toys.h @@ -23,13 +23,14 @@ namespace vpe_dof_toys { // The layer the test sets. A toy blends its layers from the lowest number up, so this one is on top. Effects take their -// layer from the table config, where layers are numbered in the tens, and a port test runs without a table anyway. It +// layer from the table config, where layers are numbered in the tens, and the test runs only without a table. It // leaves room for the offset a toy group adds to a layer when it passes it on to its toys. constexpr int TestLayer = 1000000; // What each colour channel of a matrix toy, such as an LED strip, gets for the full test value: a quarter. A whole strip // at full white draws the most current it can, some 60 mA per LED, which can overload a supply sized for what effects -// light at once; a quarter is still plain to see. The strip's own brightness and fading curve apply on top. +// light at once; a quarter is still plain to see. The strip's own brightness and fading curve apply on top, as they do +// to effects: an inverted curve, for LEDs that light at low values, sends 191 for it, as it sends 255 for off. constexpr int MatrixTestLevel = 64; enum class Kind From c933b0a80b0be016a4ecdcfb956b8da9a399f00d Mon Sep 17 00:00:00 2001 From: freezy Date: Mon, 30 Nov 2026 19:06:56 +0100 Subject: [PATCH 3/3] outputs: write a port test's seconds with a decimal point in any culture --- .../Runtime/Player/DofHardware.cs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/VisualPinball.Engine.DOF.Unity/Runtime/Player/DofHardware.cs b/VisualPinball.Engine.DOF.Unity/Runtime/Player/DofHardware.cs index 427acad..3a01f5b 100644 --- a/VisualPinball.Engine.DOF.Unity/Runtime/Player/DofHardware.cs +++ b/VisualPinball.Engine.DOF.Unity/Runtime/Player/DofHardware.cs @@ -6,6 +6,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Globalization; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -137,7 +138,7 @@ internal static (bool Success, string Message) Pulse(DofConfigStore store, Plugi return (false, Cancelled); } if (!dark.Applied || dark.State.Pending > 0) { - return (false, $"{Units(dark.Units, "DOF")} did not confirm within {confirmTimeout.TotalSeconds:0.#} s that {Their(dark.Units)} outputs " + return (false, $"{Units(dark.Units, "DOF")} did not confirm within {Seconds(confirmTimeout, "0.#")} s that {Their(dark.Units)} outputs " + "are off, so nothing was turned on: a controller that is still connecting could run its own test."); } var devices = Devices(native); @@ -184,19 +185,19 @@ internal static (bool Success, string Message) Pulse(DofConfigStore store, Plugi if (isToy) { var lit = $"{ToyName(toy)} on output {output} of device {device}"; if (cancelled) { - return (false, $"Cancelled after {open.TotalSeconds:0.##} s: {lit} was to be lit for {duration.TotalSeconds:0.##} s.{failed}"); + return (false, $"Cancelled after {Seconds(open)} s: {lit} was to be lit for {Seconds(duration)} s.{failed}"); } // as for a port: which controller drives the toy is not known, so this counts every one that wrote the opening - var requestedLight = $"Requested to light {lit} for {duration.TotalSeconds:0.##} s"; + var requestedLight = $"Requested to light {lit} for {Seconds(duration)} s"; return wrote > 0 ? (true, $"{requestedLight}: {Controllers(wrote)} acknowledged the gate opening. Delivery to the toy is unconfirmed.{failed}") : (false, $"{requestedLight}, but no controller confirmed writing it.{failed}"); } if (cancelled) { - return (false, $"Cancelled after {open.TotalSeconds:0.##} s: the pulse to output {output} of device {device} was requested for " - + $"{duration.TotalSeconds:0.##} s.{failed}"); + return (false, $"Cancelled after {Seconds(open)} s: the pulse to output {output} of device {device} was requested for " + + $"{Seconds(duration)} s.{failed}"); } - var requested = $"Requested a pulse of {duration.TotalSeconds:0.##} s on output {output} of device {device}"; + var requested = $"Requested a pulse of {Seconds(duration)} s on output {output} of device {device}"; return wrote > 0 ? (true, $"{requested}: {Controllers(wrote)} acknowledged the gate opening. Delivery to the selected port is unconfirmed.{failed}") : (false, $"{requested}, but no controller confirmed writing it.{failed}"); @@ -205,6 +206,9 @@ internal static (bool Success, string Message) Pulse(DofConfigStore store, Plugi private const string Cancelled = "Cancelled; nothing was turned on."; + // The messages are English: seconds with a decimal point, whatever the culture. + private static string Seconds(TimeSpan time, string format = "0.##") => time.TotalSeconds.ToString(format, CultureInfo.InvariantCulture); + /// A toy for a message, by its kind and name: "LED strip Matrix1". internal static string ToyName(DofToy toy) { @@ -305,7 +309,7 @@ internal static (bool Success, string Message) AllOff(DofConfigStore store, Plug } string what; if (!dark.Applied || dark.State.Failed == 0) { - what = $"did not confirm within {confirmTimeout.TotalSeconds:0.#} s that {Their(dark.Units)} outputs are off"; + what = $"did not confirm within {Seconds(confirmTimeout, "0.#")} s that {Their(dark.Units)} outputs are off"; } else if (dark.State.Pending == 0) { what = "failed"; } else {