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.

After unregister all callbacks, the new registered callbacks events are not fired in Raspberry Pi 4

See original GitHub issue

Describe the bug

In GpioController, when you register some callbacks with RegisterCallbackForPinValueChangedEvent() function, the events are fired as expected. Then, If you unregister all the callbacks registered before with UnregisterCallbackForPinValueChangedEvent() function, your events are not fired now. (This is the expected behaviour). Now, if you registered new callbacks in GpioController with RegisterCallbackForPinValueChangedEvent() function, the new events are not fired. No matter how many times you unregister and register callbacks, the events will never be fired again.

Steps to reproduce

  • Create new GpioController
  • Open one pin as input / InputPullUp / InputPullDown
  • Register some callbacks with RegisterCallbackForPinValueChangedEvent() function.
  • Unregister all of them with UnregisterCallbackForPinValueChangedEvent() function.
  • Register again some callbacks.
class Program
{
    static void Main(string[] args)
    {
        int pin = 12;
        PinMode pinMode = PinMode.InputPullUp;
        using GpioController controller = new();

        for(int i = 0; i < 3; i++)
        {
            Console.WriteLine($"Iteration: {i+1}");
            OpenAndRegisterPin(controller, pin, pinMode);
            Console.WriteLine($"Try pin:{pin} events in 30 seconds.");
            Task.Delay(30000).Wait();
            CloseAndUnregisterPin(controller, pin);
        }
    }

    protected static void OpenAndRegisterPin(GpioController controller, int pin, PinMode pinMode)
    {
        Console.WriteLine($"Opening Pin: {pin}");
        controller.OpenPin(pin, pinMode);

        Console.WriteLine($"Registering pin events.");
        controller.RegisterCallbackForPinValueChangedEvent(pin, PinEventTypes.Falling, OnFalling);
        controller.RegisterCallbackForPinValueChangedEvent(pin, PinEventTypes.Rising, OnRising);
    }

    protected static void CloseAndUnregisterPin(GpioController controller, int pin)
    {
        Console.WriteLine($"Unregistering pin events.");
        controller.UnregisterCallbackForPinValueChangedEvent(pin, OnFalling);
        controller.UnregisterCallbackForPinValueChangedEvent(pin, OnRising);

        Console.WriteLine($"Closing Pin: {pin}");
        controller.ClosePin(pin);
    }

    protected static void OnFalling(object sender, PinValueChangedEventArgs ev) 
    {
        Console.WriteLine($"OnFalling Pin:{ev.PinNumber}");
    }
    protected static void OnRising(object sender, PinValueChangedEventArgs ev) 
    {
        Console.WriteLine($"OnRising Pin:{ev.PinNumber}");
    }
}

Expected behavior

In all iterations, the console must show OnFallling and/or OnRising messages with the Pin:12. For example:

Iteration: 1
Opening Pin: 12
Registering pin events.
Try pin:12 events in 30 seconds.
OnFalling Pin:12
OnRising Pin:12
Unregistering pin events.
Closing Pin: 12
Iteration: 2
Opening Pin: 12
Registering pin events.
Try pin:12 events in 30 seconds.
OnFalling Pin:12
OnRising Pin:12
Unregistering pin events.
Closing Pin: 12
Iteration: 3
Opening Pin: 12
Registering pin events.
Try pin:12 events in 30 seconds.
OnFalling Pin:12
OnRising Pin:12
Unregistering pin events.
Closing Pin: 12

Actual behavior

Only in the iteration 1 (i == 0) the event messages are showed. In the next iterations, this doesn’t happened. For example:

Iteration: 1
Opening Pin: 12
Registering pin events.
Try pin:12 events in 30 seconds.
OnFalling Pin:12
OnRising Pin:12
Unregistering pin events.
Closing Pin: 12
Iteration: 2
Opening Pin: 12
Registering pin events.
Try pin:12 events in 30 seconds.
Unregistering pin events.
Closing Pin: 12
Iteration: 3
Opening Pin: 12
Registering pin events.
Try pin:12 events in 30 seconds.
Unregistering pin events.
Closing Pin: 12

Versions used

Checked In Raspberry Pi 4 Model B with Raspberry Pi OS

  • dotnet --info on the machine being used to build
SDK de .NET (que refleje cualquier global.json):
 Version:   5.0.400
 Commit:    d61950f9bf

  • dotnet --info on the machine where app is being run (not applicable for self-contained apps)
SDK de .NET (que refleje cualquier global.json):
 Version:   5.0.400
 Commit:    d61950f9bf

Also checked with:

SDK de .NET Core (reflejando cualquier global.json):
 Version:   3.1.411
 Commit:    d100cdf718
  • Version of System.Device.Gpio package
1.5.0
Also checked with:
1.3.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:16 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
raffaelercommented, Sep 3, 2021

Well done, great work. I am personally ok with that. Just have a bit of patience so that others may find some time to review the PR.

1reaction
nmunozgarciacommented, Sep 3, 2021

Don’t worry, @raffaeler. My apologies, I didn’t see that Readme before.

Thank you so much! I finally can run the tests.

Now I’m trying to create the test and check it out.

Should I put it in SysFsDriverTests.cs file for only the SysFsDriver driver or in GpioControllerTestBase.cs file for all the drivers?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Button event callbacks - any limits? - Raspberry Pi Forums
Is it possible to use a button to call a routine (through an event callback) a 2nd time, or is the callback blocked...
Read more >
Python button functions oddly not doing the same
run_callbacks walks the callback chain by grabbing one node, invoking the callback, and then following that node's link to the next callback in ......
Read more >
python - Implement a GPIO function with a callback calling ...
RPi.GPIO callbacks are run on a callback thread that is not the main thread. asyncio event loops are associated with particular threads, and ......
Read more >
How to use interrupts with Python on the Raspberry Pi ...
GPIO and gave an example of a simple “wait for an event” interrupt program. In this second article I will introduce “threaded callback”...
Read more >
[OBSOLETE] Raspberry Pi Device Type
I've created a device type for the Raspberry Pi that utilizes WebIOPi as a REST endpoint for SmartThings to communicate with.
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