diff --git a/RGB.NET.Devices.PicoPi/PicoPi/PicoPiHIDUpdateQueue.cs b/RGB.NET.Devices.PicoPi/PicoPi/PicoPiHIDUpdateQueue.cs index beb5f58..8ee06a2 100644 --- a/RGB.NET.Devices.PicoPi/PicoPi/PicoPiHIDUpdateQueue.cs +++ b/RGB.NET.Devices.PicoPi/PicoPi/PicoPiHIDUpdateQueue.cs @@ -9,12 +9,6 @@ namespace RGB.NET.Devices.PicoPi; /// public class PicoPiHIDUpdateQueue : UpdateQueue { - #region Constants - - private const int OFFSET_MULTIPLIER = 60; - - #endregion - #region Properties & Fields private readonly PicoPiSDK _sdk; @@ -62,15 +56,7 @@ public class PicoPiHIDUpdateQueue : UpdateQueue buffer[offset + 2] = b; } - int chunks = _dataBuffer.Length / OFFSET_MULTIPLIER; - if ((chunks * OFFSET_MULTIPLIER) < buffer.Length) chunks++; - for (int i = 0; i < chunks; i++) - { - int offset = i * OFFSET_MULTIPLIER; - int length = Math.Min(buffer.Length - offset, OFFSET_MULTIPLIER); - bool update = i == (chunks - 1); - _sdk.SendHidUpdate(buffer.Slice(offset, length), _channel, i, update); - } + _sdk.SendHidUpdate(buffer, _channel); } #endregion diff --git a/RGB.NET.Devices.PicoPi/PicoPi/PicoPiSDK.cs b/RGB.NET.Devices.PicoPi/PicoPi/PicoPiSDK.cs index f7db97b..a47916c 100644 --- a/RGB.NET.Devices.PicoPi/PicoPi/PicoPiSDK.cs +++ b/RGB.NET.Devices.PicoPi/PicoPi/PicoPiSDK.cs @@ -1,4 +1,5 @@ -using System; +// ReSharper disable MemberCanBePrivate.Global +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; @@ -36,6 +37,8 @@ public class PicoPiSDK : IDisposable private const byte COMMAND_UPDATE = 0x01; private const byte COMMAND_UPDATE_BULK = 0x02; + public const int HID_OFFSET_MULTIPLIER = 60; + #endregion #region Properties & Fields @@ -204,6 +207,24 @@ public class PicoPiSDK : IDisposable } } + /// + /// Sends a update to the device using the HID-endpoint and fragments the data if needed. + /// + /// The data to send. + /// The channel to update. + public void SendHidUpdate(in Span buffer, int channel) + { + int chunks = buffer.Length / HID_OFFSET_MULTIPLIER; + if ((chunks * HID_OFFSET_MULTIPLIER) < buffer.Length) chunks++; + for (int i = 0; i < chunks; i++) + { + int offset = i * HID_OFFSET_MULTIPLIER; + int length = Math.Min(buffer.Length - offset, HID_OFFSET_MULTIPLIER); + bool update = i == (chunks - 1); + SendHidUpdate(buffer.Slice(offset, length), channel, i, update); + } + } + /// /// Sends a update to the device using the HID-endpoint. /// @@ -260,6 +281,15 @@ public class PicoPiSDK : IDisposable _bulkTransferLength = 0; } + /// + /// Resets all leds to black. + /// + public void Reset() + { + foreach ((int channel, int ledCount, _) in Channels) + SendHidUpdate(new byte[ledCount * 3], channel); + } + private void SendHID(params byte[] data) => _hidStream.Write(data); private void SendBulk(byte[] data, int count) => _bulkWriter!.Write(data, 0, count, 1000, out int _); @@ -268,6 +298,8 @@ public class PicoPiSDK : IDisposable /// public void Dispose() { + Reset(); + _hidStream.Dispose(); _bulkDevice?.Dispose(); _usbContext?.Dispose();