]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - keyboard/fc660c/fc660c.c
2d63cdeed9769223166ecf8f1ea3f704b885b37a
[max/tmk_keyboard.git] / keyboard / fc660c / fc660c.c
1 /*
2 Copyright 2017 Jun Wako <wakojun@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 /*
19  * scan matrix
20  */
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <util/delay.h>
24 #include "print.h"
25 #include "debug.h"
26 #include "util.h"
27 #include "timer.h"
28 #include "matrix.h"
29 #include <avr/wdt.h>
30 #include "suspend.h"
31 #include "lufa.h"
32 #include "led.h"
33 #include "fc660c.h"
34
35
36 static uint32_t matrix_last_modified = 0;
37
38 // matrix state buffer(1:on, 0:off)
39 static matrix_row_t *matrix;
40 static matrix_row_t *matrix_prev;
41 static matrix_row_t _matrix0[MATRIX_ROWS];
42 static matrix_row_t _matrix1[MATRIX_ROWS];
43
44
45 void matrix_init(void)
46 {
47 #if 1
48     debug_enable = true;
49     debug_keyboard = true;
50     debug_matrix = true;
51 #endif
52
53     KEY_INIT();
54
55     // initialize matrix state: all keys off
56     for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
57     for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
58     matrix = _matrix0;
59     matrix_prev = _matrix1;
60 }
61
62 uint8_t matrix_scan(void)
63 {
64     matrix_row_t *tmp;
65
66     tmp = matrix_prev;
67     matrix_prev = matrix;
68     matrix = tmp;
69
70     uint8_t row, col;
71     for (col = 0; col < MATRIX_COLS; col++) {
72         SET_COL(col);
73         for (row = 0; row < MATRIX_ROWS; row++) {
74             //KEY_SELECT(row, col);
75             SET_ROW(row);
76             _delay_us(2);
77
78             // Not sure this is needed. This just emulates HHKB controller's behaviour.
79             if (matrix_prev[row] & (1<<col)) {
80                 KEY_HYS_ON();
81             }
82             _delay_us(10);
83
84             // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE.
85             // If V-USB interrupts in this section we could lose 40us or so
86             // and would read invalid value from KEY_STATE.
87             uint8_t last = TIMER_RAW;
88
89             KEY_ENABLE();
90
91             // Wait for KEY_STATE outputs its value.
92             _delay_us(2);
93
94             if (KEY_STATE()) {
95                 matrix[row] &= ~(1<<col);
96             } else {
97                 matrix[row] |= (1<<col);
98             }
99
100             // Ignore if this code region execution time elapses more than 20us.
101             // MEMO: 20[us] * (TIMER_RAW_FREQ / 1000000)[count per us]
102             // MEMO: then change above using this rule: a/(b/c) = a*1/(b/c) = a*(c/b)
103             if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) {
104                 matrix[row] = matrix_prev[row];
105             }
106
107             _delay_us(5);
108             KEY_HYS_OFF();
109             KEY_UNABLE();
110
111             // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE.
112             // This takes 25us or more to make sure KEY_STATE returns to idle state.
113             _delay_us(75);
114         }
115         if (matrix[row] ^ matrix_prev[row]) {
116             matrix_last_modified = timer_read32();
117         }
118     }
119     return 1;
120 }
121
122 inline
123 matrix_row_t matrix_get_row(uint8_t row)
124 {
125     return matrix[row];
126 }
127
128 void led_set(uint8_t usb_led)
129 {
130     if (usb_led & (1<<USB_LED_CAPS_LOCK)) {
131     } else {
132     }
133 }