Commit | Line | Data |
---|---|---|
dcb7ca3f JC |
1 | #include "matrix.h" |
2 | #include "debounce.h" | |
3 | #include "print.h" | |
4 | #include "debug.h" | |
5 | ||
48cac9e3 JC |
6 | /* matrix state(1:on, 0:off) */ |
7 | matrix_row_t raw_matrix[MATRIX_ROWS]; | |
8 | matrix_row_t matrix[MATRIX_ROWS]; | |
9 | ||
10 | #ifdef MATRIX_MASKED | |
11 | extern const matrix_row_t matrix_mask[]; | |
12 | #endif | |
13 | ||
dcb7ca3f JC |
14 | // user-defined overridable functions |
15 | ||
16 | __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); } | |
17 | ||
18 | __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); } | |
19 | ||
20 | __attribute__((weak)) void matrix_init_user(void) {} | |
21 | ||
22 | __attribute__((weak)) void matrix_scan_user(void) {} | |
23 | ||
24 | // helper functions | |
25 | ||
26 | inline uint8_t matrix_rows(void) { return MATRIX_ROWS; } | |
27 | ||
28 | inline uint8_t matrix_cols(void) { return MATRIX_COLS; } | |
29 | ||
48cac9e3 JC |
30 | inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); } |
31 | ||
32 | inline matrix_row_t matrix_get_row(uint8_t row) { | |
33 | // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a | |
34 | // switch blocker installed and the switch is always pressed. | |
35 | #ifdef MATRIX_MASKED | |
36 | return matrix[row] & matrix_mask[row]; | |
37 | #else | |
38 | return matrix[row]; | |
39 | #endif | |
40 | } | |
41 | ||
dcb7ca3f JC |
42 | // Deprecated. |
43 | bool matrix_is_modified(void) { | |
44 | if (debounce_active()) return false; | |
45 | return true; | |
46 | } | |
47 | ||
48 | #if (MATRIX_COLS <= 8) | |
49 | # define print_matrix_header() print("\nr/c 01234567\n") | |
50 | # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) | |
51 | # define matrix_bitpop(row) bitpop(matrix_get_row(row)) | |
52 | #elif (MATRIX_COLS <= 16) | |
53 | # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n") | |
54 | # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row)) | |
55 | # define matrix_bitpop(row) bitpop16(matrix_get_row(row)) | |
56 | #elif (MATRIX_COLS <= 32) | |
57 | # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n") | |
58 | # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row)) | |
59 | # define matrix_bitpop(row) bitpop32(matrix_get_row(row)) | |
60 | #endif | |
61 | ||
62 | void matrix_print(void) { | |
63 | print_matrix_header(); | |
64 | ||
65 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | |
66 | phex(row); | |
67 | print(": "); | |
68 | print_matrix_row(row); | |
69 | print("\n"); | |
70 | } | |
71 | } | |
72 | ||
73 | uint8_t matrix_key_count(void) { | |
74 | uint8_t count = 0; | |
75 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | |
76 | count += matrix_bitpop(i); | |
77 | } | |
78 | return count; | |
79 | } | |
48cac9e3 JC |
80 | |
81 | // CUSTOM MATRIX 'LITE' | |
82 | __attribute__((weak)) void matrix_init_custom(void) {} | |
83 | ||
84 | __attribute__((weak)) bool matrix_scan_custom(matrix_row_t current_matrix[]) { return true; } | |
85 | ||
86 | __attribute__((weak)) void matrix_init(void) { | |
87 | matrix_init_custom(); | |
88 | ||
89 | // initialize matrix state: all keys off | |
90 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | |
91 | raw_matrix[i] = 0; | |
92 | matrix[i] = 0; | |
93 | } | |
94 | ||
95 | debounce_init(MATRIX_ROWS); | |
96 | ||
97 | matrix_init_quantum(); | |
98 | } | |
99 | ||
100 | __attribute__((weak)) uint8_t matrix_scan(void) { | |
101 | bool changed = matrix_scan_custom(raw_matrix); | |
102 | ||
103 | debounce(raw_matrix, matrix, MATRIX_ROWS, changed); | |
104 | ||
105 | matrix_scan_quantum(); | |
2081c5e4 | 106 | return changed; |
48cac9e3 | 107 | } |