using HaloPixelToolBox.Core.Models.Bar;
using HaloPixelToolBox.Core.Models.Display;
using HaloPixelToolBox.Core.Models.Lighting;
using HidSharp;
using XFEExtension.NetCore.StringExtension;
namespace HaloPixelToolBox.Core.Utilities;
public partial class HaloPixelDevice
{
public HidDevice? CurrentDevice { get; set; }
public HaloPixelDevice()
{
DeviceList.Local.Changed += Local_Changed;
}
private void Local_Changed(object? sender, DeviceListChangedEventArgs e)
{
Console.WriteLine(sender.X());
}
public bool Initialize()
{
if (GetPixelDevice().FirstOrDefault() is HidDevice device)
{
CurrentDevice = device;
return true;
}
else
{
return false;
}
}
public void ShowText(string text)
{
WriteCommand(DeviceCommandKind.Text, (stream, state) =>
HidPacketBuilder.BuildTextPackets(text, GetTextCapacity(stream, state)));
}
public void SetTextLayout(HaloPixelTextLayout layout)
{
WriteCommand(DeviceCommandKind.DisplayMode, HidPacketBuilder.ConvertLayout(layout));
}
public void SetUIModel(HaloPixelUIModel haloPixelUIModel)
{
WriteCommand(DeviceCommandKind.DisplayMode, HidPacketBuilder.ConvertUIModel(haloPixelUIModel), clearsText: true);
}
public static IEnumerable<HidDevice> GetPixelDevice()
{
foreach (var device in DeviceList.Local.GetHidDevices())
{
var name = string.Empty;
try
{
name = device.GetFriendlyName();
}
catch { }
if (device.GetMaxInputReportLength() == 64 && name.Contains("花再 Halo PixelBar"))
yield return device;
}
}
public static void PrintDeviceList()
{
foreach (var subDeivce in DeviceList.Local.GetHidDevices())
{
try
{
Console.WriteLine($"""
----------------------
{subDeivce.GetFriendlyName()}
VendorID:{subDeivce.VendorID}
ProductID:{subDeivce.ProductID}
串口号:{subDeivce.GetSerialNumber()}
串口:{string.Join(',', subDeivce.GetSerialPorts())}
ReleaseNumberBcd:{subDeivce.ReleaseNumberBcd}
UsbPort:{subDeivce.GetUsbPort()}
----------------------
""");
}
catch { }
}
}
public void SetAmbientLight(AmbientLightOptions options)
{
var packet = HidPacketBuilder.BuildAmbientLight(options);
WriteCommand(DeviceCommandKind.AmbientLight, (_, _) => [packet, packet, packet]);
}
public void SetAmbientLightEnabled(bool enabled)
{
WriteCommand(DeviceCommandKind.AmbientPower, HidPacketBuilder.BuildAmbientLightPower(enabled));
}
public void SetPixelScreenColor(HaloPixelColor color)
{
WriteCommand(DeviceCommandKind.ScreenColor, HidPacketBuilder.BuildPixelScreenColor(color));
}
}
using HaloPixelToolBox.Core.Models.Bar;
using HaloPixelToolBox.Core.Models.Display;
using HaloPixelToolBox.Core.Models.Lighting;
using HidSharp;
using XFEExtension.NetCore.StringExtension;
namespace HaloPixelToolBox.Core.Utilities;
public partial class HaloPixelDevice
{
public HidDevice? CurrentDevice { get; set; }
public HaloPixelDevice()
{
DeviceList.Local.Changed += Local_Changed;
}
private void Local_Changed(object? sender, DeviceListChangedEventArgs e)
{
Console.WriteLine(sender.X());
}
public bool Initialize()
{
if (GetPixelDevice().FirstOrDefault() is HidDevice device)
{
CurrentDevice = device;
return true;
}
else
{
return false;
}
}
public void ShowText(string text)
{
WriteCommand(DeviceCommandKind.Text, (stream, state) =>
HidPacketBuilder.BuildTextPackets(text, GetTextCapacity(stream, state)));
}
public void SetTextLayout(HaloPixelTextLayout layout)
{
WriteCommand(DeviceCommandKind.DisplayMode, HidPacketBuilder.ConvertLayout(layout));
}
public void SetUIModel(HaloPixelUIModel haloPixelUIModel)
{
WriteCommand(DeviceCommandKind.DisplayMode, HidPacketBuilder.ConvertUIModel(haloPixelUIModel), clearsText: true);
}
public static IEnumerable<HidDevice> GetPixelDevice()
{
foreach (var device in DeviceList.Local.GetHidDevices())
{
var name = string.Empty;
try
{
name = device.GetFriendlyName();
}
catch { }
if (device.GetMaxInputReportLength() == 64 && name.Contains("花再 Halo PixelBar"))
yield return device;
}
}
public static void PrintDeviceList()
{
foreach (var subDeivce in DeviceList.Local.GetHidDevices())
{
try
{
Console.WriteLine($"""
----------------------
{subDeivce.GetFriendlyName()}
VendorID:{subDeivce.VendorID}
ProductID:{subDeivce.ProductID}
串口号:{subDeivce.GetSerialNumber()}
串口:{string.Join(',', subDeivce.GetSerialPorts())}
ReleaseNumberBcd:{subDeivce.ReleaseNumberBcd}
UsbPort:{subDeivce.GetUsbPort()}
----------------------
""");
}
catch { }
}
}
public void SetAmbientLight(AmbientLightOptions options)
{
var packet = HidPacketBuilder.BuildAmbientLight(options);
WriteCommand(DeviceCommandKind.AmbientLight, (_, _) => [packet, packet, packet]);
}
public void SetAmbientLightEnabled(bool enabled)
{
WriteCommand(DeviceCommandKind.AmbientPower, HidPacketBuilder.BuildAmbientLightPower(enabled));
}
public void SetPixelScreenColor(HaloPixelColor color)
{
WriteCommand(DeviceCommandKind.ScreenColor, HidPacketBuilder.BuildPixelScreenColor(color));
}
}