2020-09-09 20:57:44 -07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 The ZMK Contributors
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*/
|
2020-06-20 14:54:52 -07:00
|
|
|
|
2022-10-07 21:46:08 -07:00
|
|
|
#include <zephyr/kernel.h>
|
2020-06-20 14:54:52 -07:00
|
|
|
#include <zmk/matrix_transform.h>
|
|
|
|
#include <zmk/matrix.h>
|
2020-12-22 07:56:00 -08:00
|
|
|
#include <dt-bindings/zmk/matrix_transform.h>
|
2020-06-20 14:54:52 -07:00
|
|
|
|
2020-06-26 21:16:15 -07:00
|
|
|
#ifdef ZMK_KEYMAP_TRANSFORM_NODE
|
2020-06-20 14:54:52 -07:00
|
|
|
|
2023-04-10 00:27:19 -07:00
|
|
|
/* 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
|
|
|
|
* order to be able to quickly determine what keymap position a particular
|
|
|
|
* row,column pair is associated with, using a single lookup.
|
|
|
|
*
|
|
|
|
* We do this by creating the `transform` array at compile time, which is
|
|
|
|
* indexed by (row * ZMK_MATRIX_COLS) + column, and the value contains an
|
|
|
|
* encoded keymap index it is associated with. The keymap index is encoded
|
|
|
|
* by adding INDEX_OFFSET to it, because not all row,column pairs have an
|
|
|
|
* associated keymap index (some matrices are sparse), C globals are
|
|
|
|
* initialized to 0, and the keymap index of 0 is a valid index. We want to
|
|
|
|
* be able to detect the condition when an unassigned matrix position is
|
|
|
|
* pressed and we want to return an error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define INDEX_OFFSET 1
|
|
|
|
|
|
|
|
#define TRANSFORM_ENTRY(i, _) \
|
2020-09-13 19:53:24 -07:00
|
|
|
[(KT_ROW(DT_PROP_BY_IDX(ZMK_KEYMAP_TRANSFORM_NODE, map, i)) * ZMK_MATRIX_COLS) + \
|
2023-04-10 00:27:19 -07:00
|
|
|
KT_COL(DT_PROP_BY_IDX(ZMK_KEYMAP_TRANSFORM_NODE, map, i))] = i + INDEX_OFFSET
|
2020-06-20 14:54:52 -07:00
|
|
|
|
2023-04-10 00:27:19 -07:00
|
|
|
static uint32_t transform[] = {LISTIFY(ZMK_KEYMAP_LEN, TRANSFORM_ENTRY, (, ), 0)};
|
2020-06-20 14:54:52 -07:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2023-04-10 00:27:19 -07:00
|
|
|
int32_t zmk_matrix_transform_row_column_to_position(uint32_t row, uint32_t column) {
|
2020-06-26 21:16:15 -07:00
|
|
|
#if DT_NODE_HAS_PROP(ZMK_KEYMAP_TRANSFORM_NODE, col_offset)
|
|
|
|
column += DT_PROP(ZMK_KEYMAP_TRANSFORM_NODE, col_offset);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if DT_NODE_HAS_PROP(ZMK_KEYMAP_TRANSFORM_NODE, row_offset)
|
|
|
|
row += DT_PROP(ZMK_KEYMAP_TRANSFORM_NODE, row_offset);
|
|
|
|
#endif
|
|
|
|
|
2023-04-10 00:27:19 -07:00
|
|
|
const uint32_t matrix_index = (row * ZMK_MATRIX_COLS) + column;
|
2020-06-20 14:54:52 -07:00
|
|
|
|
2020-06-26 21:16:15 -07:00
|
|
|
#ifdef ZMK_KEYMAP_TRANSFORM_NODE
|
2023-04-10 00:27:19 -07:00
|
|
|
if (matrix_index >= ARRAY_SIZE(transform)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t value = transform[matrix_index];
|
|
|
|
|
|
|
|
if (!value) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value - INDEX_OFFSET;
|
2020-06-20 14:54:52 -07:00
|
|
|
#else
|
|
|
|
return matrix_index;
|
2020-06-26 21:16:15 -07:00
|
|
|
#endif /* ZMK_KEYMAP_TRANSFORM_NODE */
|
2020-06-20 14:54:52 -07:00
|
|
|
};
|