]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/massdrop/alt/matrix.c
[Keyboard] fixed pins for numpad_5x4 layout (#6311)
[qmk_firmware.git] / keyboards / massdrop / alt / matrix.c
1 /*
2 Copyright 2018 Massdrop Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "alt.h"
19
20 #include "d51_util.h"
21 #include "debug.h"
22 #include "clks.h"
23 #include <string.h>
24
25 matrix_row_t mlatest[MATRIX_ROWS];
26 matrix_row_t mlast[MATRIX_ROWS];
27 matrix_row_t mdebounced[MATRIX_ROWS];
28
29 uint8_t row_ports[] = { MATRIX_ROW_PORTS };
30 uint8_t row_pins[] = { MATRIX_ROW_PINS };
31 uint8_t col_ports[] = { MATRIX_COL_PORTS };
32 uint8_t col_pins[] = { MATRIX_COL_PINS };
33 uint32_t row_masks[2]; //NOTE: If more than PA PB used in the future, adjust code to accomodate
34
35 __attribute__ ((weak))
36 void matrix_init_kb(void) {
37     matrix_init_user();
38 }
39
40 __attribute__ ((weak))
41 void matrix_scan_kb(void) {
42     matrix_scan_user();
43 }
44
45 __attribute__ ((weak))
46 void matrix_init_user(void) {
47 }
48
49 __attribute__ ((weak))
50 void matrix_scan_user(void) {
51 }
52
53 void matrix_init(void)
54 {
55     memset(mlatest, 0, MATRIX_ROWS * sizeof(matrix_row_t));
56     memset(mlast, 0, MATRIX_ROWS * sizeof(matrix_row_t));
57     memset(mdebounced, 0, MATRIX_ROWS * sizeof(matrix_row_t));
58
59     row_masks[PA] = 0;
60     row_masks[PB] = 0;
61
62     uint8_t row;
63     for (row = 0; row < MATRIX_ROWS; row++)
64     {
65         PORT->Group[row_ports[row]].DIRCLR.reg = 1 << row_pins[row]; //Input
66         PORT->Group[row_ports[row]].OUTCLR.reg = 1 << row_pins[row]; //Low
67         PORT->Group[row_ports[row]].PINCFG[row_pins[row]].bit.INEN = 1; //Input Enable,
68         PORT->Group[row_ports[row]].PINCFG[row_pins[row]].bit.PULLEN = 1; //Pull Enable
69         row_masks[row_ports[row]] |= 1 << row_pins[row]; //Add pin to proper row mask
70     }
71
72     uint8_t col;
73     for (col = 0; col < MATRIX_COLS; col++)
74     {
75         PORT->Group[col_ports[col]].DIRSET.reg = 1 << col_pins[col]; //Output
76         PORT->Group[col_ports[col]].OUTCLR.reg = 1 << col_pins[col]; //Low
77     }
78
79     matrix_init_quantum();
80 }
81
82 uint64_t mdebouncing = 0;
83 uint8_t matrix_scan(void)
84 {
85     uint8_t mchanged;
86     uint8_t row;
87     uint8_t col;
88     uint32_t scans[2]; //PA PB
89
90     if (timer_read64() < mdebouncing) return 1; //mdebouncing == 0 when no debouncing active
91
92     memset(mlatest, 0, MATRIX_ROWS * sizeof(matrix_row_t)); //Zero the result buffer
93
94     for (col = 0; col < MATRIX_COLS; col++)
95     {
96         PORT->Group[col_ports[col]].OUTSET.reg = 1 << col_pins[col]; //Set col output
97
98         wait_us(1); //Delay for output
99
100         scans[PA] = PORT->Group[PA].IN.reg & row_masks[PA]; //Read PA row pins data
101         scans[PB] = PORT->Group[PB].IN.reg & row_masks[PB]; //Read PB row pins data
102
103         PORT->Group[col_ports[col]].OUTCLR.reg = 1 << col_pins[col]; //Clear col output
104
105         for (row = 0; row < MATRIX_ROWS; row++)
106         {
107             //Move scan bits from scans array into proper row bit locations
108             if (scans[row_ports[row]] & (1 << row_pins[row]))
109                 mlatest[row] |= 1 << col;
110         }
111     }
112
113     mchanged = 0; //Default to no matrix change since last
114
115     for (row = 0; row < MATRIX_ROWS; row++)
116     {
117         if (mlast[row] != mlatest[row])
118             mchanged = 1;
119         mlast[row] = mlatest[row];
120     }
121
122     if (!mchanged)
123     {
124         for (row = 0; row < MATRIX_ROWS; row++)
125             mdebounced[row] = mlatest[row];
126         mdebouncing = 0;
127     }
128     else
129     {
130         //Begin or extend debounce on change
131         mdebouncing = timer_read64() + DEBOUNCE;
132     }
133
134     matrix_scan_quantum();
135
136     return 1;
137 }
138
139 matrix_row_t matrix_get_row(uint8_t row)
140 {
141     return mdebounced[row];
142 }
143
144 void matrix_print(void)
145 {
146     char buf[(MATRIX_COLS+8)*(MATRIX_ROWS+1)] = "R C";
147     char *pbuf = buf+3;
148     uint32_t cols;
149     uint32_t rows;
150     matrix_row_t row;
151
152     for (cols = 1; cols <= MATRIX_COLS; cols++)
153     {
154         *pbuf = (cols%10)+48;
155         pbuf++;
156     }
157     *pbuf = '\r'; pbuf++;
158     *pbuf = '\n'; pbuf++;
159
160     for (rows = 1; rows <= MATRIX_ROWS; rows++)
161     {
162         row = matrix_get_row(rows-1);
163         if (rows < 10) { *pbuf = rows+48; pbuf++; *pbuf = ' '; pbuf++; *pbuf = ' '; pbuf++; }
164         else { *pbuf = (rows/10)+48; pbuf++; *pbuf = (rows%10)+48; pbuf++; *pbuf = ' '; pbuf++; }
165         for (cols = 0; cols < MATRIX_COLS; cols++)
166         {
167             if (row & 1 << cols) *pbuf = 'X';
168             else                 *pbuf = '.';
169             pbuf++;
170         }
171         *pbuf = '\r'; pbuf++;
172         *pbuf = '\n'; pbuf++;
173     }
174     *pbuf = 0;
175     dprint(buf);
176 }