PiPHP / GPIO

A PHP library for accessing the GPIO pins on a Raspberry Pi.
MIT License
431 stars 42 forks source link

Pin::getEdge/setEdge #4

Open colinodell opened 8 years ago

colinodell commented 8 years ago

Edge detection is primarily used for interrupt detection - sometimes we only care about interrupts LOW -> HIGH for example. However, it looks like this is being set via the Pin class (and can be done for output pins, which isn't useful). Would it make more sense to set this on the interrupt watcher instead?

AndrewCarterUK commented 8 years ago

Let me give this some thought!

Trying to decide if this is best as a parameter to InterruptWatcher::register(), a parameter to the input pin factory or a method on the input pin.

AndrewCarterUK commented 8 years ago

I think my main concern about having this is an InterruptWatcher::register() parameter or factory parameter is that it implies it wont change.

I'm leaning towards methods on an InputPin class.

colinodell commented 8 years ago

I feel it's more logical to define the edge you're interested in detecting at the same time you're registering the callback to handle occurrences of that edge.

For example, let's say I want a callback to fire whenever a button is pressed. I'd want to listen for any FALLING (or RISING) events and do some action. However, hooking that same callback up to a pin set to BOTH means the callback would fire twice. If I forget to set the pin's edge beforehand, or if it's modified at some other point in time, my callback isn't going to work as expected.

For Arduino, setting the edge is done when registering the interrupt callback: https://www.arduino.cc/en/Reference/AttachInterrupt

RPi.GPIO does the same thing with their wait_for_edge() function: https://sourceforge.net/p/raspberry-gpio-python/wiki/Inputs/