unosquare / wiringpi-dotnet

Provides complete managed access to the popular wiringpi C library
MIT License
68 stars 13 forks source link

Adding Bouncetime to RegisterInterruptCallback() #2

Closed BigBadPhysicist closed 5 years ago

BigBadPhysicist commented 5 years ago

I am porting some code from python to c#.

I have a simple setup using a RP3 with a SenseHat and an external Button (wired between pin 16 and GND ).

In python I used the following code to achive a bounce free function call after a button press:

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(16,GPIO.IN,pull_up_down=GPIO.PUD_UP)

def nextTick(*args)
  # do my stuff here

GPIO.add_event_detect(16,GPIO.FALLING,callback=nextTick,bouncetime=200)

with your libary i tried:

Pi.Init<BootstrapWiringPi>();
var pin = Pi.Gpio[16];
pin.PinMode = GpioPinDriveMode.Input;
pin.InputPullMode = GpioPinResistorPullMode.PullUp;
pin.RegisterInterruptCallback(EdgeDetection.FallingEdge, ButtonCallback);

this works for detecting the button press, but since the button is quiet cheap I get up to 10 function calls each time i press the button.

Would it be possible / or is it allready possible to add a bouncetime to RegisterInterruptCallback() to drop incomming interrupts in a given time window to remove bounce from the button?

k3z0 commented 5 years ago

Hi @BigBadPhysicist,

By the moment we don't support for any bounce time. Maybe we could add that feature, but I need to run some tests first.

I'll be updating this issue to let you know if we have some advance in that feature.

Greetings.

k3z0 commented 5 years ago

Hi @BigBadPhysicist,

I did some tests about interrupt callbacks using RaspberryIO and WiringPi. The current behavior you found in our library is actually the behavior of the underlying WiringPi library. However, this can be handled easily in your code:

    class Program
    {
        private static DateTime _lastInterrupt;
        private static int _count;

        static void Main(string[] args)
        {
            _lastInterrupt = DateTime.Now;

            Pi.Init<BootstrapWiringPi>();

            var pin = Pi.Gpio[17];
            pin.PinMode = GpioPinDriveMode.Input;
            pin.InputPullMode = GpioPinResistorPullMode.PullUp;
            pin.RegisterInterruptCallback(EdgeDetection.FallingEdge, Callback);

            Console.ReadKey();
        }

        static void Callback()
        {
            var now = DateTime.Now;
            if (now.Subtract(_lastInterrupt).TotalMilliseconds > 200)
            {
                $"Button Press {++_count}".Info();
                _lastInterrupt = now;
            }
        }
    }
k3z0 commented 5 years ago

@BigBadPhysicist can we close this issue?

BigBadPhysicist commented 5 years ago

Thanks for the help.

Closed!