aboutsummaryrefslogtreecommitdiff
path: root/keyboard/onekey/matrix.c
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2013-08-05 16:22:08 +0900
committertmk <nobody@nowhere>2013-08-05 16:23:17 +0900
commitb6677f10e33bfa49557dfebd0226928477ede56a (patch)
tree1716eca59ab7edc0b4084559db18e2822d67117a /keyboard/onekey/matrix.c
parent60103a12b2ca0a572da1e72eb46bff19c499fc95 (diff)
downloadqmk_firmware-b6677f10e33bfa49557dfebd0226928477ede56a.tar.gz
qmk_firmware-b6677f10e33bfa49557dfebd0226928477ede56a.zip
Add files for onekey(issue #56)
- https://github.com/tmk/tmk_keyboard/issues/56
Diffstat (limited to 'keyboard/onekey/matrix.c')
-rw-r--r--keyboard/onekey/matrix.c175
1 files changed, 175 insertions, 0 deletions
diff --git a/keyboard/onekey/matrix.c b/keyboard/onekey/matrix.c
new file mode 100644
index 000000000..cd0789c60
--- /dev/null
+++ b/keyboard/onekey/matrix.c
@@ -0,0 +1,175 @@
1/*
2Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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 <util/delay.h>
25#include "print.h"
26#include "debug.h"
27#include "util.h"
28#include "matrix.h"
29
30
31#ifndef DEBOUNCE
32# define DEBOUNCE 5
33#endif
34static uint8_t debouncing = DEBOUNCE;
35
36/* matrix state(1:on, 0:off) */
37static matrix_row_t matrix[MATRIX_ROWS];
38static matrix_row_t matrix_debouncing[MATRIX_ROWS];
39
40static matrix_row_t read_cols(void);
41static void init_cols(void);
42static void unselect_rows(void);
43static void select_row(uint8_t row);
44
45
46inline
47uint8_t matrix_rows(void)
48{
49 return MATRIX_ROWS;
50}
51
52inline
53uint8_t matrix_cols(void)
54{
55 return MATRIX_COLS;
56}
57
58void matrix_init(void)
59{
60 debug_enable = true;
61 debug_matrix = true;
62 // initialize row and col
63 unselect_rows();
64 init_cols();
65
66 // initialize matrix state: all keys off
67 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
68 matrix[i] = 0;
69 matrix_debouncing[i] = 0;
70 }
71}
72
73uint8_t matrix_scan(void)
74{
75 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
76 select_row(i);
77 _delay_us(30); // without this wait read unstable value.
78 matrix_row_t cols = read_cols();
79 if (matrix_debouncing[i] != cols) {
80 matrix_debouncing[i] = cols;
81 if (debouncing) {
82 debug("bounce!: "); debug_hex(debouncing); debug("\n");
83 }
84 debouncing = DEBOUNCE;
85 }
86 unselect_rows();
87 }
88
89 if (debouncing) {
90 if (--debouncing) {
91 _delay_ms(1);
92 } else {
93 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
94 matrix[i] = matrix_debouncing[i];
95 }
96 }
97 }
98
99 return 1;
100}
101
102bool matrix_is_modified(void)
103{
104 if (debouncing) return false;
105 return true;
106}
107
108inline
109bool matrix_is_on(uint8_t row, uint8_t col)
110{
111 return (matrix[row] & ((matrix_row_t)1<<col));
112}
113
114inline
115matrix_row_t matrix_get_row(uint8_t row)
116{
117 return matrix[row];
118}
119
120void matrix_print(void)
121{
122 print("\nr/c 0123456789ABCDEF\n");
123 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
124 phex(row); print(": ");
125 pbin_reverse16(matrix_get_row(row));
126 print("\n");
127 }
128}
129
130uint8_t matrix_key_count(void)
131{
132 uint8_t count = 0;
133 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
134 count += bitpop16(matrix[i]);
135 }
136 return count;
137}
138
139/* Column pin configuration
140 * col: 0
141 * pin: D0
142 */
143static void init_cols(void)
144{
145 // Input with pull-up(DDR:0, PORT:1)
146 DDRD &= ~(1<<0);
147 PORTD |= (1<<0);
148}
149
150static matrix_row_t read_cols(void)
151{
152 return (PIND&(1<<0) ? 0 : (1<<0));
153}
154
155/* Row pin configuration
156 * row: 0
157 * pin: D1
158 */
159static void unselect_rows(void)
160{
161 // Hi-Z(DDR:0, PORT:0) to unselect
162 DDRD &= ~0b00000010;
163 PORTD &= ~0b00000010;
164}
165
166static void select_row(uint8_t row)
167{
168 // Output low(DDR:1, PORT:0) to select
169 switch (row) {
170 case 0:
171 DDRD |= (1<<1);
172 PORTD &= ~(1<<1);
173 break;
174 }
175}