koktoh の雑記帳

気ままに書いていきます

【不定期連載】QMK 探検隊 -その2-

はじめに

これは、「C言語とか組み込みなんもわからん」男が QMK というジャングルを探検していく、ノンフィクションドキュメンタリーである……
なお、更新は亀の歩みである……

前回のおさらい

koktoh.hatenablog.com

quantum.h を確認したら、たくさん include されていたので、順番に見ていくことにした

探検開始

上から順番に探検していこう

  • wait.h
  • matrix.h
  • keymap.h
  • action_layer.h
  • eeconfig.h
  • bootloader.h
  • timer.h
  • sync_timer.h
  • config_common.h
  • gpio.h
  • atomic_util.h
  • led.h
  • action_util.h
  • action_tapping.h
  • print.h
  • send_string.h
  • suspend.h

wait.h

github.com

ソース全体

#pragma once

#include <inttypes.h>

#ifdef __cplusplus
extern "C" {
#endif

#if defined(__ARMEL__) || defined(__ARMEB__)
#    ifndef __OPTIMIZE__
#        pragma message "Compiler optimizations disabled; wait_cpuclock() won't work as designed"
#    endif

#    define wait_cpuclock(x) wait_cpuclock_allnop(x)

#    define CLOCK_DELAY_NOP8 "nop\n\t nop\n\t nop\n\t nop\n\t   nop\n\t nop\n\t nop\n\t nop\n\t"

__attribute__((always_inline)) static inline void wait_cpuclock_allnop(unsigned int n) { /* n: 1..135 */
    /* The argument n must be a constant expression.
     * That way, compiler optimization will remove unnecessary code. */
    if (n < 1) {
        return;
    }
    if (n > 8) {
        unsigned int n8 = n / 8;
        n               = n - n8 * 8;
        switch (n8) {
            case 16:
                asm volatile(CLOCK_DELAY_NOP8::: "memory");
            case 15:
                asm volatile(CLOCK_DELAY_NOP8::: "memory");
            case 14:
                asm volatile(CLOCK_DELAY_NOP8::: "memory");
            case 13:
                asm volatile(CLOCK_DELAY_NOP8::: "memory");
            case 12:
                asm volatile(CLOCK_DELAY_NOP8::: "memory");
            case 11:
                asm volatile(CLOCK_DELAY_NOP8::: "memory");
            case 10:
                asm volatile(CLOCK_DELAY_NOP8::: "memory");
            case 9:
                asm volatile(CLOCK_DELAY_NOP8::: "memory");
            case 8:
                asm volatile(CLOCK_DELAY_NOP8::: "memory");
            case 7:
                asm volatile(CLOCK_DELAY_NOP8::: "memory");
            case 6:
                asm volatile(CLOCK_DELAY_NOP8::: "memory");
            case 5:
                asm volatile(CLOCK_DELAY_NOP8::: "memory");
            case 4:
                asm volatile(CLOCK_DELAY_NOP8::: "memory");
            case 3:
                asm volatile(CLOCK_DELAY_NOP8::: "memory");
            case 2:
                asm volatile(CLOCK_DELAY_NOP8::: "memory");
            case 1:
                asm volatile(CLOCK_DELAY_NOP8::: "memory");
            case 0:
                break;
        }
    }
    switch (n) {
        case 8:
            asm volatile("nop" ::: "memory");
        case 7:
            asm volatile("nop" ::: "memory");
        case 6:
            asm volatile("nop" ::: "memory");
        case 5:
            asm volatile("nop" ::: "memory");
        case 4:
            asm volatile("nop" ::: "memory");
        case 3:
            asm volatile("nop" ::: "memory");
        case 2:
            asm volatile("nop" ::: "memory");
        case 1:
            asm volatile("nop" ::: "memory");
        case 0:
            break;
    }
}
#endif

#if defined(__AVR__)
#    include <util/delay.h>
#    define wait_ms(ms) _delay_ms(ms)
#    define wait_us(us) _delay_us(us)
#    define wait_cpuclock(x) __builtin_avr_delay_cycles(x)
#elif defined PROTOCOL_CHIBIOS
#    include <ch.h>
#    define wait_ms(ms)                     \
        do {                                \
            if (ms != 0) {                  \
                chThdSleepMilliseconds(ms); \
            } else {                        \
                chThdSleepMicroseconds(1);  \
            }                               \
        } while (0)
#    define wait_us(us)                     \
        do {                                \
            if (us != 0) {                  \
                chThdSleepMicroseconds(us); \
            } else {                        \
                chThdSleepMicroseconds(1);  \
            }                               \
        } while (0)
#elif defined PROTOCOL_ARM_ATSAM
#    include "clks.h"
#    define wait_ms(ms) CLK_delay_ms(ms)
#    define wait_us(us) CLK_delay_us(us)
#else  // Unit tests
void wait_ms(uint32_t ms);
#    define wait_us(us) wait_ms(us / 1000)
#endif

#ifdef __cplusplus
}
#endif

~85行

はっきり言って、よくわからない
とくに、 __attribute__((always_inline)) static inline void wait_cpuclock_allnop(unsigned int n) がなんなのかわからない
ディレイを実現するための、組み込み系のテクニックなのかもしれない
何もわからないので、深入りはやめよう

~最終行

ここら辺は辛うじてわかる
ターゲットとなる CPU に合わせて wait_ms() wait_us() を定義しているようだ
各 CPU の標準的な delay 系の関数を wait_xx() という関数にラップして使いまわしをしやすくしているのだろう
AVR には wait_cpuclock() という固有の関数も定義している

これによって、 CPU を変更しても wait_xx() の正確性は担保される、という仕組みだと思われる

matrix.h

github.com

ソース全体

/*
Copyright 2011 Jun Wako <wakojun@gmail.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <stdint.h>
#include <stdbool.h>

#if (MATRIX_COLS <= 8)
typedef uint8_t matrix_row_t;
#elif (MATRIX_COLS <= 16)
typedef uint16_t matrix_row_t;
#elif (MATRIX_COLS <= 32)
typedef uint32_t matrix_row_t;
#else
#    error "MATRIX_COLS: invalid value"
#endif

#define MATRIX_ROW_SHIFTER ((matrix_row_t)1)

#ifdef __cplusplus
extern "C" {
#endif

/* number of matrix rows */
uint8_t matrix_rows(void);
/* number of matrix columns */
uint8_t matrix_cols(void);
/* should be called at early stage of startup before matrix_init.(optional) */
void matrix_setup(void);
/* intialize matrix for scaning. */
void matrix_init(void);
/* scan all key states on matrix */
uint8_t matrix_scan(void);
/* whether modified from previous scan. used after matrix_scan. */
bool matrix_is_modified(void) __attribute__((deprecated));
/* whether a switch is on */
bool matrix_is_on(uint8_t row, uint8_t col);
/* matrix state on row */
matrix_row_t matrix_get_row(uint8_t row);
/* print matrix for debug */
void matrix_print(void);
/* delay between changing matrix pin state and reading values */
void matrix_output_select_delay(void);
void matrix_output_unselect_delay(void);
/* only for backwards compatibility. delay between changing matrix pin state and reading values */
void matrix_io_delay(void);

/* power control */
void matrix_power_up(void);
void matrix_power_down(void);

/* executes code for Quantum */
void matrix_init_quantum(void);
void matrix_scan_quantum(void);

void matrix_init_kb(void);
void matrix_scan_kb(void);

void matrix_init_user(void);
void matrix_scan_user(void);

#ifdef __cplusplus
}
#endif

~21行

include の宣言である

~31行

matrix_row_t の型を定義している
ここで定義した matrix_row_t が各行のスイッチの状態を保持することになる

例えば、 config.h で以下のように定義したとしよう

// 3 x 3 のマクロパッド程度
#define MATRIX_ROWS 3
#define MATRIX_COLS 3

すると、 matrix_row_tuint8_t となる
このとき、マトリクスの状態を保持する配列を2進数で表すと次のようになる

[
  00000000,
  00000000,
  00000000,
]

マトリクススキャンを行うと、この各ビットが 1 になったり 0 になったりして、キーの変化を検出するのだ

~33行

ビットを立てるための定数 MATRIX_ROW_SHIFTER を定義している
上記の例の場合、 00000001 となる

~最終行

あとはプロトタイプ宣言である
matrix.c で、各関数について見ていこう

matrix.c

github.com

ソース全体

/*
Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <stdbool.h>
#include "util.h"
#include "matrix.h"
#include "debounce.h"
#include "quantum.h"

#ifdef DIRECT_PINS
static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
#elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
#endif

/* matrix state(1:on, 0:off) */
extern matrix_row_t raw_matrix[MATRIX_ROWS];  // raw values
extern matrix_row_t matrix[MATRIX_ROWS];      // debounced values

static inline void setPinOutput_writeLow(pin_t pin) {
    ATOMIC_BLOCK_FORCEON {
        setPinOutput(pin);
        writePinLow(pin);
    }
}

static inline void setPinInputHigh_atomic(pin_t pin) {
    ATOMIC_BLOCK_FORCEON { setPinInputHigh(pin); }
}

// matrix code

#ifdef DIRECT_PINS

static void init_pins(void) {
    for (int row = 0; row < MATRIX_ROWS; row++) {
        for (int col = 0; col < MATRIX_COLS; col++) {
            pin_t pin = direct_pins[row][col];
            if (pin != NO_PIN) {
                setPinInputHigh(pin);
            }
        }
    }
}

static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
    // Start with a clear matrix row
    matrix_row_t current_row_value = 0;

    for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
        pin_t pin = direct_pins[current_row][col_index];
        if (pin != NO_PIN) {
            current_row_value |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
        }
    }

    // If the row has changed, store the row and return the changed flag.
    if (current_matrix[current_row] != current_row_value) {
        current_matrix[current_row] = current_row_value;
        return true;
    }
    return false;
}

#elif defined(DIODE_DIRECTION)
#    if (DIODE_DIRECTION == COL2ROW)

static void select_row(uint8_t row) { setPinOutput_writeLow(row_pins[row]); }

static void unselect_row(uint8_t row) { setPinInputHigh_atomic(row_pins[row]); }

static void unselect_rows(void) {
    for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
        setPinInputHigh_atomic(row_pins[x]);
    }
}

static void init_pins(void) {
    unselect_rows();
    for (uint8_t x = 0; x < MATRIX_COLS; x++) {
        setPinInputHigh_atomic(col_pins[x]);
    }
}

static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
    // Start with a clear matrix row
    matrix_row_t current_row_value = 0;

    // Select row
    select_row(current_row);
    matrix_output_select_delay();

    // For each col...
    for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
        // Select the col pin to read (active low)
        uint8_t pin_state = readPin(col_pins[col_index]);

        // Populate the matrix row with the state of the col pin
        current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
    }

    // Unselect row
    unselect_row(current_row);
    if (current_row + 1 < MATRIX_ROWS) {
        matrix_output_unselect_delay();  // wait for row signal to go HIGH
    }

    // If the row has changed, store the row and return the changed flag.
    if (current_matrix[current_row] != current_row_value) {
        current_matrix[current_row] = current_row_value;
        return true;
    }
    return false;
}

#    elif (DIODE_DIRECTION == ROW2COL)

static void select_col(uint8_t col) { setPinOutput_writeLow(col_pins[col]); }

static void unselect_col(uint8_t col) { setPinInputHigh_atomic(col_pins[col]); }

static void unselect_cols(void) {
    for (uint8_t x = 0; x < MATRIX_COLS; x++) {
        setPinInputHigh_atomic(col_pins[x]);
    }
}

static void init_pins(void) {
    unselect_cols();
    for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
        setPinInputHigh_atomic(row_pins[x]);
    }
}

static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
    bool matrix_changed = false;

    // Select col
    select_col(current_col);
    matrix_output_select_delay();

    // For each row...
    for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
        // Store last value of row prior to reading
        matrix_row_t last_row_value    = current_matrix[row_index];
        matrix_row_t current_row_value = last_row_value;

        // Check row pin state
        if (readPin(row_pins[row_index]) == 0) {
            // Pin LO, set col bit
            current_row_value |= (MATRIX_ROW_SHIFTER << current_col);
        } else {
            // Pin HI, clear col bit
            current_row_value &= ~(MATRIX_ROW_SHIFTER << current_col);
        }

        // Determine if the matrix changed state
        if ((last_row_value != current_row_value)) {
            matrix_changed |= true;
            current_matrix[row_index] = current_row_value;
        }
    }

    // Unselect col
    unselect_col(current_col);
    if (current_col + 1 < MATRIX_COLS) {
        matrix_output_unselect_delay();  // wait for col signal to go HIGH
    }

    return matrix_changed;
}

#    else
#        error DIODE_DIRECTION must be one of COL2ROW or ROW2COL!
#    endif
#else
#    error DIODE_DIRECTION is not defined!
#endif

void matrix_init(void) {
    // initialize key pins
    init_pins();

    // initialize matrix state: all keys off
    for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
        raw_matrix[i] = 0;
        matrix[i]     = 0;
    }

    debounce_init(MATRIX_ROWS);

    matrix_init_quantum();
}

uint8_t matrix_scan(void) {
    bool changed = false;

#if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
    // Set row, read cols
    for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
        changed |= read_cols_on_row(raw_matrix, current_row);
    }
#elif (DIODE_DIRECTION == ROW2COL)
    // Set col, read rows
    for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
        changed |= read_rows_on_col(raw_matrix, current_col);
    }
#endif

    debounce(raw_matrix, matrix, MATRIX_ROWS, changed);

    matrix_scan_quantum();
    return (uint8_t)changed;
}

QMK に少しだけ踏み込んだ人ならなじみのあるコードだろう

~22行

ここでもいくつか新しいものが include されている

  • util.h
  • debounce.h

とりあえず、これらは後に回すことにしよう

~29行

ここでは、 pin_t という型の配列を作っている
ifdef で判定に使われている DIRECT_PINS, ROW2COL, COL2ROWconfig.h で使われているので意味は分かるだろう

pin_ttmk_core\avr\gpio.h, tmk_core\chibios\gpio.h でそれぞれ定義されている
各 CPU に合わせて GPIO のアドレスなどを格納できるようにしているようだ

~33行

キーマトリックスの変化を格納するための配列をグローバル変数として宣言している

~44行

マトリクススキャンに有用なインライン関数を定義している

static inline void setPinOutput_writeLow(pin_t pin)

入力された pin を Low 出力する関数
ATOMIC_BLOCK_FORCEON で囲まれているのがよくわからないが、割り込みで行うとかそんな感じだろうか

ATOMIC_BLOCK_FORCEON について(2021/04/04 追記)

某所で「atomic block は割り込み禁止区間」という情報を頂いた
つまり、 ATOMIC_BLOCK_FORCEON で囲まれている部分は割り込みされずに実行されるということのようだ
情報の提供に感謝したい

static inline void setPinInputHigh_atomic(pin_t pin)

入力された pin を pull up 入力にする関数

~78行

DIRECT_PINS で実行するマトリクススキャンの関数が実装されている

static void init_pins(void)

すべてのピンを pull up 入力に初期化する

static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)

マトリクススキャンを実行する
1行ずつピンの状態を取得し、前回のスキャン時と変化があれば状態を保存する

先ほどの 3 x 3 マクロパッドで考えよう
初期状態は以下だ

[
  00000000,
  00000000,
  00000000,
]

左上のキーを押した場合、 col_index = 0 がオンである

current_row_value |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);

これにより、 000000010 だけ左シフトされ、元の状態と OR が取られるので以下のようになる

[
  00000001,
  00000000,
  00000000,
]

これを各行について繰り返すのだ

~129行

COL2ROW で実行するマトリクススキャンの関数が実装されている

static void select_row(uint8_t row) { setPinOutput_writeLow(row_pins[row]); }

入力された row のピンを Low 出力にする

static void unselect_row(uint8_t row) { setPinInputHigh_atomic(row_pins[row]); }

入力された row のピンを pull up 入力にする

static void unselect_rows(void)

すべての ROW ピンを pull up 入力にする

static void init_pins(void)

すべての ROW ピンと COL ピンを pull up 入力に初期化する

static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)

マトリクススキャンを実行する DIRECT_PINS で実行しているのとほぼ同じ操作をしている
ただし、 GPIO の変化に時間がかかるため、それを待つようにディレイが挿入されている

~186行

ROW2COL で実行するマトリクススキャンの関数が実装されている

static void select_col(uint8_t col) { setPinOutput_writeLow(col_pins[col]); }

入力された col のピンを Low 出力にする

static void unselect_col(uint8_t col) { setPinInputHigh_atomic(col_pins[col]); }

入力された col のピンを pull up 入力にする

static void init_pins(void)

すべての ROW ピンと COL ピンを pull up 入力に初期化する

static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)

マトリクススキャンを実行する
これまでとほぼ同じ操作を実行している
しかし、 ROW2COL であるため、マトリクスを保持する配列の要素を横断するようにスキャンするため、配列の更新が少し複雑になっている

DIRECT_PINS, COL2ROW では、1行ずつスキャンするので素直

  ------->
[
  00000000,
  00000000,
  00000000,
]

ROW2COL では、1列ずつスキャンするので少し複雑

[
  00000000,  |
  00000000,  |
  00000000,  V
]

~193行

DIRECT_PINS, COL2ROW, ROW2COL が定義されていなかった場合のエラー処理

~最終行

マトリクスの初期化やスキャンを実行する関数が定義されている
DIRECT_PINS, COL2ROW, ROW2COL で共通して呼び出される関数だろう

void matrix_init(void)

マトリクスの結果を保持する配列の初期化処理

uint8_t matrix_scan(void)

マトリクススキャンの実行

matrix_common.c

じつは、 matrix.c と同じ階層に matrix_common.c というファイルがある

github.com

ソース全体

#include "quantum.h"
#include "matrix.h"
#include "debounce.h"
#include "wait.h"
#include "print.h"
#include "debug.h"

#ifndef MATRIX_IO_DELAY
#    define MATRIX_IO_DELAY 30
#endif

/* matrix state(1:on, 0:off) */
matrix_row_t raw_matrix[MATRIX_ROWS];
matrix_row_t matrix[MATRIX_ROWS];

#ifdef MATRIX_MASKED
extern const matrix_row_t matrix_mask[];
#endif

// user-defined overridable functions

__attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }

__attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }

__attribute__((weak)) void matrix_init_user(void) {}

__attribute__((weak)) void matrix_scan_user(void) {}

// helper functions

inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }

inline uint8_t matrix_cols(void) { return MATRIX_COLS; }

inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }

inline matrix_row_t matrix_get_row(uint8_t row) {
    // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
    // switch blocker installed and the switch is always pressed.
#ifdef MATRIX_MASKED
    return matrix[row] & matrix_mask[row];
#else
    return matrix[row];
#endif
}

// Deprecated.
bool matrix_is_modified(void) {
    if (debounce_active()) return false;
    return true;
}

#if (MATRIX_COLS <= 8)
#    define print_matrix_header() print("\nr/c 01234567\n")
#    define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
#    define matrix_bitpop(row) bitpop(matrix_get_row(row))
#elif (MATRIX_COLS <= 16)
#    define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
#    define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
#    define matrix_bitpop(row) bitpop16(matrix_get_row(row))
#elif (MATRIX_COLS <= 32)
#    define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
#    define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
#    define matrix_bitpop(row) bitpop32(matrix_get_row(row))
#endif

void matrix_print(void) {
    print_matrix_header();

    for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
        print_hex8(row);
        print(": ");
        print_matrix_row(row);
        print("\n");
    }
}

uint8_t matrix_key_count(void) {
    uint8_t count = 0;
    for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
        count += matrix_bitpop(i);
    }
    return count;
}

/* `matrix_io_delay ()` exists for backwards compatibility. From now on, use matrix_output_unselect_delay(). */
__attribute__((weak)) void matrix_io_delay(void) { wait_us(MATRIX_IO_DELAY); }

__attribute__((weak)) void matrix_output_select_delay(void) { waitInputPinDelay(); }
__attribute__((weak)) void matrix_output_unselect_delay(void) { matrix_io_delay(); }

// CUSTOM MATRIX 'LITE'
__attribute__((weak)) void matrix_init_custom(void) {}

__attribute__((weak)) bool matrix_scan_custom(matrix_row_t current_matrix[]) { return true; }

__attribute__((weak)) void matrix_init(void) {
    matrix_init_custom();

    // initialize matrix state: all keys off
    for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
        raw_matrix[i] = 0;
        matrix[i]     = 0;
    }

    debounce_init(MATRIX_ROWS);

    matrix_init_quantum();
}

__attribute__((weak)) uint8_t matrix_scan(void) {
    bool changed = matrix_scan_custom(raw_matrix);

    debounce(raw_matrix, matrix, MATRIX_ROWS, changed);

    matrix_scan_quantum();
    return changed;
}

__attribute__((weak)) bool peek_matrix(uint8_t row_index, uint8_t col_index, bool raw) { return 0 != ((raw ? raw_matrix[row_index] : matrix[row_index]) & (MATRIX_ROW_SHIFTER << col_index)); }

matrix.c のひな型のようなものと言えそうだ
実際、 matrix.c でも使用されている matrix_io_delay(void), matrix_output_select_delay(void), matrix_output_unselect_delay(void) などが定義されている
独自の my_matrix.c のようなものを作るときに役立つと思われる

それでは、後回しにしていた include されていたものについて見ていこう

util.h

github.com

ソース全体

/*
Copyright 2011 Jun Wako <wakojun@gmail.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once

#include "bitwise.h"

// convert to L string
#define LSTR(s) XLSTR(s)
#define XLSTR(s) L## #s
// convert to string
#define STR(s) XSTR(s)
#define XSTR(s) #s

はっきり言って、よくわからない
matrix.c でも使われているような形跡は見られない
デバッグなどで使ったりするのだろうか

debounce.h

github.com

ソース全体

#pragma once

// raw is the current key state
// on entry cooked is the previous debounced state
// on exit cooked is the current debounced state
// changed is true if raw has changed since the last call
void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed);

bool debounce_active(void);

void debounce_init(uint8_t num_rows);

プロトタイプ宣言のみだ

実体はここにあるようである

github.com

あまり深入りしないが、チャタリングを軽減するためのコードが実装されているはずである

docs.qmk.fm

実装されているアルゴリズムについてはここが詳しい

まとめ

今回は wait.h, matrix.h を探検した
少しだけ QMK の理解が深まった気がする

次回予告

次回は keymap.h から見ていこうと思う