forked from tmk/tmk_keyboard
-
Notifications
You must be signed in to change notification settings - Fork 2
/
onekey.c
70 lines (58 loc) · 1.63 KB
/
onekey.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
Copyright 2017 Jun Wako <wakojun@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* scan matrix
*/
#include <stdint.h>
#include <stdbool.h>
#include <avr/io.h>
#include "debug.h"
#include "timer.h"
#include "matrix.h"
#ifndef DEBOUNCE
# define DEBOUNCE 5
#endif
/* matrix state(1:on, 0:off) */
static matrix_row_t row_debounced = 0;
static matrix_row_t row_debouncing = 0;
static bool debouncing = false;
static uint16_t debouncing_time = 0;
void matrix_init(void)
{
debug_enable = true;
debug_matrix = true;
debug_mouse = true;
// PB0: Input with pull-up(DDR:0, PORT:1)
DDRB &= ~(1<<0);
PORTB |= (1<<0);
}
uint8_t matrix_scan(void)
{
matrix_row_t r = (PINB&(1<<0) ? 0 : 1);
if (row_debouncing != r) {
row_debouncing = r;
debouncing = true;
debouncing_time = timer_read();
}
if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
row_debounced = row_debouncing;
debouncing = false;
}
return 1;
}
inline
matrix_row_t matrix_get_row(uint8_t row)
{
return row_debounced;
}