feat: Add keyboard physical layout system.

* Add bindings to allow creating multiple physical layouts that specify
  their key's physical attributes, and the matching matrix transform
  and dependant kscan to use.
* Synthesize a basic physical layout if none specified, for backwards
  compatibility.
* Update matrix transform API to explicitly pass in the selected transform
  to the API instead of using a fixed chosen transform.
* Move kscan subscription and handling into the physical layout code, so
  that selecting a different physical layout at runtime can also use the
  correct kscan instance.
* Add `physical_layouts.dtsi` file to include so you can use the
  pre-configured `&key_physical_attrs` for adding you layout keys.
This commit is contained in:
Peter Johanson
2024-04-24 18:14:02 -07:00
committed by Pete Johanson
parent 80173f8ea3
commit c5cca5b34f
13 changed files with 604 additions and 141 deletions

View File

@@ -4,12 +4,23 @@
* SPDX-License-Identifier: MIT
*/
#include <zephyr/init.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/util.h>
#include <zmk/matrix_transform.h>
#include <zmk/matrix.h>
#include <dt-bindings/zmk/matrix_transform.h>
#ifdef ZMK_KEYMAP_TRANSFORM_NODE
#define DT_DRV_COMPAT zmk_matrix_transform
struct zmk_matrix_transform {
uint32_t const *lookup_table;
size_t len;
uint8_t rows;
uint8_t columns;
uint8_t col_offset;
uint8_t row_offset;
};
/* the transform in the device tree is a list of (row,column) pairs that is
* indexed by by the keymap position of that key. We want to invert this in
@@ -28,38 +39,58 @@
#define INDEX_OFFSET 1
#define TRANSFORM_ENTRY(i, _) \
[(KT_ROW(DT_PROP_BY_IDX(ZMK_KEYMAP_TRANSFORM_NODE, map, i)) * ZMK_MATRIX_COLS) + \
KT_COL(DT_PROP_BY_IDX(ZMK_KEYMAP_TRANSFORM_NODE, map, i))] = i + INDEX_OFFSET
#if DT_HAS_COMPAT_STATUS_OKAY(zmk_matrix_transform)
static uint32_t transform[] = {LISTIFY(ZMK_KEYMAP_LEN, TRANSFORM_ENTRY, (, ), 0)};
#define TRANSFORM_LOOKUP_ENTRY(i, n) \
[(KT_ROW(DT_INST_PROP_BY_IDX(n, map, i)) * DT_INST_PROP(n, columns)) + \
KT_COL(DT_INST_PROP_BY_IDX(n, map, i))] = i + INDEX_OFFSET
#endif
#define MATRIX_TRANSFORM_INIT(n) \
static const uint32_t _CONCAT(zmk_transform_lookup_table_, n)[] = { \
LISTIFY(DT_INST_PROP_LEN(n, map), TRANSFORM_LOOKUP_ENTRY, (, ), n)}; \
const struct zmk_matrix_transform _CONCAT(zmk_matrix_transform_, DT_DRV_INST(n)) = { \
.rows = DT_INST_PROP(n, rows), \
.columns = DT_INST_PROP(n, columns), \
.col_offset = DT_INST_PROP(n, col_offset), \
.row_offset = DT_INST_PROP(n, row_offset), \
.lookup_table = _CONCAT(zmk_transform_lookup_table_, n), \
.len = ARRAY_SIZE(_CONCAT(zmk_transform_lookup_table_, n)), \
};
int32_t zmk_matrix_transform_row_column_to_position(uint32_t row, uint32_t column) {
#if DT_NODE_HAS_PROP(ZMK_KEYMAP_TRANSFORM_NODE, col_offset)
column += DT_PROP(ZMK_KEYMAP_TRANSFORM_NODE, col_offset);
#endif
DT_INST_FOREACH_STATUS_OKAY(MATRIX_TRANSFORM_INIT);
#if DT_NODE_HAS_PROP(ZMK_KEYMAP_TRANSFORM_NODE, row_offset)
row += DT_PROP(ZMK_KEYMAP_TRANSFORM_NODE, row_offset);
#endif
#elif DT_HAS_CHOSEN(zmk_kscan) && defined(ZMK_MATRIX_COLS) && defined(ZMK_MATRIX_ROWS)
const uint32_t matrix_index = (row * ZMK_MATRIX_COLS) + column;
#ifdef ZMK_KEYMAP_TRANSFORM_NODE
if (matrix_index >= ARRAY_SIZE(transform)) {
return -EINVAL;
}
const uint32_t value = transform[matrix_index];
if (!value) {
return -EINVAL;
}
return value - INDEX_OFFSET;
#else
return matrix_index;
#endif /* ZMK_KEYMAP_TRANSFORM_NODE */
const struct zmk_matrix_transform zmk_matrix_transform_default = {
.rows = ZMK_MATRIX_ROWS,
.columns = ZMK_MATRIX_COLS,
.len = ZMK_KEYMAP_LEN,
};
#else
#error "Need a matrix transform or compatible kscan selected to determine keymap size!"
`
#endif // DT_HAS_COMPAT_STATUS_OKAY(zmk_matrix_transform)
int32_t zmk_matrix_transform_row_column_to_position(zmk_matrix_transform_t mt, uint32_t row,
uint32_t column) {
column += mt->col_offset;
row += mt->row_offset;
if (!mt->lookup_table) {
return (row * mt->columns) + column;
}
uint16_t lookup_index = ((row * mt->columns) + column);
if (lookup_index >= mt->len) {
return -EINVAL;
}
int32_t val = mt->lookup_table[lookup_index];
if (val == 0) {
return -EINVAL;
}
return val - INDEX_OFFSET;
};