Add a default constructor on bindings to take GpioController
See original GitHub issueWhen you want to use other GpioController
than the default driver one, you can’t in any of the binding devices.
With introduction of FT4222 we need to be able to pass a specific GpioController
to any class using a GPIO Controller.
Ideal solution would be to add or modify all devices constructor to add the possibility to add your own GpioController
if you want.
We will need also to add a flag to dispose automatically or not the GpioController
at the end.
See usage of the FT4222 GpioController
So for example taking the DHT sensor, we would need to change the constructor and adjust the code from:
/// <summary>
/// Create a DHT sensor
/// </summary>
/// <param name="pin">The pin number (GPIO number)</param>
/// <param name="pinNumberingScheme">The GPIO pin numbering scheme</param>
public DhtBase(int pin, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical)
{
_protocol = CommunicationProtocol.OneWire;
_controller = new GpioController(pinNumberingScheme);
_pin = pin;
_controller.OpenPin(_pin);
// delay 1s to make sure DHT stable
Thread.Sleep(1000);
}
to:
private bool _autoDispose;
/// <summary>
/// Create a DHT sensor
/// </summary>
/// <param name="pin">The pin number (GPIO number)</param>
/// <param name="pinNumberingScheme">The GPIO pin numbering scheme</param>
/// <param name="gpioController">A GPIO Controller if not using the default driver one</param>
/// <param name="autoDispose">True to auto dispose the GPIO Controller when dispose</param>
public DhtBase(int pin, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical, GpioController gpioController = null, bool autoDispose = true)
{
_protocol = CommunicationProtocol.OneWire;
_autoDispose = autoDispose;
if (gpioController == null)
{ _controller = new GpioController(pinNumberingScheme); }
else
{
_controller = gpioController;
}
_pin = pin;
_controller.OpenPin(_pin);
// delay 1s to make sure DHT stable
Thread.Sleep(1000);
}
/// <inheritdoc/>
public void Dispose()
{
if (_autoDispose)
{
_controller?.Dispose();
}
_i2cDevice?.Dispose();
}
And also adjust the constructor for the DHTxx:
public Dht22(int pin, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical, GpioController gpioController = null, bool autoDispose = true)
: base(pin, pinNumberingScheme, gpioController, autoDispose)
{
}
That would be needed for all the devices using a GpioController
.
And DHT may not be a device for which the FT4222 would work. But it’s to give an example.
Other alternative option: add the FT4222 into System.Device.Gpio
. And on the driver side, check if it does exist and pick it if no other driver.
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (12 by maintainers)
Top GitHub Comments
@joperezr Note that, while most bindings do take an external GpioController, many are missing the optional
shouldDispose
argument, which results in the controller being disposed when the binding is disposed. This is often not desirable, especially when using a GpioExpander or an Ft4222 board.Actually there are many device bindings today that take in a GpioController in their constructor for this exact reason. The idea is that if you are not using the default controller because you are using a Gpio expander on your board, you can create a custom GpioController using the custom GpioDriver, and then pass that in to many of our device bindings. Just by looking at ILSpy, here is a list of some of the devices that have a constructor that take in a GpioController:
If you find places where we are internally using a GpioController, and we aren’t optionally taking one on the constructor, then we should definitely add it to that binding.