diff --git a/CUE.NET.csproj b/CUE.NET.csproj index 3932d0e..06f833a 100644 --- a/CUE.NET.csproj +++ b/CUE.NET.csproj @@ -45,6 +45,7 @@ + diff --git a/Devices/Generic/AbstractCueDevice.cs b/Devices/Generic/AbstractCueDevice.cs index 40e522a..918ecf9 100644 --- a/Devices/Generic/AbstractCueDevice.cs +++ b/Devices/Generic/AbstractCueDevice.cs @@ -39,6 +39,12 @@ namespace CUE.NET.Devices.Generic #endregion + #region Events + + public event OnExceptionEventHandler OnException; + + #endregion + #region Constructors protected AbstractCueDevice(IDeviceInfo info) @@ -142,6 +148,11 @@ namespace CUE.NET.Devices.Generic Marshal.FreeHGlobal(ptr); } + protected void ManageException(Exception ex) + { + OnException?.Invoke(this, new OnExceptionEventArgs(ex)); + } + #endregion } } diff --git a/Devices/Generic/OnExceptionEventArgs.cs b/Devices/Generic/OnExceptionEventArgs.cs new file mode 100644 index 0000000..d53b86f --- /dev/null +++ b/Devices/Generic/OnExceptionEventArgs.cs @@ -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 + } +} diff --git a/Devices/ICueDevice.cs b/Devices/ICueDevice.cs index 4a6d654..ff62eab 100644 --- a/Devices/ICueDevice.cs +++ b/Devices/ICueDevice.cs @@ -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 { + public delegate void OnExceptionEventHandler(object sender, OnExceptionEventArgs args); + public interface ICueDevice { + IDeviceInfo DeviceInfo { get; } UpdateMode UpdateMode { get; set; } float UpdateFrequency { get; set; } + event OnExceptionEventHandler OnException; + void Update(bool flushLeds = false); } } diff --git a/Devices/Keyboard/CorsairKeyboard.cs b/Devices/Keyboard/CorsairKeyboard.cs index 806b88d..365a6d5 100644 --- a/Devices/Keyboard/CorsairKeyboard.cs +++ b/Devices/Keyboard/CorsairKeyboard.cs @@ -97,22 +97,26 @@ namespace CUE.NET.Devices.Keyboard long currentTicks = DateTime.Now.Ticks; foreach (EffectTimeContainer effect in _effects) { - float deltaTime; - if (effect.TicksAtLastUpdate < 0) + try { + float deltaTime; + if (effect.TicksAtLastUpdate < 0) + { + effect.TicksAtLastUpdate = currentTicks; + deltaTime = 0f; + } + else + deltaTime = (currentTicks - effect.TicksAtLastUpdate) / 10000000f; + 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 - 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); + catch (Exception ex) { ManageException(ex); } } } @@ -136,9 +140,13 @@ namespace CUE.NET.Devices.Keyboard // ReSharper disable once MemberCanBeMadeStatic.Local - idc private void ApplyBrush(ICollection keys, IBrush brush) { - RectangleF brushRectangle = RectangleHelper.CreateRectangleFromRectangles(keys.Select(x => x.KeyRectangle)); - foreach (CorsairKey key in keys) - key.Led.Color = brush.GetColorAtPoint(brushRectangle, key.KeyRectangle.GetCenter()); + try + { + 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