aboutsummaryrefslogtreecommitdiff
path: root/keyboard/hhkb/matrix.c
diff options
context:
space:
mode:
authorJack Humbert <jack.humb@gmail.com>2016-06-21 22:39:54 -0400
committerGitHub <noreply@github.com>2016-06-21 22:39:54 -0400
commit649b33d7783cf3021928534b7ae127e0a89e8807 (patch)
treec2b5e0cf8ff4aa2918e3b88ab75dbdb071cc0a1d /keyboard/hhkb/matrix.c
parent464c8e274f993d3571fe5ea5e836fe55a3912ffe (diff)
downloadqmk_firmware-649b33d7783cf3021928534b7ae127e0a89e8807.tar.gz
qmk_firmware-649b33d7783cf3021928534b7ae127e0a89e8807.zip
Renames keyboard folder to keyboards, adds couple of tmk's fixes (#432)
* fixes from tmk's repo * rename keyboard to keyboards
Diffstat (limited to 'keyboard/hhkb/matrix.c')
-rw-r--r--keyboard/hhkb/matrix.c204
1 files changed, 0 insertions, 204 deletions
diff --git a/keyboard/hhkb/matrix.c b/keyboard/hhkb/matrix.c
deleted file mode 100644
index 2dfb2f5e1..000000000
--- a/keyboard/hhkb/matrix.c
+++ /dev/null
@@ -1,204 +0,0 @@
1/*
2Copyright 2011 Jun Wako <wakojun@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/*
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 "hhkb_avr.h"
30#include <avr/wdt.h>
31#include "suspend.h"
32#include "lufa.h"
33
34
35// matrix power saving
36#define MATRIX_POWER_SAVE 10000
37static uint32_t matrix_last_modified = 0;
38
39// matrix state buffer(1:on, 0:off)
40static matrix_row_t *matrix;
41static matrix_row_t *matrix_prev;
42static matrix_row_t _matrix0[MATRIX_ROWS];
43static matrix_row_t _matrix1[MATRIX_ROWS];
44
45
46inline
47uint8_t matrix_rows(void)
48{
49 return MATRIX_ROWS;
50}
51
52inline
53uint8_t matrix_cols(void)
54{
55 return MATRIX_COLS;
56}
57
58void matrix_init(void)
59{
60#ifdef DEBUG
61 debug_enable = true;
62 debug_keyboard = true;
63#endif
64
65 KEY_INIT();
66
67 // initialize matrix state: all keys off
68 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
69 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
70 matrix = _matrix0;
71 matrix_prev = _matrix1;
72}
73
74uint8_t matrix_scan(void)
75{
76 uint8_t *tmp;
77
78 tmp = matrix_prev;
79 matrix_prev = matrix;
80 matrix = tmp;
81
82 // power on
83 if (!KEY_POWER_STATE()) KEY_POWER_ON();
84 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
85 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
86 KEY_SELECT(row, col);
87 _delay_us(5);
88
89 // Not sure this is needed. This just emulates HHKB controller's behaviour.
90 if (matrix_prev[row] & (1<<col)) {
91 KEY_PREV_ON();
92 }
93 _delay_us(10);
94
95 // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE.
96 // If V-USB interrupts in this section we could lose 40us or so
97 // and would read invalid value from KEY_STATE.
98 uint8_t last = TIMER_RAW;
99
100 KEY_ENABLE();
101
102 // Wait for KEY_STATE outputs its value.
103 // 1us was ok on one HHKB, but not worked on another.
104 // no wait doesn't work on Teensy++ with pro(1us works)
105 // no wait does work on tmk PCB(8MHz) with pro2
106 // 1us wait does work on both of above
107 // 1us wait doesn't work on tmk(16MHz)
108 // 5us wait does work on tmk(16MHz)
109 // 5us wait does work on tmk(16MHz/2)
110 // 5us wait does work on tmk(8MHz)
111 // 10us wait does work on Teensy++ with pro
112 // 10us wait does work on 328p+iwrap with pro
113 // 10us wait doesn't work on tmk PCB(8MHz) with pro2(very lagged scan)
114 _delay_us(5);
115
116 if (KEY_STATE()) {
117 matrix[row] &= ~(1<<col);
118 } else {
119 matrix[row] |= (1<<col);
120 }
121
122 // Ignore if this code region execution time elapses more than 20us.
123 // MEMO: 20[us] * (TIMER_RAW_FREQ / 1000000)[count per us]
124 // MEMO: then change above using this rule: a/(b/c) = a*1/(b/c) = a*(c/b)
125 if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) {
126 matrix[row] = matrix_prev[row];
127 }
128
129 _delay_us(5);
130 KEY_PREV_OFF();
131 KEY_UNABLE();
132
133 // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE.
134 // This takes 25us or more to make sure KEY_STATE returns to idle state.
135#ifdef HHKB_JP
136 // Looks like JP needs faster scan due to its twice larger matrix
137 // or it can drop keys in fast key typing
138 _delay_us(30);
139#else
140 _delay_us(75);
141#endif
142 }
143 if (matrix[row] ^ matrix_prev[row]) matrix_last_modified = timer_read32();
144 }
145 // power off
146 if (KEY_POWER_STATE() &&
147 (USB_DeviceState == DEVICE_STATE_Suspended ||
148 USB_DeviceState == DEVICE_STATE_Unattached ) &&
149 timer_elapsed32(matrix_last_modified) > MATRIX_POWER_SAVE) {
150 KEY_POWER_OFF();
151 suspend_power_down();
152 }
153 return 1;
154}
155
156bool matrix_is_modified(void)
157{
158 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
159 if (matrix[i] != matrix_prev[i])
160 return true;
161 }
162 return false;
163}
164
165inline
166bool matrix_has_ghost(void)
167{
168 return false;
169}
170
171inline
172bool matrix_is_on(uint8_t row, uint8_t col)
173{
174 return (matrix[row] & (1<<col));
175}
176
177inline
178matrix_row_t matrix_get_row(uint8_t row)
179{
180 return matrix[row];
181}
182
183void matrix_print(void)
184{
185 print("\nr/c 01234567\n");
186 for (uint8_t row = 0; row < matrix_rows(); row++) {
187 xprintf("%02X: %08b\n", row, bitrev(matrix_get_row(row)));
188 }
189}
190
191uint8_t matrix_key_count(void) {
192 uint8_t count = 0;
193 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
194 count += bitpop16(matrix_get_row(r));
195 }
196 return count;
197}
198
199void matrix_power_up(void) {
200 KEY_POWER_ON();
201}
202void matrix_power_down(void) {
203 KEY_POWER_OFF();
204}