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

Changed SteelSeriesDeviceUpdateTrigger to inherit DeviceUpdateTrigger

This commit is contained in:
Darth Affe 2019-06-22 12:21:48 +02:00
parent b19ff742b6
commit 5f203859ca
2 changed files with 29 additions and 115 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;
private Task _updateTask;
private CancellationTokenSource _updateTokenSource;
private CancellationToken _updateToken;
protected bool IsRunning;
protected Task UpdateTask;
protected CancellationTokenSource UpdateTokenSource;
protected CancellationToken UpdateToken;
#endregion
@ -88,13 +88,13 @@ namespace RGB.NET.Core
/// </summary>
public void Start()
{
if (_isRunning) return;
if (IsRunning) return;
_isRunning = true;
IsRunning = true;
_updateTokenSource?.Dispose();
_updateTokenSource = new CancellationTokenSource();
_updateTask = Task.Factory.StartNew(UpdateLoop, (_updateToken = _updateTokenSource.Token), TaskCreationOptions.LongRunning, TaskScheduler.Default);
UpdateTokenSource?.Dispose();
UpdateTokenSource = new CancellationTokenSource();
UpdateTask = Task.Factory.StartNew(UpdateLoop, (UpdateToken = UpdateTokenSource.Token), TaskCreationOptions.LongRunning, TaskScheduler.Default);
}
/// <summary>
@ -102,22 +102,22 @@ namespace RGB.NET.Core
/// </summary>
public async void Stop()
{
if (!_isRunning) return;
if (!IsRunning) return;
_isRunning = false;
IsRunning = false;
_updateTokenSource.Cancel();
await _updateTask;
_updateTask.Dispose();
_updateTask = null;
UpdateTokenSource.Cancel();
await UpdateTask;
UpdateTask.Dispose();
UpdateTask = null;
}
private void UpdateLoop()
protected virtual void UpdateLoop()
{
OnStartup();
while (!_updateToken.IsCancellationRequested)
while (!UpdateToken.IsCancellationRequested)
{
if (_hasDataEvent.WaitOne(Timeout))
if (HasDataEvent.WaitOne(Timeout))
{
long preUpdateTicks = Stopwatch.GetTimestamp();
@ -135,7 +135,7 @@ namespace RGB.NET.Core
}
/// <inheritdoc />
public void TriggerHasData() => _hasDataEvent.Set();
public void TriggerHasData() => HasDataEvent.Set();
private void UpdateUpdateFrequency()
{

View File

@ -3,71 +3,24 @@
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using RGB.NET.Core;
namespace RGB.NET.Devices.SteelSeries
{
/// <summary>
/// Represents an update-trigger used to update devices with a maximum update-rate.
/// Represents an update-trigger used to update SteelSeries devices
/// </summary>
public class SteelSeriesDeviceUpdateTrigger : AbstractUpdateTrigger, IDeviceUpdateTrigger
public class SteelSeriesDeviceUpdateTrigger : DeviceUpdateTrigger
{
#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
#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).
/// &lt;= 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.
/// &lt;= 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 bool _isRunning;
private Task _updateTask;
private CancellationTokenSource _updateTokenSource;
private CancellationToken _updateToken;
#endregion
@ -76,6 +29,7 @@ namespace RGB.NET.Devices.SteelSeries
/// <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()
{ }
@ -84,49 +38,19 @@ namespace RGB.NET.Devices.SteelSeries
/// </summary>
/// <param name="updateRateHardLimit">The hard limit of the update rate of this trigger.</param>
public SteelSeriesDeviceUpdateTrigger(double updateRateHardLimit)
{
this.UpdateRateHardLimit = updateRateHardLimit;
}
: base(updateRateHardLimit)
{ }
#endregion
#region Methods
/// <summary>
/// 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()
protected override void UpdateLoop()
{
OnStartup();
while (!_updateToken.IsCancellationRequested)
while (!UpdateToken.IsCancellationRequested)
{
if (_hasDataEvent.WaitOne(Timeout))
if (HasDataEvent.WaitOne(Timeout))
{
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
}
}