-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrs80-mod2-kb.ino
More file actions
231 lines (191 loc) · 7.19 KB
/
Copy pathtrs80-mod2-kb.ino
File metadata and controls
231 lines (191 loc) · 7.19 KB
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
/*
File: trs80-mod2-kb.ino
Date: 2024.11.01
Author: Alain Boudreault - aka: ve2cuy, puyansude
-------------------------------------------------------
Description:
TODO:
*/
//#define USE_DISPLAY
#ifdef USE_DISPLAY
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
#endif
#include <PS2KeyAdvanced.h>
#define TRACE
#define USE_BUSY_PIN
#define PS2_DATA_PIN 8
#define PS2_IRQ_PIN 3 // On UNO, use pin 2 or 3
#define TRS80_KB_BUSY_PIN 10
#define TRS80_KB_DATA_PIN 11
#define TRS80_KB_CLOCK_PIN 12
#define TRS80_KB_END_PULSE_DELAY 165 // 165us produce a timing graph very close to a model II real keyboard
#define TRS80_KB_KEY_HOLD 0
PS2KeyAdvanced keyboard;
// screen
#ifdef USE_DISPLAY
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // High speed I2C
#endif
int compteur = 0; // Used with TRACE
char str[32]; // Used with sprintf()
uint16_t c; // scan code from PS2 KB
// ------------------------------------------------------------------
void setup() {
// ------------------------------------------------------------------
#ifdef USE_DISPLAY
u8g2.begin();
u8g2.clearBuffer(); // clear the display
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a font
u8g2.drawStr(0,10,"TRS-80M2-Keyboard"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
#endif
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
pinMode(TRS80_KB_BUSY_PIN, INPUT);
pinMode(TRS80_KB_CLOCK_PIN, OUTPUT);
pinMode(TRS80_KB_DATA_PIN, OUTPUT);
digitalWrite(TRS80_KB_DATA_PIN, HIGH);
keyboard.begin(PS2_DATA_PIN, PS2_IRQ_PIN);
Serial.begin(115200);
#ifdef TRACE
Serial.println("\nTRS-80 Model II Keyboard emulator");
#endif
delay(1000);
#ifdef USE_DISPLAY
u8g2.clearBuffer();
#endif
} // setup()
// ------------------------------------------------------------------
void loop() {
// ------------------------------------------------------------------
static byte statusBit = 0;
static byte key = 0;
if (keyboard.available())
{
#ifdef TRACE
//Serial.println(str);
#endif
c = keyboard.read();
if(c > 0) {
statusBit = c >> 8;
key = c & 0xFF;
sprintf(str, "%02d-%04X S:%02X K:%02X C:%c", compteur++, c, statusBit, key, key);
#ifdef TRACE
Serial.println(str);
#endif
#ifdef USE_DISPLAY
u8g2.drawStr(0,40, str); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
#endif
key = translatePS2KeyToTRS80(key, statusBit);
if ( key != 255 /*statusBit == 0 || statusBit == 1 */ /* FOR DEBUG || statusBit == 0xFF */) {
sprintf(str, "Key sent to TRS* = %02X : %c\n", key, key );
#ifdef TRACE
Serial.print(str);
#endif
// Wait if CPU is busy before sending key sequence
#ifdef USE_BUSY_PIN
while(!digitalRead(TRS80_KB_BUSY_PIN));
#endif
#ifdef USE_DISPLAY
u8g2.drawStr(0,55, str);
u8g2.sendBuffer();
#endif
keyToTRS(key);
} // if statusBit
} // if keyboard.read > 0
} // if keyboard.available()
} // loop()
/// TODO: Optimise the code
// ------------------------------------------------------------------
char translatePS2KeyToTRS80(char key, byte statusBit){
// ------------------------------------------------------------------
static char shiftNumbers[] = {')','!','"','/','$','%','?','&','*','('};
// IF Scroll Lock was released
if (statusBit == 0x81 && key == 0x02 ) {
#ifdef TRACE
Serial.println("Scroll Lock was released");
statusBit = 1;
#endif
}
if (statusBit == 0 || statusBit == 1 || statusBit == 0xC0 /* FOR DEBUG || statusBit == 0xFF */) {
key = key == 0x06 && statusBit == 0 ? 0x03 : key ; // Break
if (statusBit == 1) {
key = key == 0x02 ? TRS80_KB_KEY_HOLD : key; // Hold
key = key == 0x1E ? 0x0D : key; // Enter
key = key == 0x1C ? 0x08 : key; // Back Space
key = key == 0x1F ? 0x20 : key; // Space
key = key == 0x17 ? 0x1E : key; // Arrow UP
key = key == 0x18 ? 0x1F : key; // Arrow DOWN
key = key == 0x15 ? 0x1C : key; // Arrow LEFT
key = key == 0x16 ? 0x1D : key; // Arrow RIGHT
key = key == 0x61 ? 0x01 : key; // F1
key = key == 0x62 ? 0x02 : key; // F2
} // if statusBit == 1
// Remove 2 key from buffer if Scroll Lock was use
if (key == TRS80_KB_KEY_HOLD ) {
delay(50); // Give time for the keyboard to by ready to send the next code
keyboard.read(); // !keyboard.available() could freeze the app
delay(50); // Give time for the keyboard to by ready to send the next code
keyboard.read();
}
if ( statusBit == 0xC0 && (key >= '0' && key <='9') ) {
#ifdef TRACE
Serial.println("SHIFT number key");
#endif
return shiftNumbers[key-0x30];
}
return key;
} else
{
return 255;
}
} // translatePS2KeyToTRS80
// ------------------------------------------------------------------
void keyToTRS(char key)
// ------------------------------------------------------------------
{
#ifdef TRACE
Serial.print("In KeyToTRS with key: ");
Serial.println(key);
#endif
digitalWrite(TRS80_KB_CLOCK_PIN, LOW);
// NOTE: arduino shiftout function does not produce a valide timing between DATA and CLOCK.
//shiftOut(TRS80_DATA_PIN, TRS80_CLOCK_PIN, LSBFIRST, key);
shiftOutV2(TRS80_KB_DATA_PIN, TRS80_KB_CLOCK_PIN, LSBFIRST, key, TRS80_KB_END_PULSE_DELAY);
// END Pulse
digitalWrite(TRS80_KB_DATA_PIN, LOW);
delayMicroseconds(TRS80_KB_END_PULSE_DELAY/6);
digitalWrite(TRS80_KB_DATA_PIN, HIGH);
} // keyToTRS
/**
NOTE: On exit DATA pin will be HIGH and CLOCK will be LOW
*/
// ------------------------------------------------------------------
void shiftOutV2(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder,
uint8_t val, uint16_t speed)
// ------------------------------------------------------------------
{
uint8_t i;
for (i = 0; i < 8; i++) {
if (bitOrder == LSBFIRST)
digitalWrite(dataPin, !!(val & (1 << i)));
else
digitalWrite(dataPin, !!(val & (1 << (7 - i))));
// Data is present on the BUS 1/2 cycle before clock signal
// See Model II tech ref page 65 (keyboard timing)
delayMicroseconds(speed/2);
digitalWrite(clockPin, HIGH);
delayMicroseconds(speed/2);
digitalWrite(dataPin, HIGH); // Reset data half way in clock cycle.
delayMicroseconds(speed/2);
digitalWrite(clockPin, LOW);
delayMicroseconds(speed);
} // for i
} // shiftOutV2
// --------> END OF FILE