2 Copyright 2011 Jun Wako <wakojun@gmail.com>
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.
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.
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/>.
17 Ported to QMK by Peter Roe <pete@13bit.me>
23 #include <util/delay.h>
34 static bool is_iso_layout = false;
36 // matrix state buffer(1:on, 0:off)
37 static matrix_row_t matrix[MATRIX_ROWS];
39 static void register_key(uint8_t key);
41 __attribute__ ((weak))
42 void matrix_init_kb(void) {
46 __attribute__ ((weak))
47 void matrix_scan_kb(void) {
51 __attribute__ ((weak))
52 void matrix_init_user(void) {
55 __attribute__ ((weak))
56 void matrix_scan_user(void) {
59 void matrix_init(void)
62 DDRD |= (1<<6); PORTD |= (1<<6);
65 // wait for keyboard to boot up and receive command
68 // initialize matrix state: all keys off
69 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
71 led_set(host_keyboard_leds());
73 // debug_enable = false;
74 // debug_matrix = true;
75 // debug_keyboard = true;
76 // debug_mouse = true;
77 // print("debug enabled.\n");
80 DDRD |= (1<<6); PORTD &= ~(1<<6);
81 matrix_init_quantum();
84 #ifdef ADB_MOUSE_ENABLE
89 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
91 static report_mouse_t mouse_report = {};
93 void adb_mouse_task(void)
97 static int8_t mouseacc;
99 /* tick of last polling */
100 static uint16_t tick_ms;
102 // polling with 12ms interval
103 if (timer_elapsed(tick_ms) < 12) return;
104 tick_ms = timer_read();
106 codes = adb_host_mouse_recv();
107 // If nothing received reset mouse acceleration, and quit.
112 // Bit sixteen is button.
113 if (~codes & (1 << 15))
114 mouse_report.buttons |= MOUSE_BTN1;
115 if (codes & (1 << 15))
116 mouse_report.buttons &= ~MOUSE_BTN1;
117 // lower seven bits are movement, as signed int_7.
118 // low byte is X-axis, high byte is Y.
119 y = (codes>>8 & 0x3F);
120 x = (codes>>0 & 0x3F);
121 // bit seven and fifteen is negative
122 // usb does not use int_8, but int_7 (measuring distance) with sign-bit.
123 if (codes & (1 << 6))
125 if (codes & (1 << 14))
127 // Accelerate mouse. (They weren't meant to be used on screens larger than 320x200).
130 // Cap our two bytes per axis to one byte.
131 // Easier with a MIN-function, but since -MAX(-a,-b) = MIN(a,b)...
132 // I.E. MIN(MAX(x,-127),127) = -MAX(-MAX(x, -127), -127) = MIN(-MIN(-x,127),127)
133 mouse_report.x = -MAX(-MAX(x, -127), -127);
134 mouse_report.y = -MAX(-MAX(y, -127), -127);
136 print("adb_host_mouse_recv: "); print_bin16(codes); print("\n");
137 print("adb_mouse raw: [");
138 phex(mouseacc); print(" ");
139 phex(mouse_report.buttons); print("|");
140 print_decs(mouse_report.x); print(" ");
141 print_decs(mouse_report.y); print("]\n");
143 // Send result by usb.
144 host_mouse_send(&mouse_report);
145 // increase acceleration of mouse
146 mouseacc += ( mouseacc < ADB_MOUSE_MAXACC ? 1 : 0 );
151 uint8_t matrix_scan(void)
153 /* extra_key is volatile and more convoluted than necessary because gcc refused
154 to generate valid code otherwise. Making extra_key uint8_t and constructing codes
155 here via codes = extra_key<<8 | 0xFF; would consistently fail to even LOAD
156 extra_key from memory, and leave garbage in the high byte of codes. I tried
157 dozens of code variations and it kept generating broken assembly output. So
158 beware if attempting to make extra_key code more logical and efficient. */
159 static volatile uint16_t extra_key = 0xFFFF;
163 /* tick of last polling */
164 static uint16_t tick_ms;
169 if ( codes == 0xFFFF )
171 // polling with 12ms interval
172 if (timer_elapsed(tick_ms) < 12) return 0;
173 tick_ms = timer_read();
175 codes = adb_host_kbd_recv();
181 if (debug_matrix && codes) {
182 print("adb_host_kbd_recv: "); phex16(codes); print("\n");
185 if (codes == 0) { // no keys
187 } else if (codes == 0x7F7F) { // power key press
189 } else if (codes == 0xFFFF) { // power key release
191 } else if (key0 == 0xFF) { // error
192 xprintf("adb_host_kbd_recv: ERROR(%d)\n", codes);
193 // something wrong or plug-in
197 /* Swap codes for ISO keyboard
198 * https://github.com/tmk/tmk_keyboard/issues/35
201 * ,----------- ----------.
202 * | *a| 1| 2 =|Backspa|
203 * |----------- ----------|
205 * |----------- ----------|
206 * |CapsLo| A| '|Return |
207 * |----------- ----------|
209 * `----------- ----------'
212 * ,----------- ----------.
213 * | *a| 1| 2 =|Backspa|
214 * |----------- ----------|
215 * |Tab | Q| | ]|Retur|
216 * |----------- -----` |
217 * |CapsLo| A| '| *c| |
218 * |----------- ----------|
220 * `----------- ----------'
222 * ADB scan code USB usage
223 * ------------- ---------
224 * Key ANSI ISO ANSI ISO
225 * ---------------------------------------------
226 * *a 0x32 0x0A 0x35 0x35
227 * *b ---- 0x32 ---- 0x64
228 * *c 0x2A 0x2A 0x31 0x31(or 0x32)
231 if ((key0 & 0x7F) == 0x32) {
232 key0 = (key0 & 0x80) | 0x0A;
233 } else if ((key0 & 0x7F) == 0x0A) {
234 key0 = (key0 & 0x80) | 0x32;
238 if (key1 != 0xFF) // key1 is 0xFF when no second key.
239 extra_key = key1<<8 | 0xFF; // process in a separate call
242 matrix_scan_quantum();
246 void matrix_print(void){
251 matrix_row_t matrix_get_row(uint8_t row)
257 static void register_key(uint8_t key)
263 matrix[row] &= ~(1<<col);
265 matrix[row] |= (1<<col);