]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboard/nerd/matrix.c
f0ffe772dacc96a616c3f6213ba2affd123cb4c2
[qmk_firmware.git] / keyboard / nerd / matrix.c
1 /*
2 Copyright 2014 Ralf Schmitt <ralf@bunkertor.net>
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 <stdint.h>
19 #include <stdbool.h>
20 #include <avr/io.h>
21 #include <util/delay.h>
22 #include "print.h"
23 #include "debug.h"
24 #include "util.h"
25 #include "matrix.h"
26 #include "backlight.h"
27
28
29 #ifndef DEBOUNCE
30 #   define DEBOUNCE 5
31 #endif
32 static uint8_t debouncing = DEBOUNCE;
33
34 /* matrix state(1:on, 0:off) */
35 static matrix_row_t matrix[MATRIX_ROWS];
36 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
37
38 static uint16_t read_inputs(void);
39 static void init_inputs(void);
40 static void init_outputs(void);
41 static void reset_inputs(void);
42 static void reset_outputs(void);
43 static void select_output(uint8_t col);
44
45 inline
46 uint8_t matrix_rows(void)
47 {
48     return MATRIX_ROWS;
49 }
50
51 inline
52 uint8_t matrix_cols(void)
53 {
54     return MATRIX_COLS;
55 }
56
57 void matrix_init(void)
58 {
59     backlight_init_ports();
60     init_inputs();
61     init_outputs();
62
63     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
64         matrix[i] = 0;
65         matrix_debouncing[i] = 0;
66     }
67 }
68
69 uint8_t matrix_scan(void)
70 {
71     for (uint8_t col = 0; col < MATRIX_COLS; col++) {
72         reset_inputs();
73         reset_outputs();
74         select_output(col);
75         _delay_us(3);
76         uint16_t rows = read_inputs();
77         for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
78             bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
79             bool curr_bit = rows & (1<<row);
80             if (prev_bit != curr_bit) {
81                 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
82                 if (debouncing) {
83                     dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
84                 }
85                 debouncing = DEBOUNCE;
86             }
87         }
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 static void init_inputs(void)
141 {
142     DDRE &= ~0b01000000; // PE6 (Col 0)
143     DDRB &= ~0b00001111; // PB0 (Col 1), PB1 (Col 2), PB2 (Col 3), PB3 (Col 4)
144     DDRF &= ~0b00000001; // PF0 (Col 5)
145     DDRD &= ~0b00100001; // PD0 (Col 6), PD5 (Col 7)
146 }
147
148 static uint16_t read_inputs(void)
149 {
150     return (PINE&(1<<6) ? 0 : (1<<0)) |  // PE6 (Row 0)
151            (PINB&(1<<0) ? 0 : (1<<1)) |  // PB0 (Row 1)
152            (PINB&(1<<1) ? 0 : (1<<2)) |  // PB1 (Row 2)
153            (PINB&(1<<2) ? 0 : (1<<3)) |  // PB2 (Row 3)
154            (PINB&(1<<3) ? 0 : (1<<4)) |  // PB3 (Row 4)
155            (PINF&(1<<0) ? 0 : (1<<5)) |  // PF0 (Row 5)
156            (PIND&(1<<0) ? 0 : (1<<6)) |  // PD0 (Row 6)
157            (PIND&(1<<5) ? 0 : (1<<7));   // PD5 (Row 7)
158 }
159
160 static void reset_inputs(void)
161 {
162     PORTE |= 0b01000000; // PE6 (Col 0)
163     PORTB |= 0b00001111; // PB0 (Col 1), PB1 (Col 2), PB2 (Col 3), PB3 (Col 4)
164     PORTF |= 0b00000001; // PF0 (Col 5)
165     PORTD |= 0b00100001; // PD0 (Col 6), PD5 (Col 7)
166 }
167
168 static void init_outputs(void)
169 {
170     DDRB |= 0b00010000; // PB4 (Row 0)
171     DDRE |= 0b00000100; // PE2 (Row 1)
172     DDRF |= 0b11110010; // PF4 (Row 2), PF7 (Row 3), PF1 (Row 4), PF6 (Row 5), PF5 (Row 7)
173     DDRC |= 0b11000000; // PC6 (Row 6), PC7 (Row 9)
174     DDRD |= 0b10000000; // PD7 (Row 8)
175 }
176
177 static void reset_outputs(void)
178 {
179     PORTB |= 0b00010000; // PB4 (Row 0)
180     PORTE |= 0b00000100; // PE2 (Row 1)
181     PORTF |= 0b11110010; // PF4 (Row 2), PF7 (Row 3), PF1 (Row 4), PF6 (Row 5), PF5 (Row 7)
182     PORTC |= 0b11000000; // PC6 (Row 6), PC7 (Row 9)
183     PORTD |= 0b10000000; // PD7 (Row 8)
184 }
185
186 static void select_output(uint8_t col)
187 {
188     switch (col) {
189         case 0:
190             PORTB &= ~(1<<4);
191             break;
192         case 1:
193             PORTE &= ~(1<<2);
194             break;
195         case 2:
196             PORTF &= ~(1<<4);
197             break;
198         case 3:
199             PORTF &= ~(1<<7);
200             break;
201         case 4:
202             PORTF &= ~(1<<1);
203             break;
204         case 5:
205             PORTF &= ~(1<<6);
206             break;
207         case 6:
208             PORTC &= ~(1<<6);
209             break;
210         case 7:
211             PORTF &= ~(1<<5);
212             break;
213         case 8:
214             PORTD &= ~(1<<7);
215             break;
216         case 9:
217             PORTC &= ~(1<<7);
218             break;
219     }
220 }