]> git.gir.st - tmk_keyboard.git/blob - converter/ascii_usb/matrix.c
e1ea8e0ab8afb1984313a30c910412e2e73b7744
[tmk_keyboard.git] / converter / ascii_usb / matrix.c
1 /*
2 Copyright 2014 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 #include <stdint.h>
19 #include <stdbool.h>
20 #include <avr/io.h>
21 #include <util/delay.h>
22 #include "print.h"
23 #include "util.h"
24 #include "matrix.h"
25 #include "debug.h"
26 #include "action_util.h"
27 #include "protocol/serial.h"
28
29
30 /*
31 * Not use Matrix.
32 *
33 * ROW: 16(4bits)
34 * COL: 16(4bits)
35 *
36 * 8bit wide
37 * +---------+
38 * 0|00 ... 0F|
39 * 1|08 ... 1F|
40 * :| ... |
41 * :| ... |
42 * E|E0 ... EF|
43 * F|F0 ... FF|
44 * +---------+
45 */
46
47
48 inline
49 uint8_t matrix_rows(void)
50 {
51 return MATRIX_ROWS;
52 }
53
54 inline
55 uint8_t matrix_cols(void)
56 {
57 return MATRIX_COLS;
58 }
59
60 void matrix_init(void)
61 {
62 debug_matrix = true;
63 serial_init();
64
65 debug("init\n");
66 return;
67 }
68
69 static void type_key(uint16_t keycode)
70 {
71 if (keycode == 0) return;
72
73 uint8_t mods = keycode>>8;
74 uint8_t key = keycode&0xFF;
75 if (mods) {
76 add_mods(mods);
77 send_keyboard_report();
78 }
79
80 add_key(key);
81 send_keyboard_report();
82
83 del_key(key);
84 send_keyboard_report();
85
86 if (mods) {
87 del_mods(mods);
88 send_keyboard_report();
89 }
90 }
91 static uint16_t code2key(uint8_t code)
92 {
93 // ASCII to key combination in US laout
94 switch (code) {
95 case 0x01 ... 0x08: // Ctrl-[a-z]
96 return MOD_BIT(KC_LCTRL)<<8 | (KC_A + (code-0x01));
97 case 0x09: return KC_TAB; // TAB(Ctrl-i)
98 case 0x0A ... 0x0C: // Ctrl-[a-z]
99 return MOD_BIT(KC_LCTRL)<<8 | (KC_A + (code-0x01));
100 case 0x0D: return KC_ENTER; // Enter(Ctrl-m)
101 case 0x0E ... 0x1A: // Ctrl-[a-z]
102 return MOD_BIT(KC_LCTRL)<<8 | (KC_A + (code-0x01));
103 case 0x1B: return KC_ESC;
104 case 0x1C: return KC_RIGHT;
105 case 0x1D: return KC_LEFT;
106 case 0x1E: return KC_UP;
107 case 0x1F: return KC_DOWN;
108 case 0x20: return KC_SPACE;
109 case 0x21: return MOD_BIT(KC_LSHIFT)<<8 | KC_1; // !
110 case 0x22: return MOD_BIT(KC_LSHIFT)<<8 | KC_QUOTE; // "
111 case 0x23: return MOD_BIT(KC_LSHIFT)<<8 | KC_3; // #
112 case 0x24: return MOD_BIT(KC_LSHIFT)<<8 | KC_4; // $
113 case 0x25: return MOD_BIT(KC_LSHIFT)<<8 | KC_5; // %
114 case 0x26: return MOD_BIT(KC_LSHIFT)<<8 | KC_7; // &
115 case 0x27: return KC_QUOTE; // '
116 case 0x28: return MOD_BIT(KC_LSHIFT)<<8 | KC_9; // (
117 case 0x29: return MOD_BIT(KC_LSHIFT)<<8 | KC_0; // )
118 case 0x2A: return MOD_BIT(KC_LSHIFT)<<8 | KC_8; // *
119 case 0x2B: return MOD_BIT(KC_LSHIFT)<<8 | KC_EQUAL; // +
120 case 0x2C: return KC_COMMA; // ,
121 case 0x2D: return KC_MINUS; // -
122 case 0x2E: return KC_DOT; // .
123 case 0x2F: return KC_SLASH; // /
124 case 0x30: return KC_0;
125 case 0x31 ... 0x39: // 1-9
126 return KC_1 + (code-0x31);
127 case 0x3A: return MOD_BIT(KC_LSHIFT)<<8 | KC_SCLN; // :
128 case 0x3B: return KC_SCLN; // ;
129 case 0x3C: return MOD_BIT(KC_LSHIFT)<<8 | KC_COMMA; // <
130 case 0x3D: return KC_EQUAL; // =
131 case 0x3E: return MOD_BIT(KC_LSHIFT)<<8 | KC_DOT; // >
132 case 0x3F: return MOD_BIT(KC_LSHIFT)<<8 | KC_SLASH; // ?
133 case 0x40: return MOD_BIT(KC_LSHIFT)<<8 | KC_2; // @
134 case 0x41 ... 0x5A: // A-Z
135 return MOD_BIT(KC_LSHIFT)<<8 | (KC_A + (code-0x41));
136 case 0x5B: return KC_LBRACKET; // [
137 case 0x5C: return KC_BSLASH; //
138 case 0x5D: return KC_RBRACKET; // ]
139 case 0x5E: return MOD_BIT(KC_LSHIFT)<<8 | KC_6; // ^
140 case 0x5F: return MOD_BIT(KC_LSHIFT)<<8 | KC_MINUS; // _
141 case 0x61 ... 0x7A: // a-z
142 return KC_A + (code-0x61);
143 case 0x7B: return MOD_BIT(KC_LSHIFT)<<8 | KC_LBRACKET; // {
144 case 0x7C: return MOD_BIT(KC_LSHIFT)<<8 | KC_BSLASH; // |
145 case 0x7D: return MOD_BIT(KC_LSHIFT)<<8 | KC_RBRACKET; // }
146 case 0x7E: return MOD_BIT(KC_LSHIFT)<<8 | KC_GRAVE; // }
147 case 0x7F: return KC_DELETE; //
148 }
149 return 0;
150 }
151
152 uint8_t matrix_scan(void)
153 {
154 uint16_t code = serial_recv2();
155 if (code == -1) {
156 return 0;
157 }
158
159 print_hex8(code); print(" ");
160
161 // echo back
162 serial_send(code);
163 type_key(code2key(code));
164
165
166 return code;
167 }
168
169 inline
170 bool matrix_has_ghost(void)
171 {
172 return false;
173 }
174
175 inline
176 bool matrix_is_on(uint8_t row, uint8_t col)
177 {
178 return false;
179 }
180
181 inline
182 matrix_row_t matrix_get_row(uint8_t row)
183 {
184 return 0;
185 }
186
187 void matrix_print(void)
188 {
189 print("\nr/c 0123456789ABCDEF\n");
190 for (uint8_t row = 0; row < matrix_rows(); row++) {
191 phex(row); print(": ");
192 pbin_reverse(matrix_get_row(row));
193 print("\n");
194 }
195 }
Imprint / Impressum