Add a simple build-time test verifying that the keyboard layout defined in matrix.h is unlikely to result in an unfortunate situation

This commit is contained in:
lif 2024-07-03 18:06:02 -07:00
parent b6a63b0b0b
commit 40050f0314
2 changed files with 93 additions and 1 deletions

View File

@ -1,8 +1,14 @@
#!/bin/bash #!/bin/bash
set -e
mkdir -p build
cc test/matrix-valid.c -o build/matrix-valid
build/matrix-valid
export PICO_SDK_PATH=$(pwd)/../../pico-sdk export PICO_SDK_PATH=$(pwd)/../../pico-sdk
mkdir -p build
cd build cd build
cmake -DFAMILY=rp2040 .. cmake -DFAMILY=rp2040 ..

View File

@ -0,0 +1,86 @@
#define KBD_COLS 12
#define KBD_ROWS 6
#define KBD_MATRIX_SZ KBD_COLS * KBD_ROWS + 4
#include <stdio.h>
#include <stdint.h>
#include "../src/matrix.h"
#define WHY_HYPER_RETURN \
"Hyper+Return is required both to power on and to enter the keyboard menu.\n"
#define WHY_X \
"Pressing X in the keyboard menu is required to enter bootloader mode.\n" \
"(Bootloader mode can be exited by toggling the standby power switch.)\n"
int main() {
int i;
int hyper_loc = -1;
int x_loc = -1;
int enter_loc_fn = -1;
// irrelevant because they're both defined with [KBD_MATRIX_SZ],
// but if they were changed not to:
if (sizeof(matrix) != sizeof(matrix_fn)) {
fprintf(
stderr,
"Layer size mismatch! %zu != %zu\n",
sizeof(matrix),
sizeof(matrix_fn)
);
return 1;
}
for (i = 0; i < sizeof(matrix); i++) {
if (matrix[i] == KEY_COMPOSE) {
hyper_loc = i;
} else if (matrix[i] == KEY_X) {
x_loc = i;
}
}
if (hyper_loc == -1) {
fprintf(
stderr,
"No hyper key (KEY_COMPOSE) found on base layer (matrix)!\n"
WHY_HYPER_RETURN
);
return 1;
}
if (x_loc == -1) {
fprintf(
stderr,
"No X key (KEY_X) found on base layer (matrix)!\n"
WHY_X
);
return 1;
}
if (matrix_fn[hyper_loc] != KEY_COMPOSE) {
for (i = 0; i < sizeof(matrix_fn); i++) {
if (matrix_fn[i] == KEY_COMPOSE) {
fprintf(
stderr,
"Hyper key (KEY_COMPOSE) in different position on layers! %d != %d\n",
hyper_loc,
i
);
return 1;
}
}
fprintf(stderr, "No hyper key (KEY_COMPOSE) found on hyper layer (matrix_fn)!\n");
return 1;
}
for (i = 0; i < sizeof(matrix_fn); i++) {
if (matrix_fn[i] == KEY_ENTER) {
enter_loc_fn = i;
}
}
if (enter_loc_fn == -1) {
fprintf(
stderr,
"No return key (KEY_ENTER) found on hyper layer (matrix_fn)!\n"
WHY_HYPER_RETURN
);
return 1;
}
return 0;
}