Skip to content

Using PulseIn to detect pin interrupts #1072

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

Closed
notro opened this issue Jul 30, 2018 · 7 comments
Closed

Using PulseIn to detect pin interrupts #1072

notro opened this issue Jul 30, 2018 · 7 comments

Comments

@notro
Copy link

notro commented Jul 30, 2018

I'm trying to emulate an MCP23008 (I2C I/O Expander) and for that I need pin interrupts.

pulseio.PulseIn almost gives me what I need.
All I need to know is:

  • has an interrupt happened since last time I checked
  • what's the current pin value.

These are the problems I face with PulseIn:

  • I don't get access to the very first edge, so I loose the first interrupt.
  • If the pin is active on init I loose another edge due to idle_state
  • In case of jitter, the pulse buffer is easily overrun. I will know that there was an interrupt, but I don't know the current pin value so I have to check with DigitalInOut. This means that I loose out on the next edge.

Adding an edge counter to PulseIn would solve these problems (counter will be cleared on read).
It would also be nice to have a PulseIn.pinvalue or something.

Or would it be better to just implement proper pin interrupts?

@dhalbert
Copy link
Collaborator

@tannewt and I discussed this. He suggested that we add a .value getter property on PulseIn, so you could just read the current value. We might read the pin directly, keep track of the value in the interrupt handler, or calculate the value based on the count. Does this sound like it would solve your problem?

We would like to implement interrupts, but doing interrupt handlers is a big topic (storage management, etc.). There's the MicroPython model but we'd like something more beginner-friendly.

@notro
Copy link
Author

notro commented Jul 30, 2018

Yeah I think a value property could do it. If I haven't seen any pulses since init, I can just check the value to catch that first edge.

I forgot about pull up, can we add a pull property as well or a kw arg to the contructor?

@tannewt
Copy link
Member

tannewt commented Jul 30, 2018

I don't think the samd can pull up when EXTI is active because the pinmux is enabled.

@notro
Copy link
Author

notro commented Aug 6, 2018

@tannewt
Copy link
Member

tannewt commented Oct 8, 2018

@notro I'm skeptical the pull up will actually work. Feel free to try it though.

@gpshead
Copy link

gpshead commented Oct 19, 2020

Wouldn't a .value property on PulseIn still leave you with a race condition?

@dhalbert
Copy link
Collaborator

countio seems like the better option for this now. Some async/await support would be good too. See #5606 for countio suggestions.

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

No branches or pull requests

4 participants