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