This repository has been archived by the owner on Apr 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
USB_NeXT_Keyboard.ino
277 lines (234 loc) · 7.18 KB
/
USB_NeXT_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
// NeXT non-ADB Keyboard to USB converter
// This will take an older NeXT keyboard, talk to it, and turn the keycodes into a USB keyboard
// Requires an Arduino Micro for the USB portion - but could be ported to another micro fairly easily
// Written by Limor Fried / Adafruit Industries
// Released under BSD license - thanks NetBSD! :)
//
// Timing reference thanks to http://m0115.web.fc2.com/
// Pinouts thanks to http://www.68k.org/~degs/nextkeyboard.html
// Keycodes from http://ftp.netbsd.org/pub/NetBSD/NetBSD-release-6/src/sys/arch/next68k/dev/
#include "wsksymdef.h"
#include "nextkeyboard.h"
// the timing per bit, 50microseconds
#define TIMING 50
// pick which pins you want to use
#define KEYBOARDOUT 3
#define KEYBOARDIN 2
// comment to speed things up, uncomment for help!
//#define DEBUG
// speed up reads by caching the 'raw' pin ports
volatile uint8_t *misoportreg;
uint8_t misopin;
// our little macro
#define readkbd() ((*misoportreg) & misopin)
// debugging/activity LED
#define LED 13
#define NEXT_KMBUS_IDLE 0x200600
// NeXT Keyboard Defines
// modifiers
#define NEXT_KB_CONTROL 0x1000
#define NEXT_KB_ALTERNATE_LEFT 0x20000
#define NEXT_KB_ALTERNATE_RIGHT 0x40000
#define NEXT_KB_COMMAND_LEFT 0x8000
#define NEXT_KB_COMMAND_RIGHT 0x10000
#define NEXT_KB_SHIFT_LEFT 0x2000
#define NEXT_KB_SHIFT_RIGHT 0x4000
// special command for setting LEDs
void setLEDs(bool leftLED, bool rightLED) {
digitalWrite(KEYBOARDOUT, LOW);
delayMicroseconds(TIMING *9);
digitalWrite(KEYBOARDOUT, HIGH);
delayMicroseconds(TIMING *3);
digitalWrite(KEYBOARDOUT, LOW);
delayMicroseconds(TIMING);
if (leftLED)
digitalWrite(KEYBOARDOUT, HIGH);
else
digitalWrite(KEYBOARDOUT, LOW);
delayMicroseconds(TIMING);
if (rightLED)
digitalWrite(KEYBOARDOUT, HIGH);
else
digitalWrite(KEYBOARDOUT, LOW);
delayMicroseconds(TIMING);
digitalWrite(KEYBOARDOUT, LOW);
delayMicroseconds(TIMING *7);
digitalWrite(KEYBOARDOUT, HIGH);
}
void query() {
// query the keyboard for data
digitalWrite(KEYBOARDOUT, LOW);
delayMicroseconds(TIMING *5);
digitalWrite(KEYBOARDOUT, HIGH);
delayMicroseconds(TIMING );
digitalWrite(KEYBOARDOUT, LOW);
delayMicroseconds(TIMING *3);
digitalWrite(KEYBOARDOUT, HIGH);
}
void nextreset() {
// reset the keyboard
digitalWrite(KEYBOARDOUT, LOW);
delayMicroseconds(TIMING);
digitalWrite(KEYBOARDOUT, HIGH);
delayMicroseconds(TIMING*4);
digitalWrite(KEYBOARDOUT, LOW);
delayMicroseconds(TIMING);
digitalWrite(KEYBOARDOUT, HIGH);
delayMicroseconds(TIMING*6);
digitalWrite(KEYBOARDOUT, LOW);
delayMicroseconds(TIMING*10);
digitalWrite(KEYBOARDOUT, HIGH);
}
void setup() {
// set up pin directions
pinMode(KEYBOARDOUT, OUTPUT);
pinMode(KEYBOARDIN, INPUT);
pinMode(LED, OUTPUT);
misoportreg = portInputRegister(digitalPinToPort(KEYBOARDIN));
misopin = digitalPinToBitMask(KEYBOARDIN);
// according to http://cfile7.uf.tistory.com/image/14448E464F410BF22380BB
query();
delay(5);
nextreset();
delay(8);
query();
delay(5);
nextreset();
delay(8);
Keyboard.begin();
#ifdef DEBUG
while (!Serial)
Serial.begin(57600);
Serial.println("NeXT");
#endif
}
uint32_t getresponse() {
// bitbang timing, read 22 bits 50 microseconds apart
cli();
while ( readkbd() );
delayMicroseconds(TIMING/2);
uint32_t data = 0;
for (uint8_t i=0; i < 22; i++) {
if (readkbd())
data |= ((uint32_t)1 << i);
delayMicroseconds(TIMING);
}
sei();
return data;
}
void loop() {
digitalWrite(LED, LOW);
delay(20);
uint32_t resp;
query();
resp = getresponse();
// check for a 'idle' response, we'll do nothing
if (resp == NEXT_KMBUS_IDLE) return;
// turn on the LED when we get real resposes!
digitalWrite(LED, HIGH);
// keycode is the lower 7 bits
uint8_t keycode = resp & 0xFF;
keycode /= 2;
#ifdef DEBUG
Serial.print('['); Serial.print(resp, HEX); Serial.print("] ");
Serial.print("keycode: "); Serial.print(keycode);
#endif
// modifiers! you can remap these here,
// but I suggest doing it in the OS instead
if (resp & NEXT_KB_CONTROL)
Keyboard.press(KEY_LEFT_CTRL);
else
Keyboard.release(KEY_LEFT_CTRL);
if (resp & NEXT_KB_SHIFT_LEFT) {
Keyboard.press(KEY_LEFT_SHIFT);
} else {
Keyboard.release(KEY_LEFT_SHIFT);
}
if (resp & NEXT_KB_SHIFT_RIGHT) {
Keyboard.press(KEY_RIGHT_SHIFT);
} else {
Keyboard.release(KEY_RIGHT_SHIFT);
}
boolean shiftPressed = (resp & (NEXT_KB_SHIFT_LEFT|NEXT_KB_SHIFT_RIGHT)) != 0;
// turn on shift LEDs if shift is held down
if (shiftPressed)
setLEDs(true, true);
else
setLEDs(false, false);
if (resp & NEXT_KB_COMMAND_LEFT)
Keyboard.press(KEY_LEFT_GUI);
else
Keyboard.release(KEY_LEFT_GUI);
if (resp & NEXT_KB_COMMAND_RIGHT)
Keyboard.press(KEY_RIGHT_GUI);
else
Keyboard.release(KEY_RIGHT_GUI);
if (resp & NEXT_KB_ALTERNATE_LEFT)
Keyboard.press(KEY_LEFT_ALT);
else
Keyboard.release(KEY_LEFT_ALT);
if (resp & NEXT_KB_ALTERNATE_RIGHT)
Keyboard.press(KEY_RIGHT_ALT);
else
Keyboard.release(KEY_RIGHT_ALT);
if (keycode == 0) return;
for (int i = 0; i< 100; i++) {
if (nextkbd_keydesc_us[i*3] == keycode) {
keysym_t keydesc = nextkbd_keydesc_us[i*3+1];
char ascii = (char) keydesc;
#ifdef DEBUG
Serial.print("--> "); Serial.print(ascii); Serial.print(" / "); Serial.print(keydesc, HEX);
#endif
int code;
switch (keydesc) {
case KS_KP_Enter:
case KS_Return: code = KEY_RETURN; break;
case KS_Escape: code = KEY_ESC; break;
case KS_BackSpace: code = KEY_BACKSPACE; break;
case KS_Up: code = KEY_UP_ARROW; break;
case KS_Down: code = KEY_DOWN_ARROW; break;
case KS_Left: code = KEY_LEFT_ARROW; break;
case KS_Right: code = KEY_RIGHT_ARROW; break;
// hacks for two tricky numpad keys
case KS_KP_Equal: code = (shiftPressed ? KS_bar : ascii); break;
case KS_KP_Divide:
if (shiftPressed) {
Keyboard.release(KEY_RIGHT_SHIFT);
Keyboard.release(KEY_LEFT_SHIFT);
code = KS_backslash;
} else {
code = ascii;
}
break;
// remap the other special keys because the KeyboardMouse can't send proper vol/brightness anyway
case KS_AudioLower: code = KEY_INSERT; break;
case KS_AudioRaise: code = KEY_DELETE; break;
case KS_Cmd_BrightnessUp: code = KEY_PAGE_UP; break;
case KS_Cmd_BrightnessDown: code = KEY_PAGE_DOWN; break;
case 0:
default: code = ascii;
}
if ((resp & 0xF00) == 0x400) { // down press
#ifdef DEBUG
Serial.println(" v ");
#endif
Keyboard.press(code);
break;
}
if ((resp & 0xF00) == 0x500) {
Keyboard.release(code);
#ifdef DEBUG
Serial.println(" ^ ");
#endif
break;
}
// re-press shift if need be
if (keydesc == KS_KP_Divide && shiftPressed) {
if (resp & NEXT_KB_SHIFT_LEFT)
Keyboard.press(KEY_LEFT_SHIFT);
if (resp & NEXT_KB_SHIFT_RIGHT)
Keyboard.press(KEY_RIGHT_SHIFT);
}
}
}
}