]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/bminiex/matrix.c
Keyboard: bminiex : Working backlight (#4171)
[qmk_firmware.git] / keyboards / bminiex / 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 #include "backlight.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 __attribute__ ((weak))
34 void matrix_init_user(void) {}
35 __attribute__ ((weak))
36 void matrix_scan_user(void) {}
37 __attribute__ ((weak))
38 void matrix_init_kb(void) {
39   matrix_init_user();
40 }
41 __attribute__ ((weak))
42 void matrix_scan_kb(void) {
43   matrix_scan_user();
44 }
45
46 void matrix_init(void) {
47     // all outputs for rows high
48     DDRB = 0xFF;
49     PORTB = 0xFF;
50     // all inputs for columns
51     DDRA = 0x00;
52     DDRC &= ~(0x111111<<2);
53     DDRD &= ~(1<<PIND7);
54     // all columns are pulled-up
55     PORTA = 0xFF;
56     PORTC |= (0b111111<<2);
57     PORTD |= (1<<PIND7);
58
59     // initialize matrix state: all keys off
60     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
61         matrix[row] = 0x00;
62         matrix_debouncing[row] = 0x00;
63     }
64     
65     matrix_init_quantum();
66 }
67
68 void matrix_set_row_status(uint8_t row) {
69     DDRB = (1 << row);
70     PORTB = ~(1 << row);
71 }
72
73 uint8_t bit_reverse(uint8_t x) {
74     x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa);
75     x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc);
76     x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0);
77     return x;
78 }
79
80 uint8_t matrix_scan(void) {
81     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
82         matrix_set_row_status(row);
83         _delay_us(5);
84
85         matrix_row_t cols = (
86             // cols 0..7, PORTA 0 -> 7
87             (~PINA) & 0xFF
88         ) | (
89             // cols 8..13, PORTC 7 -> 0
90             bit_reverse((~PINC) & 0xFF) << 8
91         ) | (
92             // col 14, PORTD 7
93             ((~PIND) & (1 << PIND7)) << 7
94         );
95
96         if (matrix_debouncing[row] != cols) {
97             matrix_debouncing[row] = cols;
98             debouncing = DEBOUNCE;
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     }
111
112     matrix_scan_quantum();
113
114     return 1;
115 }
116
117 inline matrix_row_t matrix_get_row(uint8_t row) {
118     return matrix[row];
119 }
120
121 void matrix_print(void) {
122 }