]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/ymd75/matrix.c
Remove more commented out MCUs
[qmk_firmware.git] / keyboards / ymd75 / matrix.c
1 /*
2 Copyright 2017 Luiz Ribeiro <luizribeiro@gmail.com>
3 Modified 2018 by Wayne K Jones <github.com/WarmCatUK>
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 <avr/io.h>
20 #include <util/delay.h>
21
22 #include "matrix.h"
23
24 #ifndef DEBOUNCE
25 #   define DEBOUNCE     5
26 #endif
27
28 static uint8_t debouncing = DEBOUNCE;
29
30 static matrix_row_t matrix[MATRIX_ROWS];
31 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
32
33 void matrix_init(void) {
34     // all outputs for rows high
35     DDRB = 0xFF;
36     PORTB = 0xFF;
37     // all inputs for columns
38     DDRA = 0x00;
39     DDRC &= ~(0x111111<<2);
40     DDRD &= ~(1<<PIND7);
41     // all columns are pulled-up
42     PORTA = 0xFF;
43     PORTC |= (0b111111<<2);
44     PORTD |= (1<<PIND7);
45
46     // initialize matrix state: all keys off
47     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
48         matrix[row] = 0x00;
49         matrix_debouncing[row] = 0x00;
50     }
51     matrix_init_quantum();  // missing from original port by Luiz
52 }
53
54 void matrix_set_row_status(uint8_t row) {
55     DDRB = (1 << row);
56     PORTB = ~(1 << row);
57 }
58
59 uint8_t bit_reverse(uint8_t x) {
60     x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa);
61     x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc);
62     x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0);
63     return x;
64 }
65
66 uint8_t matrix_scan(void) {
67     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
68         matrix_set_row_status(row);
69         _delay_us(5);
70
71         matrix_row_t cols = (
72             // cols 0..7, PORTA 0 -> 7
73             (~PINA) & 0xFF
74         ) | (
75             // cols 8..13, PORTC 7 -> 0
76             bit_reverse((~PINC) & 0xFF) << 8
77         ) | (
78                   // col 14, PORTD 7
79                   ((~PIND) & (1 << PIND7)) << 7
80                   );
81
82         if (matrix_debouncing[row] != cols) {
83             matrix_debouncing[row] = cols;
84             debouncing = DEBOUNCE;
85         }
86     }
87
88     if (debouncing) {
89         if (--debouncing) {
90             _delay_ms(1);
91         } else {
92             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
93                 matrix[i] = matrix_debouncing[i];
94             }
95         }
96     }
97     matrix_scan_quantum();  // also missing in original PS2AVRGB implementation
98     //matrix_scan_user();
99
100     return 1;
101 }
102
103 inline matrix_row_t matrix_get_row(uint8_t row) {
104     return matrix[row];
105 }
106
107 void matrix_print(void) {
108 }