mirror of
https://github.com/DarthAffe/RGB.NET.git
synced 2025-12-13 10:08:31 +00:00
162 lines
5.1 KiB
C#
162 lines
5.1 KiB
C#
// ReSharper disable MemberCanBePrivate.Global
|
|
|
|
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.
|
|
/// </summary>
|
|
public class SteelSeriesDeviceUpdateTrigger : AbstractUpdateTrigger, IDeviceUpdateTrigger
|
|
{
|
|
#region Constants
|
|
|
|
private const long FLUSH_TIMER = 5 * 1000 * TimeSpan.TicksPerMillisecond; // every 5 seconds flush the device 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).
|
|
/// <= 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 bool _isRunning;
|
|
private Task _updateTask;
|
|
private CancellationTokenSource _updateTokenSource;
|
|
private CancellationToken _updateToken;
|
|
|
|
#endregion
|
|
|
|
#region Constructors
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="SteelSeriesDeviceUpdateTrigger"/> class.
|
|
/// </summary>
|
|
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)
|
|
{
|
|
this.UpdateRateHardLimit = 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()
|
|
{
|
|
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)));
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void TriggerHasData() => _hasDataEvent.Set();
|
|
|
|
private void UpdateUpdateFrequency()
|
|
{
|
|
UpdateFrequency = MaxUpdateRate;
|
|
if ((UpdateFrequency <= 0) || ((UpdateRateHardLimit > 0) && (UpdateRateHardLimit < UpdateFrequency)))
|
|
UpdateFrequency = UpdateRateHardLimit;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|