-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlqp02-teletype.ino
243 lines (219 loc) · 6.67 KB
/
lqp02-teletype.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
#include "ps2_Keyboard.h"
#include "ps2_NeutralTranslator.h"
#include "ps2_SimpleDiagnostics.h"
// #include "ps2_KeyboardLeds.h"
#define Printer Serial1
#define Computer Serial2
#define Sun Serial3
#define ESCAPE 0x1B
// PS/2 keyboard interface
static const int clockPin = 4;
static const int dataPin = 5;
bool debug = 1;
bool local = 0;
bool rolledUp = 0;
int linesToRoll = 12;
int rollUpToShow() {
if(!rolledUp) {
Printer.printf("%c[16t",ESCAPE); // reset line 1 to current line (don't do a form feed! :)
Printer.printf("%c[%de",ESCAPE,linesToRoll);
rolledUp = 1;
}
return 0;
}
int rollDownToPrint() {
if(rolledUp) {
Printer.printf("%c[%dk",ESCAPE,linesToRoll);
rolledUp = 0;
}
return 0;
}
int sendEscapeSequence(int f) {
Serial.printf("sending escape sequence %d\n",f);
switch(f) {
case 1: // reset printer
Printer.printf("%cc",ESCAPE);
Printer.printf("%c[11l",ESCAPE);
break;
case 2: // advance paper to next line
Printer.printf("%cE",ESCAPE);
break;
case 3: // set right margin wrap mode
Printer.printf("%c[?7h",ESCAPE);
break;
case 4: // reset right margin wrap mode
Printer.printf("%c[?7l",ESCAPE);
break;
case 5: // enter new line mode
Printer.printf("%c[20h",ESCAPE);
break;
case 6: // reset new line mode
Printer.printf("%c[20l",ESCAPE);
break;
case 7: // set 8 column tabs
Printer.printf("%c[9;17;25;33;41;49;57;65;73;81u",ESCAPE);
break;
case 8: // unset column tabs
Printer.printf("%c[3g",ESCAPE);
break;
case 9: // set 15 char/inch
Printer.printf("%c[90;48 G",ESCAPE);
break;
case 10: // form feed
Printer.printf("%c",0x0c);
break;
case 11: // line feed
Printer.printf("%cD",ESCAPE);
break;
case 12: // reverse line feed
Printer.printf("%cM",ESCAPE);
break;
default: return -1;
}
return 0;
}
int16_t unshiftTable[70] = {
0x7F, 0x1B, 0x08, 0x09, 0x0A, 0x20,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0x2E, 0x0A, 0x2B, 0x2D, 0x2A, 0x2F,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0x27, 0x2C, 0x2D, 0x2E, 0x2F, -1,
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
0x7A, 0x3B, 0x5C, 0x5B, 0x5D, 0x3D
};
int16_t shiftTable[70] = {
0x7F, 0x1B, 0x08, -1, 0x0A, 0x20,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1,
0x29, 0x21, 0x40, 0x23, 0x24, 0x25, 0x5E, 0x26, 0x2A, 0x28,
0x22, 0x3C, 0x5F, 0x3E, 0x3F, -1,
0x7E, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
0x5A, 0x3A, 0x7C, 0x7B, 0x7D, 0x2B
};
int16_t keycode2ascii(uint16_t c) {
// check for special bits so we can bail
if (c & ~0x607F) {
return -1;
}
if (!(c & 0x2000)) {
int d = (c & 0x7f) - 0x1A;
if (d > 0x46 && d <= 0x52) { // I'm a function key
sendEscapeSequence(c - 0x60);
}
if (d >= 0 && d < sizeof(shiftTable)) {
if (c & 0x4000) {
return shiftTable[d];
} else {
return unshiftTable[d];
}
}
} else {
// handle CTRL
// for CTRL-letter, nuke shift bit
if ((c & 0xFF) >= 0x41 && (c & 0xFF) <= 0x5A) {
c = c & ~0x4000;
}
if (c >= 0x2041 && c <= 0x205A) {
return c & 0x1F;
}
switch (c) {
case 0x6032: // ^@
return 0;
case 0x205D: // ^[
return 0x1B;
case 0x205C: // ^BACKSLASH
return 0x1C;
case 0x205E: // ^]
return 0x1D;
case 0x6036: // ^^
return 0x1E;
case 0x603C: // ^_
return 0x1F;
}
}
return -1;
}
typedef ps2::SimpleDiagnostics<32> Diagnostics;
static Diagnostics diagnostics;
static ps2::Keyboard<dataPin,clockPin,16,Diagnostics> ps2Keyboard(diagnostics);
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
ps2Keyboard.begin();
Printer.begin(9600,SERIAL_7E1);
Printer.attachRts(2);
Printer.attachCts(20);
sendEscapeSequence(1); // reset
sendEscapeSequence(7); // set tabstops
sendEscapeSequence(9); // set 15 chars/inch 8 lines/inch
Computer.begin(1200,SERIAL_8N1);
Computer.attachRts(11);
Computer.attachCts(23);
Sun.begin(1200,SERIAL_8N1);
}
static ps2::NeutralTranslator translator;
bool pxon = 1;
bool timedBefore = 0;
unsigned long lastPrint = 0;
void loop() {
// diagnostics.setLedIndicator<LED_BUILTIN, ps2::DiagnosticsLedBlink::heartbeat>();
int fromComputer;
int fromPrinter;
unsigned long sinceLastPrint = millis() - lastPrint;
if (timedBefore && sinceLastPrint > 500) {
rollUpToShow();
}
if (Computer.available() > 0 && pxon) {
fromComputer = Computer.read();
if (!local) {
rollDownToPrint();
Printer.write(fromComputer);
lastPrint = millis();
timedBefore = 1;
}
if (debug) {
Serial.printf("computer: 0x%02x %c\n",fromComputer,fromComputer);
}
}
if (Printer.available() > 0) {
fromPrinter = Printer.read();
fromPrinter &= 0x7F;
switch (fromPrinter) {
case 0x13:
pxon = 0;
break;
case 0x11:
pxon = 1;
break;
}
Serial.printf("printer: 0x%02x %c\n",fromPrinter,fromPrinter);
}
if (Sun.available() > 0) {
unsigned int fromSun;
fromSun = Sun.read();
if (debug) {
Serial.printf("sunkbd: 0x%02x\n",fromSun);
}
}
ps2::KeyboardOutput scanCode = ps2Keyboard.readScanCode();
if (scanCode != ps2::KeyboardOutput::none) {
// Serial.printf("kbd_scan = %0x\n",scanCode);
ps2::KeyCode translated = translator.translatePs2Keycode(scanCode);
if (translated != ps2::KeyCode::PS2_NONE) {
int c = keycode2ascii(translated);
if (c >= 0) {
if (debug) {
Serial.printf("keyboard: 0x%02x %c\n",c,c);
}
if (!local) {
Computer.write(c);
} else {
Printer.write(c);
}
}
}
}
}