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