aboutsummaryrefslogtreecommitdiff
path: root/keyboards/nullbitsco/nibble/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/nullbitsco/nibble/matrix.c')
-rw-r--r--keyboards/nullbitsco/nibble/matrix.c95
1 files changed, 95 insertions, 0 deletions
diff --git a/keyboards/nullbitsco/nibble/matrix.c b/keyboards/nullbitsco/nibble/matrix.c
new file mode 100644
index 000000000..8ec9da284
--- /dev/null
+++ b/keyboards/nullbitsco/nibble/matrix.c
@@ -0,0 +1,95 @@
1/* Copyright 2020 Jay Greco
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 "quantum.h"
17
18#define COL_SHIFTER ((uint32_t)1)
19
20// Column pins
21static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
22static const uint8_t col_pins[MATRIX_MUX_COLS] = MATRIX_COL_MUX_PINS;
23
24// Internal functions
25
26static void init_pins(void) {
27 // Set cols to outputs, low
28 for (uint8_t pin = 0; pin < MATRIX_MUX_COLS; pin++) {
29 setPinOutput(col_pins[pin]);
30 }
31
32 // Unselect cols
33 for (uint8_t bit = 0; bit < MATRIX_MUX_COLS; bit++) {
34 writePinLow(col_pins[bit]);
35 }
36
37 // Set rows to input, pullup
38 for (uint8_t pin = 0; pin < MATRIX_ROWS; pin++) {
39 setPinInputHigh(row_pins[pin]);
40 }
41}
42
43static void select_col(uint8_t col)
44{
45 for (uint8_t bit = 0; bit < MATRIX_MUX_COLS; bit++) {
46 uint8_t state = (col & (0b1 << bit)) >> bit;
47 writePin(col_pins[bit], state);
48 }
49}
50
51static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
52{
53 bool matrix_changed = false;
54 select_col(current_col);
55 wait_us(5);
56
57 // Read each row sequentially
58 for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
59 {
60 matrix_row_t last_row_value = current_matrix[row_index];
61
62 if (!readPin(row_pins[row_index]))
63 {
64 current_matrix[row_index] |= (COL_SHIFTER << current_col);
65 }
66 else
67 {
68 current_matrix[row_index] &= ~(COL_SHIFTER << current_col);
69 }
70
71 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
72 {
73 matrix_changed = true;
74 }
75 }
76
77 return matrix_changed;
78}
79
80// Matrix scan functions
81
82void matrix_init_custom(void) {
83 init_pins();
84}
85
86bool matrix_scan_custom(matrix_row_t current_matrix[]) {
87 bool changed = false;
88
89 //Set col, read rows
90 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
91 changed |= read_rows_on_col(current_matrix, current_col);
92 }
93
94 return (uint8_t)changed;
95} \ No newline at end of file