XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

HaloPixelToolBox

【WinUI3】基于USB HID通讯的花再音响工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using HaloPixelToolBox.Core.Models.Notifications;
using System.Text;

namespace HaloPixelToolBox.Core.Utilities;

/// <summary>
/// 来自 TempoHub 的 SendTextToDeviceHelper / device_text_query_parse。
/// source_id=1 为聊天消息提示,source_id=0 为普通文本 / 歌词。
/// </summary>
public static class HaloPixelMessageProtocol
{
    public const byte MessageSourceId = 1;
    private const int MaxPacketTextBytes = 55; // 64 - header(6) - source/length(2) - checksum(1)

    public static byte[] BuildCapacityQuery() => HidPacketBuilder.BuildEdifierPacket(0xe7, [MessageSourceId]);

    public static bool TryParseCapacity(ReadOnlySpan<byte> report, out MessageNotificationCapacity capacity)
    {
        capacity = default;
        if (report.Length > 1 && report[0] == 0 && report[1] == 0x2f)
            report = report[1..];
        // 实机设备回复使用 BB;AA 为主机指令头(部分固件也会回显 AA)。
        if (report.Length < 11 || report[0] is not (0x2f or 0x55) || report[1] is not (0xbb or 0xaa)
            || report[2] != 0xec || report[3] != 0xe7)
            return false;

        var payloadLength = (report[4] << 8) | report[5];
        if (payloadLength < 4 || payloadLength + 7 > report.Length || report[6] != MessageSourceId)
            return false;
        var checksum = 0;
        for (var index = 1; index < payloadLength + 6; index++)
            checksum += report[index];
        if ((byte)checksum != report[payloadLength + 6] || report[7] == 0)
            return false;

        capacity = new(report[7], report[8]);
        return true;
    }

    public static IReadOnlyList<byte[]> BuildNotification(string text, MessageNotificationCapacity capacity)
    {
        ArgumentException.ThrowIfNullOrWhiteSpace(text);
        if (capacity.MaxTextLength == 0)
            throw new ArgumentOutOfRangeException(nameof(capacity));

        var byteLimit = Math.Min(255, capacity.MaxTextLength * 3);
        var textBuffer = new List<byte>(byteLimit);
        var characters = 0;
        foreach (var rune in text.EnumerateRunes())
        {
            if (Rune.IsControl(rune))
                continue;
            if (characters >= capacity.MaxTextLength || textBuffer.Count + rune.Utf8SequenceLength > byteLimit)
                break;
            textBuffer.AddRange(Encoding.UTF8.GetBytes(rune.ToString()));
            characters++;
        }
        if (textBuffer.Count == 0)
            throw new ArgumentException("消息提示不能为空。", nameof(text));

        // 0 表示不分包;实际 HID 报告仍限制为 64 字节。
        var packetLimit = capacity.MaxPacketTextBytes == 0
            ? MaxPacketTextBytes : Math.Min(MaxPacketTextBytes, (int)capacity.MaxPacketTextBytes);
        var result = new List<byte[]>();
        for (var offset = 0; offset < textBuffer.Count; offset += packetLimit)
        {
            var payload = new List<byte> { MessageSourceId, (byte)textBuffer.Count };
            payload.AddRange(textBuffer.Skip(offset).Take(packetLimit));
            result.Add(HidPacketBuilder.BuildEdifierPacket(0xe8, payload));
        }
        return result;
    }
}