Author Topic: You can put rotary encoders in a keyboard matrix!  (Read 1692 times)

0 Members and 1 Guest are viewing this topic.

Offline Snipeye

  • Thread Starter
  • Posts: 26
You can put rotary encoders in a keyboard matrix!
« on: Sat, 11 May 2024, 21:14:57 »
I've managed 32, in the pictures below I built a board that does 30 - you need about 2K+ scans/second to avoid missing a transition, but that's not too hard to achieve.

Additionally, since encoders have the "common" pin between A and B, you need 2 matrix slots in the same row (if diodes are oriented col2row) or the same or the same column (if diodes are oriented row2col).

Aside from that, it's pretty straightforward.

* IMG_7584_1080.mp4 (29495 kB - downloaded 36 times.)

307853-1

307855-2

I have a PR to QMK to show how it's done, visible at https://github.com/qmk/qmk_firmware/pull/23700 if you care.

Offline fpazos

  • Posts: 167
Re: You can put rotary encoders in a keyboard matrix!
« Reply #1 on: Sun, 12 May 2024, 05:06:34 »
Very interesting, seems really useful for custom midi controllers.
 

Offline Findecanor

  • Posts: 5043
  • Location: Koriko
Re: You can put rotary encoders in a keyboard matrix!
« Reply #2 on: Sun, 12 May 2024, 07:02:24 »
you need about 2K+ scans/second to avoid missing a transition, but that's not too hard to achieve.
That's interesting. How do you debounce them?

Do you report a step on the next change and suppress only the opposite turning direction for the bounce period?
Or do you not need to?
🍉

Offline Snipeye

  • Thread Starter
  • Posts: 26
Re: You can put rotary encoders in a keyboard matrix!
« Reply #3 on: Sun, 12 May 2024, 10:09:35 »
Very interesting, seems really useful for custom midi controllers.

Yeah, that's what the 32-encoder board I designed (for a client) does - a synth of some sort; I'll ask him if he's OK if I post pictures.  Various knobs to change variables like frequency, wave type (sawtooth, etc)... I don't know what else, but he was asking for as many knobs as he could possibly get, haha.

As far as this board... it's not useful except to prove that the encoders work in the matrix.  IDK if you watched the video (first attachment), but I've set it up so that turns on alphabetic keys shift their mapping (a -> b, b -> c on clockwise turns, and backward on counterclockwise).  Really don't know what else to do with it, lol - 30 volume controls?  Somebody suggested "30 volume controls but each one only controls 3.3% of the total volume" or "30-player pong somehow"...

Offline Snipeye

  • Thread Starter
  • Posts: 26
Re: You can put rotary encoders in a keyboard matrix!
« Reply #4 on: Sun, 12 May 2024, 10:14:11 »
you need about 2K+ scans/second to avoid missing a transition, but that's not too hard to achieve.
That's interesting. How do you debounce them?

Do you report a step on the next change and suppress only the opposite turning direction for the bounce period?
Or do you not need to?

The traditional method for debouncing encoders is to pass the raw outputs through a state machine (I didn't look closely, but I think it's described right in the stackexchange post https://electronics.stackexchange.com/questions/360637/quadrature-encoder-most-efficient-software-implementation).  QMK debounces individual keys in the matrix, and those debounced values are currently getting handled by the state machine so it's sort of double-debounced, which is providing some less-than-graceful results (the default key debouncing is too aggressive/not a good fit for an encoder).

I've written (for the client I mentioned in my previous post, with 32 encoders) a bitfield-state machine that handles all 32 encoders in parallel by performing bitwise operations on uint32_t variables that worked really well - I plan on submitting another PR to QMK to implement this method since it debounces all encoders at once (instead of running a loop that debounces them all individually).

Offline homenick

  • Posts: 1
Re: You can put rotary encoders in a keyboard matrix!
« Reply #5 on: Tue, 28 May 2024, 20:39:03 »
you need about 2K+ scans/second to avoid missing a transition, but that's not too hard to achieve.
That's interesting. How do you debounce them?

Do you report a step on the next change and suppress only the opposite turning direction for the bounce period?
Or do you not need to?

The traditional method for debouncing encoders is to pass the raw outputs through a state machine (I didn't look closely, but I think it's described right in the stackexchange post https://electronics.stackexchange.com/questions/360637/quadrature-encoder-most-efficient-software-implementationpixel speedrun).  QMK debounces individual keys in the matrix, and those debounced values are currently getting handled by the state machine so it's sort of double-debounced, which is providing some less-than-graceful results (the default key debouncing is too aggressive/not a good fit for an encoder).

I've written (for the client I mentioned in my previous post, with 32 encoders) a bitfield-state machine that handles all 32 encoders in parallel by performing bitwise operations on uint32_t variables that worked really well - I plan on submitting another PR to QMK to implement this method since it debounces all encoders at once (instead of running a loop that debounces them all individually).
I am sorry, but I've searched the article but couldn't find relevant information about traditional method for debouncing encoders vs state machine.

Offline Snipeye

  • Thread Starter
  • Posts: 26
Re: You can put rotary encoders in a keyboard matrix!
« Reply #6 on: Sat, 01 June 2024, 11:17:00 »
I am sorry, but I've searched the article but couldn't find relevant information about traditional method for debouncing encoders vs state machine.

So a traditional "debounce" would be "make sure the pin stays in a certain state for a certain amount of time/readings before we allow it to change again."  That can work with encoders, but a better approach is a state machine to enforce valid transitions.  That approach is described pretty well here:

https://forum.arduino.cc/t/reading-rotary-encoders-as-a-state-machine/937388

The actual code can be adapted all sorts of ways.