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