2 Copyright 2014 Warren Janssens <warren.janssens@gmail.com>
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.
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.
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/>.
24 #include <util/delay.h>
25 #include "action_layer.h"
36 static uint8_t debouncing = DEBOUNCE;
38 /* matrix state(1:on, 0:off) */
39 static uint8_t matrix[MATRIX_ROWS];
40 static uint8_t matrix_debouncing[MATRIX_ROWS];
42 static matrix_row_t read_row(uint8_t row);
43 static void unselect_rows(void);
44 static void select_rows(uint8_t row);
46 __attribute__ ((weak))
47 void matrix_init_quantum(void) {
51 __attribute__ ((weak))
52 void matrix_scan_quantum(void) {
56 __attribute__ ((weak))
57 void matrix_init_kb(void) {
61 __attribute__ ((weak))
62 void matrix_scan_kb(void) {
66 __attribute__ ((weak))
67 void matrix_init_user(void) {
70 __attribute__ ((weak))
71 void matrix_scan_user(void) {
75 uint8_t matrix_rows(void)
82 uint8_t matrix_cols(void)
87 void matrix_init(void)
89 //debug_enable = true;
91 //dprint("matrix_init"); dprintln();
96 // output low (multiplexers)
100 // input with pullup (matrix)
104 // input with pullup (program and keypad buttons)
108 // initialize row and col
111 // initialize matrix state: all keys off
112 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
114 matrix_debouncing[i] = 0;
119 uint8_t matrix_scan(void)
122 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
124 uint8_t row = read_row(i);
125 if (matrix_debouncing[i] != row) {
126 matrix_debouncing[i] = row;
128 debug("bounce!: "); debug_hex(debouncing); debug("\n");
130 debouncing = DEBOUNCE;
139 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
140 matrix[i] = matrix_debouncing[i];
144 matrix_scan_quantum();
148 bool matrix_is_modified(void)
150 if (debouncing) return false;
155 bool matrix_is_on(uint8_t row, uint8_t col)
157 return (matrix[row] & ((matrix_row_t)1<<col));
161 matrix_row_t matrix_get_row(uint8_t row)
166 void matrix_print(void)
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));
176 uint8_t matrix_key_count(void)
179 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
180 count += bitpop16(matrix[i]);
185 static matrix_row_t read_row(uint8_t row)
187 _delay_us(30); // without this wait read unstable value.
189 //keypad and program buttons
192 return ~(PINC | 0b00111111);
197 static void unselect_rows(void)
199 // set A,B,C,G to 0 (F4 - F7)
203 static void select_rows(uint8_t row)
205 // set A,B,C,G to row value
210 /* Row pin configuration
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
218 PB0 21 c1 f6 f8 f7 5 4 3 2 1 =+
219 PB1 22 c2 f3 f5 f4 t r e w q TAB
220 PB2 23 c3 ESC f2 f1 g f d s a CL
221 PB3 24 c4 f9 f11 f10 b v c x z LS UP DN [{ ]}
222 PB4 25 c5 f12 SL PS RT LT §± `~ 6 7 8 9 0 -_
223 PB5 26 c6 PB PGM KPD y u i o p \
224 PB6 27 c7 LC DL BS RC EN SP h j k l ;: '"
225 PB7 28 c8 RA PU PD n m ,< .> /? RS