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
DigitalReader last_pressed and last_triggered #62
DigitalReader last_pressed and last_triggered #62
Conversation
…reader-improvements
I realized that Docs updated here: https://awonak.github.io/EuroPi/generated/europi.html#europi.Button.last_pressed |
Alright, this PR is good to go. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this is a bug or an intended function, but if you haven't assigned a handler then the digital input (din or buttons) will return the current time in ticks even if pressed, as the resetting of its self.last_rising_ms
is done in the bounce wrapper function.
Thinking about it this shouldn't be an issue as using .value()
rather than handler functionality at the same time as last_pressed()
is very unintuitive and doesn't have many, if any, use cases
That is a very good point. I would argue that if a script calls Currently, when the default value is 0, If the default value of Another potential solution is to set the default def show_menu_header(self):
# Returns None if unpressed, otherwise returns the duration since last press.
last_pressed = b1.last_pressed()
if last_pressed and last_pressed < MENU_DURATION:
oled.fill_rect(0, 0, OLED_WIDTH, CHAR_HEIGHT, 1)
oled.text(f"{self.pages[self.page]}", 0, 0, 0) I'll think about this some more and graciously welcome any other suggestions. |
Another proposal, # europi.py
class HandlerNotYetCalled(Exception):
pass
...
def _duration_since_last_rising(self):
"""Return the duration in milliseconds since the last trigger."""
if self.last_rising_ms is None:
raise HandlerNotYetCalled
return time.ticks_diff(time.ticks_ms(), self.last_rising_ms)
# polyrhythm_seequencer.py
...
def show_menu_header(self):
try:
if b1.last_pressed() < MENU_DURATION:
oled.fill_rect(0, 0, OLED_WIDTH, CHAR_HEIGHT, 1)
oled.text(f"{self.pages[self.page]}", 0, 0, 0)
except HandlerNotYetCalled:
pass |
The second solution looks much better to me! I think it's best to consider this a programming error and make the user aware of it rather than make it more difficult to use the method properly (your first example in the polyrhythm script) |
I have updated the code and docs to match the second proposal which throws an exception prior to handler being first called. If this seems like too much wrapper code, then we can revert back to |
Reverted back to simply returning ticks_ms of the last button press, or 0 prior to being pressed. I think this is much cleaner and less error prone. |
Is this one (and the IRQ refactor) ready to go now? All seems to be working great when I run it! |
ticks_diff()
DigitalReader.last_pressed
to betterDigitalReader.last_rising_ms
last_pressed
andlast_trigger
to the Button and DigitalInput classes.This addresses the first part of #60 with the
last_pressed()
etc methods. A future PR will address the falling edge handler.