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