mirror of
https://github.com/DarthAffe/RGB.NET.git
synced 2025-12-13 10:08:31 +00:00
Changed SteelSeriesDeviceUpdateTrigger to inherit DeviceUpdateTrigger
This commit is contained in:
parent
b19ff742b6
commit
5f203859ca
@ -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()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -3,71 +3,24 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using RGB.NET.Core;
|
using RGB.NET.Core;
|
||||||
|
|
||||||
namespace RGB.NET.Devices.SteelSeries
|
namespace RGB.NET.Devices.SteelSeries
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents an update-trigger used to update devices with a maximum update-rate.
|
/// Represents an update-trigger used to update SteelSeries devices
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class SteelSeriesDeviceUpdateTrigger : AbstractUpdateTrigger, IDeviceUpdateTrigger
|
public class SteelSeriesDeviceUpdateTrigger : DeviceUpdateTrigger
|
||||||
{
|
{
|
||||||
#region Constants
|
#region Constants
|
||||||
|
|
||||||
private const long FLUSH_TIMER = 5 * 1000 * TimeSpan.TicksPerMillisecond; // every 5 seconds flush the device to prevent timeouts
|
private const long FLUSH_TIMER = 5 * 1000 * TimeSpan.TicksPerMillisecond; // flush the device every 5 seconds to prevent timeouts
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Properties & Fields
|
#region Properties & Fields
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the timeout used by the blocking wait for data availability.
|
|
||||||
/// </summary>
|
|
||||||
public int Timeout { get; set; } = 100;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the update frequency used by the trigger if not limited by data shortage.
|
|
||||||
/// </summary>
|
|
||||||
public double UpdateFrequency { get; private set; }
|
|
||||||
|
|
||||||
private double _maxUpdateRate;
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the maximum update rate of this trigger (is overwriten if the <see cref="UpdateRateHardLimit"/> is smaller).
|
|
||||||
/// <= 0 removes the limit.
|
|
||||||
/// </summary>
|
|
||||||
public double MaxUpdateRate
|
|
||||||
{
|
|
||||||
get => _maxUpdateRate;
|
|
||||||
set
|
|
||||||
{
|
|
||||||
_maxUpdateRate = value;
|
|
||||||
UpdateUpdateFrequency();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private double _updateRateHardLimit;
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the hard limit of the update rate of this trigger. Updates will never perform faster then then this value if it's set.
|
|
||||||
/// <= 0 removes the limit.
|
|
||||||
/// </summary>
|
|
||||||
public double UpdateRateHardLimit
|
|
||||||
{
|
|
||||||
get => _updateRateHardLimit;
|
|
||||||
protected set
|
|
||||||
{
|
|
||||||
_updateRateHardLimit = value;
|
|
||||||
UpdateUpdateFrequency();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private AutoResetEvent _hasDataEvent = new AutoResetEvent(false);
|
|
||||||
|
|
||||||
private long _lastUpdateTimestamp;
|
private long _lastUpdateTimestamp;
|
||||||
private bool _isRunning;
|
|
||||||
private Task _updateTask;
|
|
||||||
private CancellationTokenSource _updateTokenSource;
|
|
||||||
private CancellationToken _updateToken;
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -76,6 +29,7 @@ namespace RGB.NET.Devices.SteelSeries
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="SteelSeriesDeviceUpdateTrigger"/> class.
|
/// Initializes a new instance of the <see cref="SteelSeriesDeviceUpdateTrigger"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="updateRateHardLimit">The hard limit of the update rate of this trigger.</param>
|
||||||
public SteelSeriesDeviceUpdateTrigger()
|
public SteelSeriesDeviceUpdateTrigger()
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
@ -84,49 +38,19 @@ namespace RGB.NET.Devices.SteelSeries
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="updateRateHardLimit">The hard limit of the update rate of this trigger.</param>
|
/// <param name="updateRateHardLimit">The hard limit of the update rate of this trigger.</param>
|
||||||
public SteelSeriesDeviceUpdateTrigger(double updateRateHardLimit)
|
public SteelSeriesDeviceUpdateTrigger(double updateRateHardLimit)
|
||||||
{
|
: base(updateRateHardLimit)
|
||||||
this.UpdateRateHardLimit = updateRateHardLimit;
|
{ }
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
|
|
||||||
/// <summary>
|
protected override void UpdateLoop()
|
||||||
/// Starts the trigger.
|
|
||||||
/// </summary>
|
|
||||||
public void Start()
|
|
||||||
{
|
|
||||||
if (_isRunning) return;
|
|
||||||
|
|
||||||
_isRunning = true;
|
|
||||||
|
|
||||||
_updateTokenSource?.Dispose();
|
|
||||||
_updateTokenSource = new CancellationTokenSource();
|
|
||||||
_updateTask = Task.Factory.StartNew(UpdateLoop, (_updateToken = _updateTokenSource.Token), TaskCreationOptions.LongRunning, TaskScheduler.Default);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Stops the trigger.
|
|
||||||
/// </summary>
|
|
||||||
public async void Stop()
|
|
||||||
{
|
|
||||||
if (!_isRunning) return;
|
|
||||||
|
|
||||||
_isRunning = false;
|
|
||||||
|
|
||||||
_updateTokenSource.Cancel();
|
|
||||||
await _updateTask;
|
|
||||||
_updateTask.Dispose();
|
|
||||||
_updateTask = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private 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();
|
||||||
|
|
||||||
@ -146,16 +70,6 @@ namespace RGB.NET.Devices.SteelSeries
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public void TriggerHasData() => _hasDataEvent.Set();
|
|
||||||
|
|
||||||
private void UpdateUpdateFrequency()
|
|
||||||
{
|
|
||||||
UpdateFrequency = MaxUpdateRate;
|
|
||||||
if ((UpdateFrequency <= 0) || ((UpdateRateHardLimit > 0) && (UpdateRateHardLimit < UpdateFrequency)))
|
|
||||||
UpdateFrequency = UpdateRateHardLimit;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user