#keypad
1 messages · Page 1 of 1 (latest)
less calls to each .update(), less setup, since you don't need to setup each pin separately
It does all the polling in the background and presents you with a time-ordered queue of events. I think it makes it easier to track the current state of each button, and simplifies the loop that handles them.
Yeah, and what Neradoc said.
manual labeling?
like my debouncing code with each pin set up for debouncing and whatnot
although I'm seriously struggling to grok this keypad code
mostly how do I "if button 1 pressed do click, if button 2 pressed do hold_click"
Something like this:
buttons = keypad.Keys((board.D9, board.D10, board.D11))
while True:
if event := buttons.events.get():
if event.pressed: # ignore releases
button = event.key_number # e.g. board.D9 is #0
if button == 0:
# blah
elif button == 1:
# blah blah
elif button == 2:
# blah blah blah
I don't understand what := is doing there
is that if event (set event to the updated thing and continue)?
so if event (update the button event.get()) thing then update the button variable then if button then blah if button then blah blah?
event = buttons.events.get()
if event: # get() returns None if no event detected
# handle the event
wait, I like learning new things
what did := do in that code?
oh
wait
okay one sec
yeah, so it's simultaneously updating and running an if check
Yeah. It's especially convenient in while loops
while thing := next_thing(): vs
while True:
thing = next_thing()
if not thing:
break
Says there "new in 3.8" but it's a feature that MicroPython hence CircuitPython picked up
wait, is := a new thing for circuitpython?
regardless very cool trick
https://github.com/xgpt/AT_CircuitPython/blob/main/mouse_with_hold_working_code.py << You helped with this! thank you!
Contribute to xgpt/AT_CircuitPython development by creating an account on GitHub.
you and neradoc
I think it was there in 6.x
that's a little snappier than the debouncer method and less klutzy than the time.sleep(0.2) method
the keypad library seems very nice for this
I'll definitely be using this much much more. I love making devices like mice and keypads for people with disabilities and frankly circuitpython makes testing/revising these simple devices SOOOO MUCH FASTER than using QMK for less than a full keyboard. These folks typically find fewer features to be the killer feature for my devices
I literally made a custom mouse that's a hardware replacement for windows mouse keys because the user kept triggering the calculator shortcut on their regular number pad/keyboard
No more problems
yeah the walrus operator := is new from 7, one of the things we picked up when finally getting CP up to date with Micropython in 2021
Good for you! That sounds like such satisfying stuff to work on
WALRUS OPERATOR lol
love it
and yeah, my job is freaking awesome.
totally not a programmer by trade though. just a nerd who learned how to solder
note that the (keypad) events have a timestamp, you could use that to reject accidental double clicks, presses that happen too fast
that's going to be useful for another resident of mine
where is keypad documented I can't seem to find it
i just find tutorials for keypads 😛
I need to RTFM before asking questions more. I really appreciate the knowledge dump though. Y'all have been crazy helpful
I just need to finish designing a case for this thing and it'll be set for them. programming is done I think
I don't understand the timestamp thing you were mentioning
keypad checks the state of the keys every (by default) 0.02 sec, notes if any keys have changed state, and generates an event accordingly. One of the fields in the event is a timestamp, which is the time it discovered the state change. The events are queued up, so the time you pull the event out of the queue may very well be later than the event occurred.
"timestamp (int) – The time in milliseconds that the keypress occurred in the supervisor.ticks_ms time system."
so the point of that is what?
You could do things like autorepeat a key if it's been held for a certain time -- the start time is in the event
Or screen out clicks (starts) that are too close together, possibly indicating a user tremor.
Basically, they're there if you need them, and if they weren't there and you needed timestamps, rolling your own would be pretty tedious and less accurate.
is there sample code for how to screen out tremor clicks?
Not to my knowledge. It would probably look like debouncing at a slower time scale.
I'd consider looking for short gaps between the release and next press of the same key, and treat that gap as unintentional.
so lastPressed = timestamp()
then if button:
nested if (timestamp - lastPressed) >= 0.5:
do the thing
else continue
??
Seems right, if the intent is "a new press has to be at least 0.5 sec from the previous press to count."