question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a default constructor on bindings to take GpioController

See original GitHub issue

When 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:closed
  • Created 4 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
pgrawehrcommented, Mar 18, 2020

@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.

1reaction
joperezrcommented, Mar 18, 2020

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:

image

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mcp23xxx Constructor (Iot.Device. ...
If not specified, the default controller will be used. bankStyle: BankStyle. The current bank style of the ports. This does not set the...
Read more >
Why does the model binder need an empty constructor
Having default constructor allows run-time to create new object without knowledge of particular arguments to your custom constructor.
Read more >
Interfaces/Objects - The UniFFI user guide
In addition to the default constructor connected to the ::new() method, you can specify alternate named constructors to create object instances in different ......
Read more >
Classes - pybind11 documentation
The syntax for binding constructors was previously introduced, but it only works when a constructor of the appropriate arguments actually exists on the...
Read more >
Gpio led driver linux. * There is an overall "any bank"
I'm able to use either JTAG or my bootloader (setting the registers linux; embedded; linux-device-driver; gpio; Share. Steps that involve while writing the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found