2 Copyright 2014 Kai Ryu <kai1103@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>
29 #ifdef UART_RGB_ENABLE
36 static uint8_t debouncing
= DEBOUNCE
;
38 /* matrix state(1:on, 0:off) */
39 static matrix_row_t matrix
[MATRIX_ROWS
];
40 static matrix_row_t matrix_debouncing
[MATRIX_ROWS
];
42 static matrix_row_t
read_cols(void);
43 static void init_cols(void);
44 static void init_rows(void);
45 static void unselect_rows(void);
46 static void select_row(uint8_t row
);
49 uint8_t matrix_rows(void)
55 uint8_t matrix_cols(void)
60 void matrix_init(void)
63 #ifdef UART_RGB_ENABLE
74 // initialize row and col
78 // initialize matrix state: all keys off
79 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) {
81 matrix_debouncing
[i
] = 0;
85 uint8_t matrix_scan(void)
87 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
89 _delay_us(30); // without this wait read unstable value.
90 matrix_row_t cols
= read_cols();
91 if (matrix_debouncing
[i
] != cols
) {
92 matrix_debouncing
[i
] = cols
;
94 debug("bounce!: "); debug_hex(debouncing
); debug("\n");
96 debouncing
= DEBOUNCE
;
105 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
106 matrix
[i
] = matrix_debouncing
[i
];
114 bool matrix_is_modified(void)
116 if (debouncing
) return false;
121 bool matrix_is_on(uint8_t row
, uint8_t col
)
123 return (matrix
[row
] & ((matrix_row_t
)1<<col
));
127 matrix_row_t
matrix_get_row(uint8_t row
)
132 void matrix_print(void)
134 print("\nr/c 0123456789ABCDEF\n");
135 for (uint8_t row
= 0; row
< MATRIX_ROWS
; row
++) {
136 phex(row
); print(": ");
137 pbin_reverse16(matrix_get_row(row
));
142 uint8_t matrix_key_count(void)
145 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
146 count
+= bitpop16(matrix
[i
]);
151 /* Column pin configuration
152 * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
153 * pin: F7 F6 F5 F4 F1 F0 E6 D7 D6 D5 D1 D0 B7 B6 B0 C7
155 static void init_cols(void)
157 // Input with pull-up(DDR:0, PORT:1)
164 DDRB
&= ~(_BV(PB6
) | _BV(PB7
)| _BV(PB0
));
165 PORTB
|= (_BV(PB6
) | _BV(PB7
)| _BV(PB0
));
175 static matrix_row_t
read_cols(void)
178 return (PINF
&_BV(PF7
) ? 0 : (1<<0)) |
179 (PINF
&_BV(PF6
) ? 0 : (1<<1)) |
180 (PINF
&_BV(PF5
) ? 0 : (1<<2)) |
181 (PINF
&_BV(PF4
) ? 0 : (1<<3)) |
182 (PINF
&_BV(PF1
) ? 0 : (1<<4)) |
183 (PINF
&_BV(PF0
) ? 0 : (1<<5)) |
184 (PINE
&_BV(PE6
) ? 0 : (1<<6)) |
185 (PIND
&_BV(PD7
) ? 0 : (1<<7)) |
186 (PIND
&_BV(PD6
) ? 0 : (1<<8)) |
187 (PIND
&_BV(PD5
) ? 0 : (1<<9)) |
188 (PIND
&_BV(PD1
) ? 0 : (1<<10)) |
189 (PIND
&_BV(PD0
) ? 0 : (1<<11)) |
190 (PINB
&_BV(PB7
) ? 0 : (1<<12)) |
191 (PINB
&_BV(PB6
) ? 0 : (1<<13)) |
192 (PINB
&_BV(PB0
) ? 0 : (1<<14)) |
193 (PINC
&_BV(PC7
) ? 0 : (1<<15));
196 /* Row pin configuration
198 * pin: B3 0 1 0 1 0 1 1
203 static void init_rows(void)
205 DDRB
|= (1<<PB1
| 1<<PB2
| 1<<PB3
);
208 static void unselect_rows(void)
210 // Hi-Z(DDR:0, PORT:0) to unselect
216 static void select_row(uint8_t row
)
218 // Output low(DDR:1, PORT:0) to select
219 (row
& (1<<0)) ? (PORTB
|= (1<<PB3
)) : (PORTB
&= ~(1<<PB3
));
220 (row
& (1<<1)) ? (PORTB
|= (1<<PB2
)) : (PORTB
&= ~(1<<PB2
));
221 (row
& (1<<2)) ? (PORTB
|= (1<<PB1
)) : (PORTB
&= ~(1<<PB1
));