]>
git.gir.st - tmk_keyboard.git/blob - keyboard/fc660c/fc660c.c
2 Copyright 2017 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/>.
23 #include <util/delay.h>
36 static uint32_t matrix_last_modified
= 0;
38 // matrix state buffer(1:on, 0:off)
39 static matrix_row_t
*matrix
;
40 static matrix_row_t
*matrix_prev
;
41 static matrix_row_t _matrix0
[MATRIX_ROWS
];
42 static matrix_row_t _matrix1
[MATRIX_ROWS
];
45 void matrix_init(void)
49 debug_keyboard
= true;
55 // LEDs on CapsLock and Insert
56 DDRB
|= (1<<6) | (1<<7);
57 PORTB
|= (1<<6) | (1<<7);
59 // initialize matrix state: all keys off
60 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) _matrix0
[i
] = 0x00;
61 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) _matrix1
[i
] = 0x00;
63 matrix_prev
= _matrix1
;
66 uint8_t matrix_scan(void)
75 for (col
= 0; col
< MATRIX_COLS
; col
++) {
77 for (row
= 0; row
< MATRIX_ROWS
; row
++) {
78 //KEY_SELECT(row, col);
82 // Not sure this is needed. This just emulates HHKB controller's behaviour.
83 if (matrix_prev
[row
] & (1<<col
)) {
88 // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE.
89 // If V-USB interrupts in this section we could lose 40us or so
90 // and would read invalid value from KEY_STATE.
91 uint8_t last
= TIMER_RAW
;
95 // Wait for KEY_STATE outputs its value.
99 matrix
[row
] &= ~(1<<col
);
101 matrix
[row
] |= (1<<col
);
104 // Ignore if this code region execution time elapses more than 20us.
105 // MEMO: 20[us] * (TIMER_RAW_FREQ / 1000000)[count per us]
106 // MEMO: then change above using this rule: a/(b/c) = a*1/(b/c) = a*(c/b)
107 if (TIMER_DIFF_RAW(TIMER_RAW
, last
) > 20/(1000000/TIMER_RAW_FREQ
)) {
108 matrix
[row
] = matrix_prev
[row
];
115 // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE.
116 // This takes 25us or more to make sure KEY_STATE returns to idle state.
119 if (matrix
[row
] ^ matrix_prev
[row
]) {
120 matrix_last_modified
= timer_read32();
127 matrix_row_t
matrix_get_row(uint8_t row
)
132 void led_set(uint8_t usb_led
)
134 if (usb_led
& (1<<USB_LED_CAPS_LOCK
)) {