-
Notifications
You must be signed in to change notification settings - Fork 0
/
paper-steno.ino
280 lines (230 loc) · 7.28 KB
/
paper-steno.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
#define LENGTH(a) (sizeof(a) / sizeof(a[0]))
// -----------------------------------------------------------------------------
// TX Bolt byte/bit pairs: `ST` = star, `NN` = number bar, `XX` = none.
// Size of stroke representation/TX Bolt packet.
#define STROKE_BYTES 4
#define TX_BOLT(BYTE, BIT) (((BYTE)<<4) | (BIT))
#define TX_BYTE(TX) ((TX)>>4)
#define TX_BIT(TX) ((TX) & 0x7)
#define S_ TX_BOLT(0, 0)
#define T_ TX_BOLT(0, 1)
#define K_ TX_BOLT(0, 2)
#define P_ TX_BOLT(0, 3)
#define W_ TX_BOLT(0, 4)
#define H_ TX_BOLT(0, 5)
#define R_ TX_BOLT(1, 0)
#define A TX_BOLT(1, 1)
#define O TX_BOLT(1, 2)
#define ST TX_BOLT(1, 3)
#define E TX_BOLT(1, 4)
#define U TX_BOLT(1, 5)
#define _F TX_BOLT(2, 0)
#define _R TX_BOLT(2, 1)
#define _P TX_BOLT(2, 2)
#define _B TX_BOLT(2, 3)
#define _L TX_BOLT(2, 4)
#define _G TX_BOLT(2, 5)
#define _T TX_BOLT(3, 0)
#define _S TX_BOLT(3, 1)
#define _D TX_BOLT(3, 2)
#define _Z TX_BOLT(3, 3)
#define NN TX_BOLT(3, 4)
#define XX TX_BOLT(3, 5)
// -----------------------------------------------------------------------------
// Config etc.
#define MAX_REPEAT 8
// Percent variation in chord timing that causes a sequence break.
#define BREAK_MARGIN 25
// Percent of chord delay to use for repeat (smaller is faster).
#define REPEAT_DELAY 67
#define DEBOUNCE_MS 50
#define CHECK_MS 5
#define CHECKS (DEBOUNCE_MS/CHECK_MS)
#define STENOMOD
// -----------------------------------------------------------------------------
// Layouts
#ifdef JASON_4x6
// Paper Steno layout
#define COLUMNS 4
#define ROWS 6
byte column_pins[COLUMNS] = {2, 3, 4, 5};
byte row_pins[ROWS] = {6, 7, 8, 9, 10, 11};
// map (col, row) to (byte, bit) of TX Bolt stroke.
const byte key_info[COLUMNS][ROWS] = {
// We use the top half of the `S-` key as the number "bar".
// If you don't need it, you can change it back to another `S_`.
{_P, _B, XX, ST, NN, S_},
{_L, _G, O, A, H_, R_},
{_T, _S, U, E, P_, W_},
{_D, _Z, _R, _F, T_, K_}
};
#endif
#ifdef STENOMOD
#define COLUMNS 4
#define ROWS 6
byte column_pins[COLUMNS] = {11, 10, 9, 8};
byte row_pins[ROWS] = {14, 15, 16, 17, 18, 19};
// map (col, row) to (byte, bit) of TX Bolt stroke.
const byte key_info[COLUMNS][ROWS] = {
// We use the top half of the `S-` key as the number "bar".
// If you don't need it, you can change it back to another `S_`.
{S_, T_, K_, P_, W_, H_},
{R_, A, O, ST, E, U},
{_F, _R, _P, _B, _L, _G},
{_T, _S, _D, _Z, NN, S_}
};
#endif // STENOMOD
// -----------------------------------------------------------------------------
// Key Input and Debouncing
void read_debounced_keys_into(byte *out) {
static byte past_keys[CHECKS][STROKE_BYTES];
static byte index = 0;
// Read current keys.
byte *keys = past_keys[index];
if(++index >= LENGTH(past_keys)) index = 0;
for(byte i=0; i<STROKE_BYTES; ++i) keys[i] = 0;
for(byte col=0; col<LENGTH(column_pins); ++col) {
pinMode(column_pins[col], OUTPUT);
digitalWrite(column_pins[col], LOW);
delayMicroseconds(10);
for(byte row=0; row<LENGTH(row_pins); ++row) {
const byte info = key_info[col][row];
const byte i = TX_BYTE(info), b = 1 << TX_BIT(info);
if(digitalRead(row_pins[row]) == LOW) keys[i] |= b;
}
pinMode(column_pins[col], INPUT);
}
keys[3] &= 0x1f;
// Return debounced values.
out[0] = 0x3f; out[1] = 0x3f; out[2] = 0x3f; out[3] = 0x1f;
for(byte i=0; i<CHECKS; ++i) {
for(byte j=0; j<STROKE_BYTES; ++j) {
// Clear keys that are off in any of the checks.
out[j] &= past_keys[i][j];
}
}
}
// -----------------------------------------------------------------------------
// TX Bolt output
void send_keys(byte *keys) {
static byte last_sent = 0;
boolean first = true;
for(byte i=0; i<STROKE_BYTES; ++i) {
if(keys[i]) {
if(first && i > last_sent) Serial.write(0);
Serial.write(keys[i] | (i << 6));
first = false; last_sent = i;
}
}
if(first) { Serial.write(0); last_sent = 0; }
}
// -----------------------------------------------------------------------------
// Stroke helper functions
boolean empty, changed, triggered;
void compare_keys(const byte *prev, const byte *cur, const byte *ignore) {
empty = true; changed = false; triggered = false;
for(byte i=0; i<STROKE_BYTES; ++i) {
const byte different = prev[i] ^ cur[i]; // XOR is a bitwise not-equal.
const byte released = different & prev[i]; // previously pressed.
empty &= !cur[i];
changed |= different;
triggered |= released & ~ignore[i];
}
}
void remove_ups(byte *ignore, const byte *cur) {
for(byte i=0; i<STROKE_BYTES; ++i) ignore[i] &= cur[i];
}
void set_keys(byte *dst, const byte *src) {
for(byte i=0; i<STROKE_BYTES; ++i) dst[i] = src[i];
}
void clear_keys(byte *keys) {
for(byte i=0; i<STROKE_BYTES; ++i) keys[i] = 0;
}
boolean same_keys(byte *a, byte *b) {
for(byte i=0; i<STROKE_BYTES; ++i) {
if(a[i] != b[i]) return false;
}
return true;
}
// -----------------------------------------------------------------------------
// Repeat chord sequences.
byte chords[MAX_REPEAT][STROKE_BYTES];
unsigned int times[MAX_REPEAT];
byte first, count, last; // Use chords/times as circular buffers.
byte repeat;
unsigned int per_repeat;
byte next(byte n) {
n += first; if(n >= MAX_REPEAT) n -= MAX_REPEAT;
return n;
}
void record(byte keys[STROKE_BYTES], unsigned int now) {
if(count == MAX_REPEAT) { last = first; first = next(1); }
else { last = next(count); ++count; }
set_keys(chords[last], keys);
times[last] = now;
}
void break_sequence(unsigned int last_press) {
const unsigned int per_stroke = last_press - times[last];
const unsigned int margin = per_stroke * BREAK_MARGIN / 100;
for(byte i=count; i>0; --i) {
byte c = next(i-1);
const unsigned int dt = last_press - times[c];
if(dt < per_stroke - margin || dt > per_stroke + margin) {
first = next(i); count = count - i;
break;
}
last_press = times[c];
}
}
boolean choose_loop(byte chord[STROKE_BYTES]) {
for(byte i=0; i<count; ++i) {
byte c = next(i);
if(same_keys(chord, chords[c])) {
first = c; count = count - i;
return true;
}
}
return false;
}
// -----------------------------------------------------------------------------
// Initialization and main loop.
void setup() {
for(byte i=0; i<LENGTH(column_pins); ++i) pinMode(column_pins[i], INPUT);
for(byte i=0; i<LENGTH(row_pins); ++i) pinMode(row_pins[i], INPUT_PULLUP);
first = 0; count = 0; last = 0;
repeat = 0; per_repeat = 0;
Serial.begin(9600);
}
void loop() {
static byte keys[STROKE_BYTES], prev_keys[STROKE_BYTES];
static byte ignore[STROKE_BYTES];
static unsigned int last_change;
const unsigned int now = millis();
read_debounced_keys_into(keys);
compare_keys(prev_keys, keys, ignore);
if(triggered) {
send_keys(prev_keys);
record(prev_keys, last_change);
set_keys(ignore, keys);
} else remove_ups(ignore, keys);
if(changed) {
last_change = now; per_repeat = 0;
set_keys(prev_keys, keys);
} else if(!empty) {
const unsigned int hold = now - last_change;
const unsigned int threshold = last_change - times[last];
if(per_repeat == 0 && count > 0 && hold >= threshold) {
break_sequence(last_change);
if(choose_loop(keys)) {
repeat = 0; per_repeat = threshold * REPEAT_DELAY / 100;
set_keys(ignore, keys);
}
}
if(per_repeat != 0 && hold >= per_repeat) {
send_keys(chords[next(repeat)]);
++repeat; if(repeat == count) repeat = 0;
last_change = now;
}
}
delay(CHECK_MS);
}