Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event based input detection - proposal #21

Closed
drahoslove opened this issue Jun 4, 2018 · 1 comment
Closed

Event based input detection - proposal #21

drahoslove opened this issue Jun 4, 2018 · 1 comment

Comments

@drahoslove
Copy link
Collaborator

drahoslove commented Jun 4, 2018

Hi, I'd like to contribute again and implement functionality inspired by this bcm2835 lib example.

(In my eyes this has higher priority than software pwm duscussed in #20)

Reason

The use case is, when you want to detect short input signal, eg. fast button press (or something much much shorter), and you don't want to waste much CPU (prevent rapid pooling), but you don't mind waiting a bit to actually process the action.

Let's say the button accuation time when pressed may be as short as 10ms. In current stage, only way to reliably detect it is to sample input every 5ms, like in following example.

pin.Input()
for {
	if pin.Read() == rpio.High {
		println("button pressed")
	}
	time.Sleep(5*time.Millisecond) // wait time determines shortes detectable signal length
}

This is not much CPU effecient for long running instances.

Solution

I propose to use rise/fall edge event detection (without interruption handling like in github.com/brian-armstrong/gpio, but it would still be an improvement).

Two new pin method would be implemented. (Better naming ideas are welcomed).

  • func(pin) Detect(edge) - for enabling/disabling detection of rising and/or falling edge event
    • It would write to GPREN and GPFEN registers - this functionality scans pins using clock and detect 011 and/or 100 patterns.
    • The edge argument would be one of: NoEdge, RiseEdge, FallEdge, AnyEdge.
  • func(pin) EdgeDetected() bool - for checking if event has occured since last call
    • done by reading and cleaning bits of GPEDS register

Example of usage:

pin.Input()
pin.Detect(rpio.RiseEdge)

for {
	if pin.EdgeDetected() {
		prinln("button pressed")
	}
	time.Sleep(500*time.Millisecond)
 	// wait time only affect user experience; signal length may be as short as two CLK ticks

}

It would (partially) solve issues #18.

In the future this functionaluty might be extended over intteruption handling using blocking channel, which would avoid the neccessity to use any sleeps.

I will create pull request soon, if there are no objections.

@stianeikeland
Copy link
Owner

This seems well thought out, and I would definitely be open for a edge detect feature PR like this 👍

Thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants