private int OnExecute() { var options = new LedMatrixOptions() { Brightness = (byte)Brightness, }; // Check if we have only extreme colors if (Brightness == 100 && FullSaturation(Color) && FullSaturation(BackgroundColor) ) { options.PwmBits = 1; } var matrix = new LedMatrix(options); var canvas = matrix.CreateOffscreenCanvas(); var font = new BdfFont(Font); while (!Console.KeyAvailable) { var text = DateTime.UtcNow.ToString(Format); canvas.Fill(BackgroundColor); canvas.DrawText(font, XOrigin, YOrigin + font.Baseline, Color, text, Spacing); matrix.SwapOnVsync(canvas); Thread.Sleep(TimeSpan.FromMilliseconds(250)); } return(0); }
public NativeLedMatrixOptions(LedMatrixOptions options) { chain_length = options.ChainLength; rows = options.Rows; cols = options.Columns; hardware_mapping = options.HardwareMapping != null?Marshal.StringToHGlobalAnsi(options.HardwareMapping) : IntPtr.Zero; inverse_colors = (byte)(options.InverseColors ? 1 : 0); led_rgb_sequence = options.LedRgbSequence != null?Marshal.StringToHGlobalAnsi(options.LedRgbSequence) : IntPtr.Zero; pixel_mapper_config = options.PixelMapperConfig != null?Marshal.StringToHGlobalAnsi(options.PixelMapperConfig) : IntPtr.Zero; panel_type = options.PanelType != null?Marshal.StringToHGlobalAnsi(options.PanelType) : IntPtr.Zero; parallel = options.Parallel; multiplexing = (int)options.Multiplexing; pwm_bits = options.PwmBits; pwm_lsb_nanoseconds = options.PwmLsbNanoseconds; pwm_dither_bits = options.PwmDitherBits; scan_mode = (int)options.ScanMode; show_refresh_rate = (byte)(options.ShowRefreshRate ? 1 : 0); limit_refresh_rate_hz = options.LimitRefreshRateHz; brightness = options.Brightness; disable_hardware_pulsing = (byte)(options.DisableHardwarePulsing ? 1 : 0); row_address_type = (int)options.RowAddressType; }
public static void AddLedMatrix(this IServiceCollection services) { services.AddSingleton <LedMatrixOptions>(svc => { svc.GetRequiredService <ILogger <Program> >().LogDebug("Initializing LedMatrixOptions"); return(new LedMatrixOptions { chain_length = 2, rows = 64, led_rgb_sequence = "RBG", }); }); services.AddSingleton <LedMatrix>(svc => { LedMatrixOptions options = svc.GetRequiredService <LedMatrixOptions>(); svc.GetRequiredService <ILogger <Program> >().LogDebug("Initializing LedMatrix"); try { return(new LedMatrix(options)); } catch (Exception ex) { svc.GetRequiredService <ILogger <Program> >().LogWarning(ex, "Error initializing LedMatrix"); } svc.GetRequiredService <ILogger <Program> >().LogDebug("Initializing LedMatrix, without options"); return(new LedMatrix(rows: options.rows, chained: options.chain_length, parallel: options.parallel)); }); }
public LedMatrixManager(ILogger <LedMatrixManager> logger) { this.m_logger = logger; this.m_sem = new SemaphoreSlim(1); this.m_options = new LedMatrixOptions() { brightness = 100, chain_length = 2, rows = 64, parallel = 1, hardware_mapping = "RBG", }; }
public static void Dump(this LedMatrixOptions options, StreamWriter output) { output.WriteLine("--- {0} ---", options.GetType().Name); foreach (MemberInfo mi in options.GetType().GetMembers()) { if (mi.MemberType == MemberTypes.Field) { output.WriteLine("{0}={1}", mi.Name, ((FieldInfo)mi).GetValue(options)); continue; } if (mi.MemberType == MemberTypes.Property && ((PropertyInfo)mi).CanRead) { output.WriteLine("{0}={1}", mi.Name, ((PropertyInfo)mi).GetValue(options)); continue; } } }
private static int RunApp() { app.Out.WriteLine("Hello World!"); int brightnessValue = 100; if (brightness.HasValue()) { if (int.TryParse(brightness.Value(), out brightnessValue)) { if (brightnessValue < 1 || brightnessValue > 100) { app.Error.WriteLine("Brightness value outside allowed values"); return(-1); } } else { app.Error.WriteLine("Invalid brightness value"); return(-1); } } //long uid = UnixUserInfo.GetRealUserId(); //if (uid != 0) //{ // app.Out.WriteLine("You must run this app as root."); // return -1; //} //ServiceCollection serviceConfig = new ServiceCollection(); //serviceConfig.AddLogging(); //serviceConfig.AddOptions(); //serviceConfig.AddBcm(); //serviceConfig.AddOpenGl(); //serviceConfig.AddLedMatrix(); //using (ServiceProvider services = serviceConfig.BuildServiceProvider()) //{ // services.GetRequiredService<ILoggerFactory>().AddConsole(LogLevel.Information); // return RunApp(services); //} using (LedMatrixOptions options = new LedMatrixOptions() { chain_length = 2, parallel = 1, rows = 64, led_rgb_sequence = "RBG", pwm_bits = 8, show_refresh_rate = 1, brightness = brightnessValue, }) using (LedMatrix matrix = new LedMatrix(options.rows, options.chain_length, options.parallel)) using (BcmHost host = new BcmHost()) using (Resource target = host.Dispman.CreateResource(VC_IMAGE_TYPE_T.VC_IMAGE_RGB888, matrix.CanvasWidth, matrix.CanvasHeight)) using (Display display = host.Dispman.DisplayOpenOffscreen(target, DISPMANX_TRANSFORM_T.DISPMANX_NO_ROTATE)) //using (Display display = host.Dispman.DisplayOpen(Screen.MAIN_LCD)) using (ScopedElement element = ScopedElement.Create(host, display, null, destRect: display.Rectangle, srcRect: Rescale(display.Rectangle))) using (DispmanWindow window = new DispmanWindow(element, display.Width, display.Height)) using (EglDisplay eglDisp = new EglDisplay()) using (EglContext ctx = new EglContext(eglDisp)) using (EglSurface surface = new EglSurface(eglDisp, window)) using (Data data = new Data()) { Rectangle rect = display.Rectangle; app.Out.WriteLine("Ready to go, screen is {0}x{1}", display.Width, display.Height); Stopwatch swatch = Stopwatch.StartNew(); app.Out.WriteLine("Initializing 3D context"); ctx.MakeCurrent(surface); app.Out.WriteLine("init_ogl"); init_ogl(); app.Out.WriteLine("init_model_proj"); init_model_proj(rect, data); app.Out.WriteLine("init_textures"); init_textures(data); app.Out.WriteLine("Initializing buffer"); //int bufferLength = rect.Width * rect.Height * 3; //byte[] buffer = new byte[bufferLength]; app.Out.WriteLine("Init complete, starting loop"); //Render loop bool run = true; Console.CancelKeyPress += (s, e) => { if (run) { run = false; e.Cancel = true; } }; Console.TreatControlCAsInput = true; int pitch = Utils.ALIGN_UP(3 * display.Width, 32); byte[] image = new byte[pitch * display.Height]; app.Out.WriteLine("Buffer size: {0}", image.Length); while (run) { if (Console.KeyAvailable) { var key = Console.ReadKey(true); if (key.Key == ConsoleKey.C && key.Modifiers == ConsoleModifiers.Control) { run = false; Console.TreatControlCAsInput = false; continue; } if (key.Key == ConsoleKey.Add || key.Key == ConsoleKey.Subtract) { int newB; if (key.Key == ConsoleKey.Add) { newB = Math.Min(options.brightness + 1, 100); } else { newB = Math.Max(options.brightness - 1, 0); } if (options.brightness != newB) { options.brightness = newB; app.Out.WriteLine("New brightness: {0}%", newB); if (!matrix.UpdateOptions(options)) { app.Out.WriteLine("Update option failed"); } } } } update_model(); redraw_scene(surface); //extract data to buffer //target.WriteData(1, buffer, rect); target.ReadData(rect, image, pitch); //Display image to matrix matrix.UpdateCanvas(canvas => { for (int x = 0; x < canvas.Width; x++) { for (int y = 0; y < canvas.Height; y++) { int baseAddr = (x * canvas.Width + y) * 3; try { byte r = image[baseAddr + 0]; byte g = image[baseAddr + 1]; byte b = image[baseAddr + 2]; canvas.SetPixel(x, y, r, g, b); } catch (IndexOutOfRangeException ex) { app.Out.WriteLine("X={0} Y={1} baseAddre={2} pitch={3}", x, y, baseAddr, pitch); System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(ex).Throw(); } } } }); app.Out.Write("."); } return(0); } }
private static int RunApp() { app.Out.WriteLine("Hello World!"); if (String.IsNullOrEmpty(image.Value)) { app.ShowHelp(); return(-1); } if (!File.Exists(image.Value)) { app.Error.WriteLine("File not found"); return(-1); } int brightnessValue = 0; if (brightness.HasValue()) { if (int.TryParse(brightness.Value(), out brightnessValue)) { if (brightnessValue < 1 || brightnessValue > 100) { app.Error.WriteLine("Brightness value outside allowed values"); return(-1); } } else { app.Error.WriteLine("Invalid brightness value"); return(-1); } } Stopwatch swatch = Stopwatch.StartNew(); using (LedMatrixOptions options = new LedMatrixOptions() { chain_length = 2, rows = 64, led_rgb_sequence = "RBG", brightness = brightnessValue, }) using (Image <Rgba32> gif = Image.Load(image.Value)) //using (LedMatrix matrix = new LedMatrix(rows: 64, chained: 2, parallel: 1)) using (LedMatrix matrix = new LedMatrix(options)) { Size size = new Size(matrix.CanvasWidth, matrix.CanvasHeight); int framesCount = gif.Frames.Count; Console.WriteLine("Canvas size: {0}x{1}", size.Width, size.Height); gif.Mutate(ops => { ops.Resize(size); }); //string path = Path.Combine(Environment.CurrentDirectory, "block64.gif"); //Console.WriteLine("Saving path={0}", path); //using (var output = File.Create(path)) //{ // gif.SaveAsGif(output); //} bool run = true; Console.CancelKeyPress += (s, e) => { if (run) { run = false; e.Cancel = true; } }; while (run) { for (int i = 0; i < framesCount; i++) { long cur = swatch.ElapsedMilliseconds; var frame = gif.Frames[i]; matrix.UpdateCanvas(canvas => { for (int x = 0; x < size.Width; x++) { for (int y = 0; y < size.Height; y++) { Rgba32 pix = frame[x, y]; canvas.SetPixel(x, y, pix.R, pix.G, pix.B); } } Console.Write("."); }); //matrix.UpdateCanvasAsImageSharp(canvasImage => //{ // canvasImage.DrawImage(frames[i].Image, 1, size, Point.Empty); //}); long elapsed = swatch.ElapsedMilliseconds - cur; long wait = frame.MetaData.FrameDelay * 10 - elapsed; if (wait > 0) { Thread.Sleep((int)wait); } } } } return(0); }