예제 #1
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         _mmalCamera.Cleanup();
     }
 }
예제 #2
0
        public static void TakePictureManualMode()
        {
            MMALCamera cam = MMALCamera.Instance;

            AsyncContext.Run(async() =>
            {
                using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/", "jpg"))
                    using (var imgEncoder = new MMALImageEncoder(imgCaptureHandler))
                        using (var nullSink = new MMALNullSinkComponent())
                        {
                            cam.ConfigureCameraSettings();

                            // Create our component pipeline.
                            imgEncoder.ConfigureOutputPort(0, MMALEncoding.JPEG, MMALEncoding.I420, 90);

                            cam.Camera.StillPort.ConnectTo(imgEncoder);
                            cam.Camera.PreviewPort.ConnectTo(nullSink);

                            // Camera warm up time
                            await Task.Delay(2000);
                            await cam.ProcessAsync(cam.Camera.StillPort);
                        }
            });

            cam.Cleanup();
        }
예제 #3
0
        public static void TakeVideoManualMode()
        {
            MMALCamera cam = MMALCamera.Instance;

            AsyncContext.Run(async() =>
            {
                //using (var vidCaptureHandler = new VideoStreamCaptureHandler("/home/pi/videos/", "mjpeg"))
                using (var vidCaptureHandler = new VideoStreamCaptureHandler("/home/pi/videos/", "avi"))
                    using (var vidEncoder = new MMALVideoEncoder(vidCaptureHandler))
                        //using (var ffCaptureHandler = FFmpegCaptureHandler.RawVideoToAvi("/home/pi/videos/", "testing1234"))
                        //using (var vidEncoder = new MMALVideoEncoder(ffCaptureHandler))
                        using (var renderer = new MMALVideoRenderer())
                        {
                            cam.ConfigureCameraSettings();

                            // Create our component pipeline. Here we are using the H.264 standard with a YUV420 pixel format. The video will be taken at 25Mb/s.
                            vidEncoder.ConfigureOutputPort(0, MMALEncoding.MJPEG, MMALEncoding.I420, 90, 25000000);

                            cam.Camera.VideoPort.ConnectTo(vidEncoder);
                            cam.Camera.PreviewPort.ConnectTo(renderer);

                            // Camera warm up time
                            await Task.Delay(2000);

                            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));

                            // Take video for 3 minutes.
                            await cam.ProcessAsync(cam.Camera.VideoPort, cts.Token);
                        }
            });

            cam.Cleanup();
        }
예제 #4
0
        private void Dispose(bool isDisposing)
        {
            if (isDisposing)
            {
                _cameraDevice.Cleanup();
                _cameraDevice = null;
            }

            GC.SuppressFinalize(this);
        }
예제 #5
0
        static void Main(string[] args)
        {
            MMALCamera cam = MMALCamera.Instance;

            // Create observable that will generate an incrementing number every second
            var observable = Observable.Generate(1, x => true, x => x + 1, x => x, x => TimeSpan.FromSeconds(1));

            var relay  = OutputPort.Create(17, OutputPort.InitialValue.Low).Result;
            var light1 = OutputPort.Create(27, OutputPort.InitialValue.Low).Result;
            var light2 = OutputPort.Create(22, OutputPort.InitialValue.Low).Result;
            var button = InputPort.Create(24, GpioEdge.Both).Result;

            // Write true whenever the number is even and odd when the number is odd
            using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/", "jpg"))
                using (observable.Select(x => x % 2 == 0).Subscribe(relay))
                    using (observable.Select(x => x % 2 == 0).Subscribe(light1))
                        //using (observable.Select(x => x % 2 != 0).Subscribe(light2))
                        //using (button.Do(pressed => Console.WriteLine(pressed)).Subscribe())
                        using (button.Subscribe(light2))
                            using (var i2cBus = new I2CBusPI("/dev/i2c-1"))
                            {
                                var takePictureTask = cam.TakePicture(imgCaptureHandler, MMALEncoding.JPEG, MMALEncoding.I420);

                                var i2cDevice = new I2CDevicePI(i2cBus, Display.DefaultI2CAddress);

                                var sensor = new BME280Sensor(i2cBus, 1014);

                                var display = new SSD1306.Display(i2cDevice, 128, 64);
                                display.Init();

                                var dfont = new AdafruitSinglePageFont();

                                for (int i = 0; i < 100; i++)
                                {
                                    display.WriteLineBuff(dfont, $"Temperature: {sensor.ReadTemperature().Result} °C", $"Pressure: {sensor.ReadPressure().Result} Pa", $"Humidity: {sensor.ReadHumidity().Result} %", $"Altitude: {sensor.ReadAltitude().Result} m", "Line 5", "Line 6", "Line 7", "Line 8");
                                    display.DisplayUpdate();
                                }

                                //for (int i = 0; i < 100; i++)
                                //    display.DrawPixel(i, i);

                                takePictureTask.Wait();
                                display.ClearDisplay();
                            }
            // releasing relay
            relay.Write(true);
            // turning of light
            light1.Write(false);
            light2.Write(false);
            // Cleanup disposes all unmanaged resources and unloads Broadcom library. To be called when no more processing is to be done
            // on the camera.
            cam.Cleanup();
        }
예제 #6
0
        /// <summary>
        /// <inheritdoc />
        /// </summary>
        public virtual void Dispose()
        {
            Console.WriteLine("Shutting down pipeline.");
            VideoCaptureHandler?.Dispose();
            SnapshotCaptureHandler?.Dispose();
            MotionCaptureHandler?.Dispose();
            VideoEncoder?.Dispose();
            SnapshotEncoder?.Dispose();
            Resizer?.Dispose();
            Splitter?.Dispose();

            Console.WriteLine("Shutting down camera.");
            Cam?.Cleanup();
        }
예제 #7
0
        public static async Task TakeVideoAsync()
        {
            MMALCameraConfig.VideoResolution = new Resolution(640, 480);
            MMALCameraConfig.Flips           = MMAL_PARAM_MIRROR_T.MMAL_PARAM_MIRROR_VERTICAL;
            MMALCameraConfig.InlineHeaders   = true;
            MMALCameraConfig.VideoProfile    = MMAL_VIDEO_PROFILE_T.MMAL_VIDEO_PROFILE_H264_HIGH;
            // Singleton initialized lazily. Reference once in your application.
            MMALCamera cam = MMALCamera.Instance;

            using (var vidCaptureHandler = new VideoStreamCaptureHandler("/home/pi/videos/", "avi"))
            {
                var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));

                await cam.TakeVideo(vidCaptureHandler, cts.Token);
            }

            // Cleanup disposes all unmanaged resources and unloads Broadcom library. To be called when no more processing is to be done
            // on the camera.
            cam.Cleanup();
        }
예제 #8
0
        public static async Task TakePictureAsync()
        {
            MMALCameraConfig.Annotate = new AnnotateImage
            {
                ShowDateText = true,
                ShowTimeText = true
            };
            MMALCameraConfig.StatsPass = true;
            // Singleton initialized lazily. Reference once in your application.
            MMALCamera cam = MMALCamera.Instance;

            using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/", "jpg"))
            {
                await cam.TakePicture(imgCaptureHandler, MMALEncoding.JPEG, MMALEncoding.I420);
            }

            // Cleanup disposes all unmanaged resources and unloads Broadcom library. To be called when no more processing is to be done
            // on the camera.
            cam.Cleanup();
        }
예제 #9
0
        static async Task Main(string[] args)
        {
            System.Console.WriteLine("Starting");
            const int lightCount = 12;
            var       settings   = new SpiConnectionSettings(0, 0)
            {
                ClockFrequency = 2_400_000,
                Mode           = SpiMode.Mode0,
                DataBitLength  = 8
            };

            // Create a Neo Pixel x8 stick on spi 0.0
            var spi = SpiDevice.Create(settings);

            var neo = new Ws2812b(spi, lightCount);

            var img = neo.Image;

            img.Clear();
            for (var x = 0; x < lightCount; x++)
            {
                img.SetPixel(x, 0, Color.White);
            }
            neo.Update();

            MMALCamera cam = MMALCamera.Instance;

            for (var y = 0; y < 50; y++)
            {
                using (var imgCaptureHandler = new ImageStreamCaptureHandler($"_testing{y}.jpg")){
                    await cam.TakePicture(imgCaptureHandler, MMALEncoding.JPEG, MMALEncoding.I420);
                }
                Step();
            }

            img.Clear();
            neo.Update();
            cam.Cleanup();
        }
예제 #10
0
        static void Main(string[] args)
        {
            //load app properties
            ReadConfigFile();
            idScope = GetProperty("idScope");
            enrollmentGroupPrimaryKey   = GetProperty("enrollmentGroupPrimaryKey");
            enrollmentGroupSecondaryKey = GetProperty("enrollmentGroupSecondaryKey");
            registrationId         = GetProperty("registrationId");
            deviceStringConnection = GetProperty("deviceStringConnection");
            pathToSavePicture      = GetProperty("pathToSavePicture");

            bool dpsInfoOk = !string.IsNullOrWhiteSpace(idScope) && !string.IsNullOrWhiteSpace(enrollmentGroupPrimaryKey) &&
                             !string.IsNullOrWhiteSpace(enrollmentGroupSecondaryKey) && !string.IsNullOrWhiteSpace(registrationId);
            bool directConnection = !string.IsNullOrWhiteSpace(deviceStringConnection);

            if (!(dpsInfoOk || directConnection))
            {
                Console.WriteLine("ID Scope, Keys and Registration ID must be provided if using DPS, otherwise fill the Device Connection String");
                Console.ReadLine();
            }
            else
            {
                //connect device directly if device string connection is known
                if (!string.IsNullOrWhiteSpace(deviceStringConnection))
                {
                    //this install a root certificate in the OS - applicable if you using this device as a downstream to connect through a Edge Gateway.
                    if (deviceStringConnection.Contains("GatewayHostName"))
                    {
                        InstallCACert();
                    }

                    deviceClient = DeviceClient.CreateFromConnectionString(deviceStringConnection, TransportType.Mqtt_Tcp_Only);
                }
                else
                {
                    //Provision through DPS and return DeviceClient object
                    ProvisioningDeviceClientWrapper provisionWrapper = new ProvisioningDeviceClientWrapper(
                        idScope, registrationId, enrollmentGroupPrimaryKey, enrollmentGroupSecondaryKey);

                    deviceClient = provisionWrapper.RunAsync().GetAwaiter().GetResult();
                }

                // Create a handler for the direct method call
                deviceClient.SetMethodHandlerAsync("TakePicture", TakePicture, null).Wait();

                //Initiate camera (singleton pattern) - must be executed just once - Attention - while this app is running the camera can´t be used by another program
                cam = MMALCamera.Instance;

                //open device connection
                deviceClient.OpenAsync().ConfigureAwait(false);

                //execute this method to program continue running
                Console.ReadLine();

                //close device connection
                deviceClient.CloseAsync().ConfigureAwait(false);

                // Only call when you no longer require the camera, i.e. on app shutdown.
                cam.Cleanup();
            }
        }