aboutsummaryrefslogtreecommitdiff
path: root/keyboard/atomic_old/matrix.c
diff options
context:
space:
mode:
authorJack Humbert <jack.humb@gmail.com>2015-08-21 23:14:48 -0400
committerJack Humbert <jack.humb@gmail.com>2015-08-21 23:14:48 -0400
commit476e29d1190ac45b810109512bbb50cc4769493b (patch)
tree1d493bae3b0ae91a6202918aa1bf53fb0da936fa /keyboard/atomic_old/matrix.c
parent2d76b5c3d421c984f6b4b9da757383cc87e3f808 (diff)
parentb191f8c60fbbaf1fb55d67edb86a6c33489b2ce3 (diff)
downloadqmk_firmware-476e29d1190ac45b810109512bbb50cc4769493b.tar.gz
qmk_firmware-476e29d1190ac45b810109512bbb50cc4769493b.zip
Merge pull request #26 from jackhumbert/midi
Midi
Diffstat (limited to 'keyboard/atomic_old/matrix.c')
-rw-r--r--keyboard/atomic_old/matrix.c211
1 files changed, 211 insertions, 0 deletions
diff --git a/keyboard/atomic_old/matrix.c b/keyboard/atomic_old/matrix.c
new file mode 100644
index 000000000..98102cb69
--- /dev/null
+++ b/keyboard/atomic_old/matrix.c
@@ -0,0 +1,211 @@
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 "action_layer.h"
26#include "print.h"
27#include "debug.h"
28#include "util.h"
29#include "matrix.h"
30
31
32#ifndef DEBOUNCE
33# define DEBOUNCE 10
34#endif
35static uint8_t debouncing = DEBOUNCE;
36
37/* matrix state(1:on, 0:off) */
38static matrix_row_t matrix[MATRIX_ROWS];
39static matrix_row_t matrix_debouncing[MATRIX_ROWS];
40
41static matrix_row_t read_cols(void);
42static void init_cols(void);
43static void unselect_rows(void);
44static void select_row(uint8_t row);
45
46
47inline
48uint8_t matrix_rows(void)
49{
50 return MATRIX_ROWS;
51}
52
53inline
54uint8_t matrix_cols(void)
55{
56 return MATRIX_COLS;
57}
58
59
60void matrix_init(void)
61{
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
100 return 1;
101}
102
103bool matrix_is_modified(void)
104{
105 if (debouncing) return false;
106 return true;
107}
108
109inline
110bool matrix_is_on(uint8_t row, uint8_t col)
111{
112 return (matrix[row] & ((matrix_row_t)1<<col));
113}
114
115inline
116matrix_row_t matrix_get_row(uint8_t row)
117{
118 return matrix[row];
119}
120
121void matrix_print(void)
122{
123 print("\nr/c 0123456789ABCDEF\n");
124 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
125 phex(row); print(": ");
126 pbin_reverse16(matrix_get_row(row));
127 print("\n");
128 }
129}
130
131uint8_t matrix_key_count(void)
132{
133 uint8_t count = 0;
134 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
135 count += bitpop16(matrix[i]);
136 }
137 return count;
138}
139
140/* Column pin configuration
141 * col: 0 1 2 3 4 5 6 7 8 9 10 11
142 * pin: F0 F1 F4 F5 F6 F7 B6 B5 B4 D7 D5 D4
143 */
144
145static void init_cols(void)
146{
147 DDRC &= ~(1<<6 | 1<<7);
148 PORTC |= (1<<6 | 1<<7);
149 DDRD &= ~(1<<4 | 1<<5 | 1<<6 | 1<<7);
150 PORTD |= (1<<4 | 1<<5 | 1<<6 | 1<<7);
151 DDRB &= ~(1<<4 | 1<<5 | 1<<6);
152 PORTB |= (1<<4 | 1<<5 | 1<<6);
153 DDRF &= ~(1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
154 PORTF |= (1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
155}
156
157static matrix_row_t read_cols(void)
158{
159 return (PINC&(1<<6) ? 0 : (1<< 0)) |
160 (PINC&(1<<7) ? 0 : (1<< 1)) |
161 (PIND&(1<<5) ? 0 : (1<< 2)) |
162 (PIND&(1<<4) ? 0 : (1<< 3)) |
163 (PIND&(1<<6) ? 0 : (1<< 4)) |
164 (PIND&(1<<7) ? 0 : (1<< 5)) |
165 (PINB&(1<<4) ? 0 : (1<< 6)) |
166 (PINB&(1<<5) ? 0 : (1<< 7)) |
167 (PINB&(1<<6) ? 0 : (1<< 8)) |
168 (PINF&(1<<7) ? 0 : (1<< 9)) |
169 (PINF&(1<<6) ? 0 : (1<<10)) |
170 (PINF&(1<<5) ? 0 : (1<<11)) |
171 (PINF&(1<<4) ? 0 : (1<<12)) |
172 (PINF&(1<<1) ? 0 : (1<<13)) |
173 (PINF&(1<<0) ? 0 : (1<<14));
174}
175
176/* Row pin configuration
177 * row: 0 1 2 3
178 * pin: B0 B1 B2 B3
179 */
180static void unselect_rows(void)
181{
182 // Hi-Z(DDR:0, PORT:0) to unselect
183 DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<7);
184 PORTB |= (1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<7);
185}
186
187static void select_row(uint8_t row)
188{
189 switch (row) {
190 case 0:
191 DDRB |= (1<<0);
192 PORTB &= ~(1<<0);
193 break;
194 case 1:
195 DDRB |= (1<<1);
196 PORTB &= ~(1<<1);
197 break;
198 case 2:
199 DDRB |= (1<<2);
200 PORTB &= ~(1<<2);
201 break;
202 case 3:
203 DDRB |= (1<<3);
204 PORTB &= ~(1<<3);
205 break;
206 case 4:
207 DDRB |= (1<<7);
208 PORTB &= ~(1<<7);
209 break;
210 }
211}