]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/unloved_bastard/matrix.c
Some more tweaks to make everything look nice
[qmk_firmware.git] / keyboards / unloved_bastard / matrix.c
1 /*
2   Copyright 2017 Gabriel Young <gabeplaysdrums@live.com>
3   Copyright 2018 Alexander Fougner <fougner89 at 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 #include <stdint.h>
20 #include <stdbool.h>
21 #include <avr/io.h>
22 #include <util/delay.h>
23 #include "print.h"
24 #include "debug.h"
25 #include "util.h"
26 #include "matrix.h"
27
28 #ifndef DEBOUNCING_DELAY
29 #   define DEBOUNCING_DELAY 5
30 #endif
31 static uint8_t debouncing = DEBOUNCING_DELAY;
32
33 static matrix_row_t matrix[MATRIX_ROWS];
34 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
35
36 static matrix_row_t scan_col(void) {
37     return (
38         (PINC&(1<<7) ? 0 : ((matrix_row_t)1<<0)) |
39         (PINB&(1<<6) ? 0 : ((matrix_row_t)1<<1)) |
40         (PINB&(1<<1) ? 0 : ((matrix_row_t)1<<2)) |
41         (PINB&(1<<0) ? 0 : ((matrix_row_t)1<<3)) |
42         (PINB&(1<<5) ? 0 : ((matrix_row_t)1<<4)) |
43         (PINB&(1<<3) ? 0 : ((matrix_row_t)1<<5)) |
44         (PINB&(1<<4) ? 0 : ((matrix_row_t)1<<6)) |
45         (PINB&(1<<2) ? 0 : ((matrix_row_t)1<<7))
46     );
47 }
48
49 static void select_col(uint8_t col) {
50     switch (col) {
51         case 0: PORTD = (PORTD & ~0b00111111) | 0b00110011; break;
52         case 1: PORTD = (PORTD & ~0b00111111) | 0b00100011; break;
53         case 2: PORTD = (PORTD & ~0b00111111) | 0b00010111; break;
54         case 3: PORTD = (PORTD & ~0b00111111) | 0b00110011; break;
55         case 4: PORTD = (PORTD & ~0b00111111) | 0b00010011; break;
56         case 5: PORTD = (PORTD & ~0b00111111) | 0b00011011; break;
57         case 6: PORTD = (PORTD & ~0b00111111) | 0b00110100; break;
58         case 7: PORTD = (PORTD & ~0b00111111) | 0b00111010; break;
59         case 8: PORTD = (PORTD & ~0b00111111) | 0b00111000; break;
60         case 9: PORTD = (PORTD & ~0b00111111) | 0b00111100; break;
61         case 10: PORTD = (PORTD & ~0b00111111) | 0b00110010; break;
62         case 11: PORTD = (PORTD & ~0b00111111) | 0b00011111; break;
63         case 12: PORTD = (PORTD & ~0b00111111) | 0b00001111; break;
64         case 13: PORTD = (PORTD & ~0b00111111) | 0b00100111; break;
65         case 14: PORTD = (PORTD & ~0b00111111) | 0b00000111; break;
66         case 15: PORTD = (PORTD & ~0b00111111) | 0b00110110; break;
67         case 16: PORTD = (PORTD & ~0b00111111) | 0b00001011; break;
68         case 17: PORTD = (PORTD & ~0b00111111) | 0b00000011; break;
69     }
70 }
71
72 void matrix_init(void) {
73     /* Row output pins */
74     DDRD  |=  0b00111111;
75     /* Column input pins */
76     DDRC  &= ~0b10000000;
77     DDRB  &= ~0b01111111;
78     PORTC |=  0b10000000;
79     PORTB |=  0b01111111;
80
81     for (uint8_t i=0; i < MATRIX_ROWS; i++)
82         matrix[i] = matrix_debouncing[i] = 0;
83
84     matrix_init_quantum();
85 }
86
87 uint8_t matrix_scan(void) {
88     for (uint8_t col = 0; col < MATRIX_COLS; col++) {
89         select_col(col);
90         _delay_us(3);
91         matrix_row_t col_scan = scan_col();
92         for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
93             bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
94             bool curr_bit = col_scan & (1<<row);
95             if (prev_bit != curr_bit) {
96                 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
97                 debouncing = DEBOUNCING_DELAY;
98             }
99         }
100     }
101
102     if (debouncing) {
103         if (--debouncing)
104             _delay_ms(1);
105         else
106             for (uint8_t i = 0; i < MATRIX_ROWS; i++)
107                 matrix[i] = matrix_debouncing[i];
108     }
109
110     matrix_scan_quantum();
111     return 1;
112 }
113
114 inline matrix_row_t matrix_get_row(uint8_t row) {
115     return matrix[row];
116 }
117
118 void matrix_print(void) {
119 #ifndef NO_PRINT
120     print("\nr\\c ABCDEFGHIJKLMNOPQR\n");
121     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
122         matrix_row_t matrix_row = matrix_get_row(row);
123         xprintf("%02X: ", row);
124         for (uint8_t col = 0; col < MATRIX_COLS; col++) {
125             bool curr_bit = matrix_row & (1<<col);
126             xprintf("%c", curr_bit ? '*' : '.');
127         }
128         print("\n");
129     }
130 #endif
131 }
132
133 uint8_t matrix_key_count(void) {
134     uint8_t count = 0;
135     for (uint8_t row = 0; row < MATRIX_ROWS; row++)
136         count += bitpop32(matrix[row]);
137     return count;
138 }