From 9a5fe75b60fce3aef363c90b6fcf037d3e28acf6 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Sat, 13 Nov 2021 19:53:13 +0100 Subject: [PATCH] Improved custom update data to be easier to use --- .../Extensions/CustomUpdateDataExtension.cs | 43 +++++++++++++++++++ RGB.NET.Core/RGBSurface.cs | 8 ++-- RGB.NET.Core/Update/CustomUpdateData.cs | 26 ++++++++++- RGB.NET.Core/Update/ManualUpdateTrigger.cs | 10 ++++- RGB.NET.Core/Update/TimerUpdateTrigger.cs | 17 +++++++- 5 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 RGB.NET.Core/Extensions/CustomUpdateDataExtension.cs diff --git a/RGB.NET.Core/Extensions/CustomUpdateDataExtension.cs b/RGB.NET.Core/Extensions/CustomUpdateDataExtension.cs new file mode 100644 index 0000000..4c73e84 --- /dev/null +++ b/RGB.NET.Core/Extensions/CustomUpdateDataExtension.cs @@ -0,0 +1,43 @@ +namespace RGB.NET.Core; + +/// +/// Offers some extensions for easier use of . +/// +public static class CustomUpdateDataExtension +{ + /// + /// Sets the -Parameter to the given value. + /// + /// The update-data to modify. + /// The value to set. + /// The modified update-data. + public static CustomUpdateData FlushLeds(this CustomUpdateData customUpdateData, bool value = true) + { + customUpdateData[CustomUpdateDataIndex.FLUSH_LEDS] = value; + return customUpdateData; + } + + /// + /// Sets the -Parameter to the given value. + /// + /// The update-data to modify. + /// The value to set. + /// The modified update-data. + public static CustomUpdateData Render(this CustomUpdateData customUpdateData, bool value = true) + { + customUpdateData[CustomUpdateDataIndex.RENDER] = value; + return customUpdateData; + } + + /// + /// Sets the -Parameter to the given value. + /// + /// The update-data to modify. + /// The value to set. + /// The modified update-data. + public static CustomUpdateData UpdateDevices(this CustomUpdateData customUpdateData, bool value = true) + { + customUpdateData[CustomUpdateDataIndex.UPDATE_DEVICES] = value; + return customUpdateData; + } +} \ No newline at end of file diff --git a/RGB.NET.Core/RGBSurface.cs b/RGB.NET.Core/RGBSurface.cs index b266f79..f8ec437 100644 --- a/RGB.NET.Core/RGBSurface.cs +++ b/RGB.NET.Core/RGBSurface.cs @@ -132,7 +132,7 @@ public sealed class RGBSurface : AbstractBindable, IDisposable /// Perform a full update for all devices. Updates only dirty by default, or all , if flushLeds is set to true. /// /// Specifies whether all , (including clean ones) should be updated. - public void Update(bool flushLeds = false) => Update(null, new CustomUpdateData(("flushLeds", flushLeds))); + public void Update(bool flushLeds = false) => Update(null, new CustomUpdateData((CustomUpdateDataIndex.FLUSH_LEDS, flushLeds))); private void Update(object? updateTrigger, CustomUpdateData customData) => Update(updateTrigger as IUpdateTrigger, customData); @@ -140,9 +140,9 @@ public sealed class RGBSurface : AbstractBindable, IDisposable { try { - bool flushLeds = customData["flushLeds"] as bool? ?? false; - bool render = customData["render"] as bool? ?? true; - bool updateDevices = customData["updateDevices"] as bool? ?? true; + bool flushLeds = customData[CustomUpdateDataIndex.FLUSH_LEDS] as bool? ?? false; + bool render = customData[CustomUpdateDataIndex.RENDER] as bool? ?? true; + bool updateDevices = customData[CustomUpdateDataIndex.UPDATE_DEVICES] as bool? ?? true; lock (UpdateTriggers) lock (Devices) diff --git a/RGB.NET.Core/Update/CustomUpdateData.cs b/RGB.NET.Core/Update/CustomUpdateData.cs index 60507c6..a16011f 100644 --- a/RGB.NET.Core/Update/CustomUpdateData.cs +++ b/RGB.NET.Core/Update/CustomUpdateData.cs @@ -2,6 +2,30 @@ namespace RGB.NET.Core; +/// +/// Represents an index used to identify data in the . +/// +public static class CustomUpdateDataIndex +{ + /// + /// Checked by the to see if all LEDs needs to be flushed even if they aren't changed in this update. + /// default: false + /// + public const string FLUSH_LEDS = "flushLeds"; + + /// + /// Checked by the to see if the surface should be rendered in this update. + /// default: true + /// + public const string RENDER = "render"; + + /// + /// Checked by the to see if devies should be updated after rendering. + /// default: true + /// + public const string UPDATE_DEVICES = "updateDevices"; +} + /// /// Represents a set of custom data, each indexed by a string-key. /// @@ -47,4 +71,4 @@ public class CustomUpdateData } #endregion -} \ No newline at end of file +} diff --git a/RGB.NET.Core/Update/ManualUpdateTrigger.cs b/RGB.NET.Core/Update/ManualUpdateTrigger.cs index c46645d..f9b315c 100644 --- a/RGB.NET.Core/Update/ManualUpdateTrigger.cs +++ b/RGB.NET.Core/Update/ManualUpdateTrigger.cs @@ -19,6 +19,8 @@ public sealed class ManualUpdateTrigger : AbstractUpdateTrigger private CancellationTokenSource? UpdateTokenSource { get; set; } private CancellationToken UpdateToken { get; set; } + private CustomUpdateData? _customUpdateData; + /// /// Gets the time it took the last update-loop cycle to run. /// @@ -71,7 +73,11 @@ public sealed class ManualUpdateTrigger : AbstractUpdateTrigger /// /// Triggers an update. /// - public void TriggerUpdate() => _mutex.Set(); + public void TriggerUpdate(CustomUpdateData? updateData = null) + { + _customUpdateData = updateData; + _mutex.Set(); + } private void UpdateLoop() { @@ -82,7 +88,7 @@ public sealed class ManualUpdateTrigger : AbstractUpdateTrigger if (_mutex.WaitOne(100)) { long preUpdateTicks = Stopwatch.GetTimestamp(); - OnUpdate(); + OnUpdate(_customUpdateData); LastUpdateTime = ((Stopwatch.GetTimestamp() - preUpdateTicks) / 10000.0); } } diff --git a/RGB.NET.Core/Update/TimerUpdateTrigger.cs b/RGB.NET.Core/Update/TimerUpdateTrigger.cs index 1579379..6f0fe37 100644 --- a/RGB.NET.Core/Update/TimerUpdateTrigger.cs +++ b/RGB.NET.Core/Update/TimerUpdateTrigger.cs @@ -17,6 +17,8 @@ public class TimerUpdateTrigger : AbstractUpdateTrigger private readonly object _lock = new(); + private readonly CustomUpdateData? _customUpdateData; + /// /// Gets or sets the update loop of this trigger. /// @@ -62,6 +64,19 @@ public class TimerUpdateTrigger : AbstractUpdateTrigger Start(); } + /// + /// Initializes a new instance of the class. + /// + /// A value indicating if the trigger should automatically right after construction. + public TimerUpdateTrigger(CustomUpdateData? customUpdateData, bool autostart = true) + { + this._customUpdateData = customUpdateData; + + if (autostart) + // ReSharper disable once VirtualMemberCallInConstructor - HACK DarthAffe 01.06.2021: I've no idea how to correctly handle that case, for now just disable it + Start(); + } + #endregion #region Methods @@ -118,7 +133,7 @@ public class TimerUpdateTrigger : AbstractUpdateTrigger { long preUpdateTicks = Stopwatch.GetTimestamp(); - OnUpdate(); + OnUpdate(_customUpdateData); if (UpdateFrequency > 0) {