Configuration
keypad is implemented as a __using__ macro so that you can put it in any module you want
to handle the keypress events. Because it is small GenServer, it accepts the same options for supervision
to configure the child spec and passes them along to GenServer:
defmodule MyModule do
use Keypad, restart: :transient, shutdown: 10_000
endIt also has its own set of options to pass to configure the keypad connections. At a minimum, you must
pass either :size or a custom matrix with :matrix:
:size- If supplied without:matrixit will select the default matrix for the specified size. The delaration isrow x col, so:one_by_fourwould be 1 row, 4 columns.:four_by_fouror"4x4"- Standard 12-digit keypad withA,B,C, andDkeys:four_by_threeor"4x3"- Standard 12-digit keypad:one_by_fouror"1x4"
:matrix- A custom matrix to use for mapping keypresses to. Will take precedence over:sizeif supplied- Typically, these are
binaryvalues. However, these values are pulled from List and in theory can be anything you want. i.e. atom, integer, or even annonymous function
- Typically, these are
:row_pins- List of integers which map to corresponding GPIO to set asINPUTpins for keypard rows- On raspberry pi, these will also set the internal resistor to
PULL_UPand inactive HIGH. For all other hardware, you will probably need to make sure to place some 10K resistors between your pin and ground. see Setup doc for some examples - defaults to
[17, 27, 23, 24]
- On raspberry pi, these will also set the internal resistor to
:col_pins- List of integers which map to corresponding GPIO to asOUTPUTpins for keypad columns- defaults to
[5, 6, 13, 26]
- defaults to
Examples
Using a default matrix with custom row and column pins:
defmodule MyModule do
use Keypad, row_pins: [22,23,24,25], col_pins: [5,6,7], size: "4x3"
@impl true
def handle_keypress(key, state) do
IO.inspect(key, label: "KEYPRESS: ")
state
end
endConfiguring a custom matrix and specific row and column pins:
defmodule MyModule do
use Keypad, row_pins: [5,6,7], col_pins: [22,23,24,25], matrix: [
["c", "o", "o", "l"],
[:b, :e, :a, :n],
[1, 2, 3, 4]
]
@impl true
def handle_keypress(key, state) do
IO.inspect(key, label: "KEYPRESS: ")
state
end
endCustom matrix using default pins:
defmodule MyModule do
use Keypad, matrix: [
["c", "o", "o", "l"],
[:b, :e, :a, :n],
[1, 2, 3, 4].
['*', '%', '#', "+"]
]
@impl true
def handle_keypress(key, state) do
IO.inspect(key, label: "KEYPRESS: ")
state
end
end