aboutsummaryrefslogtreecommitdiff
path: root/keyboards/kinesis/alvicstep/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/kinesis/alvicstep/matrix.c')
-rw-r--r--keyboards/kinesis/alvicstep/matrix.c228
1 files changed, 228 insertions, 0 deletions
diff --git a/keyboards/kinesis/alvicstep/matrix.c b/keyboards/kinesis/alvicstep/matrix.c
new file mode 100644
index 000000000..cb0d5ad7d
--- /dev/null
+++ b/keyboards/kinesis/alvicstep/matrix.c
@@ -0,0 +1,228 @@
1/*
2Copyright 2014 Warren Janssens <warren.janssens@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#include "led.h"
31#include "config.h"
32
33#ifndef DEBOUNCE
34# define DEBOUNCE 5
35#endif
36static uint8_t debouncing = DEBOUNCE;
37
38/* matrix state(1:on, 0:off) */
39static uint8_t matrix[MATRIX_ROWS];
40static uint8_t matrix_debouncing[MATRIX_ROWS];
41
42static matrix_row_t read_row(uint8_t row);
43static void unselect_rows(void);
44static void select_rows(uint8_t row);
45
46__attribute__ ((weak))
47void matrix_init_quantum(void) {
48 matrix_init_kb();
49}
50
51__attribute__ ((weak))
52void matrix_scan_quantum(void) {
53 matrix_scan_kb();
54}
55
56__attribute__ ((weak))
57void matrix_init_kb(void) {
58 matrix_init_user();
59}
60
61__attribute__ ((weak))
62void matrix_scan_kb(void) {
63 matrix_scan_user();
64}
65
66__attribute__ ((weak))
67void matrix_init_user(void) {
68}
69
70__attribute__ ((weak))
71void matrix_scan_user(void) {
72}
73
74inline
75uint8_t matrix_rows(void)
76{
77 return MATRIX_ROWS;
78}
79
80
81inline
82uint8_t matrix_cols(void)
83{
84 return MATRIX_COLS;
85}
86
87void matrix_init(void)
88{
89 //debug_enable = true;
90
91 //dprint("matrix_init"); dprintln();
92 // output high (leds)
93 DDRD = 0xFF;
94 PORTD = 0xFF;
95
96 // output low (multiplexers)
97 DDRF = 0xFF;
98 PORTF = 0x00;
99
100 // input with pullup (matrix)
101 DDRB = 0x00;
102 PORTB = 0xFF;
103
104 // input with pullup (program and keypad buttons)
105 DDRC = 0x00;
106 PORTC = 0xFF;
107
108 // initialize row and col
109 unselect_rows();
110
111 // initialize matrix state: all keys off
112 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
113 matrix[i] = 0;
114 matrix_debouncing[i] = 0;
115 }
116
117}
118
119uint8_t matrix_scan(void)
120{
121
122 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
123 select_rows(i);
124 uint8_t row = read_row(i);
125 if (matrix_debouncing[i] != row) {
126 matrix_debouncing[i] = row;
127 if (debouncing) {
128 debug("bounce!: "); debug_hex(debouncing); debug("\n");
129 }
130 debouncing = DEBOUNCE;
131 }
132 unselect_rows();
133 }
134
135 if (debouncing) {
136 if (--debouncing) {
137 _delay_ms(1);
138 } else {
139 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
140 matrix[i] = matrix_debouncing[i];
141 }
142 }
143 }
144 matrix_scan_quantum();
145 return 1;
146}
147
148bool matrix_is_modified(void)
149{
150 if (debouncing) return false;
151 return true;
152}
153
154inline
155bool matrix_is_on(uint8_t row, uint8_t col)
156{
157 return (matrix[row] & ((matrix_row_t)1<<col));
158}
159
160inline
161matrix_row_t matrix_get_row(uint8_t row)
162{
163 return matrix[row];
164}
165
166void matrix_print(void)
167{
168 print("\nr/c 01234567\n");
169 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
170 phex(row); print(": ");
171 pbin_reverse(matrix_get_row(row));
172 print("\n");
173 }
174}
175
176uint8_t matrix_key_count(void)
177{
178 uint8_t count = 0;
179 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
180 count += bitpop16(matrix[i]);
181 }
182 return count;
183}
184
185static matrix_row_t read_row(uint8_t row)
186{
187 _delay_us(30); // without this wait read unstable value.
188
189 //keypad and program buttons
190 if (row == 12)
191 {
192 return ~(PINC | 0b00111111);
193 }
194 return ~PINB;
195}
196
197static void unselect_rows(void)
198{
199 // set A,B,C,G to 0 (F4 - F7)
200 PORTF &= 0x0F;
201}
202
203static void select_rows(uint8_t row)
204{
205 // set A,B,C,G to row value
206 PORTF |= row << 4;
207}
208
209
210/* Row pin configuration
211PF0 A
212PF1 B
213PF2 C
214PF3 G 0 = U4, 1 = U5
215
216 4y0 4y1 4y2 4y3 4y4 4y5 4y6 4y7 5y0 5y1 5y2 5y3 5y4 5y5 5y6 5y7
217 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13 r14 r15 r16
218PB0 21 c1 f6 f8 f7 5 4 3 2 1 =+
219PB1 22 c2 f3 f5 f4 t r e w q TAB
220PB2 23 c3 ESC f2 f1 g f d s a CL
221PB3 24 c4 f9 f11 f10 b v c x z LS UP DN [{ ]}
222PB4 25 c5 f12 SL PS RT LT §± `~ 6 7 8 9 0 -_
223PB5 26 c6 PB PGM KPD y u i o p \
224PB6 27 c7 LC DL BS RC EN SP h j k l ;: '"
225PB7 28 c8 RA PU PD n m ,< .> /? RS
226 */
227
228