]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboard/planck/matrix.c
added planck folder
[qmk_firmware.git] / keyboard / planck / matrix.c
1 /*
2 Copyright 2012 Jun Wako <wakojun@gmail.com>
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 "action_layer.h"
26 #include "print.h"
27 #include "debug.h"
28 #include "util.h"
29 #include "matrix.h"
30
31
32 #ifndef DEBOUNCE
33 #   define DEBOUNCE     10
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
72 uint8_t matrix_scan(void)
73 {
74     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
75         select_row(i);
76         _delay_us(30);  // without this wait read unstable value.
77         matrix_row_t cols = read_cols();
78         if (matrix_debouncing[i] != cols) {
79             matrix_debouncing[i] = cols;
80             if (debouncing) {
81                 debug("bounce!: "); debug_hex(debouncing); debug("\n");
82             }
83             debouncing = DEBOUNCE;
84         }
85         unselect_rows();
86     }
87
88     if (debouncing) {
89         if (--debouncing) {
90             _delay_ms(1);
91         } else {
92             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
93                 matrix[i] = matrix_debouncing[i];
94             }
95         }
96     }
97
98     uint8_t layer = biton32(layer_state);
99     switch (layer) {
100         case 1:
101         case 2:
102             DDRC |= (1<<7);
103             PORTC |= (1<<7);
104             break;
105         case 0:
106             DDRC &= ~(1<<7);
107             PORTC &= ~(1<<7);
108             break;
109     }
110
111     return 1;
112 }
113
114 bool matrix_is_modified(void)
115 {
116     if (debouncing) return false;
117     return true;
118 }
119
120 inline
121 bool matrix_is_on(uint8_t row, uint8_t col)
122 {
123     return (matrix[row] & ((matrix_row_t)1<<col));
124 }
125
126 inline
127 matrix_row_t matrix_get_row(uint8_t row)
128 {
129     return matrix[row];
130 }
131
132 void matrix_print(void)
133 {
134     print("\nr/c 0123456789ABCDEF\n");
135     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
136         phex(row); print(": ");
137         pbin_reverse16(matrix_get_row(row));
138         print("\n");
139     }
140 }
141
142 uint8_t matrix_key_count(void)
143 {
144     uint8_t count = 0;
145     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
146         count += bitpop16(matrix[i]);
147     }
148     return count;
149 }
150
151 /* Column pin configuration
152  * col: 0  1  2  3  4  5  6  7  8  9  10 11
153  * pin: F0 F1 F4 F5 F6 F7 B6 B5 B4 D7 D5 D4
154  */
155
156 static void init_cols(void)
157 {
158     DDRF  &= ~(1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
159     PORTF |=  (1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
160     DDRD  &= ~(1<<0);
161     PORTD |=  (1<<0);
162     DDRB  &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<7);
163     PORTB |=  (1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<7);
164 }
165
166 static matrix_row_t read_cols(void)
167 {
168     return (PINB&(1<<0) ? 0 : (1<< 0)) |
169            (PINB&(1<<1) ? 0 : (1<< 1)) |
170            (PINB&(1<<2) ? 0 : (1<< 2)) |
171            (PINB&(1<<3) ? 0 : (1<< 3)) |
172            (PINB&(1<<7) ? 0 : (1<< 4)) |
173            (PIND&(1<<0) ? 0 : (1<< 5)) |
174            (PINF&(1<<7) ? 0 : (1<< 6)) |
175            (PINF&(1<<6) ? 0 : (1<< 7)) |
176            (PINF&(1<<5) ? 0 : (1<< 8)) |
177            (PINF&(1<<4) ? 0 : (1<< 9)) |
178            (PINF&(1<<1) ? 0 : (1<<10)) |
179            (PINF&(1<<0) ? 0 : (1<<11));
180 }
181
182 /* Row pin configuration
183  * row: 0  1  2  3
184  * pin: B0 B1 B2 B3
185  */
186 static void unselect_rows(void)
187 {
188     // Hi-Z(DDR:0, PORT:0) to unselect
189     DDRB  &= ~0b01110000;
190     PORTB &= ~0b01110000;
191     DDRD  &= ~0b10000000;
192     PORTD &= ~0b10000000;
193 }
194
195 static void select_row(uint8_t row)
196 {
197     switch (row) {
198         case 0:
199             DDRB  |= (1<<6);
200             PORTB &= ~(1<<6);
201             break;
202         case 1:
203             DDRB  |= (1<<5);
204             PORTB &= ~(1<<5);
205             break;
206         case 2:
207             DDRB  |= (1<<4);
208             PORTB &= ~(1<<4);
209             break;
210         case 3:
211             DDRD  |= (1<<7);
212             PORTD &= ~(1<<7);
213             break;
214     }
215 }