aboutsummaryrefslogtreecommitdiff
path: root/keyboards/frosty_flake/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/frosty_flake/matrix.c')
-rw-r--r--keyboards/frosty_flake/matrix.c135
1 files changed, 135 insertions, 0 deletions
diff --git a/keyboards/frosty_flake/matrix.c b/keyboards/frosty_flake/matrix.c
new file mode 100644
index 000000000..05dffdb64
--- /dev/null
+++ b/keyboards/frosty_flake/matrix.c
@@ -0,0 +1,135 @@
1/*
2 Copyright 2017 Gabriel Young <gabeplaysdrums@live.com>
3
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.
8
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.
13
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/>.
16*/
17
18#include <stdint.h>
19#include <stdbool.h>
20#include <avr/io.h>
21#include <util/delay.h>
22#include "print.h"
23#include "debug.h"
24#include "util.h"
25#include "matrix.h"
26
27#ifndef DEBOUNCING_DELAY
28# define DEBOUNCING_DELAY 5
29#endif
30static uint8_t debouncing = DEBOUNCING_DELAY;
31
32static matrix_row_t matrix[MATRIX_ROWS];
33static matrix_row_t matrix_debouncing[MATRIX_ROWS];
34
35static matrix_row_t scan_col(void) {
36 return (
37 (PINC&(1<<7) ? 0 : ((matrix_row_t)1<<0)) |
38 (PINB&(1<<5) ? 0 : ((matrix_row_t)1<<1)) |
39 (PINB&(1<<4) ? 0 : ((matrix_row_t)1<<2)) |
40 (PINB&(1<<6) ? 0 : ((matrix_row_t)1<<3)) |
41 (PINB&(1<<1) ? 0 : ((matrix_row_t)1<<4)) |
42 (PINB&(1<<2) ? 0 : ((matrix_row_t)1<<5)) |
43 (PINB&(1<<3) ? 0 : ((matrix_row_t)1<<6)) |
44 (PINB&(1<<0) ? 0 : ((matrix_row_t)1<<7))
45 );
46}
47
48static void select_col(uint8_t col) {
49 switch (col) {
50 case 0: PORTD = (PORTD & ~0b01111011) | 0b00011011; break;
51 case 1: PORTD = (PORTD & ~0b01111011) | 0b01000011; break;
52 case 2: PORTD = (PORTD & ~0b01111011) | 0b01101010; break;
53 case 3: PORTD = (PORTD & ~0b01111011) | 0b01111001; break;
54 case 4: PORTD = (PORTD & ~0b01111011) | 0b01100010; break;
55 case 5: PORTD = (PORTD & ~0b01111011) | 0b01110001; break;
56 case 6: PORTD = (PORTD & ~0b01111011) | 0b01100001; break;
57 case 7: PORTD = (PORTD & ~0b01111011) | 0b01110000; break;
58 case 8: PORTD = (PORTD & ~0b01111011) | 0b01100000; break;
59 case 9: PORTD = (PORTD & ~0b01111011) | 0b01101000; break;
60 case 10: PORTD = (PORTD & ~0b01111011) | 0b00101011; break;
61 case 11: PORTD = (PORTD & ~0b01111011) | 0b00110011; break;
62 case 12: PORTD = (PORTD & ~0b01111011) | 0b00100011; break;
63 case 13: PORTD = (PORTD & ~0b01111011) | 0b01111000; break;
64 case 14: PORTD = (PORTD & ~0b01111011) | 0b00010011; break;
65 case 15: PORTD = (PORTD & ~0b01111011) | 0b01101001; break;
66 case 16: PORTD = (PORTD & ~0b01111011) | 0b00001011; break;
67 case 17: PORTD = (PORTD & ~0b01111011) | 0b00111011; break;
68 }
69}
70
71void matrix_init(void) {
72 /* Row output pins */
73 DDRD |= 0b01111011;
74 /* Column input pins */
75 DDRC &= ~0b10000000;
76 DDRB &= ~0b01111111;
77 PORTC |= 0b10000000;
78 PORTB |= 0b01111111;
79
80 for (uint8_t i=0; i < MATRIX_ROWS; i++)
81 matrix[i] = matrix_debouncing[i] = 0;
82
83 matrix_init_quantum();
84}
85
86uint8_t matrix_scan(void) {
87 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
88 select_col(col);
89 _delay_us(3);
90 matrix_row_t col_scan = scan_col();
91 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
92 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
93 bool curr_bit = col_scan & (1<<row);
94 if (prev_bit != curr_bit) {
95 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
96 debouncing = DEBOUNCING_DELAY;
97 }
98 }
99 }
100
101 if (debouncing) {
102 if (--debouncing)
103 _delay_ms(1);
104 else
105 for (uint8_t i = 0; i < MATRIX_ROWS; i++)
106 matrix[i] = matrix_debouncing[i];
107 }
108
109 matrix_scan_quantum();
110 return 1;
111}
112
113inline matrix_row_t matrix_get_row(uint8_t row) {
114 return matrix[row];
115}
116
117void matrix_print(void) {
118 print("\nr\\c ABCDEFGHIJKLMNOPQR\n");
119 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
120 matrix_row_t matrix_row = matrix_get_row(row);
121 xprintf("%02X: ", row);
122 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
123 bool curr_bit = matrix_row & (1<<col);
124 xprintf("%c", curr_bit ? '*' : '.');
125 }
126 print("\n");
127 }
128}
129
130uint8_t matrix_key_count(void) {
131 uint8_t count = 0;
132 for (uint8_t row = 0; row < MATRIX_ROWS; row++)
133 count += bitpop32(matrix[row]);
134 return count;
135} \ No newline at end of file