using HaloPixelToolBox.Core.Models.Bar;
using HaloPixelToolBox.Core.Models.Display;
using HaloPixelToolBox.Core.Models.Lighting;
using System.Text;
namespace HaloPixelToolBox.Core.Utilities;
public class HidPacketBuilder
{
///
/// 文本头
///
public static readonly byte[] TextHeader =
[
0x2E, 0xAA, 0xEC, 0xE8, 0x00
];
///
/// 布局头
///
public static readonly byte[] LayoutHeader =
[
0x2E, 0xAA, 0xEC, 0xEF, 0x00, 0x09, 0x01, 0xf0, 0xb4, 0xc8, 0x00, 0x02, 0x00
];
///
/// 固定包长度(64 bytes)
///
private const int FixedPacketLength = 64;
///
/// 构造 HID 协议包
///
public static byte[] BuildText(string text)
{
var bytes = EncodeText(text, 50);
return BuildEdifierPacket(0xe8, [0x00, (byte)bytes.Length, .. bytes]);
}
public static byte[] BuildTextCapacityQuery() => BuildEdifierPacket(0xe7, [0x00]);
public static bool TryParseTextCapacity(ReadOnlySpan report, out HaloPixelTextCapacity capacity)
{
capacity = default;
if (report.Length > 1 && report[0] == 0 && report[1] == 0x2f)
report = report[1..];
if (report.Length < 11 || report[0] is not (0x2f or 0x55) || report[1] is not (0xbb or 0xaa)
|| report[2] != 0xec || report[3] != 0xe7 || report[6] != 0)
return false;
var payloadLength = (report[4] << 8) | report[5];
if (payloadLength < 4 || payloadLength + 7 > report.Length || report[7] == 0)
return false;
var checksum = 0;
for (var index = 1; index < payloadLength + 6; index++)
checksum += report[index];
if ((byte)checksum != report[payloadLength + 6])
return false;
capacity = new(report[7], report[8]);
return true;
}
public static IReadOnlyList BuildTextPackets(string text, HaloPixelTextCapacity capacity)
{
if (capacity.MaxTextLength == 0)
throw new ArgumentOutOfRangeException(nameof(capacity));
var bytes = EncodeText(text, Math.Min(255, capacity.MaxTextLength * 3));
// Each fragment carries the total UTF-8 length; the firmware joins them before decoding.
var packetLimit = capacity.MaxPacketTextBytes == 0
? 50 : Math.Min(55, (int)capacity.MaxPacketTextBytes);
var packets = new List();
for (var offset = 0; offset < bytes.Length || packets.Count == 0; offset += packetLimit)
{
var length = Math.Min(packetLimit, bytes.Length - offset);
packets.Add(BuildEdifierPacket(0xe8, [0x00, (byte)bytes.Length, .. bytes.AsSpan(offset, length)]));
}
return packets;
}
private static byte[] EncodeText(string text, int byteLimit)
{
ArgumentNullException.ThrowIfNull(text);
var bytes = new List();
Span buffer = stackalloc byte[4];
foreach (var rune in text.EnumerateRunes())
{
if (Rune.IsControl(rune))
continue;
if (bytes.Count + rune.Utf8SequenceLength > byteLimit)
break;
var length = rune.EncodeToUtf8(buffer);
for (var index = 0; index < length; index++)
bytes.Add(buffer[index]);
}
return [.. bytes];
}
///
/// 转换布局枚举到对应的 HID 包字节数组
///
public static (byte R, byte G, byte B) CurrentColor { get; set; } = (0xf0, 0xb4, 0xc8);
///
/// 转换布局枚举到对应的 HID 包字节数组
///
///
///
public static byte[] ConvertLayout(HaloPixelTextLayout haloPixelTextLayout)
{
byte[] payload = haloPixelTextLayout switch
{
HaloPixelTextLayout.Left => [0x01, CurrentColor.R, CurrentColor.G, CurrentColor.B, 0x00, 0x02, 0x00, 0x00, 0xff],
HaloPixelTextLayout.Center => [0x01, CurrentColor.R, CurrentColor.G, CurrentColor.B, 0x00, 0x02, 0x00, 0x01, 0xff],
HaloPixelTextLayout.Right => [0x01, CurrentColor.R, CurrentColor.G, CurrentColor.B, 0x00, 0x02, 0x00, 0x02, 0xff],
HaloPixelTextLayout.Stretch => [0x01, CurrentColor.R, CurrentColor.G, CurrentColor.B, 0x00, 0x02, 0x00, 0x03, 0xff],
HaloPixelTextLayout.ScrollLeftToRight => [0x01, CurrentColor.R, CurrentColor.G, CurrentColor.B, 0x00, 0x02, 0x01, 0x00, 0xff],
HaloPixelTextLayout.ScrollRightToLeft => [0x01, CurrentColor.R, CurrentColor.G, CurrentColor.B, 0x00, 0x02, 0x01, 0x01, 0xff],
_ => [],
};
if (payload.Length == 0) return [];
var list = new List { 0x2e, 0xaa, 0xec, 0xef, 0x00, 0x09 };
list.AddRange(payload);
int acc = 124;
foreach (var b in payload)
{
acc += b + 2;
}
list.Add((byte)(acc % 256));
list.Add(0x00);
return Build(list);
}
public static byte[] ConvertUIModel(HaloPixelUIModel haloPixelUIModel)
{
byte[] payload = haloPixelUIModel switch
{
HaloPixelUIModel.Clock => [0x02, CurrentColor.R, CurrentColor.G, CurrentColor.B, 0x00, 0x01, 0x00, 0xff, 0xff],
HaloPixelUIModel.Game => [0x02, CurrentColor.R, CurrentColor.G, CurrentColor.B, 0x00, 0x01, 0x01, 0xff, 0xff],
HaloPixelUIModel.Work => [0x02, CurrentColor.R, CurrentColor.G, CurrentColor.B, 0x00, 0x01, 0x02, 0xff, 0xff],
HaloPixelUIModel.Read => [0x02, CurrentColor.R, CurrentColor.G, CurrentColor.B, 0x00, 0x01, 0x03, 0xff, 0xff],
HaloPixelUIModel.Cats => [0x02, CurrentColor.R, CurrentColor.G, CurrentColor.B, 0x00, 0x01, 0x04, 0xff, 0xff],
HaloPixelUIModel.Dogs => [0x02, CurrentColor.R, CurrentColor.G, CurrentColor.B, 0x00, 0x01, 0x05, 0xff, 0xff],
HaloPixelUIModel.Memes => [0x02, CurrentColor.R, CurrentColor.G, CurrentColor.B, 0x00, 0x01, 0x06, 0xff, 0xff],
HaloPixelUIModel.Cyber => [0x02, CurrentColor.R, CurrentColor.G, CurrentColor.B, 0x00, 0x01, 0x07, 0xff, 0xff],
HaloPixelUIModel.Waves => [0x02, CurrentColor.R, CurrentColor.G, CurrentColor.B, 0x00, 0x01, 0x08, 0xff, 0xff],
_ => [],
};
if (payload.Length == 0) return [];
var list = new List { 0x2e, 0xaa, 0xec, 0xef, 0x00, 0x09 };
list.AddRange(payload);
int acc = 124;
foreach (var b in payload)
{
acc += b + 2;
}
list.Add((byte)(acc % 256));
list.Add(0x00);
return Build(list);
}
///
/// 构造 HID 协议包
///
public static byte[] Build(byte[] bytes) => Build(bytes.ToList());
///
/// 构造 HID 协议包
///
public static byte[] Build(List bytes)
{
// Padding 补 0 到固定长度(64 字节)
while (bytes.Count < FixedPacketLength)
bytes.Add(0x00);
return [.. bytes];
}
///
/// 校验算法
///
public static int Checksum(byte[] textBytes)
{
var acc = 128;
foreach (var ch in textBytes.Select(v => (char)v))
{
acc += ch + 2;
}
return acc % 256;
}
///
/// 把包转成 hex 字符串(小写,不带空格)
///
public static string ToHex(byte[] packet)
{
return BitConverter.ToString(packet).Replace("-", "").ToLower();
}
///
/// 构造像素屏主题颜色包。
/// 协议来自 LiLyric 的 RGBController.set_pixel_color:2E AA EC EF 00 04 03 + RGB + checksum。
///
public static byte[] BuildPixelScreenColor(HaloPixelColor color)
=> BuildEdifierPacket(0xef, [0x03, color.Red, color.Green, color.Blue]);
///
/// 构造氛围灯效果包。
/// 协议来自 LiLyric 的 RGBController._set_light_color:2E AA EC 6B 00 07 13 + effect + RGB + brightness + speed + checksum。
///
public static byte[] BuildAmbientLight(AmbientLightOptions options)
{
var speed = Math.Clamp(options.Speed, (byte)1, (byte)10);
byte[] payload =
[
0x13,
(byte)options.Effect,
options.Color.Red,
options.Color.Green,
options.Color.Blue,
ConvertAmbientBrightness(options.Brightness),
speed
];
return BuildEdifierPacket(0x6b, payload);
}
///
/// 构造氛围灯开关包。
/// 协议来自 TempoHub 的 mood_lighting_splice/set_device_light:B 字段 1=开启、0=关闭。
///
public static byte[] BuildAmbientLightPower(bool enabled)
{
byte mode = enabled ? (byte)0x01 : (byte)0x00;
return BuildEdifierPacket(0x6b, [0x00, 0x00, 0x00, 0x00, mode, 0xff, 0xff]);
}
private static byte ConvertAmbientBrightness(AmbientLightBrightness brightness) => brightness switch
{
AmbientLightBrightness.Low => 0x14,
AmbientLightBrightness.Medium => 0x28,
AmbientLightBrightness.High => 0x3c,
_ => 0x3c
};
///
/// 构造通用 Edifier HID 包:2E AA EC + 指令号 + payload 长度 + payload + 校验。
///
public static byte[] BuildEdifierPacket(byte commandIndex, IReadOnlyCollection payload)
{
var packet = new List(6 + payload.Count + 1)
{
0x2e,
0xaa,
0xec,
commandIndex,
(byte)((payload.Count >> 8) & 0xff),
(byte)(payload.Count & 0xff)
};
packet.AddRange(payload);
packet.Add(CalculateEdifierChecksum(packet));
return Build(packet);
}
private static byte CalculateEdifierChecksum(IReadOnlyList packetWithoutChecksum)
{
var sum = 0;
for (var index = 1; index < packetWithoutChecksum.Count; index++)
sum += packetWithoutChecksum[index];
return (byte)(sum & 0xff);
}
}