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);
47 __attribute__ ((weak))
48 void matrix_init_kb(void) {
52 __attribute__ ((weak))
53 void matrix_scan_kb(void) {
57 __attribute__ ((weak))
58 void matrix_init_user(void) {
61 __attribute__ ((weak))
62 void matrix_scan_user(void) {
66 uint8_t matrix_rows(void)
73 uint8_t matrix_cols(void)
78 void matrix_init(void)
80 //debug_enable = true;
82 //dprint("matrix_init"); dprintln();
87 // output low (multiplexers)
91 // input with pullup (matrix)
95 // input with pullup (program and keypad buttons)
99 // initialize row and col
102 // initialize matrix state: all keys off
103 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
105 matrix_debouncing[i] = 0;
110 uint8_t matrix_scan(void)
113 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
115 uint8_t row = read_row(i);
116 if (matrix_debouncing[i] != row) {
117 matrix_debouncing[i] = row;
119 debug("bounce!: "); debug_hex(debouncing); debug("\n");
121 debouncing = DEBOUNCE;
130 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
131 matrix[i] = matrix_debouncing[i];
135 matrix_scan_quantum();
139 bool matrix_is_modified(void)
141 if (debouncing) return false;
146 bool matrix_is_on(uint8_t row, uint8_t col)
148 return (matrix[row] & ((matrix_row_t)1<<col));
152 matrix_row_t matrix_get_row(uint8_t row)
157 void matrix_print(void)
159 print("\nr/c 01234567\n");
160 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
161 phex(row); print(": ");
162 pbin_reverse(matrix_get_row(row));
167 uint8_t matrix_key_count(void)
170 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
171 count += bitpop16(matrix[i]);
176 static matrix_row_t read_row(uint8_t row)
178 _delay_us(30); // without this wait read unstable value.
180 //keypad and program buttons
183 return ~(PINC | 0b00111111);
188 static void unselect_rows(void)
190 // set A,B,C,G to 0 (F4 - F7)
194 static void select_rows(uint8_t row)
196 // set A,B,C,G to row value
201 /* Row pin configuration
207 4y0 4y1 4y2 4y3 4y4 4y5 4y6 4y7 5y0 5y1 5y2 5y3 5y4 5y5 5y6 5y7
208 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13 r14 r15 r16
209 PB0 21 c1 f6 f8 f7 5 4 3 2 1 =+
210 PB1 22 c2 f3 f5 f4 t r e w q TAB
211 PB2 23 c3 ESC f2 f1 g f d s a CL
212 PB3 24 c4 f9 f11 f10 b v c x z LS UP DN [{ ]}
213 PB4 25 c5 f12 SL PS RT LT §± `~ 6 7 8 9 0 -_
214 PB5 26 c6 PB PGM KPD y u i o p \
215 PB6 27 c7 LC DL BS RC EN SP h j k l ;: '"
216 PB7 28 c8 RA PU PD n m ,< .> /? RS