]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/sixkeyboard/matrix.c
[Keyboard] Snagpad Configurator bugfix and readme refactor (#6381)
[qmk_firmware.git] / keyboards / sixkeyboard / matrix.c
1 /*
2
3 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /*
20  * scan matrix
21  */
22 #include <stdint.h>
23 #include <stdbool.h>
24 #include <avr/io.h>
25 #include <util/delay.h>
26 #include "action_layer.h"
27 #include "print.h"
28 #include "debug.h"
29 #include "util.h"
30 #include "matrix.h"
31 #include "sixkeyboard.h"
32 #include <string.h>
33
34 /* matrix state(1:on, 0:off) */
35 static matrix_row_t matrix[MATRIX_ROWS];
36 static matrix_row_t matrix_stage[MATRIX_ROWS];
37 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
38
39 static uint16_t debouncing_time;
40 static bool debouncing = false;
41
42 __attribute__ ((weak))
43 void matrix_init_kb(void) {
44     matrix_init_user();
45 }
46
47 __attribute__ ((weak))
48 void matrix_scan_kb(void) {
49     matrix_scan_user();
50 }
51
52 __attribute__ ((weak))
53 void matrix_init_user(void) {
54 }
55
56 __attribute__ ((weak))
57 void matrix_scan_user(void) {
58 }
59
60 inline
61 uint8_t matrix_rows(void)
62 {
63     return MATRIX_ROWS;
64 }
65
66 inline
67 uint8_t matrix_cols(void)
68 {
69     return MATRIX_COLS;
70 }
71
72 void matrix_init(void)
73 {
74
75     DDRC  &= ~(1<<7);
76     PORTC |=  (1<<7);
77     DDRB  &= ~(1<<7 | 1<<5);
78     PORTB |=  (1<<7 | 1<<5);
79     DDRD  &= ~(1<<6 | 1<<4 | 1<<1);
80     PORTD |=  (1<<6 | 1<<4 | 1<<1);
81
82     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
83         matrix[i] = 0;
84         matrix_debouncing[i] = 0;
85         matrix_stage[i] = 0;
86     }
87
88     matrix_init_quantum();
89
90 }
91
92 uint8_t matrix_scan(void)
93 {
94     matrix_stage[0] = (PINC&(1<<7) ? 0 : (1<<0)) | (PINB&(1<<7) ? 0 : (1<<1)) | (PINB&(1<<5) ? 0 : (1<<2));
95     matrix_stage[1] = (PIND&(1<<6) ? 0 : (1<<0)) | (PIND&(1<<1) ? 0 : (1<<1)) | (PIND&(1<<4) ? 0 : (1<<2));
96
97     if (memcmp(matrix_debouncing, matrix_stage, sizeof(matrix)) != 0) {
98         debouncing = true;
99         debouncing_time = timer_read();
100     }
101
102     matrix_debouncing[0] = matrix_stage[0];
103     matrix_debouncing[1] = matrix_stage[1];
104
105     if (debouncing && (timer_elapsed(debouncing_time) > 20)) {
106         for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
107             matrix[i] = matrix_debouncing[i];
108         }
109         debouncing = false;
110     }
111
112     matrix_scan_quantum();
113
114     return 1;
115 }
116
117 bool matrix_is_modified(void)
118 {
119     return true;
120 }
121
122 inline
123 bool matrix_is_on(uint8_t row, uint8_t col)
124 {
125     return (matrix[row] & ((matrix_row_t)1<<col));
126 }
127
128 inline
129 matrix_row_t matrix_get_row(uint8_t row)
130 {
131     return matrix[row];
132 }
133
134 void matrix_print(void)
135 {
136 }
137
138 uint8_t matrix_key_count(void)
139 {
140     uint8_t count = 0;
141     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
142         count += bitpop16(matrix[i]);
143     }
144     return count;
145 }
146