public FoundationPlate(PiTop4Board module) : base(module)
        {
            _digitalConnectedDeviceFactory = new ConnectedDeviceFactory <DigitalPort, DigitalPortDeviceBase>(deviceType =>
            {
                var ctorSignature = new[] { typeof(DigitalPort), typeof(IGpioControllerFactory) };
                var ctor          = deviceType.GetConstructor(ctorSignature);
                if (ctor != null)
                {
                    return(devicePort => (DigitalPortDeviceBase)Activator.CreateInstance(deviceType, devicePort, module) !);
                }

                throw new InvalidOperationException(
                    $"Cannot find suitable constructor for type {deviceType}, looking for signature {ctorSignature}");
            });

            _analogueConnectedDeviceFactory = new ConnectedDeviceFactory <AnaloguePort, AnaloguePortDeviceBase>(
                deviceType =>
            {
                var ctorSignature = new[] { typeof(AnaloguePort), typeof(int), typeof(II2CDeviceFactory) };
                var ctor          = deviceType.GetConstructor(ctorSignature);
                if (ctor != null)
                {
                    return(devicePort =>
                           (AnaloguePortDeviceBase)Activator.CreateInstance(deviceType, devicePort, DefaultI2CAddress, module) !);
                }

                throw new InvalidOperationException(
                    $"Cannot find suitable constructor for type {deviceType}, looking for signature {ctorSignature}");
            });

            RegisterForDisposal(_digitalConnectedDeviceFactory);
            RegisterForDisposal(_analogueConnectedDeviceFactory);
        }
        public static void DisposeDevice(this PiTop4Board module, FileSystemCamera device)
        {
            var factory = module.GetDeviceFactory <FileSystemCameraSettings, FileSystemCamera>();

            AssertFactory(factory);
            factory.DisposeDevice(device);
        }
        public static void DisposeDevice <T>(this PiTop4Board module, T device)
            where T : ICamera
        {
            var factory = module.GetDeviceFactory <int, ICamera>();

            AssertFactory(factory);
            factory.DisposeDevice(device);
        }
Exemplo n.º 4
0
        public Wizard(PiTop4Board board, IWizardStep <T>[] steps)
        {
            _steps   = steps;
            _display = board.Display;

            board.UpButton.PressedChanged += (sender, pressed) => {
                if (pressed)
                {
                    steps[_currentStep].Up();
                }
            };

            board.DownButton.PressedChanged += (sender, pressed) => {
                if (pressed)
                {
                    steps[_currentStep].Down();
                }
            };

            board.SelectButton.PressedChanged += (sender, pressed) => {
                steps[_currentStep].Confirm(_data);
                var nextStep = _currentStep + 1;
                if (nextStep < steps.Length)
                {
                    _currentStep = nextStep;
                    steps[_currentStep].Initialize(_display, _font);
                }
                else
                {
                    _display.Draw((context, cr) => {
                        context.Clear(Color.Black);
                        var rect = TextMeasurer.Measure("Done!", new RendererOptions(_font));
                        var x    = 1;
                        var y    = 1;
                        context.DrawText("Done!", _font, Color.Aqua, new PointF(x, y));
                    });
                    CurrentState = WizardState.Completed;
                }
            };

            board.CancelButton.PressedChanged += (sender, pressed) => {
                _display.Draw((context, cr) => {
                    context.Clear(Color.Black);
                    var rect = TextMeasurer.Measure("Cancelled.", new RendererOptions(_font));
                    var x    = 1;
                    var y    = 1;
                    context.DrawText("Cancelled.", _font, Color.Aqua, new PointF(x, y));
                });
                CurrentState = WizardState.Cancelled;
            };
        }
        public void cannot_use_factory_if_is_not_initialised()
        {
            PiTop4Board.Configure(new DummyGpioController());
            using var module = PiTop4Board.Instance;
            var action = new Action(() =>
                                    module.GetOrCreateCamera <FileSystemCamera>(new DirectoryInfo(Path.GetTempPath()))
                                    );

            action.Should().Throw <InvalidOperationException>()
            .Which
            .Message
            .Should()
            .Match("Cannot find a factory if type PiTop.Abstractions.IConnectedDeviceFactory<PiTop.Camera.FileSystemCameraSettings, PiTop.Camera.FileSystemCamera>, make sure to configure the module calling UseCamera first.");
        }
        public static T GetOrCreateCamera <T>(this PiTop4Board module, FileSystemCameraSettings settings) where T : FileSystemCamera
        {
            IConnectedDeviceFactory <FileSystemCameraSettings, FileSystemCamera> factory = null !;

            try
            {
                factory = module.GetDeviceFactory <FileSystemCameraSettings, FileSystemCamera>();
            }
            catch (KeyNotFoundException

                   )
            {
            }

            AssertFactory(factory);
            return(factory.GetOrCreateDevice <T>(settings));
        }
Exemplo n.º 7
0
        private static void SetupBoard()
        {
            Console.WriteLine("SetupBoard");
            board = PiTop4Board.Instance;

            display = board.Display;

            board.SelectButton.Pressed += BoardSelectButtonPressed;
            board.CancelButton.Pressed += BoardCancelButtonPressed;
            board.UpButton.Pressed     += BoardUpButtonPressed;
            board.DownButton.Pressed   += BoardDownButtonPressed;
            board.BatteryStateChanged  += BoardOnBatteryStateChanged;

#if RUNTIMEMODE
            MainPage();
#endif
        }
Exemplo n.º 8
0
        public ExpansionPlate(PiTop4Board module) : base(module)
        {
            _foundationPlate = module.GetOrCreatePlate <FoundationPlate>();

            _servoMotorsFactory = new ConnectedDeviceFactory <ServoMotorPort, ServoMotor>(deviceType =>
            {
                var ctorSignature = new[] { typeof(ServoMotorPort), typeof(SMBusDevice) };
                var ctor          = deviceType.GetConstructor(ctorSignature);
                if (ctor != null)
                {
                    return(devicePort =>
                           (ServoMotor)Activator.CreateInstance(deviceType, devicePort, GetOrCreateMcu()) !);
                }
                throw new InvalidOperationException(
                    $"Cannot find suitable constructor for type {deviceType}, looking for signature {ctorSignature}");
            });

            _encoderMotorFactory = new ConnectedDeviceFactory <EncoderMotorPort, EncoderMotor>(
                deviceType =>
            {
                var ctorSignature = new[] { typeof(EncoderMotorPort), typeof(SMBusDevice) };
                var ctor          = deviceType.GetConstructor(ctorSignature);
                if (ctor != null)
                {
                    return(devicePort =>
                           (EncoderMotor)Activator.CreateInstance(deviceType, devicePort, GetOrCreateMcu()) !);
                }
                throw new InvalidOperationException(
                    $"Cannot find suitable constructor for type {deviceType}, looking for signature {ctorSignature}");
            });

            // set up heartbeat
            RegisterForDisposal(Observable.Interval(HEARTBEAT_SEND_INTERVAL, TaskPoolScheduler.Default).Subscribe(_ =>
            {
                GetOrCreateMcu().WriteByte(REGISTER_HEARTBEAT, HEARTBEAT_SECONDS_BEFORE_SHUTDOWN);
            }));

            RegisterForDisposal(_servoMotorsFactory);
            RegisterForDisposal(_encoderMotorFactory);
            RegisterForDisposal(() =>
            {
                _imu?.Dispose();
                _mcu?.I2c.Dispose();
            });
        }
        public static T GetOrCreateCamera <T>(this PiTop4Board module, int index)
            where T : ICamera
        {
            IConnectedDeviceFactory <int, ICamera> factory = null !;

            try
            {
                factory = module.GetDeviceFactory <int, ICamera>();
            }
            catch (KeyNotFoundException

                   )
            {
            }

            AssertFactory(factory);
            return(factory.GetOrCreateDevice <T>(index));
        }
        public static PiTop4Board UseCamera(this PiTop4Board module)
        {
            module.AddDeviceFactory <int, ICamera>(deviceType =>
            {
                var ctorSignature = new[] { typeof(int) };
                var ctor          = deviceType.GetConstructor(ctorSignature);

                if (ctor != null)
                {
                    return(cameraIndex =>
                           (ICamera)Activator.CreateInstance(deviceType, cameraIndex) !);
                }

                throw new InvalidOperationException(
                    $"Cannot find suitable constructor for type {deviceType}, looking for signature {ctorSignature}");
            });

            module.AddDeviceFactory <FileSystemCameraSettings, FileSystemCamera>(deviceType =>
            {
                return(settings => new FileSystemCamera(settings));
            });

            return(module);
        }
Exemplo n.º 11
0
 protected SMBusDevice GetOrCreateMcu()
 {
     return(_mcu ??= new SMBusDevice(PiTop4Board.GetOrCreateI2CDevice(I2C_ADDRESS_PLATE_MCU)));
 }
Exemplo n.º 12
0
 public static FoundationPlate GetOrCreateFoundationPlate(this PiTop4Board module)
 {
     return(module.GetOrCreatePlate <FoundationPlate>());
 }
Exemplo n.º 13
0
 public FoundationPlateTests()
 {
     PiTop4Board.Configure(new DummyGpioController());
     _module = PiTop4Board.Instance;
 }
Exemplo n.º 14
0
 public FileSystemCameraTests()
 {
     PiTop4Board.Configure(new DummyGpioController());
     _module = PiTop4Board.Instance;
 }
Exemplo n.º 15
0
 public MmkPlateTests()
 {
     PiTop4Board.Configure(new DummyGpioController(), i2cDeviceFactory: settings => new DummyI2CDevice(settings));
     _module = PiTop4Board.Instance;
 }
Exemplo n.º 16
0
 public static ExpansionPlate GetOrCreateExpansionPlate(this PiTop4Board module)
 {
     return(module.GetOrCreatePlate <ExpansionPlate>());
 }
 public static T GetOrCreateCamera <T>(this PiTop4Board module, DirectoryInfo directory, string imageFileSearchPattern = "*.png") where T : FileSystemCamera
 {
     return(module.GetOrCreateCamera <T>(
                new FileSystemCameraSettings(directory, imageFileSearchPattern)));
 }