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