60 lines
1.9 KiB
C#
60 lines
1.9 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)
|
|
{
|
|
// 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, 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);
|
|
}
|
|
} |