using SkiaSharp; using System.Threading.Tasks; public class MatrixRenderer { private readonly WledDdpClient _wled; private readonly int _width = 64; private readonly int _height = 64; private readonly string _fontType = "monospace"; public MatrixRenderer(string wledIp) { _wled = new WledDdpClient(wledIp); } // public async Task DrawAndSendFrameAsync(float currentTemp, int currentWater, DateTime currentTime) //public async Task DrawAndSendFrameAsync(string currentTemp) { using SKBitmap bitmap = new SKBitmap(_width, _height, SKColorType.Rgba8888, SKAlphaType.Premul); using SKCanvas canvas = new SKCanvas(bitmap); canvas.Clear(SKColors.Black); // --- DRAW CLOCK --- using SKPaint clockPaint = new SKPaint { Color = SKColors.Red, IsAntialias = false }; using SKTypeface typeface = SKTypeface.FromFamilyName(_fontType); using SKFont clockFont = new SKFont(typeface, 14); //string timeFormat = (currentTime.Second % 2 == 0) ? "HH:mm" : "HH mm"; string timeFormat = "HH:mm:ss"; string timeString = currentTime.ToString(timeFormat); canvas.DrawText(timeString, 4, 16, SKTextAlign.Left, clockFont, clockPaint); // --- DRAW TEMPERATURE --- using SKPaint textPaintTemp = new SKPaint { Color = SKColors.Green, IsAntialias = false }; using SKTypeface typefaceTemp = SKTypeface.FromFamilyName(_fontType); using SKFont textFont = new SKFont { Size = 14 }; canvas.DrawText($"{currentTemp:F1}ºc", 4, 33, SKTextAlign.Left, textFont, textPaintTemp); //Water using SKPaint textPaintWater = new SKPaint { Color = SKColors.Blue, IsAntialias = false }; using SKTypeface typefaceWater = SKTypeface.FromFamilyName(_fontType); using SKFont textFontWater = new SKFont { Size = 14 }; canvas.DrawText($"{currentWater:F1}%", 4, 50, SKTextAlign.Left, textFontWater, textPaintWater); // 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); } }