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