]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/fc980c/matrix.c
[Keyboard] leds in default keymap (#6357)
[qmk_firmware.git] / keyboards / fc980c / matrix.c
1 /*
2 Copyright 2017 Balz Guenat
3 based on work by Jun Wako <wakojun@gmail.com>
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 /*
20  * scan matrix
21  */
22 #include <stdint.h>
23 #include <stdbool.h>
24 #include "wait.h"
25 #include "print.h"
26 #include "debug.h"
27 #include "util.h"
28 #include "timer.h"
29 #include "matrix.h"
30 #include "led.h"
31 // #include QMK_KEYBOARD_H
32
33
34 // Timer resolution check
35 #if (1000000/TIMER_RAW_FREQ > 20)
36 #   error "Timer resolution(>20us) is not enough for HHKB matrix scan tweak on V-USB."
37 #endif
38
39
40 /*
41  * Pin configuration for ATMega32U4
42  *
43  * Row:     PD4-6, PD7(~EN)
44  * Col:     PB0-3
45  * Key:     PC6(pull-uped)
46  * Hys:     PC7
47  */
48 static inline void KEY_ENABLE(void) { (PORTD &= ~(1<<7)); }
49 static inline void KEY_UNABLE(void) { (PORTD |=  (1<<7)); }
50 static inline bool KEY_STATE(void) { return (PINC & (1<<6)); }
51 static inline void KEY_HYS_ON(void) { (PORTC |=  (1<<7)); }
52 static inline void KEY_HYS_OFF(void) { (PORTC &= ~(1<<7)); }
53 static inline void KEY_INIT(void)
54 {
55     /* Col */
56     DDRB  |=  0x0F;
57     /* Key: input with pull-up */
58     DDRC  &= ~(1<<6);
59     PORTC |=  (1<<6);
60     /* Hys */
61     DDRC  |=  (1<<7);
62     /* Row */
63     DDRD  |=  0xF0;
64
65     KEY_UNABLE();
66     KEY_HYS_OFF();
67 }
68 static inline void SET_ROW(uint8_t ROW)
69 {
70     // PD4-6
71     PORTD = (PORTD & 0x8F) | ((ROW & 0x07) << 4);
72 }
73 static inline void SET_COL(uint8_t COL)
74 {
75     // PB0-3
76     PORTB = (PORTB & 0xF0) | (COL & 0x0F);
77 }
78
79 static uint32_t matrix_last_modified = 0;
80
81 // matrix state buffer(1:on, 0:off)
82 static matrix_row_t *matrix;
83 static matrix_row_t *matrix_prev;
84 static matrix_row_t _matrix0[MATRIX_ROWS];
85 static matrix_row_t _matrix1[MATRIX_ROWS];
86
87
88
89 __attribute__ ((weak))
90 void matrix_init_kb(void) {
91     matrix_init_user();
92 }
93
94 __attribute__ ((weak))
95 void matrix_scan_kb(void) {
96     matrix_scan_user();
97 }
98
99 __attribute__ ((weak))
100 void matrix_init_user(void) {
101 }
102
103 __attribute__ ((weak))
104 void matrix_scan_user(void) {
105 }
106
107
108 void matrix_init(void)
109 {
110     debug_enable = true;
111     debug_matrix = true;
112
113     KEY_INIT();
114
115     // LEDs on NumLock, CapsLock and ScrollLock(PB4, PB5, PB6)
116     DDRB  |= (1<<4) | (1<<5) | (1<<6);
117     PORTB |= (1<<4) | (1<<5) | (1<<6);
118
119     // initialize matrix state: all keys off
120     for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
121     for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
122     matrix = _matrix0;
123     matrix_prev = _matrix1;
124     matrix_init_quantum();
125 }
126
127 uint8_t matrix_scan(void)
128 {
129     matrix_row_t *tmp;
130
131     tmp = matrix_prev;
132     matrix_prev = matrix;
133     matrix = tmp;
134
135     uint8_t row, col;
136     for (col = 0; col < MATRIX_COLS; col++) {
137         SET_COL(col);
138         for (row = 0; row < MATRIX_ROWS; row++) {
139             //KEY_SELECT(row, col);
140             SET_ROW(row);
141             _delay_us(2);
142
143             // Not sure this is needed. This just emulates HHKB controller's behaviour.
144             if (matrix_prev[row] & (1<<col)) {
145                 KEY_HYS_ON();
146             }
147             _delay_us(10);
148
149             // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE.
150             // If V-USB interrupts in this section we could lose 40us or so
151             // and would read invalid value from KEY_STATE.
152             uint8_t last = TIMER_RAW;
153
154             KEY_ENABLE();
155
156             // Wait for KEY_STATE outputs its value.
157             _delay_us(2);
158
159             if (KEY_STATE()) {
160                 matrix[row] &= ~(1<<col);
161             } else {
162                 matrix[row] |= (1<<col);
163             }
164
165             // Ignore if this code region execution time elapses more than 20us.
166             // MEMO: 20[us] * (TIMER_RAW_FREQ / 1000000)[count per us]
167             // MEMO: then change above using this rule: a/(b/c) = a*1/(b/c) = a*(c/b)
168             if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) {
169                 matrix[row] = matrix_prev[row];
170             }
171
172             _delay_us(5);
173             KEY_HYS_OFF();
174             KEY_UNABLE();
175
176             // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE.
177             // This takes 25us or more to make sure KEY_STATE returns to idle state.
178             _delay_us(75);
179         }
180         if (matrix[row] ^ matrix_prev[row]) {
181             matrix_last_modified = timer_read32();
182         }
183     }
184     matrix_scan_quantum();
185     return 1;
186 }
187
188 inline
189 matrix_row_t matrix_get_row(uint8_t row) {
190     return matrix[row];
191 }
192
193 void matrix_print(void)
194 {
195 #if (MATRIX_COLS <= 8)
196     print("r/c 01234567\n");
197 #elif (MATRIX_COLS <= 16)
198     print("r/c 0123456789ABCDEF\n");
199 #elif (MATRIX_COLS <= 32)
200     print("r/c 0123456789ABCDEF0123456789ABCDEF\n");
201 #endif
202
203     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
204
205 #if (MATRIX_COLS <= 8)
206         xprintf("%02X: %08b%s\n", row, bitrev(matrix_get_row(row)),
207 #elif (MATRIX_COLS <= 16)
208         xprintf("%02X: %016b%s\n", row, bitrev16(matrix_get_row(row)),
209 #elif (MATRIX_COLS <= 32)
210         xprintf("%02X: %032b%s\n", row, bitrev32(matrix_get_row(row)),
211 #endif
212 #ifdef MATRIX_HAS_GHOST
213         matrix_has_ghost_in_row(row) ?  " <ghost" : ""
214 #else
215         ""
216 #endif
217         );
218     }
219 }