Merge remote-tracking branch 'upstream/master'
[jackhill/qmk/firmware.git] / keyboards / xd84 / custom_matrix_helper.c
1 /* Copyright 2019
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16 #include <stdint.h>
17 #include <stdbool.h>
18 #include "wait.h"
19 #include "print.h"
20 #include "debug.h"
21 #include "util.h"
22 #include "matrix.h"
23 #include "debounce.h"
24 #include "quantum.h"
25
26 //_____COMMON__________________________________________________________________
27 // user-defined overridable functions
28 __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
29 __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
30 __attribute__((weak)) void matrix_init_user(void) {}
31 __attribute__((weak)) void matrix_scan_user(void) {}
32
33
34 //_____COULD BE COMMON_________________________________________________________
35 /* matrix state(1:on, 0:off) */
36 /*static*/ matrix_row_t raw_matrix[MATRIX_ROWS];
37 /*static*/ matrix_row_t matrix[MATRIX_ROWS];
38
39 #if (MATRIX_COLS <= 8)
40 # define print_matrix_header() print("\nr/c 01234567\n")
41 # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
42 # define matrix_bitpop(i) bitpop(matrix[i])
43 # define ROW_SHIFTER ((uint8_t)1)
44 #elif (MATRIX_COLS <= 16)
45 # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
46 # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
47 # define matrix_bitpop(i) bitpop16(matrix[i])
48 # define ROW_SHIFTER ((uint16_t)1)
49 #elif (MATRIX_COLS <= 32)
50 # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
51 # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
52 # define matrix_bitpop(i) bitpop32(matrix[i])
53 # define ROW_SHIFTER ((uint32_t)1)
54 #endif
55
56 __attribute__ ((weak))
57 uint8_t matrix_rows(void) {
58 return MATRIX_ROWS;
59 }
60
61 __attribute__ ((weak))
62 uint8_t matrix_cols(void) {
63 return MATRIX_COLS;
64 }
65
66 __attribute__ ((weak))
67 matrix_row_t matrix_get_row(uint8_t row) {
68 return matrix[row];
69 }
70
71 __attribute__ ((weak))
72 uint8_t matrix_key_count(void) {
73 uint8_t count = 0;
74 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
75 count += matrix_bitpop(i);
76 }
77 return count;
78 }
79
80 __attribute__ ((weak))
81 void matrix_print(void) {
82 print_matrix_header();
83
84 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
85 phex(row); print(": ");
86 print_matrix_row(row);
87 print("\n");
88 }
89 }
90
91
92 //_____CUSTOM MATRIX 'LITE'____________________________________________________
93 __attribute__ ((weak))
94 void custom_matrix_init(void) {
95 }
96
97 __attribute__ ((weak))
98 bool custom_matrix_scan(matrix_row_t current_matrix[]) {
99 bool changed = true;
100 return changed;
101 }
102
103 __attribute__ ((weak))
104 void matrix_init(void) {
105
106 custom_matrix_init();
107
108 // initialize matrix state: all keys off
109 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
110 raw_matrix[i] = 0;
111 matrix[i] = 0;
112 }
113
114 debounce_init(MATRIX_ROWS);
115
116 matrix_init_quantum();
117 }
118
119 __attribute__ ((weak))
120 uint8_t matrix_scan(void) {
121 bool changed = custom_matrix_scan(raw_matrix);
122
123 debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
124
125 matrix_scan_quantum();
126 return 1;
127 }