#keypad

1 messages · Page 1 of 1 (latest)

solemn oracle
#

@

#

easier how?

delicate panther
#

less calls to each .update(), less setup, since you don't need to setup each pin separately

molten frost
#

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.

solemn oracle
#

hmm

#

interesting

#

so it's taking away the manual labeling and whatnot

molten frost
#

manual labeling?

solemn oracle
#

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"

molten frost
#

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
solemn oracle
#

I don't understand what := is doing there

#

is that if event (set event to the updated thing and continue)?

molten frost
#

Sorry, it's such a convenient Python feature.

#

it's short for

solemn oracle
#

so if event (update the button event.get()) thing then update the button variable then if button then blah if button then blah blah?

molten frost
#
  event = buttons.events.get()
  if event: # get() returns None if no event detected
    # handle the event
solemn oracle
#

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

molten frost
#

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

solemn oracle
#

wait, is := a new thing for circuitpython?

#

regardless very cool trick

#

you and neradoc

molten frost
#

I think it was there in 6.x

solemn oracle
#

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

delicate panther
#

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

molten frost
#

Good for you! That sounds like such satisfying stuff to work on

solemn oracle
#

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

delicate panther
#

note that the (keypad) events have a timestamp, you could use that to reject accidental double clicks, presses that happen too fast

solemn oracle
#

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

solemn oracle
#

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

molten frost
#

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."

solemn oracle
#

so the point of that is what?

molten frost
#

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.

solemn oracle
#

is there sample code for how to screen out tremor clicks?

molten frost
#

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.

solemn oracle
#

so lastPressed = timestamp()

#

then if button:

#

nested if (timestamp - lastPressed) >= 0.5:

#

do the thing

#

else continue

#

??

molten frost
#

Seems right, if the intent is "a new press has to be at least 0.5 sec from the previous press to count."