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 void using_again_the_same_connection_settings_return_the_same_device_instance() { var factory = new ConnectedDeviceFactory <int, SimpleDevice>(type => { return(i => (SimpleDevice)Activator.CreateInstance(type, i)); }); var firstDevice = factory.GetOrCreateDevice <SimpleDevice>(1); var secondDevice = factory.GetOrCreateDevice <SimpleDevice>(1); firstDevice.Should().BeSameAs(secondDevice); }
public void disposing_the_factory_disposes_all_devices() { var factory = new ConnectedDeviceFactory <int, SimpleDevice>(type => { return(i => (SimpleDevice)Activator.CreateInstance(type, i)); }); var devices = Enumerable.Range(0, 10).Select(factory.GetOrCreateDevice <SimpleDevice>).ToList(); factory.Dispose(); devices.Should().NotContain(device => device.IsDisposed == false); }
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(); }); }