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