You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.6 KiB

  1. # Custom Matrix
  2. QMK provides a mechanism to supplement or replace the default matrix scanning routine with your own code.
  3. The reasons to use this feature include:
  4. * Extra hardware between the keyboard's switches and MCU pins
  5. * I/O multiplexer
  6. * Line decoder
  7. * Irregular switch matrix
  8. * Simultaneous use of `COL2ROW` and `ROW2COL`
  9. ## Prerequisites
  10. Implementing custom matrix usually involves compilation of an additional source file. It is recommended that for consistency, this file is called `matrix.c`.
  11. Add a new file to your keyboard directory:
  12. ```
  13. keyboards/<keyboard>/matrix.c
  14. ```
  15. And to configure compilation for the new file, add this to your `rules.mk`:
  16. ```make
  17. SRC += matrix.c
  18. ```
  19. ## 'lite'
  20. Provides a default implementation for various scanning functions, reducing the boilerplate code when implementing custom matrix.
  21. To configure it, add this to your `rules.mk`:
  22. ```make
  23. CUSTOM_MATRIX = lite
  24. ```
  25. And implement the following functions in a `matrix.c` file in your keyboard folder:
  26. ```c
  27. void matrix_init_custom(void) {
  28. // TODO: initialize hardware here
  29. }
  30. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  31. bool matrix_has_changed = false;
  32. // TODO: add matrix scanning routine here
  33. return matrix_has_changed;
  34. }
  35. ```
  36. ## Full Replacement
  37. When more control over the scanning routine is required, you can choose to implement the full scanning routine.
  38. To configure it, add this to your rules.mk:
  39. ```make
  40. CUSTOM_MATRIX = yes
  41. ```
  42. And implement the following functions in a `matrix.c` file in your keyboard folder:
  43. ```c
  44. matrix_row_t matrix_get_row(uint8_t row) {
  45. // TODO: return the requested row data
  46. }
  47. void matrix_print(void) {
  48. // TODO: use print() to dump the current matrix state to console
  49. }
  50. void matrix_init(void) {
  51. // TODO: initialize hardware and global matrix state here
  52. // Unless hardware debouncing - Init the configured debounce routine
  53. debounce_init(MATRIX_ROWS);
  54. // This *must* be called for correct keyboard behavior
  55. matrix_init_kb();
  56. }
  57. uint8_t matrix_scan(void) {
  58. bool changed = false;
  59. // TODO: add matrix scanning routine here
  60. // Unless hardware debouncing - use the configured debounce routine
  61. changed = debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
  62. // This *must* be called for correct keyboard behavior
  63. matrix_scan_kb();
  64. return changed;
  65. }
  66. ```
  67. And also provide defaults for the following callbacks:
  68. ```c
  69. __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
  70. __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
  71. __attribute__((weak)) void matrix_init_user(void) {}
  72. __attribute__((weak)) void matrix_scan_user(void) {}
  73. ```