]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/converter/adb_usb/matrix.c
Fix pinout of split hand and LED, remove flip half option
[qmk_firmware.git] / keyboards / converter / adb_usb / matrix.c
1 /*
2 Copyright 2011 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 Ported to QMK by Peter Roe <pete@13bit.me>
18 */
19
20 #include <stdint.h>
21 #include <stdbool.h>
22 #include <avr/io.h>
23 #include <util/delay.h>
24 #include "print.h"
25 #include "util.h"
26 #include "debug.h"
27 #include "adb.h"
28 #include "matrix.h"
29 #include "report.h"
30 #include "host.h"
31 #include "led.h"
32 #include "timer.h"
33
34 static bool is_iso_layout = false;
35
36 // matrix state buffer(1:on, 0:off)
37 static matrix_row_t matrix[MATRIX_ROWS];
38
39 static void register_key(uint8_t key);
40
41 __attribute__ ((weak))
42 void matrix_init_kb(void) {
43     matrix_init_user();
44 }
45
46 __attribute__ ((weak))
47 void matrix_scan_kb(void) {
48     matrix_scan_user();
49 }
50
51 __attribute__ ((weak))
52 void matrix_init_user(void) {
53 }
54
55 __attribute__ ((weak))
56 void matrix_scan_user(void) {
57 }
58
59 void matrix_init(void)
60 {
61     // LED on
62     DDRD |= (1<<6); PORTD |= (1<<6);
63
64     adb_host_init();
65     // wait for keyboard to boot up and receive command
66     _delay_ms(2000);
67
68     // initialize matrix state: all keys off
69     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
70
71     led_set(host_keyboard_leds());
72
73     // debug_enable = false;
74     // debug_matrix = true;
75     // debug_keyboard = true;
76     // debug_mouse = true;
77     // print("debug enabled.\n");
78
79     // LED off
80     DDRD |= (1<<6); PORTD &= ~(1<<6);
81     matrix_init_quantum();
82 }
83
84 #ifdef ADB_MOUSE_ENABLE
85
86 #ifdef MAX
87 #undef MAX
88 #endif
89 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
90
91 static report_mouse_t mouse_report = {};
92
93 void adb_mouse_task(void)
94 {
95     uint16_t codes;
96     int16_t x, y;
97     static int8_t mouseacc;
98
99     /* tick of last polling */
100     static uint16_t tick_ms;
101
102     // polling with 12ms interval
103     if (timer_elapsed(tick_ms) < 12) return;
104     tick_ms = timer_read();
105
106     codes = adb_host_mouse_recv();
107     // If nothing received reset mouse acceleration, and quit.
108     if (!codes) {
109         mouseacc = 1;
110         return;
111     };
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))
124           x = (x-0x40);
125     if (codes & (1 << 14))
126          y = (y-0x40);
127     // Accelerate mouse. (They weren't meant to be used on screens larger than 320x200).
128     x *= mouseacc;
129     y *= mouseacc;
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);
135     if (debug_mouse) {
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");
142     }
143     // Send result by usb.
144     host_mouse_send(&mouse_report);
145     // increase acceleration of mouse
146     mouseacc += ( mouseacc < ADB_MOUSE_MAXACC ? 1 : 0 );
147     return;
148 }
149 #endif
150
151 uint8_t matrix_scan(void)
152 {
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;
160     uint16_t codes;
161     uint8_t key0, key1;
162
163     /* tick of last polling */
164     static uint16_t tick_ms;
165
166     codes = extra_key;
167     extra_key = 0xFFFF;
168
169     if ( codes == 0xFFFF )
170     {
171         // polling with 12ms interval
172         if (timer_elapsed(tick_ms) < 12) return 0;
173         tick_ms = timer_read();
174
175         codes = adb_host_kbd_recv();
176     }
177
178     key0 = codes>>8;
179     key1 = codes&0xFF;
180
181     if (debug_matrix && codes) {
182         print("adb_host_kbd_recv: "); phex16(codes); print("\n");
183     }
184
185     if (codes == 0) {                           // no keys
186         return 0;
187     } else if (codes == 0x7F7F) {   // power key press
188         register_key(0x7F);
189     } else if (codes == 0xFFFF) {   // power key release
190         register_key(0xFF);
191     } else if (key0 == 0xFF) {      // error
192         xprintf("adb_host_kbd_recv: ERROR(%d)\n", codes);
193         // something wrong or plug-in
194         matrix_init();
195         return key1;
196     } else {
197         /* Swap codes for ISO keyboard
198          * https://github.com/tmk/tmk_keyboard/issues/35
199          *
200          * ANSI
201          * ,-----------    ----------.
202          * | *a|  1|  2     =|Backspa|
203          * |-----------    ----------|
204          * |Tab  |  Q|     |  ]|   *c|
205          * |-----------    ----------|
206          * |CapsLo|  A|    '|Return  |
207          * |-----------    ----------|
208          * |Shift   |      Shift     |
209          * `-----------    ----------'
210          *
211          * ISO
212          * ,-----------    ----------.
213          * | *a|  1|  2     =|Backspa|
214          * |-----------    ----------|
215          * |Tab  |  Q|     |  ]|Retur|
216          * |-----------    -----`    |
217          * |CapsLo|  A|    '| *c|    |
218          * |-----------    ----------|
219          * |Shif| *b|      Shift     |
220          * `-----------    ----------'
221          *
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)
229          */
230         if (is_iso_layout) {
231             if ((key0 & 0x7F) == 0x32) {
232                 key0 = (key0 & 0x80) | 0x0A;
233             } else if ((key0 & 0x7F) == 0x0A) {
234                 key0 = (key0 & 0x80) | 0x32;
235             }
236         }
237         register_key(key0);
238         if (key1 != 0xFF)       // key1 is 0xFF when no second key.
239             extra_key = key1<<8 | 0xFF; // process in a separate call
240     }
241
242     matrix_scan_quantum();
243     return 1;
244 }
245
246 void matrix_print(void){
247
248 }
249
250 inline
251 matrix_row_t matrix_get_row(uint8_t row)
252 {
253     return matrix[row];
254 }
255
256 inline
257 static void register_key(uint8_t key)
258 {
259     uint8_t col, row;
260     col = key&0x07;
261     row = (key>>3)&0x0F;
262     if (key&0x80) {
263         matrix[row] &= ~(1<<col);
264     } else {
265         matrix[row] |=  (1<<col);
266     }
267 }