forked from devonshire/arduino-atari-hid-keyboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
USB_HID_Keyboard.ino
417 lines (388 loc) · 9.61 KB
/
USB_HID_Keyboard.ino
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include <Keyboard.h>
/*
* -------------------------------------------------------------------------
* Interface Atari ST Keyboard to USB HID Keyboard
* -------------------------------------------------------------------------
* Initial idea and some original code provided by user 'joska' of
* http://www.atari-forum.com - license unknown
* -------------------------------------------------------------------------
* Copyright Kevin Peat 2017
* My changes and additions are licensed public domain
* -------------------------------------------------------------------------
* Developed for use with an Arduino Leonardo as it is able to act directly
* as a USB keyboard controller so doesn't require the Arduino firmware to
* be modified as some of the other Arduinos (eg. Uno) would do
* -------------------------------------------------------------------------
*/
//#define DEBUG
// ST keyboard reset pin
const int ST_KB_RESET = 4;
// Atari modifier key codes
const uint8_t ST_LEFT_CTRL = 0x1D;
const uint8_t ST_LEFT_SHIFT = 0x2A;
const uint8_t ST_LEFT_ALT = 0x38;
const uint8_t ST_RIGHT_SHIFT = 0x36;
const uint8_t ST_CAPS_LOCK = 0x3A;
// Arduino Leonardo modifier key codes
const uint8_t ARD_LEFT_CTRL = 0x80;
const uint8_t ARD_LEFT_SHIFT = 0x81;
const uint8_t ARD_LEFT_ALT = 0x82;
const uint8_t ARD_RIGHT_SHIFT = 0x85;
const uint8_t ARD_CAPS_LOCK = 0xC1;
// Arduino Leonardo special key codes
const uint8_t ARD_UP_ARROW = 0xDA;
const uint8_t ARD_DOWN_ARROW = 0xD9;
const uint8_t ARD_LEFT_ARROW = 0xD8;
const uint8_t ARD_RIGHT_ARROW = 0xD7;
const uint8_t ARD_BACKSPACE = 0xB2;
const uint8_t ARD_TAB = 0xB3;
const uint8_t ARD_RETURN = 0xB0;
const uint8_t ARD_ESC = 0xB1;
const uint8_t ARD_INSERT = 0xD1;
const uint8_t ARD_DELETE = 0xD4;
const uint8_t ARD_HOME = 0xD2;
const uint8_t ARD_F1 = 0xC2;
const uint8_t ARD_F2 = 0xC3;
const uint8_t ARD_F3 = 0xC4;
const uint8_t ARD_F4 = 0xC5;
const uint8_t ARD_F5 = 0xC6;
const uint8_t ARD_F6 = 0xC7;
const uint8_t ARD_F7 = 0xC8;
const uint8_t ARD_F8 = 0xC9;
const uint8_t ARD_F9 = 0xCA;
const uint8_t ARD_F10 = 0xCB;
const uint8_t ARD_F11 = 0xCC;
const uint8_t ARD_F12 = 0xCD;
// Keyboard auto-repeat
static uint8_t last_make; // Last make char
static unsigned long last_make_time; // Last make time (milliseconds)
int auto_repeat_delay = 500; // Keyboard auto-repeat delay (milliseconds)
int auto_repeat_rate = 25; // Keyboard auto-repeat rate (milliseconds)
// Key scancodes
// Use ST scancode as index to find the corresponding USB scancode.
// Make-codes are single byte, with some exceptions.
// These are escaped with a 0xe0 byte, when this appear in this table a
// simple switch is used to look up the correct scancode.
// All break codes are the same as the scancodes, but are prefixed with 0xf0.
// The escaped keys are first escaped, then 0xf0, then scancode.
uint8_t scanCodes[] =
{
0x00, // (Nothing)
ARD_ESC, // Esc
0x31, // 1
0x32, // 2
0x33, // 3
0x34, // 4
0x35, // 5
0x36, // 6
0x37, // 7
0x38, // 8
0x39, // 9
0x30, // 0
0x2D, // -
0x3D, // == (Mapped to =)
ARD_BACKSPACE, // Backspace
ARD_TAB, // Tab
0x71, // q
0x77, // w
0x65, // e
0x72, // r
0x74, // t
0x79, // y
0x75, // u
0x69, // i
0x6F, // o
0x70, // p
0x5B, // [
0x5D, // ]
ARD_RETURN, // Enter
ARD_LEFT_CTRL, // Control
0x61, // a
0x73, // s
0x64, // d
0x66, // f
0x67, // g
0x68, // h
0x6A, // j
0x6B, // k
0x6C, // l
0x3B, // ;
0x27, // ' (Mapped to '")
0xE0, // #
ARD_LEFT_SHIFT, // Lshift
0x7E, // #~ (Mapped to ~)
0x7A, // z
0x78, // x
0x63, // c
0x76, // v
0x62, // b
0x6E, // n
0x6D, // m
0x2C, // ,
0x2E, // .
0x2F, // /
ARD_RIGHT_SHIFT, // Rshift
0x37, // (Not used)
ARD_LEFT_ALT, // Alternate
0x20, // Space
ARD_CAPS_LOCK, // CapsLock
ARD_F1, // F1
ARD_F2, // F2
ARD_F3, // F3
ARD_F4, // F4
ARD_F5, // F5
ARD_F6, // F6
ARD_F7, // F7
ARD_F8, // F8
ARD_F9, // F9
ARD_F10, // F10
0x45, // (Not used)
0x46, // (Not used)
0xE0, // Clr/Home
0xE0, // Up Arrow
0x49, // (Not used)
0x2D, // N-
0xE0, // Left Arrow
0x4c, // (Not used)
0xE0, // Right Arrow
0x2B, // N+
0x4f, // (Not used)
0xE0, // Down Arrow
0x51, // (Not used)
0xE0, // Insert
0xE0, // Delete
0x54, // (Not used)
0x55, // (Not used)
0x56, // (Not used)
0x57, // (Not used)
0x58, // (Not used)
0x59, // (Not used)
0x5a, // (Not used)
0x5b, // (Not used)
0x5c, // (Not used)
0x5d, // (Not used)
0x5e, // (Not used)
0x5f, // (Not used)
0x5C, // ISO Key
0xB2, // Undo (Mapped to Backspace)
ARD_F12, // Help (Mapped to F12 which is the Hatari menu key)
0x28, // N(
0x29, // N)
0xE0, // N/
0x2A, // N*
0x37, // N7
0x38, // N8
0x39, // N9
0x34, // N4
0x35, // N5
0x36, // N6
0x31, // N1
0x32, // N2
0x33, // N3
0x30, // N0
0x2E, // N.
0xE0 // NEnter
};
void setup(void)
{
// Initialize keyboard:
Keyboard.begin();
// Open serial port from Atari keyboard
Serial1.begin(7812);
#ifdef DEBUG
// Open serial port to PC
Serial.begin(9600);
#endif
// Reset ST keyboard
delay(200);
reset_st_keyboard();
delay(200);
// Empty serial buffer before starting
while(Serial1.available() > 0) Serial1.read();
}
void loop()
{
// Process incoming Atari keypresses
if (Serial1.available() > 0) process_keypress(Serial1.read());
// Handle keyboard auto-repeat
auto_repeat();
}
// Reset ST Keyboard
void reset_st_keyboard(void)
{
Serial1.print(0x80);
Serial1.print(1);
pinMode(ST_KB_RESET, OUTPUT);
digitalWrite(ST_KB_RESET, HIGH);
delay(20);
digitalWrite(ST_KB_RESET, LOW);
delay(20);
digitalWrite(ST_KB_RESET, HIGH);
}
// Process each keypress
void process_keypress(uint8_t key)
{
// Keypress
if (((key & 0x7f) > 0) && ((key & 0x7f) < 0x73))
{
// Break codes (other than modifiers) do not need to be sent
// to the PC as the Leonardo keyboard interface handles that
if (key & 0x80) // Break
{
last_make = 0;
last_make_time = 0;
process_modifier(key);
}
else // Make
{
last_make = key;
last_make_time = millis();
convert_scancode(key);
}
}
}
// Convert from ST scancode to PC scancode
void convert_scancode(uint8_t key)
{
uint8_t break_code = key & 0x80;
uint8_t pc_code = scanCodes[key & 0x7f];
uint8_t escaped = (pc_code == 0xe0 ? true:false);
#ifdef DEBUG
Serial.print("Atari scancode: ");
Serial.println(key, DEC);
Serial.print("PC scancode: ");
Serial.println(pc_code, DEC);
Serial.print("Break code: ");
Serial.println(break_code, DEC);
Serial.print("Escaped: ");
Serial.println(escaped, DEC);
#endif
// Handle modifier key presses
if (process_modifier(key)) return;
// Special handling required for escaped keypresses
if (escaped)
{
switch (key & 0x7f)
{
case 0x48: // Up arrow
send_escaped_key(ARD_UP_ARROW);
break;
case 0x4b: // Left arrow
send_escaped_key(ARD_LEFT_ARROW);
break;
case 0x4d: // Right arrow
send_escaped_key(ARD_RIGHT_ARROW);
break;
case 0x50: // Down arrow
send_escaped_key(ARD_DOWN_ARROW);
break;
case 0x52: // Insert
send_escaped_key(ARD_INSERT);
break;
case 0x53: // Delete
send_escaped_key(ARD_DELETE);
break;
case 0x47: // Clr/Home
send_escaped_key(ARD_HOME);
break;
case 0x65: // Num /
send_escaped_key(0x2F);
break;
case 0x72: // Num Enter
send_escaped_key(ARD_RETURN);
break;
case 0x2b: // Tilde
send_escaped_key(0x23);
break;
case 0x62: // Help
send_escaped_key(ARD_F1);
break;
}
}
else
{
Keyboard.write(pc_code);
}
}
// Send code for escaped Atari keypresses
void send_escaped_key(uint8_t key)
{
Keyboard.press(key);
delay(20);
Keyboard.release(key);
}
// Process modifier keypresses
boolean process_modifier(uint8_t key)
{
// Modifier key press
switch (key)
{
case ST_LEFT_CTRL:
Keyboard.press(ARD_LEFT_CTRL);
return true;
case ST_LEFT_SHIFT:
Keyboard.press(ARD_LEFT_SHIFT);
return true;
case ST_LEFT_ALT:
Keyboard.press(ARD_LEFT_ALT);
return true;
case ST_RIGHT_SHIFT:
Keyboard.press(ARD_RIGHT_SHIFT);
return true;
case ST_CAPS_LOCK:
Keyboard.press(ARD_CAPS_LOCK);
return true;
}
// Modifier key release
switch (key & 0x7f)
{
case ST_LEFT_CTRL:
Keyboard.release(ARD_LEFT_CTRL);
return true;
case ST_LEFT_SHIFT:
Keyboard.release(ARD_LEFT_SHIFT);
return true;
case ST_LEFT_ALT:
Keyboard.release(ARD_LEFT_ALT);
return true;
case ST_RIGHT_SHIFT:
Keyboard.release(ARD_RIGHT_SHIFT);
return true;
case ST_CAPS_LOCK:
Keyboard.release(ARD_CAPS_LOCK);
return true;
}
return false;
}
// Keyboard auto repeat
void auto_repeat(void)
{
static unsigned long last_repeat;
static byte key_repeating; // True if key being repeated
// Don't want to repeat modifiers
switch (last_make)
{
case ST_LEFT_CTRL:
case ST_LEFT_SHIFT:
case ST_RIGHT_SHIFT:
case ST_LEFT_ALT:
case ST_CAPS_LOCK:
case 0x00: // No key held down
key_repeating = false;
return;
}
// Delay to first repeat
if (!key_repeating && (millis() - last_make_time > auto_repeat_delay))
{
key_repeating = true;
last_repeat = millis();
return;
}
// Start repeating
if (key_repeating && (millis() - last_repeat > auto_repeat_rate))
{
last_repeat = millis();
convert_scancode(last_make);
}
}