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