private static int RunApplication(IServiceProvider svc) { //TODO put in config const int camNumber = 0; IBcmHost host = svc.GetRequiredService <IBcmHost>(); LedMatrix matrix = svc.GetRequiredService <LedMatrix>(); CameraInfo camInfo; using (var camInfoComp = host.MMAL.ComponentCreateCameraInfo()) { Console.WriteLine("Getting camera info"); camInfo = camInfoComp.GetCameraInfo(camNumber); } Console.WriteLine($"Camera name='{camInfo.Name}' res={camInfo.Width}x{camInfo.Height}"); //create_camera_component using (var camera = host.MMAL.ComponentCreateCamera()) { create_camera_component(camera, camNumber); //camera.Enable(); while (true) { //TODO : capture image matrix.UpdateCanvas(canvas => { //TODO : put image to canvas }); } } app.Error.WriteLine("Camera capture not implemented"); return(-1); }
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); }