Files
WLEDClient/MatrixRenderer.cs
2026-08-08 01:05:40 +01:00

63 lines
2.1 KiB
C#

using SkiaSharp;
using System.Threading.Tasks;
public class MatrixRenderer
{
private readonly WledDdpClient _wled;
private readonly int _width = 64;
private readonly int _height = 64;
public MatrixRenderer(string wledIp)
{
_wled = new WledDdpClient(wledIp);
}
//
public async Task DrawAndSendFrameAsync(float currentTemp, int currentWater)
//public async Task DrawAndSendFrameAsync(string currentTemp)
{
// 1. Create a 64x64 canvas
using SKBitmap bitmap = new SKBitmap(_width, _height, SKColorType.Rgba8888, SKAlphaType.Premul);
using SKCanvas canvas = new SKCanvas(bitmap);
// 2. Clear background to black
canvas.Clear(SKColors.Black);
// 3. Draw something (e.g., Temperature Text)
// Paint now only handles the color and rendering style
using SKPaint textPaint = new SKPaint
{
Color = SKColors.Orange,
IsAntialias = false // Keep crisp pixels for LED matrices
};
// Font handles the typography
using SKFont textFont = new SKFont
{
Size = 16
};
// DrawText now takes the string, X, Y, the font, and the paint
//canvas.DrawText($"{currentTemp:F1}c", 4, 32, textFont, textPaint);
// DrawText now takes the string, X, Y, alignment, the font, and the paint
canvas.DrawText($"{currentTemp:F1}c", 4, 12, SKTextAlign.Left, textFont, textPaint);
canvas.DrawText($"{currentWater:F1}%", 4, 32, SKTextAlign.Left, textFont, textPaint);
// 4. Convert 32-bit RGBA to 24-bit RGB for WLED
byte[] rgbaBytes = bitmap.Bytes;
byte[] rgbBytes = new byte[_width * _height * 3];
int rgbIndex = 0;
for (int i = 0; i < rgbaBytes.Length; i += 4)
{
rgbBytes[rgbIndex++] = rgbaBytes[i]; // R
rgbBytes[rgbIndex++] = rgbaBytes[i + 1]; // G
rgbBytes[rgbIndex++] = rgbaBytes[i + 2]; // B
// We ignore rgbaBytes[i + 3] (Alpha channel)
}
// 5. Fire it over UDP!
await _wled.SendFrameAsync(rgbBytes);
}
}