1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-13 10:08:31 +00:00

Merge pull request #80 from DarthAffe/SteelSeries/RefreshLeds

Added refresh logic to prevent the SDK from timing out
This commit is contained in:
DarthAffe 2019-08-18 16:36:05 +02:00 committed by GitHub
commit c4a00f2d77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 111 additions and 23 deletions

View File

@ -53,12 +53,12 @@ namespace RGB.NET.Core
} }
} }
private AutoResetEvent _hasDataEvent = new AutoResetEvent(false); protected AutoResetEvent HasDataEvent = new AutoResetEvent(false);
private bool _isRunning; protected bool IsRunning;
private Task _updateTask; protected Task UpdateTask;
private CancellationTokenSource _updateTokenSource; protected CancellationTokenSource UpdateTokenSource;
private CancellationToken _updateToken; protected CancellationToken UpdateToken;
#endregion #endregion
@ -88,13 +88,13 @@ namespace RGB.NET.Core
/// </summary> /// </summary>
public void Start() public void Start()
{ {
if (_isRunning) return; if (IsRunning) return;
_isRunning = true; IsRunning = true;
_updateTokenSource?.Dispose(); UpdateTokenSource?.Dispose();
_updateTokenSource = new CancellationTokenSource(); UpdateTokenSource = new CancellationTokenSource();
_updateTask = Task.Factory.StartNew(UpdateLoop, (_updateToken = _updateTokenSource.Token), TaskCreationOptions.LongRunning, TaskScheduler.Default); UpdateTask = Task.Factory.StartNew(UpdateLoop, (UpdateToken = UpdateTokenSource.Token), TaskCreationOptions.LongRunning, TaskScheduler.Default);
} }
/// <summary> /// <summary>
@ -102,22 +102,22 @@ namespace RGB.NET.Core
/// </summary> /// </summary>
public async void Stop() public async void Stop()
{ {
if (!_isRunning) return; if (!IsRunning) return;
_isRunning = false; IsRunning = false;
_updateTokenSource.Cancel(); UpdateTokenSource.Cancel();
await _updateTask; await UpdateTask;
_updateTask.Dispose(); UpdateTask.Dispose();
_updateTask = null; UpdateTask = null;
} }
private void UpdateLoop() protected virtual void UpdateLoop()
{ {
OnStartup(); OnStartup();
while (!_updateToken.IsCancellationRequested) while (!UpdateToken.IsCancellationRequested)
{ {
if (_hasDataEvent.WaitOne(Timeout)) if (HasDataEvent.WaitOne(Timeout))
{ {
long preUpdateTicks = Stopwatch.GetTimestamp(); long preUpdateTicks = Stopwatch.GetTimestamp();
@ -135,7 +135,7 @@ namespace RGB.NET.Core
} }
/// <inheritdoc /> /// <inheritdoc />
public void TriggerHasData() => _hasDataEvent.Set(); public void TriggerHasData() => HasDataEvent.Set();
private void UpdateUpdateFrequency() private void UpdateUpdateFrequency()
{ {

View File

@ -15,6 +15,7 @@ namespace RGB.NET.Devices.SteelSeries
#region Properties & Fields #region Properties & Fields
private string _deviceType; private string _deviceType;
private Dictionary<object, Color> _lastDataSet;
#endregion #endregion
@ -35,9 +36,21 @@ namespace RGB.NET.Devices.SteelSeries
#region Methods #region Methods
protected override void OnUpdate(object sender, CustomUpdateData customData)
{
if ((customData != null) && (customData["refresh"] as bool? ?? false))
{
if ((_lastDataSet != null) && (_lastDataSet.Count != 0))
Update(_lastDataSet);
}
else
base.OnUpdate(sender, customData);
}
/// <inheritdoc /> /// <inheritdoc />
protected override void Update(Dictionary<object, Color> dataSet) protected override void Update(Dictionary<object, Color> dataSet)
{ {
_lastDataSet = dataSet;
SteelSeriesSDK.UpdateLeds(_deviceType, dataSet.ToDictionary(x => ((SteelSeriesLedId)x.Key).GetAPIName(), x => x.Value.ToIntArray())); SteelSeriesSDK.UpdateLeds(_deviceType, dataSet.ToDictionary(x => ((SteelSeriesLedId)x.Key).GetAPIName(), x => x.Value.ToIntArray()));
} }

View File

@ -0,0 +1,75 @@
// ReSharper disable MemberCanBePrivate.Global
using System;
using System.Diagnostics;
using System.Threading;
using RGB.NET.Core;
namespace RGB.NET.Devices.SteelSeries
{
/// <summary>
/// Represents an update-trigger used to update SteelSeries devices
/// </summary>
public class SteelSeriesDeviceUpdateTrigger : DeviceUpdateTrigger
{
#region Constants
private const long FLUSH_TIMER = 5 * 1000 * TimeSpan.TicksPerMillisecond; // flush the device every 5 seconds to prevent timeouts
#endregion
#region Properties & Fields
private long _lastUpdateTimestamp;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="SteelSeriesDeviceUpdateTrigger"/> class.
/// </summary>
/// <param name="updateRateHardLimit">The hard limit of the update rate of this trigger.</param>
public SteelSeriesDeviceUpdateTrigger()
{ }
/// <summary>
/// Initializes a new instance of the <see cref="SteelSeriesDeviceUpdateTrigger"/> class.
/// </summary>
/// <param name="updateRateHardLimit">The hard limit of the update rate of this trigger.</param>
public SteelSeriesDeviceUpdateTrigger(double updateRateHardLimit)
: base(updateRateHardLimit)
{ }
#endregion
#region Methods
protected override void UpdateLoop()
{
OnStartup();
while (!UpdateToken.IsCancellationRequested)
{
if (HasDataEvent.WaitOne(Timeout))
{
long preUpdateTicks = Stopwatch.GetTimestamp();
OnUpdate();
if (UpdateFrequency > 0)
{
_lastUpdateTimestamp = Stopwatch.GetTimestamp();
double lastUpdateTime = ((_lastUpdateTimestamp - preUpdateTicks) / (double)TimeSpan.TicksPerMillisecond);
int sleep = (int)((UpdateFrequency * 1000.0) - lastUpdateTime);
if (sleep > 0)
Thread.Sleep(sleep);
}
}
else if (((Stopwatch.GetTimestamp() - _lastUpdateTimestamp) > FLUSH_TIMER))
OnUpdate(new CustomUpdateData(("refresh", true)));
}
}
#endregion
}
}

View File

@ -37,9 +37,9 @@ namespace RGB.NET.Devices.SteelSeries
public IEnumerable<IRGBDevice> Devices { get; private set; } public IEnumerable<IRGBDevice> Devices { get; private set; }
/// <summary> /// <summary>
/// The <see cref="DeviceUpdateTrigger"/> used to trigger the updates for SteelSeries devices. /// The <see cref="SteelSeriesDeviceUpdateTrigger"/> used to trigger the updates for SteelSeries devices.
/// </summary> /// </summary>
public DeviceUpdateTrigger UpdateTrigger { get; } public SteelSeriesDeviceUpdateTrigger UpdateTrigger { get; }
#endregion #endregion
@ -54,7 +54,7 @@ namespace RGB.NET.Devices.SteelSeries
if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(SteelSeriesDeviceProvider)}"); if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(SteelSeriesDeviceProvider)}");
_instance = this; _instance = this;
UpdateTrigger = new DeviceUpdateTrigger(); UpdateTrigger = new SteelSeriesDeviceUpdateTrigger();
} }
#endregion #endregion