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

Added reset method and moved HID-packet fragmentation into the SDK

This commit is contained in:
Darth Affe 2021-12-15 21:55:55 +01:00
parent 3e53e5711f
commit 0b561a40bd
2 changed files with 34 additions and 16 deletions

View File

@ -9,12 +9,6 @@ namespace RGB.NET.Devices.PicoPi;
/// </summary>
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

View File

@ -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
}
}
/// <summary>
/// Sends a update to the device using the HID-endpoint and fragments the data if needed.
/// </summary>
/// <param name="data">The data to send.</param>
/// <param name="channel">The channel to update.</param>
public void SendHidUpdate(in Span<byte> 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);
}
}
/// <summary>
/// Sends a update to the device using the HID-endpoint.
/// </summary>
@ -260,6 +281,15 @@ public class PicoPiSDK : IDisposable
_bulkTransferLength = 0;
}
/// <summary>
/// Resets all leds to black.
/// </summary>
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
/// <inheritdoc />
public void Dispose()
{
Reset();
_hidStream.Dispose();
_bulkDevice?.Dispose();
_usbContext?.Dispose();