Skip to content

Using PulseIn to detect pin interrupts #1072

Closed

Description

@notro

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?

Activity

  1. dhalbert commented on Jul 30, 2018

    @dhalbert
    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.

  2. notro commented on Jul 30, 2018

    @notro
    Author

    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?

  3. tannewt commented on Jul 30, 2018

    @tannewt
    Member

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

  4. notro commented on Aug 6, 2018

    @notro
    Author
  5. added this to the Long term milestone on Oct 8, 2018
  6. tannewt commented on Oct 8, 2018

    @tannewt
    Member

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

  7. gpshead commented on Oct 19, 2020

    @gpshead

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

  8. dhalbert commented on Dec 24, 2021

    @dhalbert
    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions