]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboard/planck/matrix_pcb.c
Update for Atomic PCB Rev 0
[qmk_firmware.git] / keyboard / planck / matrix_pcb.c
1 /*
2 Copyright 2012 Jun Wako 
3 Generated by planckkeyboard.com (2014 Jack Humbert)
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /*
20  * scan matrix
21  */
22 #include <stdint.h>
23 #include <stdbool.h>
24 #include <avr/io.h>
25 #include <util/delay.h>
26 #include "print.h"
27 #include "debug.h"
28 #include "util.h"
29 #include "matrix.h"
30 #include "backlight.h" // TODO fix this dependency 
31
32
33 #ifndef DEBOUNCE
34 #   define DEBOUNCE 10
35 #endif
36 static uint8_t debouncing = DEBOUNCE;
37
38 /* matrix state(1:on, 0:off) */
39 static matrix_row_t matrix[MATRIX_ROWS];
40 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
41
42 static matrix_row_t read_cols(void);
43 static void init_cols(void);
44 static void unselect_rows(void);
45 static void select_row(uint8_t row);
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     // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
62     MCUCR |= (1<<JTD);
63     MCUCR |= (1<<JTD);
64
65     backlight_init_ports();
66
67     // Turn status LED on
68     DDRE |= (1<<6);
69     PORTE |= (1<<6);
70     
71     // initialize row and col
72     unselect_rows();
73     init_cols();
74
75     // initialize matrix state: all keys off
76     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
77         matrix[i] = 0;
78         matrix_debouncing[i] = 0;
79     }
80 }
81
82 uint8_t matrix_scan(void)
83 {
84     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
85         select_row(i);
86         _delay_us(30);  // without this wait read unstable value.
87         matrix_row_t cols = read_cols();
88         if (matrix_debouncing[i] != cols) {
89             matrix_debouncing[i] = cols;
90             if (debouncing) {
91                 debug("bounce!: "); debug_hex(debouncing); debug("\n");
92             }
93             debouncing = DEBOUNCE;
94         }
95         unselect_rows();
96     }
97
98     if (debouncing) {
99         if (--debouncing) {
100             _delay_ms(1);
101         } else {
102             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
103                 matrix[i] = matrix_debouncing[i];
104             }
105         }
106     }
107
108     return 1;
109 }
110
111 bool matrix_is_modified(void)
112 {
113     if (debouncing) return false;
114     return true;
115 }
116
117 inline
118 bool matrix_is_on(uint8_t row, uint8_t col)
119 {
120     return (matrix[row] & ((matrix_row_t)1<col));
121 }
122
123 inline
124 matrix_row_t matrix_get_row(uint8_t row)
125 {
126     return matrix[row];
127 }
128
129 void matrix_print(void)
130 {
131     print("\nr/c 0123456789ABCDEF\n");
132     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
133         phex(row); print(": ");
134         pbin_reverse16(matrix_get_row(row));
135         print("\n");
136     }
137 }
138
139 uint8_t matrix_key_count(void)
140 {
141     uint8_t count = 0;
142     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
143         count += bitpop16(matrix[i]);
144     }
145     return count;
146 }
147
148 //
149 // Planck PCB Rev 1 Pin Assignments
150 //
151 // Column: 0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11
152 // Pin:    F1, F0, B0, C7, F4, F5, F6, F7, D4, D6, B4, D7
153 //
154
155 static void init_cols(void)
156 {
157     DDRB &= ~(1<<4 | 1<<0);
158     PORTB |= (1<<4 | 1<<0);
159     DDRC &= ~(1<<7);
160     PORTC |= (1<<7);
161     DDRD &= ~(1<<7 | 1<<6 | 1<<4);
162     PORTD |= (1<<7 | 1<<6 | 1<<4);
163     DDRF &= ~(1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
164     PORTF |= (1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
165     
166 }
167
168 static matrix_row_t read_cols(void)
169 {
170   return (PINF&(1<<1) ? 0 : (1<<0)) |
171          (PINF&(1<<0) ? 0 : (1<<1)) |
172          (PINB&(1<<0) ? 0 : (1<<2)) |
173          (PINC&(1<<7) ? 0 : (1<<3)) |
174          (PINF&(1<<4) ? 0 : (1<<4)) |
175          (PINF&(1<<5) ? 0 : (1<<5)) |
176          (PINF&(1<<6) ? 0 : (1<<6)) |
177          (PINF&(1<<7) ? 0 : (1<<7)) |
178          (PIND&(1<<4) ? 0 : (1<<8)) |
179          (PIND&(1<<6) ? 0 : (1<<9)) |
180          (PINB&(1<<4) ? 0 : (1<<10)) |
181          (PIND&(1<<7) ? 0 : (1<<11));
182          
183 }
184
185 //
186 // Planck PCB Rev 1 Pin Assignments
187 //
188 // Row: 0,  1,  2,  3
189 // Pin: D0, D5, B5, B6
190 //
191
192 static void unselect_rows(void)
193 {
194     DDRB &= ~(1<<5 | 1<<6);
195     PORTB |= (1<<5 | 1<<6);
196     DDRD &= ~(1<<0 | 1<<5);
197     PORTD |= (1<<0 | 1<<5);
198     
199 }
200
201 static void select_row(uint8_t row)
202 {
203     switch (row) {
204         case 0:
205             DDRD  |= (1<<0);
206             PORTD &= ~(1<<0);
207             break;
208         case 1:
209             DDRD  |= (1<<5);
210             PORTD &= ~(1<<5);
211             break;
212         case 2:
213             DDRB  |= (1<<5);
214             PORTB &= ~(1<<5);
215             break;
216         case 3:
217             DDRB  |= (1<<6);
218             PORTB &= ~(1<<6);
219             break;
220         
221     }
222 }