aboutsummaryrefslogtreecommitdiff
path: root/keyboards/ymdk_np21/matrix.c
diff options
context:
space:
mode:
authorsanosis <github@sanosis.waw.pl>2018-01-28 21:58:03 +0100
committerJack Humbert <jack.humb@gmail.com>2018-01-28 15:58:03 -0500
commit2480e5d69a11fd0774d16e57384b69c1e92cbfc8 (patch)
tree18fc6b69054a032c8c2afabfd92ecbb8d3b6c558 /keyboards/ymdk_np21/matrix.c
parent056ecb1463de9fd5ab17e4b15893b2fbd2b1c339 (diff)
downloadqmk_firmware-2480e5d69a11fd0774d16e57384b69c1e92cbfc8.tar.gz
qmk_firmware-2480e5d69a11fd0774d16e57384b69c1e92cbfc8.zip
Support for YMDK NP21 (#2316)
* ymdk_np21 initial support Base support of ymdk_np21 - based on jj40. Full grid layout * Update README.md Replacing description. * Adding YMDK NP21 to comunity list. Adding YMDK NP21 to community supported list.
Diffstat (limited to 'keyboards/ymdk_np21/matrix.c')
-rw-r--r--keyboards/ymdk_np21/matrix.c118
1 files changed, 118 insertions, 0 deletions
diff --git a/keyboards/ymdk_np21/matrix.c b/keyboards/ymdk_np21/matrix.c
new file mode 100644
index 000000000..a9e9cb539
--- /dev/null
+++ b/keyboards/ymdk_np21/matrix.c
@@ -0,0 +1,118 @@
1/*
2Copyright 2017 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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
27static uint8_t debouncing = DEBOUNCE;
28
29static matrix_row_t matrix[MATRIX_ROWS];
30static matrix_row_t matrix_debouncing[MATRIX_ROWS];
31
32void matrix_set_row_status(uint8_t row);
33uint8_t bit_reverse(uint8_t x);
34
35void 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 matrix_init_quantum(); // missing from original port by Luiz
55}
56
57uint8_t matrix_scan(void) {
58 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
59 matrix_set_row_status(row);
60 _delay_us(5);
61
62 matrix_row_t cols = (
63 // cols 0..7, PORTA 0 -> 7
64 (~PINA) & 0xFF
65 ) | (
66 // cols 8..13, PORTC 7 -> 0
67 bit_reverse((~PINC) & 0xFF) << 8
68 ) | (
69 // col 14, PORTD 7
70 ((~PIND) & (1 << PIND7)) << 7
71 );
72
73 if (matrix_debouncing[row] != cols) {
74 matrix_debouncing[row] = cols;
75 debouncing = DEBOUNCE;
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 }
88
89 matrix_scan_quantum(); // also missing in original PS2AVRGB implementation
90
91 return 1;
92}
93
94void matrix_scan_kb(void) {
95 // Looping keyboard code goes here
96 // This runs every cycle (a lot)
97 matrix_scan_user();
98};
99
100// declarations
101void matrix_set_row_status(uint8_t row) {
102 DDRB = (1 << row);
103 PORTB = ~(1 << row);
104}
105
106uint8_t bit_reverse(uint8_t x) {
107 x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa);
108 x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc);
109 x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0);
110 return x;
111}
112
113inline matrix_row_t matrix_get_row(uint8_t row) {
114 return matrix[row];
115}
116
117void matrix_print(void) {
118}