forked from caru/StenoFW
-
Notifications
You must be signed in to change notification settings - Fork 2
/
StenoFW.ino
385 lines (350 loc) · 10.7 KB
/
StenoFW.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
/**
* StenoFW is a firmware for Stenoboard keyboards.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2014 Emanuele Caruso. See LICENSE.txt for details.
* Copyright 2016 Carl Hauser under the same license.
*/
// #include "Stenoboard.h"
// #include "Volksboard.h"
#include "Volksboard_3.h"
long debounceMillis = 20;
// Keyboard state variables
boolean isStrokeInProgress = false;
boolean currentChord[ROWS][COLS];
boolean currentKeyReadings[ROWS][COLS];
boolean debouncingKeys[ROWS][COLS];
unsigned long debouncingMicros[ROWS][COLS];
// Other state variables
int ledIntensity = 1; // Min 0 - Max 255
// Protocol state
#define GEMINI 0
#define TXBOLT 1
#define NKRO 2
int protocol = GEMINI;
// This is called when the keyboard is connected
void setup() {
Keyboard.begin();
Serial.begin(9600);
#ifdef DRIVECOLUMNS
for (int i = 0; i < COLS; i++) {
pinMode(colPins[i], OUTPUT);
digitalWrite(colPins[i], HIGH);
}
for (int i = 0; i < ROWS; i++) {
pinMode(rowPins[i], INPUT_PULLUP);
}
#elif defined(DRIVECODEDCOLUMNS)
for (int i = 0; i < 3; i++) {
pinMode(colPins[i], OUTPUT);
}
for (int i = 0; i < ROWS; i++) {
pinMode(rowPins[i], INPUT_PULLUP);
}
#else
for (int i = 0; i < COLS; i++)
pinMode(colPins[i], INPUT_PULLUP);
for (int i = 0; i < ROWS; i++) {
pinMode(rowPins[i], OUTPUT);
digitalWrite(rowPins[i], HIGH);
}
#endif
pinMode(ledPin, OUTPUT);
analogWrite(ledPin, ledIntensity);
clearBooleanMatrixes();
}
// Read key states and handle all chord events
void loop() {
readKeys();
boolean isAnyKeyPressed = true;
// If stroke is not in progress, check debouncing keys
if (!isStrokeInProgress) {
checkAlreadyDebouncingKeys();
if (!isStrokeInProgress) checkNewDebouncingKeys();
}
// If any key was pressed, record all pressed keys
if (isStrokeInProgress) {
isAnyKeyPressed = recordCurrentKeys();
}
// If all keys have been released, send the chord and reset global state
if (!isAnyKeyPressed) {
sendChord();
clearBooleanMatrixes();
isStrokeInProgress = false;
}
}
// Record all pressed keys into current chord. Return false if no key is currently pressed
boolean recordCurrentKeys() {
boolean isAnyKeyPressed = false;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (currentKeyReadings[i][j] == true) {
currentChord[i][j] = true;
isAnyKeyPressed = true;
}
}
}
return isAnyKeyPressed;
}
// If a key is pressed, add it to debouncing keys and record the time
void checkNewDebouncingKeys() {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (currentKeyReadings[i][j] == true && debouncingKeys[i][j] == false) {
debouncingKeys[i][j] = true;
debouncingMicros[i][j] = micros();
}
}
}
}
// Check already debouncing keys. If a key debounces, start chord recording.
void checkAlreadyDebouncingKeys() {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (debouncingKeys[i][j] == true && currentKeyReadings[i][j] == false) {
debouncingKeys[i][j] = false;
continue;
}
if (debouncingKeys[i][j] == true && (micros() - debouncingMicros[i][j]) / 1000 > debounceMillis) {
isStrokeInProgress = true;
currentChord[i][j] = true;
return;
}
}
}
}
// Set all values of all boolean matrixes to false
void clearBooleanMatrixes() {
clearBooleanMatrix(currentChord, false);
clearBooleanMatrix(currentKeyReadings, false);
clearBooleanMatrix(debouncingKeys, false);
}
// Set all values of the passed matrix to the given value
void clearBooleanMatrix(boolean booleanMatrix[][COLS], boolean value) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
booleanMatrix[i][j] = value;
}
}
}
#ifdef DRIVECOLUMNS
// Read all keys
void readKeys() {
for (int i = 0; i < COLS; i++) {
digitalWrite(colPins[i], LOW);
for (int j = 0; j < ROWS; j++)
currentKeyReadings[i][j] = digitalRead(rowPins[j]) == LOW ? true : false;
digitalWrite(colPins[i], HIGH);
}
}
#elif defined(DRIVECODEDCOLUMNS)
void readKeys() {
int toggle = 0;
digitalWrite(colPins[0], HIGH);
digitalWrite(colPins[1], LOW);
digitalWrite(colPins[2], HIGH);
for (int k = 0; k < 2; k++) {
for (int i = 0; i < 3; i++) {
digitalWrite(colPins[i], toggle);
toggle ^= 1;
for (int j = 0; j < ROWS; j++)
currentKeyReadings[k*3+i][j] = digitalRead(rowPins[j]) == LOW ? true : false;
}
}
}
// This code works for exactly six columns and 3 pins.
// colPins[0] should be attached to A0 on the decoder chips
// colPins[1] to A1
// colPins[2] to A2
// This generates column addresses on the output column pins in the order
// 100 - 4
// 110 - 6
// 010 - 2
// 011 - 3
// 001 - 1
// 101 - 5
// Note that the column addresses are designed so that at least 1 bit is 1 in
// all cases. Hopefully this is good enough to derive power for the decoder
// chip from the signal lines!
// Given sequence of generated addresses above, the board's column wires
// need to be hooked to the associated Y pins of the decoder chips in that same order:
// Column 0 to Y4
// Column 1 to Y6
// Column 2 to Y2
// Column 3 to Y3
// Column 4 to Y1
// Column 5 to Y5
#else
// Read all keys
void readKeys() {
for (int i = 0; i < ROWS; i++) {
digitalWrite(rowPins[i], LOW);
for (int j = 0; j < COLS; j++)
currentKeyReadings[i][j] = digitalRead(colPins[j]) == LOW ? true : false;
digitalWrite(rowPins[i], HIGH);
}
}
#endif
// Send current chord using NKRO Keyboard emulation
void sendChordNkro() {
int keyCounter = 0;
char qwertyKeys[ROWS * COLS];
boolean firstKeyPressed = false;
// Calculate qwerty keys array using qwertyMapping[][]
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLS; j++)
if (currentChord[i][j]) {
qwertyKeys[keyCounter] = qwertyMapping[i][j];
keyCounter++;
}
// Emulate keyboard key presses
for (int i = 0; i < keyCounter; i++) {
if (qwertyKeys[i] != ' ') {
Keyboard.press(qwertyKeys[i]);
if (!firstKeyPressed) firstKeyPressed = true;
else Keyboard.release(qwertyKeys[i]);
}
}
Keyboard.releaseAll();
}
// Send current chord over serial using the Gemini protocol.
void sendChordGemini() {
// Initialize chord bytes
byte chordBytes[] = {B10000000, 0, 0, 0, 0, 0};
// Calculate chord bytes using the geminiBytes and geminiBits arrays
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLS; j++)
if (currentChord[i][j]) {
chordBytes[geminiBytes[i][j]] |= geminiBits[i][j];
}
// Send chord bytes over serial
for (int i = 0; i < 6; i++) {
Serial.write(chordBytes[i]);
}
}
void sendChordTxBolt() {
byte chordIdentifier[] = {B00000000, B01000000, B10000000, B11000000};
byte chordBytes[] = {0, 0, 0, 0};
// TX Bolt uses a variable length packet. Only those bytes that have active
// keys are sent. The header bytes indicate which keys are being sent. They
// must be sent in order. It is a good idea to send a zero after every packet.
// 00XXXXXX 01XXXXXX 10XXXXXX 110XXXXX
// HWPKTS UE*OAR GLBPRF #ZDST
// Calculate chord bytes using the txboltBytes and txboltBits arrays
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLS; j++)
if (currentChord[i][j]) {
chordBytes[txboltBytes[i][j]] |= txboltBits[i][j];
}
// now send the non-zero chord bytes adding the correct identifier to each
for (int i = 0; i < 4; i++) {
if (chordBytes[i]) {
chordBytes[i] |= chordIdentifier[i];
Serial.write(chordBytes[i]);
}
}
Serial.write((byte) 0);
}
// Send the chord using the current protocol. If there are fn keys
// pressed, delegate to the corresponding function instead.
// In future versions, there should also be a way to handle fn keys presses before
// they are released, eg. for mouse emulation functionality or custom key presses.
void sendChord() {
// If fn keys have been pressed, delegate to corresponding method and return
if (Key_Fn1 && Key_Fn2) {
fn1fn2();
return;
} else if (Key_Fn1) {
fn1();
return;
} else if (Key_Fn2) {
fn2();
return;
}
if (protocol == NKRO) {
sendChordNkro();
} else if (protocol == GEMINI) {
sendChordGemini();
} else {
sendChordTxBolt();
}
}
// Fn1 functions
//
// This function is called when "fn1" key has been pressed, but not "fn2".
// Tip: maybe it is better to avoid using "fn1" key alone in order to avoid
// accidental activation?
//
// Current functions:
// PH-PB -> Set NKRO Keyboard emulation mode
// PH-G -> Set Gemini PR protocol mode
// PH-B -> Set TX Bolt protocol mode
void fn1() {
// "PH" -> Set protocol
if (Key_P && Key_H) {
// "-PB" -> NKRO Keyboard
if (Key__P && Key__B) {
protocol = NKRO;
}
// "-G" -> Gemini PR
else if (Key__G) {
protocol = GEMINI;
}
// "-B" -> TX Bolt
else if (Key__B) {
protocol = TXBOLT;
}
}
}
// Fn2 functions
//
// This function is called when "fn2" key has been pressed, but not "fn1".
// Tip: maybe it is better to avoid using "fn2" key alone in order to avoid
// accidental activation?
//
// Current functions: none.
void fn2() {
}
// NEED TO THINK ABOUT THIS FOR VOLKSBOARD which doesn't have an LED --
// though I suppose we could go ahead and let it happen on an unconnected
// pin.
// Fn1-Fn2 functions
//
// This function is called when both "fn1" and "fn1" keys have been pressed.
//
// Current functions:
// HR-P -> LED intensity up
// HR-F -> LED intensity down
void fn1fn2() {
// "HR" -> Change LED intensity
if (Key_H && Key_R) {
// "-P" -> LED intensity up
if (Key__P) {
if (ledIntensity == 0) ledIntensity +=1;
else if(ledIntensity < 50) ledIntensity += 10;
else ledIntensity += 30;
if (ledIntensity > 255) ledIntensity = 0;
analogWrite(ledPin, ledIntensity);
}
// "-F" -> LED intensity down
if (Key__F) {
if(ledIntensity == 0) ledIntensity = 255;
else if(ledIntensity < 50) ledIntensity -= 10;
else ledIntensity -= 30;
if (ledIntensity < 1) ledIntensity = 0;
analogWrite(ledPin, ledIntensity);
}
}
}