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

Added PicoPi device provider

This commit is contained in:
Darth Affe 2021-04-24 20:41:55 +02:00
parent a1955ec377
commit 5d3071235b
12 changed files with 610 additions and 0 deletions

View File

@ -0,0 +1,9 @@
namespace RGB.NET.Devices.PicoPi.Enum
{
public enum UpdateMode
{
Auto = 0x00,
HID = 0x01,
BULK = 0x02,
}
}

View File

@ -0,0 +1,23 @@
using RGB.NET.Core;
namespace RGB.NET.Devices.PicoPi
{
public static class LedMappings
{
#region Properties & Fields
public static LedMapping<int> StripeMapping = new();
#endregion
#region Constructors
static LedMappings()
{
for (int i = 0; i < 255; i++)
StripeMapping.Add(LedId.LedStripe1 + i, i);
}
#endregion
}
}

View File

@ -0,0 +1,52 @@
using System;
using RGB.NET.Core;
namespace RGB.NET.Devices.PicoPi
{
public class PicoPiBulkUpdateQueue : UpdateQueue
{
#region Properties & Fields
private readonly PicoPiSDK _sdk;
private readonly int _channel;
private readonly byte[] _dataBuffer;
#endregion
#region Constructors
public PicoPiBulkUpdateQueue(IDeviceUpdateTrigger updateTrigger, PicoPiSDK sdk, int channel, int ledCount)
: base(updateTrigger)
{
this._sdk = sdk;
this._channel = channel;
_dataBuffer = new byte[ledCount * 3];
}
#endregion
#region Methods
protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet)
{
Span<byte> buffer = _dataBuffer;
foreach ((object key, Color color) in dataSet)
{
int index = key as int? ?? -1;
if (index < 0) continue;
(byte _, byte r, byte g, byte b) = color.GetRGBBytes();
int offset = index * 3;
buffer[offset] = r;
buffer[offset + 1] = g;
buffer[offset + 2] = b;
}
_sdk.SendBulkUpdate(buffer, _channel);
}
#endregion
}
}

View File

@ -0,0 +1,66 @@
using System;
using RGB.NET.Core;
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;
private readonly int _channel;
private readonly byte[] _dataBuffer;
#endregion
#region Constructors
public PicoPiHIDUpdateQueue(IDeviceUpdateTrigger updateTrigger, PicoPiSDK sdk, int channel, int ledCount)
: base(updateTrigger)
{
this._sdk = sdk;
this._channel = channel;
_dataBuffer = new byte[ledCount * 3];
}
#endregion
#region Methods
protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet)
{
Span<byte> buffer = _dataBuffer;
foreach ((object key, Color color) in dataSet)
{
int index = key as int? ?? -1;
if (index < 0) continue;
(byte _, byte r, byte g, byte b) = color.GetRGBBytes();
int offset = index * 3;
buffer[offset] = r;
buffer[offset + 1] = g;
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);
}
}
#endregion
}
}

View File

@ -0,0 +1,36 @@
using RGB.NET.Core;
namespace RGB.NET.Devices.PicoPi
{
public class PicoPiRGBDevice : AbstractRGBDevice<PicoPiRGBDeviceInfo>
{
#region Properties & Fields
private readonly LedMapping<int> _ledMapping;
#endregion
#region Constructors
public PicoPiRGBDevice(PicoPiRGBDeviceInfo deviceInfo, IUpdateQueue updateQueue, LedMapping<int> ledMapping)
: base(deviceInfo, updateQueue)
{
this._ledMapping = ledMapping;
}
#endregion
#region Methods
internal void Initialize()
{
for (int i = 0; i < DeviceInfo.LedCount; i++)
AddLed(_ledMapping[i], new Point(i * 10, 0), new Size(10, 10), i);
}
/// <inheritdoc />
protected override object GetLedCustomData(LedId ledId) => _ledMapping.TryGetValue(ledId, out int index) ? index : -1;
#endregion
}
}

View File

@ -0,0 +1,42 @@
using RGB.NET.Core;
namespace RGB.NET.Devices.PicoPi
{
public class PicoPiRGBDeviceInfo : IRGBDeviceInfo
{
#region Properties & Fields
public RGBDeviceType DeviceType { get; }
public string DeviceName { get; }
public string Manufacturer => "RGB.NET";
public string Model { get; }
public object? LayoutMetadata { get; set; }
public int Id { get; }
public int Version { get; }
public int Channel { get; }
public int LedCount { get; }
#endregion
#region Constructors
internal PicoPiRGBDeviceInfo(RGBDeviceType deviceType, string model, int id, int version, int channel, int ledCount)
{
this.DeviceType = deviceType;
this.Model = model;
this.Id = id;
this.Version = version;
this.Channel = channel;
this.LedCount = ledCount;
DeviceName = DeviceHelper.CreateDeviceName(Manufacturer, $"{Model} {id} (Channel {channel})");
}
#endregion
#region Methods
#endregion
}
}

View File

@ -0,0 +1,189 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using HidSharp;
using LibUsbDotNet.LibUsb;
using LibUsbDotNet.Main;
namespace RGB.NET.Devices.PicoPi
{
public class PicoPiSDK : IDisposable
{
#region Constants
public const int VENDOR_ID = 0x1209;
public const int HID_BULK_CONTROLLER_PID = 0x2812;
private const byte COMMAND_CHANNEL_COUNT = 0x01;
private const byte COMMAND_LEDCOUNTS = 0x0A;
private const byte COMMAND_PINS = 0x0B;
private const byte COMMAND_ID = 0x0E;
private const byte COMMAND_VERSION = 0x0F;
private const byte COMMAND_UPDATE = 0x01;
private const byte COMMAND_UPDATE_BULK = 0x02;
#endregion
#region Properties & Fields
private readonly HidDevice _hidDevice;
private readonly HidStream _hidStream;
private UsbContext? _usbContext;
private IUsbDevice? _bulkDevice;
private UsbEndpointWriter? _bulkWriter;
private readonly byte[] _hidSendBuffer;
private readonly byte[] _bulkSendBuffer;
private int _bulkTransferLength = 0;
public bool IsBulkSupported { get; private set; }
public int Id { get; }
public int Version { get; }
public IReadOnlyList<(int channel, int ledCount)> Channels { get; }
#endregion
#region Constructors
public PicoPiSDK(HidDevice device)
{
this._hidDevice = device;
_hidSendBuffer = new byte[_hidDevice.GetMaxOutputReportLength() - 1];
_hidStream = _hidDevice.Open();
LoadBulkDevice();
Id = GetId();
Version = GetVersion();
Channels = new ReadOnlyCollection<(int channel, int ledCount)>(GetChannels().ToList());
_bulkSendBuffer = new byte[(Channels.Sum(c => c.ledCount + 1) * 3) + 5];
}
#endregion
#region Methods
private void LoadBulkDevice()
{
try
{
_usbContext = new UsbContext();
// DarthAffe 24.04.2021: Not using .Find as it's not returning the device :(
IEnumerable<IUsbDevice> devices = _usbContext.List().Where(d => (d.VendorId == _hidDevice.VendorID) && (d.ProductId == _hidDevice.ProductID));
foreach (IUsbDevice device in devices)
{
try
{
device.Open();
if (device.Info.SerialNumber == _hidDevice.GetSerialNumber())
{
_bulkDevice = device;
break;
}
device.Dispose();
}
catch { /**/ }
}
if (_bulkDevice != null)
{
_bulkDevice.ClaimInterface(1);
_bulkWriter = _bulkDevice.OpenEndpointWriter(WriteEndpointID.Ep02, EndpointType.Bulk);
IsBulkSupported = true;
}
}
catch
{
_bulkWriter = null;
try { _bulkDevice?.Dispose(); } catch { /**/ }
try { _usbContext?.Dispose(); } catch { /**/ }
_bulkDevice = null;
}
}
private int GetId()
{
SendHID(0x00, COMMAND_ID);
return Read()[1];
}
private int GetVersion()
{
SendHID(0x00, COMMAND_VERSION);
return Read()[1];
}
private IEnumerable<(int channel, int ledCount)> GetChannels()
{
SendHID(0x00, COMMAND_CHANNEL_COUNT);
int channelCount = Read()[1];
for (int i = 1; i <= channelCount; i++)
{
SendHID(0x00, (byte)((i << 4) | COMMAND_LEDCOUNTS));
int ledCount = Read()[1];
if (ledCount > 0)
yield return (i, ledCount);
}
}
public void SendHidUpdate(in Span<byte> data, int channel, int chunk, bool update)
{
if (data.Length == 0) return;
Span<byte> sendBuffer = _hidSendBuffer;
sendBuffer[0] = 0x00;
sendBuffer[1] = (byte)((channel << 4) | COMMAND_UPDATE);
sendBuffer[2] = update ? (byte)1 : (byte)0;
sendBuffer[3] = (byte)chunk;
data.CopyTo(sendBuffer.Slice(4, data.Length));
SendHID(_hidSendBuffer);
}
public void SendBulkUpdate(in Span<byte> data, int channel)
{
if ((data.Length == 0) || !IsBulkSupported) return;
Span<byte> sendBuffer = new Span<byte>(_bulkSendBuffer).Slice(2);
int payloadSize = data.Length;
sendBuffer[_bulkTransferLength++] = (byte)((channel << 4) | COMMAND_UPDATE_BULK);
sendBuffer[_bulkTransferLength++] = (byte)((payloadSize >> 8) & 0xFF);
sendBuffer[_bulkTransferLength++] = (byte)(payloadSize & 0xFF);
data.CopyTo(sendBuffer.Slice(_bulkTransferLength, payloadSize));
_bulkTransferLength += payloadSize;
}
public void FlushBulk()
{
if (_bulkTransferLength == 0) return;
_bulkSendBuffer[0] = (byte)((_bulkTransferLength >> 8) & 0xFF);
_bulkSendBuffer[1] = (byte)(_bulkTransferLength & 0xFF);
SendBulk(_bulkSendBuffer, _bulkTransferLength + 2);
_bulkTransferLength = 0;
}
private void SendHID(params byte[] data) => _hidStream.Write(data);
private void SendBulk(byte[] data, int count) => _bulkWriter!.Write(data, 0, count, 1000, out int _);
private byte[] Read() => _hidStream.Read();
public void Dispose()
{
_hidStream.Dispose();
_bulkDevice?.Dispose();
_usbContext?.Dispose();
}
#endregion
}
}

View File

@ -0,0 +1,122 @@
// ReSharper disable MemberCanBePrivate.Global
using System;
using System.Collections.Generic;
using System.Linq;
using HidSharp;
using RGB.NET.Core;
using RGB.NET.Devices.PicoPi.Enum;
using RGB.NET.HID;
namespace RGB.NET.Devices.PicoPi
{
/// <inheritdoc />
/// <summary>
/// Represents a device provider responsible for PicoPi-devices.
/// </summary>
// ReSharper disable once InconsistentNaming
public class PicoPiDeviceProvider : AbstractRGBDeviceProvider
{
#region Constants
private const int AUTO_UPDATE_MODE_CHUNK_THRESHOLD = 2;
#endregion
#region Properties & Fields
private static PicoPiDeviceProvider? _instance;
/// <summary>
/// Gets the singleton <see cref="PicoPiDeviceProvider"/> instance.
/// </summary>
public static PicoPiDeviceProvider Instance => _instance ?? new PicoPiDeviceProvider();
public static HIDLoader<int, int> DeviceDefinitions { get; } = new(PicoPiSDK.VENDOR_ID)
{
{ PicoPiSDK.HID_BULK_CONTROLLER_PID, RGBDeviceType.LedStripe, "WS1812B-Controller", LedMappings.StripeMapping, 0 },
};
private readonly List<PicoPiSDK> _sdks = new();
public UpdateMode UpdateMode { get; set; } = UpdateMode.Auto;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="PicoPiDeviceProvider"/> class.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown if this constructor is called even if there is already an instance of this class.</exception>
public PicoPiDeviceProvider()
{
if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(PicoPiDeviceProvider)}");
_instance = this;
}
#endregion
#region Methods
protected override void InitializeSDK() { }
/// <inheritdoc />
protected override IEnumerable<IRGBDevice> GetLoadedDevices(RGBDeviceType loadFilter)
{
DeviceDefinitions.LoadFilter = loadFilter;
return base.GetLoadedDevices(loadFilter);
}
/// <inheritdoc />
protected override IEnumerable<IRGBDevice> LoadDevices()
{
IEnumerable<(HIDDeviceDefinition<int, int> definition, HidDevice device)> devices = DeviceDefinitions.GetConnectedDevices();
foreach ((HIDDeviceDefinition<int, int> definition, HidDevice device) in devices)
{
PicoPiSDK sdk = new(device);
_sdks.Add(sdk);
IDeviceUpdateTrigger updateTrigger = GetUpdateTrigger(sdk.Id);
foreach ((int channel, int ledCount) in sdk.Channels)
{
PicoPiRGBDevice picoPiDevice = new(new PicoPiRGBDeviceInfo(definition.DeviceType, definition.Name, sdk.Id, sdk.Version, channel, ledCount), GetUpdateQueue(updateTrigger, sdk, channel, ledCount), definition.LedMapping);
picoPiDevice.Initialize();
yield return picoPiDevice;
}
if (sdk.IsBulkSupported)
updateTrigger.Update += (_, _) => sdk.FlushBulk();
}
}
private IUpdateQueue GetUpdateQueue(IDeviceUpdateTrigger updateTrigger, PicoPiSDK sdk, int channel, int ledCount)
{
switch (UpdateMode)
{
case UpdateMode.HID:
return new PicoPiHIDUpdateQueue(updateTrigger, sdk, channel, ledCount);
case UpdateMode.BULK:
if (!sdk.IsBulkSupported) throw new NotSupportedException("Bulk updates aren't supported for this device. Make sure the firmware is built with bulk support and the libusb driver is installed.");
return new PicoPiBulkUpdateQueue(updateTrigger, sdk, channel, ledCount);
case UpdateMode.Auto:
if (!sdk.IsBulkSupported || (sdk.Channels.Sum(c => (int)Math.Ceiling(c.ledCount / 20.0)) <= AUTO_UPDATE_MODE_CHUNK_THRESHOLD)) return new PicoPiHIDUpdateQueue(updateTrigger, sdk, channel, ledCount);
return new PicoPiBulkUpdateQueue(updateTrigger, sdk, channel, ledCount);
default: throw new IndexOutOfRangeException($"Update mode {UpdateMode} is not supported.");
}
}
protected override void Reset()
{
base.Reset();
foreach (PicoPiSDK sdk in _sdks)
sdk.Dispose();
_sdks.Clear();
}
#endregion
}
}

View File

@ -0,0 +1,61 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net5.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<Authors>Darth Affe</Authors>
<Company>Wyrez</Company>
<Language>en-US</Language>
<NeutralLanguage>en-US</NeutralLanguage>
<Title>RGB.NET.Devices.PicoPi</Title>
<AssemblyName>RGB.NET.Devices.PicoPi</AssemblyName>
<AssemblyTitle>RGB.NET.Devices.PicoPi</AssemblyTitle>
<PackageId>RGB.NET.Devices.PicoPi</PackageId>
<RootNamespace>RGB.NET.Devices.PicoPi</RootNamespace>
<Description>PicoPi-Device-Implementations of RGB.NET</Description>
<Summary>PicoPi-Device-Implementations of RGB.NET, a C# (.NET) library for accessing various RGB-peripherals</Summary>
<Copyright>Copyright © Darth Affe 2020</Copyright>
<PackageCopyright>Copyright © Darth Affe 2020</PackageCopyright>
<PackageIconUrl>http://lib.arge.be/icon.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/DarthAffe/RGB.NET</PackageProjectUrl>
<PackageLicenseUrl>https://raw.githubusercontent.com/DarthAffe/RGB.NET/master/LICENSE</PackageLicenseUrl>
<RepositoryType>Github</RepositoryType>
<RepositoryUrl>https://github.com/DarthAffe/RGB.NET</RepositoryUrl>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageReleaseNotes></PackageReleaseNotes>
<Version>0.0.1</Version>
<AssemblyVersion>0.0.1</AssemblyVersion>
<FileVersion>0.0.1</FileVersion>
<OutputPath>..\bin\</OutputPath>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IncludeSource>True</IncludeSource>
<IncludeSymbols>True</IncludeSymbols>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<DefineConstants>$(DefineConstants);TRACE;DEBUG</DefineConstants>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<NoWarn>$(NoWarn);CS1591;CS1572;CS1573</NoWarn>
<DefineConstants>$(DefineConstants);RELEASE</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="LibUsbDotNet" Version="3.0.87-alpha" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\RGB.NET.Core\RGB.NET.Core.csproj" />
<ProjectReference Include="..\RGB.NET.HID\RGB.NET.HID.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=picopi/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View File

@ -0,0 +1 @@
Check https://github.com/DarthAffe/RGB.NET-PicoPi for the required firmware.

View File

@ -41,6 +41,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RGB.NET.Layout", "RGB.NET.L
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RGB.NET.HID", "RGB.NET.HID\RGB.NET.HID.csproj", "{4F4B7329-4858-4314-BA32-9DF3B1B33482}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RGB.NET.Devices.PicoPi", "RGB.NET.Devices.PicoPi\RGB.NET.Devices.PicoPi.csproj", "{7FC5C7A8-7B27-46E7-A8E8-DB80568F49C5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -115,6 +117,10 @@ Global
{4F4B7329-4858-4314-BA32-9DF3B1B33482}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F4B7329-4858-4314-BA32-9DF3B1B33482}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F4B7329-4858-4314-BA32-9DF3B1B33482}.Release|Any CPU.Build.0 = Release|Any CPU
{7FC5C7A8-7B27-46E7-A8E8-DB80568F49C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7FC5C7A8-7B27-46E7-A8E8-DB80568F49C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7FC5C7A8-7B27-46E7-A8E8-DB80568F49C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7FC5C7A8-7B27-46E7-A8E8-DB80568F49C5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -133,6 +139,7 @@ Global
{A3FD5AD7-040A-47CA-A278-53493A25FF8A} = {92D7C263-D4C9-4D26-93E2-93C1F9C2CD16}
{E0732B34-3F96-4DD9-AFD5-0E34B833AD6D} = {D13032C6-432E-4F43-8A32-071133C22B16}
{DD46DB2D-85BE-4962-86AE-E38C9053A548} = {D13032C6-432E-4F43-8A32-071133C22B16}
{7FC5C7A8-7B27-46E7-A8E8-DB80568F49C5} = {D13032C6-432E-4F43-8A32-071133C22B16}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7F222AD4-1F9E-4AAB-9D69-D62372D4C1BA}