]> git.gir.st - tmk_keyboard.git/blob - keyboard/onekey/onekey.c
onekey: Fix for V-USB of ATtiny85 and key scan
[tmk_keyboard.git] / keyboard / onekey / onekey.c
1 /*
2 Copyright 2017 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 /*
19 * scan matrix
20 */
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <avr/io.h>
24 #include "debug.h"
25 #include "timer.h"
26 #include "matrix.h"
27
28
29 #ifndef DEBOUNCE
30 # define DEBOUNCE 5
31 #endif
32 /* matrix state(1:on, 0:off) */
33 static matrix_row_t row_debounced = 0;
34 static matrix_row_t row_debouncing = 0;
35 static bool debouncing = false;
36 static uint16_t debouncing_time = 0;
37
38
39 void matrix_init(void)
40 {
41 debug_enable = true;
42 debug_matrix = true;
43 debug_mouse = true;
44
45 // PB0: Input with pull-up(DDR:0, PORT:1)
46 DDRB &= ~(1<<0);
47 PORTB |= (1<<0);
48 }
49
50 uint8_t matrix_scan(void)
51 {
52 matrix_row_t r = (PINB&(1<<0) ? 0 : 1);
53 if (row_debouncing != r) {
54 row_debouncing = r;
55 debouncing = true;
56 debouncing_time = timer_read();
57 }
58
59 if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
60 row_debounced = row_debouncing;
61 debouncing = false;
62 }
63 return 1;
64 }
65
66 inline
67 matrix_row_t matrix_get_row(uint8_t row)
68 {
69 return row_debounced;
70 }
Imprint / Impressum