3 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
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.
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.
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/>.
25 #include <util/delay.h>
26 #include "action_layer.h"
31 #include "sixkeyboard.h"
34 /* matrix state(1:on, 0:off) */
35 static matrix_row_t matrix[MATRIX_ROWS];
36 static matrix_row_t matrix_stage[MATRIX_ROWS];
37 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
39 static uint16_t debouncing_time;
40 static bool debouncing = false;
42 __attribute__ ((weak))
43 void matrix_init_kb(void) {
47 __attribute__ ((weak))
48 void matrix_scan_kb(void) {
52 __attribute__ ((weak))
53 void matrix_init_user(void) {
56 __attribute__ ((weak))
57 void matrix_scan_user(void) {
61 uint8_t matrix_rows(void)
67 uint8_t matrix_cols(void)
72 void matrix_init(void)
77 DDRB &= ~(1<<7 | 1<<5);
78 PORTB |= (1<<7 | 1<<5);
79 DDRD &= ~(1<<6 | 1<<4 | 1<<1);
80 PORTD |= (1<<6 | 1<<4 | 1<<1);
82 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
84 matrix_debouncing[i] = 0;
88 matrix_init_quantum();
92 uint8_t matrix_scan(void)
94 matrix_stage[0] = (PINC&(1<<7) ? 0 : (1<<0)) | (PINB&(1<<7) ? 0 : (1<<1)) | (PINB&(1<<5) ? 0 : (1<<2));
95 matrix_stage[1] = (PIND&(1<<6) ? 0 : (1<<0)) | (PIND&(1<<1) ? 0 : (1<<1)) | (PIND&(1<<4) ? 0 : (1<<2));
97 if (memcmp(matrix_debouncing, matrix_stage, sizeof(matrix)) != 0) {
99 debouncing_time = timer_read();
102 matrix_debouncing[0] = matrix_stage[0];
103 matrix_debouncing[1] = matrix_stage[1];
105 if (debouncing && (timer_elapsed(debouncing_time) > 20)) {
106 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
107 matrix[i] = matrix_debouncing[i];
112 matrix_scan_quantum();
117 bool matrix_is_modified(void)
123 bool matrix_is_on(uint8_t row, uint8_t col)
125 return (matrix[row] & ((matrix_row_t)1<<col));
129 matrix_row_t matrix_get_row(uint8_t row)
134 void matrix_print(void)
138 uint8_t matrix_key_count(void)
141 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
142 count += bitpop16(matrix[i]);