]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - keyboard/hbkb/matrix.c
core: Clean up code of Locking key support
[max/tmk_keyboard.git] / keyboard / hbkb / 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 #include <stdint.h>
19 #include <stdbool.h>
20 #include <avr/io.h>
21 #include <util/delay.h>
22 #include "print.h"
23 #include "debug.h"
24 #include "util.h"
25 #include "matrix.h"
26
27
28 /*
29  * Happy Buckling Keyboard(IBM Model M mod)
30  *
31  * Pin usage:
32  *   COL: PD0-7
33  *   ROW: PB0-7, PF4-7
34  */
35 #ifndef DEBOUNCE
36 #   define DEBOUNCE     10
37 #endif
38 static uint8_t debouncing = DEBOUNCE;
39
40 /* matrix state(1:on, 0:off) */
41 static matrix_row_t matrix[MATRIX_ROWS];
42 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
43
44 static matrix_row_t read_cols(void);
45 static void unselect_rows(void);
46 static void select_row(uint8_t row);
47
48
49 void matrix_init(void)
50 {
51     // JTAG disable for PORT F. write JTD bit twice within four cycles.
52     MCUCR |= (1<<JTD);
53     MCUCR |= (1<<JTD);
54
55     // initialize rows
56     unselect_rows();
57
58     // initialize columns to input with pull-up(DDR:0, PORT:1)
59     DDRD = 0x00;
60     PORTD = 0xFF;
61
62     // initialize matrix state: all keys off
63     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
64         matrix[i] = 0;
65         matrix_debouncing[i] = 0;
66     }
67 }
68
69 uint8_t matrix_scan(void)
70 {
71     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
72         select_row(i);
73         _delay_us(30);  // without this wait read unstable value.
74         matrix_row_t cols = read_cols();
75         if (matrix_debouncing[i] != cols) {
76             matrix_debouncing[i] = cols;
77             if (debouncing) {
78                 debug("bounce!: "); debug_hex(debouncing); debug("\n");
79             }
80             debouncing = DEBOUNCE;
81         }
82         unselect_rows();
83     }
84
85     if (debouncing) {
86         if (--debouncing) {
87             _delay_ms(1);
88         } else {
89             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
90                 matrix[i] = matrix_debouncing[i];
91             }
92         }
93     }
94
95     return 1;
96 }
97
98 inline
99 matrix_row_t matrix_get_row(uint8_t row)
100 {
101     return matrix[row];
102 }
103
104 inline
105 static matrix_row_t read_cols(void)
106 {
107     return ~PIND;
108 }
109
110 inline
111 static void unselect_rows(void)
112 {
113     // Hi-Z(DDR:0, PORT:0) to unselect
114     DDRB  &= ~0b11111111;
115     PORTB &= ~0b11111111;
116     DDRF  &= ~0b11110000;
117     PORTF &= ~0b11110000;
118 }
119
120 inline
121 static void select_row(uint8_t row)
122 {
123     // Output low(DDR:1, PORT:0) to select
124     switch (row) {
125         case 0:
126         case 1:
127         case 2:
128         case 3:
129         case 4:
130         case 5:
131         case 6:
132         case 7:
133             DDRB  |=  (1<<row);
134             PORTB &= ~(1<<row);
135             break;
136         case 8:
137             DDRF  |=  (1<<4);
138             PORTF &= ~(1<<4);
139             break;
140         case 9:
141         case 10:
142         case 11:
143             DDRF  |=  (1<<(row-4));
144             PORTF &= ~(1<<(row-4));
145             break;
146     }
147 }