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

Merge pull request #22 from DarthAffe/Development

Merged exception-handling to fix #9
This commit is contained in:
DarthAffe 2015-10-10 12:31:07 +02:00
commit 62fba47519
5 changed files with 65 additions and 17 deletions

View File

@ -45,6 +45,7 @@
<Compile Include="Devices\Generic\Enums\CorsairAccessMode.cs" /> <Compile Include="Devices\Generic\Enums\CorsairAccessMode.cs" />
<Compile Include="Devices\Generic\Enums\CorsairDeviceCaps.cs" /> <Compile Include="Devices\Generic\Enums\CorsairDeviceCaps.cs" />
<Compile Include="Devices\Generic\Enums\CorsairDeviceType.cs" /> <Compile Include="Devices\Generic\Enums\CorsairDeviceType.cs" />
<Compile Include="Devices\Generic\OnExceptionEventArgs.cs" />
<Compile Include="Devices\Keyboard\Brushes\AbstractBrush.cs" /> <Compile Include="Devices\Keyboard\Brushes\AbstractBrush.cs" />
<Compile Include="Devices\Keyboard\Brushes\Gradient\AbstractGradient.cs" /> <Compile Include="Devices\Keyboard\Brushes\Gradient\AbstractGradient.cs" />
<Compile Include="Devices\Keyboard\Brushes\Gradient\GradientStop.cs" /> <Compile Include="Devices\Keyboard\Brushes\Gradient\GradientStop.cs" />

View File

@ -39,6 +39,12 @@ namespace CUE.NET.Devices.Generic
#endregion #endregion
#region Events
public event OnExceptionEventHandler OnException;
#endregion
#region Constructors #region Constructors
protected AbstractCueDevice(IDeviceInfo info) protected AbstractCueDevice(IDeviceInfo info)
@ -142,6 +148,11 @@ namespace CUE.NET.Devices.Generic
Marshal.FreeHGlobal(ptr); Marshal.FreeHGlobal(ptr);
} }
protected void ManageException(Exception ex)
{
OnException?.Invoke(this, new OnExceptionEventArgs(ex));
}
#endregion #endregion
} }
} }

View File

@ -0,0 +1,22 @@
using System;
namespace CUE.NET.Devices.Generic
{
public class OnExceptionEventArgs : EventArgs
{
#region Properties & Fields
public Exception Exception { get; }
#endregion
#region Constructors
public OnExceptionEventArgs(Exception exception)
{
this.Exception = exception;
}
#endregion
}
}

View File

@ -1,15 +1,21 @@
using CUE.NET.Devices.Generic.Enums; using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Generic.Enums;
namespace CUE.NET.Devices namespace CUE.NET.Devices
{ {
public delegate void OnExceptionEventHandler(object sender, OnExceptionEventArgs args);
public interface ICueDevice public interface ICueDevice
{ {
IDeviceInfo DeviceInfo { get; } IDeviceInfo DeviceInfo { get; }
UpdateMode UpdateMode { get; set; } UpdateMode UpdateMode { get; set; }
float UpdateFrequency { get; set; } float UpdateFrequency { get; set; }
event OnExceptionEventHandler OnException;
void Update(bool flushLeds = false); void Update(bool flushLeds = false);
} }
} }

View File

@ -97,22 +97,26 @@ namespace CUE.NET.Devices.Keyboard
long currentTicks = DateTime.Now.Ticks; long currentTicks = DateTime.Now.Ticks;
foreach (EffectTimeContainer effect in _effects) foreach (EffectTimeContainer effect in _effects)
{ {
float deltaTime; try
if (effect.TicksAtLastUpdate < 0)
{ {
float deltaTime;
if (effect.TicksAtLastUpdate < 0)
{
effect.TicksAtLastUpdate = currentTicks;
deltaTime = 0f;
}
else
deltaTime = (currentTicks - effect.TicksAtLastUpdate) / 10000000f;
effect.TicksAtLastUpdate = currentTicks; effect.TicksAtLastUpdate = currentTicks;
deltaTime = 0f; effect.Effect.Update(deltaTime);
ApplyBrush((effect.Effect.KeyList ?? this).ToList(), effect.Effect.EffectBrush);
if (effect.Effect.IsDone)
effectsToRemove.Add(effect.Effect);
} }
else catch (Exception ex) { ManageException(ex); }
deltaTime = (currentTicks - effect.TicksAtLastUpdate) / 10000000f;
effect.TicksAtLastUpdate = currentTicks;
effect.Effect.Update(deltaTime);
ApplyBrush((effect.Effect.KeyList ?? this).ToList(), effect.Effect.EffectBrush);
if (effect.Effect.IsDone)
effectsToRemove.Add(effect.Effect);
} }
} }
@ -136,9 +140,13 @@ namespace CUE.NET.Devices.Keyboard
// ReSharper disable once MemberCanBeMadeStatic.Local - idc // ReSharper disable once MemberCanBeMadeStatic.Local - idc
private void ApplyBrush(ICollection<CorsairKey> keys, IBrush brush) private void ApplyBrush(ICollection<CorsairKey> keys, IBrush brush)
{ {
RectangleF brushRectangle = RectangleHelper.CreateRectangleFromRectangles(keys.Select(x => x.KeyRectangle)); try
foreach (CorsairKey key in keys) {
key.Led.Color = brush.GetColorAtPoint(brushRectangle, key.KeyRectangle.GetCenter()); RectangleF brushRectangle = RectangleHelper.CreateRectangleFromRectangles(keys.Select(x => x.KeyRectangle));
foreach (CorsairKey key in keys)
key.Led.Color = brush.GetColorAtPoint(brushRectangle, key.KeyRectangle.GetCenter());
}
catch (Exception ex) { ManageException(ex); }
} }
#endregion #endregion