]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/frosty_flake/matrix.c
Swap rows and columns to match the schematic
[qmk_firmware.git] / keyboards / frosty_flake / matrix.c
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 #define CONFIG_LED_IO \
28   DDRB |= (1<<7); \
29   DDRC |= (1<<5) | (1<<6);
30
31 #define USB_LED_CAPS_LOCK_ON    PORTC &= ~(1<<5)
32 #define USB_LED_CAPS_LOCK_OFF   PORTC |=  (1<<5)
33 #define USB_LED_NUM_LOCK_ON     PORTB &= ~(1<<7)
34 #define USB_LED_NUM_LOCK_OFF    PORTB |=  (1<<7)
35 #define USB_LED_SCROLL_LOCK_ON  PORTC &= ~(1<<6)
36 #define USB_LED_SCROLL_LOCK_OFF PORTC |=  (1<<6)
37
38 #ifndef DEBOUNCING_DELAY
39 #   define DEBOUNCING_DELAY 5
40 #endif
41 static uint8_t debouncing = DEBOUNCING_DELAY;
42
43 static matrix_row_t matrix[MATRIX_ROWS];
44 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
45
46 static matrix_row_t scan_col(void);
47 static void select_col(uint8_t row);
48
49 void matrix_init(void) {
50     /* Row output pins */
51     DDRD  |=  0b01111011;
52     /* Column input pins */
53     DDRC  &= ~0b10000000;
54     DDRB  &= ~0b01111111;
55     PORTC |=  0b10000000;
56     PORTB |=  0b01111111;
57
58     for (uint8_t i=0; i < MATRIX_ROWS; i++)
59         matrix[i] = matrix_debouncing[i] = 0;
60
61     matrix_init_quantum();
62 }
63
64 uint8_t matrix_scan(void) {
65   for (uint8_t col = 0; col < MATRIX_COLS; col++) {
66     select_col(col);
67     _delay_us(3);
68     matrix_row_t col_scan = scan_col();
69     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
70       bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
71       bool curr_bit = col_scan & (1<<row);
72       if (prev_bit != curr_bit) {
73         matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
74         debouncing = DEBOUNCING_DELAY;
75       }
76     }
77   }
78
79   if (debouncing) {
80     if (--debouncing)
81       _delay_ms(1);
82     else
83       for (uint8_t i = 0; i < MATRIX_ROWS; i++)
84         matrix[i] = matrix_debouncing[i];
85   }
86
87   matrix_scan_quantum();
88   return 1;
89 }
90
91 inline matrix_row_t matrix_get_row(uint8_t row) {
92   return matrix[row];
93 }
94
95 void matrix_print(void) {
96   print("\nr\\c ABCDEFGHIJKLMNOPQR\n");
97   for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
98     matrix_row_t matrix_row = matrix_get_row(row);
99     xprintf("%02X: ", row);
100     for (uint8_t col = 0; col < MATRIX_COLS; col++) {
101       bool curr_bit = matrix_row & (1<<col);
102       xprintf("%c", curr_bit ? '*' : '.');
103     }
104     print("\n");
105   }
106 }
107
108 uint8_t matrix_key_count(void) {
109   uint8_t count = 0;
110   for (uint8_t row = 0; row < MATRIX_ROWS; row++)
111     count += bitpop32(matrix[row]);
112   return count;
113 }
114
115 static matrix_row_t scan_col(void) {
116     return (
117         (PINC&(1<<7) ? 0 : ((matrix_row_t)1<<0)) |
118         (PINB&(1<<5) ? 0 : ((matrix_row_t)1<<1)) |
119         (PINB&(1<<4) ? 0 : ((matrix_row_t)1<<2)) |
120         (PINB&(1<<6) ? 0 : ((matrix_row_t)1<<3)) |
121         (PINB&(1<<1) ? 0 : ((matrix_row_t)1<<4)) |
122         (PINB&(1<<2) ? 0 : ((matrix_row_t)1<<5)) |
123         (PINB&(1<<3) ? 0 : ((matrix_row_t)1<<6)) |
124         (PINB&(1<<0) ? 0 : ((matrix_row_t)1<<7))
125     );
126 }
127
128 static void select_col(uint8_t col) {
129     switch (col) {
130         case  0: PORTD = (PORTD & ~0b01111011) | 0b00011011; break;
131         case  1: PORTD = (PORTD & ~0b01111011) | 0b01000011; break;
132         case  2: PORTD = (PORTD & ~0b01111011) | 0b01101010; break;
133         case  3: PORTD = (PORTD & ~0b01111011) | 0b01111001; break;
134         case  4: PORTD = (PORTD & ~0b01111011) | 0b01100010; break;
135         case  5: PORTD = (PORTD & ~0b01111011) | 0b01110001; break;
136         case  6: PORTD = (PORTD & ~0b01111011) | 0b01100001; break;
137         case  7: PORTD = (PORTD & ~0b01111011) | 0b01110000; break;
138         case  8: PORTD = (PORTD & ~0b01111011) | 0b01100000; break;
139         case  9: PORTD = (PORTD & ~0b01111011) | 0b01101000; break;
140         case 10: PORTD = (PORTD & ~0b01111011) | 0b00101011; break;
141         case 11: PORTD = (PORTD & ~0b01111011) | 0b00110011; break;
142         case 12: PORTD = (PORTD & ~0b01111011) | 0b00100011; break;
143         case 13: PORTD = (PORTD & ~0b01111011) | 0b01111000; break;
144         case 14: PORTD = (PORTD & ~0b01111011) | 0b00010011; break;
145         case 15: PORTD = (PORTD & ~0b01111011) | 0b01101001; break;
146         case 16: PORTD = (PORTD & ~0b01111011) | 0b00001011; break;
147         case 17: PORTD = (PORTD & ~0b01111011) | 0b00111011; break;
148     }
149 }