2 Copyright 2012 Jun Wako <wakojun@gmail.com>
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.
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.
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/>.
24 #include <util/delay.h>
34 static uint8_t debouncing
= DEBOUNCE
;
36 /* matrix state(1:on, 0:off) */
37 static matrix_row_t matrix
[MATRIX_ROWS
];
38 static matrix_row_t matrix_debouncing
[MATRIX_ROWS
];
40 static matrix_row_t matrix_reversed
[MATRIX_COLS
];
41 static matrix_row_t matrix_reversed_debouncing
[MATRIX_COLS
];
43 static matrix_row_t
read_cols(void);
44 static void init_cols(void);
45 static void unselect_rows(void);
46 static void select_row(uint8_t row
);
50 uint8_t matrix_rows(void)
56 uint8_t matrix_cols(void)
61 void matrix_init(void)
63 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
67 // initialize row and col
71 // initialize matrix state: all keys off
72 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) {
74 matrix_debouncing
[i
] = 0;
78 uint8_t matrix_scan(void)
80 for (uint8_t i
= 0; i
< MATRIX_COLS
; i
++) {
82 _delay_us(30); // without this wait read unstable value.
83 matrix_row_t rows
= read_cols();
84 if (matrix_reversed_debouncing
[i
] != rows
) {
85 matrix_reversed_debouncing
[i
] = rows
;
87 debug("bounce!: "); debug_hex(debouncing
); debug("\n");
89 debouncing
= DEBOUNCE
;
98 for (uint8_t i
= 0; i
< MATRIX_COLS
; i
++) {
99 matrix_reversed
[i
] = matrix_reversed_debouncing
[i
];
104 for (uint8_t y
= 0; y
< MATRIX_ROWS
; y
++) {
105 matrix_row_t row
= 0;
106 for (uint8_t x
= 0; x
< MATRIX_COLS
; x
++) {
107 row
|= ((matrix_reversed
[x
] & (1<<y
)) >> y
) << x
;
115 bool matrix_is_modified(void)
117 if (debouncing
) return false;
122 bool matrix_is_on(uint8_t row
, uint8_t col
)
124 return (matrix
[row
] & ((matrix_row_t
)1<<col
));
128 matrix_row_t
matrix_get_row(uint8_t row
)
133 void matrix_print(void)
135 print("\nr/c 0123456789ABCDEF\n");
136 for (uint8_t row
= 0; row
< MATRIX_ROWS
; row
++) {
137 phex(row
); print(": ");
138 pbin_reverse16(matrix_get_row(row
));
143 /* Column pin configuration
144 * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13
145 * pin: F0 F1 E6 C7 C6 B6 D4 B1 B0 B5 B4 D7 D6 B3 (Rev.A)
148 static void init_cols(void)
150 // Input with pull-up(DDR:0, PORT:1)
151 // DDRF &= ~(1<<0 | 1<<1 | 1<<4 | 1<<5);
152 // PORTF |= (1<<0 | 1<<1 | 1<<4 | 1<<5);
153 // DDRD &= ~(1<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
154 // PORTD |= (1<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
155 // DDRB &= ~(1<<1 | 1<<3);
156 // PORTB |= (1<<1 | 1<<3);
158 // because I'm a dumbass
159 DDRB
&= ~(1<<0 | 1<<2 | 1<<4 | 1<<5 | 1<<6);
160 PORTB
|= (1<<0 | 1<<2 | 1<<4 | 1<<5 | 1<<6);
163 static matrix_row_t
read_cols(void)
165 // return (PINF&(1<<5) ? 0 : (1<<0)) |
166 // (PINB&(1<<1) ? 0 : (1<<1)) |
167 // (PINF&(1<<0) ? 0 : (1<<2)) |
168 // (PINF&(1<<1) ? 0 : (1<<3)) |
169 // (PINF&(1<<4) ? 0 : (1<<4)) |
170 // (PINB&(1<<3) ? 0 : (1<<5)) |
171 // (PIND&(1<<7) ? 0 : (1<<6)) |
172 // (PIND&(1<<6) ? 0 : (1<<7)) |
173 // (PIND&(1<<4) ? 0 : (1<<8)) |
174 // (PIND&(1<<5) ? 0 : (1<<9)) |
175 // (PIND&(1<<3) ? 0 : (1<<10)) |
176 // (PIND&(1<<2) ? 0 : (1<<11)) |
177 // (PIND&(1<<1) ? 0 : (1<<12));
179 //because I'm a dumbass
180 return (PINB
&(1<<0) ? 0 : (1<<0)) |
181 (PINB
&(1<<2) ? 0 : (1<<1)) |
182 (PINB
&(1<<4) ? 0 : (1<<2)) |
183 (PINB
&(1<<5) ? 0 : (1<<3)) |
184 (PINB
&(1<<6) ? 0 : (1<<4));
187 /* Row pin configuration
189 * pin: D0 D1 D2 D3 D5
191 static void unselect_rows(void)
193 // Hi-Z(DDR:0, PORT:0) to unselect
194 // DDRB &= ~0b01110101;
195 // PORTB &= ~0b01110101;
197 //because I'm a dumbass
199 PORTB
&= ~0b00001010;
201 PORTD
&= ~0b11111110;
203 PORTF
&= ~0b00110011;
206 static void select_row(uint8_t row
)
208 // Output low(DDR:1, PORT:0) to select
232 //because I'm a dumbass