Connecting Matrix Keypads
In the matrix mode (kp.mode = 0 — PL_KP_MODE_MATRIX) the kp. object works with "matrix" keypads formed by scan and return lines. The kp. object supports up to eight scan and eight return lines, which means that you can build a keypad with up to 64 keys. A sample schematic diagram of a typical keypad is shown below.
During the scanning process, the kp. object "activates" one scan line after another. The line is activated by setting it LOW, while keeping all other scan lines HIGH. For each scan line, the kp. object samples the state of return lines. If any return line is at LOW, this means the key located at the intersection of this return line and the currently active scan line is pressed.
Detailed discussion of the matrix keypad schematics falls outside the scope of this manual. I will only notice, in passing, that diodes D1-D4 are necessary and should not be omitted. For the best results, use Schottky diodes — they have low drop voltage. Pull-up resistors R1-R4 prevent the return lines from floating and should be present as well.
To build a keypad, you will need to have at least one return line. A sensible count of scan lines, however, starts from two! Having a single scan line is like having no scan lines whatsoever — you might just as well ground this single scan line (i.e., always keep it active):
On platforms with output buffer control, all intended scan lines should be configured as outputs, and all return lines — as inputs (see io.num, io.enabled).
Scan lines can optionally perform the second function of driving LEDs. One such LED can be connected to each scan line, preferably through a buffer, as shown in the diagram below. These LEDs can be used for any purpose you desire — and this purpose can be completely unrelated to the keypad itself.
If the LEDs are connected as shown in the diagram, you will turn them ON by settings their corresponding control lines LOW (use io.lineset to achieve this).
Each time the kp. object is about to scan for pressed keys — and this happens every 10ms — it will first set all scan lines to HIGH. This is necessary for the scanning process. Before doing so, the kp. object will memorize the state of each scan line. This state will be restored after the scan "pass" is complete. Scanning happens so fast that your eye won't be able to notice any LED flickering.