public void Dispose()
 {
     Console.WriteLine("Clean up sensors");
     _mpu9250.Dispose();
     _bme280.Dispose();
     _vl53L0X.Dispose();
     _bh1750Fvi.Dispose();
     _amg88xx.Dispose();
 }
        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();
            });
        }