From 292b29517b91591c3118523fcfdc56d34f5cdac5 Mon Sep 17 00:00:00 2001 From: Jon Carrier Date: Wed, 6 Aug 2025 22:56:43 -0400 Subject: [PATCH] Fixed regressions for #40 Prior attempt at fixing #40 introduced the following issues: - Output state would turn off upon loading the application. This is generally undesirable behavior. - A null exception would occur if the output was on while starting up. --- libdp100/PowerSupply.cs | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/libdp100/PowerSupply.cs b/libdp100/PowerSupply.cs index c56e3e9..53dfc33 100644 --- a/libdp100/PowerSupply.cs +++ b/libdp100/PowerSupply.cs @@ -103,6 +103,11 @@ public class PowerSupply /// private bool[] presetsValid = new bool[NumPresets]; + /// + /// Indicates that the "Presets" object has been applied to the volatile preset's state. + /// + private bool[] presetsLoaded = new bool[NumPresets]; + /// /// Indicates that the "SystemParams" object has been populated with valid data. /// @@ -525,8 +530,14 @@ public PowerSupplyResult SetOutput(bool outputOn, ushort millivolts, ushort mill Output.Setpoint.Voltage = millivolts; Output.Setpoint.Current = milliamps; + if (Presets[Output.Preset] == null) + { + Presets[Output.Preset] = new PowerSupplySetpoint(Output.Preset); + } + // Output state affects the volatile state of a preset (group). Presets[Output.Preset].Copy(Output.Setpoint); + presetsValid[Output.Preset] = true; } return result; @@ -894,6 +905,14 @@ public PowerSupplyResult UsePreset(byte preset) } } + if (result == PowerSupplyResult.OK) + { + if (!presetsLoaded[preset]) + { + result = SetOutput(Presets[preset]); + } + } + if (result == PowerSupplyResult.OK) { Output.Setpoint.Copy(Presets[preset]); @@ -1042,18 +1061,6 @@ public PowerSupplyResult Reload() { return result; } - - result = UsePreset(i); - if (result != PowerSupplyResult.OK) - { - return result; - } - - result = SetOutput(Presets[i]); - if (result != PowerSupplyResult.OK) - { - return result; - } } return result;