-
Notifications
You must be signed in to change notification settings - Fork 13
/
main_xt.c
321 lines (298 loc) · 5.06 KB
/
main_xt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* This file is part of ukbdc.
*
* ukbdc is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* ukbdc is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ukbdc; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "platforms.h"
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#include <util/delay.h>
#include <stdbool.h>
#include "usb_keyboard.h"
#include "io.h"
#include "hid.h"
#include "rawhid.h"
#include "rawhid_protocol.h"
#include "dataflash.h"
#include "layout.h"
#include "hc595.h"
#include <stdio.h> /* sprintf */
volatile uint8_t matrix[19][8] = {
{41, 0, 0, 0, 0, 52, 0, 0},
{20, 49, 36, 21, 6, 7, 35, 48},
{0, 0, 58, 0, 0, 0, 0, 0},
{19, 50, 37, 22, 5, 8, 34, 47},
{0, 0, 59, 0, 0, 27, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{16, 0, 0, 25, 2, 11, 31, 44},
{17, 56, 39, 24, 3, 10, 32, 45},
{14, 0, 0, 40, 0, 13, 29, 42},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{15, 0, 0, 26, 1, 12, 30, 43},
{18, 51, 38, 23, 4, 9, 33, 46},
{0, 0, 0, 0, 54, 0, 0, 0},
{0, 0, 57, 0, 0, 0, 0, 55},
{0, 0, 0, 60, 28, 0, 53, 0},
};
bool states[19][8] = {{0}};
bool is_modifier(uint8_t key_n)
{
static const uint8_t modifiers[] = {41, 52, 53, 54, 55, 57, 58, 59, 60};
for (uint8_t i = 0; i < sizeof(modifiers); ++i)
if (key_n == modifiers[i])
return true;
return false;
}
uint16_t get_input()
{
uint16_t ret = 0;
for (int i = 0; i < 16; ++i) {
ret <<= 1;
ret |= IO_get(i) ? 0x0001 : 0x0000;
}
return ret;
}
void scan_matrix()
{
bool changed = false;
for (uint8_t i = 0; i < 8; ++i) {
for (int j = 0; j < 8; ++j)
IO_set(j | 0x80, true);
IO_set((i) | 0x80, false);
_delay_us(10);
for (uint8_t j = 0; j < 19; ++j) {
bool state = !IO_get(j);
uint8_t layer = 0;
uint8_t code = LAYOUT_get_scancode(layer, matrix[j][i]);
if (state != states[j][i]) {
changed = true;
states[j][i] = state;
switch (state) {
case true:
if (code == KEY_ESC && (HID_scancode_is_pressed(KEY_LEFT_SHIFT) || HID_scancode_is_pressed(KEY_RIGHT_SHIFT)))
code = KEY_TILDE;
HID_set_scancode_state(code, true);
break;
case false:
if (code == KEY_ESC)
HID_set_scancode_state(KEY_TILDE, false);
HID_set_scancode_state(code, false);
break;
}
}
}
}
if (changed)
HID_commit_state();
}
uint8_t xt_to_hid[128] = {
0x00,
0x29,
0x1e,
0x1f,
0x20,
0x21,
0x22,
0x23,
0x24,
0x25,
0x26,
0x27,
0x2d,
0x2e,
0x2a,
0x2b,
0x14,
0x1a,
0x08,
0x15,
0x17,
0x1c,
0x18,
0x0c,
0x12,
0x13,
0x2f,
0x30,
0x28,
0xe0,
0x04,
0x16,
0x07,
0x09,
0x0a,
0x0b,
0x0d,
0x0e,
0x0f,
0x33,
0x34,
0x35,
0xe1,
0x31,
0x1d,
0x1b,
0x06,
0x19,
0x05,
0x11,
0x10,
0x36,
0x37,
0x38,
0xe5,
0x55,
0xe2,
0x2c,
0x39,
0x3a,
0x3b,
0x3c,
0x3d,
0x3e,
0x3f,
0x40,
0x41,
0x42,
0x43,
0x53,
0x47,
0x5f,
0x60,
0x61,
0x56,
0x5c,
0x5d,
0x5e,
0x57,
0x59,
0x5a,
0x5b,
0x62,
0x63,
0x00,
0x00,
0x64,
0x44,
0x45,
0x67,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x68,
0x69,
0x6a,
0x6b,
0x6c,
0x6d,
0x6e,
0x6f,
0x70,
0x71,
0x72,
0x00,
0x88,
0x00,
0x00,
0x87,
0x00,
0x00,
0x73,
0x00,
0x00,
0x8a,
0x00,
0x8b,
0x00,
0x89,
0x85,
0x00
};
int main(void)
{
clock_prescale_set(clock_div_1);
for (int i = 0; i < NUM_IO; ++i) {
IO_config(i, INPUT);
IO_set(i, true);
}
PORTB |= _BV(PB0);
DDRB |= _BV(PB2) | _BV(PB1) | _BV(PB0); // MOSI, SCK, MEMCS
DDRC |= _BV(PC7) | _BV(PC6); // LATCH, BUFEN
DDRB &= ~_BV(PB3); // MISO
PORTB &= ~_BV(PB3);
#define XT_CLK 8
#define XT_DATA 14
IO_config(XT_CLK, INPUT);
IO_config(XT_DATA, INPUT);
IO_set(XT_CLK, true);
IO_set(XT_DATA, true);
USB_init();
while (USB_get_configuration() == 0)
;
HID_commit_state();
TCCR0A = 0x00;
TCCR0B = 0x03; /* clk_io / 64 */
TIMSK0 = _BV(TOIE0);
while(1) {
if (IO_get(XT_CLK) == true)
continue;
uint16_t recv = 0;
for (int i = 0; i < 10; ++i) {
while (IO_get(XT_CLK) == false)
;
// rising edge
if (IO_get(XT_DATA) == true)
recv |= 1 << i;
if (i != 9)
// wait for falling edge
while (IO_get(XT_CLK) == true)
;
}
// remove start bit
uint8_t code = recv >> 1;
HID_set_scancode_state(xt_to_hid[code & 0x7f], !(code >> 7));
HID_commit_state();
}
while (1)
;
}
void MAIN_handle_sof()
{
}
ISR(TIMER0_OVF_vect)
{
uint8_t prev_endp = USB_get_endpoint();
static uint8_t num = 0;
static bool st = false;
if (num > 16) {
num = 0;
// send keystrokes
} else {
++num;
}
USB_set_endpoint(prev_endp);
}