-
Notifications
You must be signed in to change notification settings - Fork 5
/
c64-keyboard-v2.ino
235 lines (169 loc) · 6.92 KB
/
c64-keyboard-v2.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
#include <Keyboard.h>
// ***********************************************************************
// ** **
// ** **
// ** Commodore 64 USB HID for Pro Micro **
// ** Originally by DJ Sures (Synthiam.com) (c)2019 **
// ** **
// ** Updated: July 17, 2021 by Chris Garrett @makerhacks **
// ** to fix cursor up and CTRL/C= behaviour **
// ** **
// ** C= + F7: F12 (eg. bring VICE menu) **
// ***********************************************************************
// C64 Arduino
// KeyBoard Pro Micro
// Pin Pin
// Rows
// 20 2 - SDA
// 19 3 - SCL
// 18 4 - A6
// 17 5
// 16 6 - A7
// 15 7 -
// 14 8 - A8
// 13 9 - A9
// Columns
// 12 10 - A10
// 11 16 - MOSI
// 10 14 - MISO
// 9 15 - SCLK
// 8 18 - A0
// 7 19 - A1
// 6 20 - A2
// 5 21 - A3
// 4 N/C
// 3 1
// 2 N/C
// 1 0
// Uncomment if you wish to debug keyboard through serial monitor at 9600 baud
//#define DEBUG
#include <HID.h>
#include <Keyboard.h>
int lastKeyState[80];
long lastDebounceTime[80];
int debounceDelay = 50;
int RowPinMap[8] = {9, 3, 4, 5, 6, 7, 8, 2};
int ColPinMap[10] = {10, 16, 14, 21, 18, 19, 20, 15, 1, 0};
// Keyboard matrix from http://sta.c64.org/cbm64kbdlay.html
uint16_t keyMap[80] = {
// Del Return LR F7 F1 F3 F5 UD Null Null
KEY_BACKSPACE, KEY_RETURN, KEY_RIGHT_ARROW, KEY_F7, KEY_F1, KEY_F3, KEY_F5, KEY_DOWN_ARROW, NULL, NULL,
// 3 W A 4 Z S E LSHFT Joy2Down Joy1Down
'3', 'w', 'a', '4', 'z', 's', 'e', KEY_LEFT_SHIFT, '2', '*',
// 5 R D 6 C F T X Joy2Left Joy1Left
'5', 'r', 'd', '6', 'c', 'f', 't', 'x', '4', '-',
// 7 Y G 8 B H U V Joy2Right Joy1Right
'7', 'y', 'g', '8', 'b', 'h', 'u', 'v', '6', '+',
// 9 I J Zero M K O N Joy2F1 Joy1F1
'9', 'i', 'j', '0', 'm', 'k', 'o', 'n', '5', KEY_TAB,
// + P L - . : @ , Joy2F2 Joy1F2
'+', 'p', 'l', '-', '.', ':', '@', ',', '3', '1',
// Pound * ; Home RSHFT = Pi / Restore Null
KEY_INSERT, '*', ';', KEY_HOME, KEY_LEFT_SHIFT, '=', KEY_DELETE, '/', KEY_PAGE_UP, NULL,
// 1 BS CTRL 2 SPC C= Q RunStop Joy2Up Joy1Up
'1', KEY_ESC, KEY_LEFT_CTRL, '2', ' ', KEY_TAB, 'q', KEY_ESC, '8', '\\',
};
void setup() {
#ifdef DEBUG
Serial.begin(9600);
#else
Keyboard.begin();
#endif
for (int i = 0; i < 80; i++)
lastKeyState[i] = false;
for (int Row = 0; Row < 8; Row++)
pinMode(RowPinMap[Row], INPUT_PULLUP);
for (int Col = 0; Col < 10; Col++)
pinMode(ColPinMap[Col], INPUT_PULLUP);
}
void loop() {
for (int Row = 0; Row < 8; Row++) {
int RowPin = RowPinMap[Row];
pinMode(RowPin, OUTPUT);
digitalWrite(RowPin, LOW);
for (int Col = 0; Col < 10; Col++) {
int keyPos = Col + (Row * 10);
int outChar = keyMap[keyPos];
bool isKeyDown = !digitalRead(ColPinMap[Col]);
if (millis() < lastDebounceTime[keyPos] + debounceDelay)
continue;
// left shift key
if (lastKeyState[17] || lastKeyState[64] ) {
if (outChar == '2') {
outChar = '"';
} else if (outChar == '6') {
outChar = '&';
} else if (outChar == '8') {
outChar = '(';
} else if (outChar == '9') {
outChar = ')';
} else if (outChar == KEY_RIGHT_ARROW) {
outChar = KEY_LEFT_ARROW;
if (isKeyDown)
Keyboard.release(keyMap[17]); // left shift
} else if (outChar == KEY_DOWN_ARROW) {
outChar = KEY_UP_ARROW;
if (isKeyDown)
Keyboard.release(keyMap[17]); // left shift
} else if (outChar == '7') {
outChar = '\'';
if (isKeyDown)
Keyboard.release(keyMap[17]); // left shift
} else if (outChar == ';') {
outChar = ']';
if (isKeyDown)
Keyboard.release(keyMap[17]); // left shift
} else if (outChar == ':') {
outChar = '[';
if (isKeyDown)
Keyboard.release(keyMap[17]); // left shift
}
}
if (lastKeyState[17] && lastKeyState[72] && isKeyDown && outChar == 'w') {
lastKeyState[17] = false;
lastKeyState[72] = false;
Keyboard.releaseAll();
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press('w');
Keyboard.releaseAll();
delay(500);
continue;
}
// C= + F7
if (lastKeyState[75] && outChar == 200) outChar=KEY_F12;
if (isKeyDown && !lastKeyState[keyPos]) {
lastKeyState[keyPos] = true;
lastDebounceTime[keyPos] = millis();
#ifndef DEBUG
if (outChar != NULL)
Keyboard.press(outChar);
#else
Serial.print("col: ");
Serial.print(Col);
Serial.print(", row: ");
Serial.print(Row);
Serial.print(", Position: ");
Serial.print(keyPos);
Serial.print(", char:");
Serial.print(outChar);
Serial.println();
#endif
}
if (!isKeyDown && lastKeyState[keyPos]) {
lastKeyState[keyPos] = false;
lastDebounceTime[keyPos] = millis();
#ifndef DEBUG
if (outChar != NULL)
Keyboard.release(outChar);
#else
Serial.print("Released ");
Serial.print(keyPos);
Serial.println();
#endif
}
}
digitalWrite(RowPin, HIGH);
delay(1);
pinMode(RowPin, INPUT_PULLUP);
}
}