]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/ut47/matrix.c
18d420dbbb181853a41c55d2028e9fac9411f35d
[qmk_firmware.git] / keyboards / ut47 / matrix.c
1 /*
2 Copyright 2018 Carlos Filoteo
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 /*
19  * scan matrix
20  */
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <avr/io.h>
24 #include <util/delay.h>
25 #include "print.h"
26 #include "debug.h"
27 #include "util.h"
28 #include "matrix.h"
29 #include "protocol/serial.h"
30
31
32 #ifndef DEBOUNCE
33 #   define DEBOUNCE     5
34 #endif
35 static uint8_t debouncing = DEBOUNCE;
36
37 /* matrix state(1:on, 0:off) */
38 static matrix_row_t matrix[MATRIX_ROWS];
39 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
40
41 static matrix_row_t read_cols(void);
42 static void init_cols(void);
43 static void unselect_rows(void);
44 static void select_row(uint8_t row);
45
46
47 inline
48 uint8_t matrix_rows(void)
49 {
50     return MATRIX_ROWS;
51 }
52
53 inline
54 uint8_t matrix_cols(void)
55 {
56     return MATRIX_COLS;
57 }
58
59 void matrix_init(void)
60 {
61     // initialize row and col
62     unselect_rows();
63     init_cols();
64
65     // initialize matrix state: all keys off
66     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
67         matrix[i] = 0;
68         matrix_debouncing[i] = 0;
69     }
70
71     serial_init();
72 }
73
74 uint8_t matrix_scan(void)
75 {
76     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
77         select_row(i);
78         _delay_us(30);  // without this wait read unstable value.
79         matrix_row_t cols = read_cols();
80         if (matrix_debouncing[i] != cols) {
81             matrix_debouncing[i] = cols;
82             if (debouncing) {
83                 debug("bounce!: "); debug_hex(debouncing); debug("\n");
84             }
85             debouncing = DEBOUNCE;
86         }
87         unselect_rows();
88     }
89
90     if (debouncing) {
91         if (--debouncing) {
92             _delay_ms(1);
93         } else {
94             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
95                 matrix[i] = matrix_debouncing[i];
96             }
97         }
98     }
99
100     return 1;
101 }
102
103 bool matrix_is_modified(void)
104 {
105     if (debouncing) return false;
106     return true;
107 }
108
109 inline
110 bool matrix_is_on(uint8_t row, uint8_t col)
111 {
112     return (matrix[row] & ((matrix_row_t)1<<col));
113 }
114
115 inline
116 matrix_row_t matrix_get_row(uint8_t row)
117 {
118     return matrix[row];
119 }
120
121 void matrix_print(void)
122 {
123     print("\nr/c 0123456789ABCDEF\n");
124     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
125         phex(row); print(": ");
126         pbin_reverse16(matrix_get_row(row));
127         print("\n");
128     }
129 }
130
131 uint8_t matrix_key_count(void)
132 {
133     uint8_t count = 0;
134     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
135         count += bitpop16(matrix[i]);
136     }
137     return count;
138 }
139
140 /* Column pin configuration
141  * col: 0   1   2   3   4   5   6   7   8   9   10  11
142  * pin: D7  E6  B4  B5  B6  B2  B3  B1  F7  F6  F5  F4
143  */
144
145 static void  init_cols(void)
146 {
147     // Input with pull-up(DDR:0, PORT:1)
148     DDRF  &= ~(1<<4 | 1<<5 | 1<<6 | 1<<7);
149     PORTF |=  (1<<4 | 1<<5 | 1<<6 | 1<<7);
150     DDRE  &= ~(1<<6);
151     PORTE |=  (1<<6);
152     DDRD  &= ~(1<<7);
153     PORTD |=  (1<<7);
154     DDRB  &= ~(1<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6);
155     PORTB |=  (1<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6);
156 }
157
158 static matrix_row_t read_cols(void)
159 {
160     return (PIND&(1<<7) ? 0 : (1<<0)) |
161            (PINE&(1<<6) ? 0 : (1<<1)) |
162            (PINB&(1<<4) ? 0 : (1<<2)) |
163            (PINB&(1<<5) ? 0 : (1<<3)) |
164            (PINB&(1<<6) ? 0 : (1<<4)) |
165            (PINB&(1<<2) ? 0 : (1<<5)) |
166            (PINB&(1<<3) ? 0 : (1<<6)) |
167            (PINB&(1<<1) ? 0 : (1<<7)) |
168            (PINF&(1<<7) ? 0 : (1<<8)) |
169            (PINF&(1<<6) ? 0 : (1<<9)) |
170            (PINF&(1<<5) ? 0 : (1<<10)) |
171            (PINF&(1<<4) ? 0 : (1<<11));
172 }
173
174 /* Row pin configuration
175  * row: 0   1   2   3
176  * pin: D1  D0  D4  C6
177  */
178
179 static void unselect_rows(void)
180 {
181     // Hi-Z(DDR:0, PORT:0) to unselect
182     DDRD  &= ~0b00010011;
183     PORTD &= ~0b00010011;
184     DDRC  &= ~0b01000000;
185     PORTC &= ~0b01000000;
186 }
187
188 static void select_row(uint8_t row)
189 {
190     // Output low(DDR:1, PORT:0) to select
191     switch (row) {
192         case 0:
193             DDRD  |= (1<<1);
194             PORTD &= ~(1<<1);
195             break;
196         case 1:
197             DDRD  |= (1<<0);
198             PORTD &= ~(1<<0);
199             break;
200         case 2:
201             DDRD  |= (1<<4);
202             PORTD &= ~(1<<4);
203             break;
204         case 3:
205             DDRC  |= (1<<6);
206             PORTC &= ~(1<<6);
207             break;
208     }
209 }