-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathkeyboardscanner.ino
509 lines (486 loc) · 10.3 KB
/
keyboardscanner.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
Moura's Keyboard Scanner: turn you broken (or unused) keyboard in a MIDI controller
Copyright (C) 2017 Daniel Moura <oxe@oxesoft.com>
This code is originally hosted at https://github.com/oxesoft/keyboardscanner
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/>.
*/
#include <DIO2.h> // install the library DIO2
#define KEYS_NUMBER 61
#define KEY_OFF 0
#define KEY_START 1
#define KEY_ON 2
#define KEY_RELEASED 3
#define KEY_SUSTAINED 4
#define KEY_SUSTAINED_RESTART 5
#define MIN_TIME_MS 3
#define MAX_TIME_MS 50
#define MAX_TIME_MS_N (MAX_TIME_MS - MIN_TIME_MS)
#define PEDAL_PIN 21
//find out the pins using a multimeter, starting from the first key
//see the picture key_scheme.png to understand how to map the inputs and outputs
//the following configuration is specific for PSR530
//thanks Leandro Meucchi, from Argentina, by the PDF
//take a look at the scheme detailed in PSR530.pdf and modify the following mapping according to the wiring of your keyboard
#define PIN_A1 33
#define PIN_A2 32
#define PIN_A3 31
#define PIN_A4 30
#define PIN_A5 29
#define PIN_A6 28
#define PIN_A7 27
#define PIN_A8 26
#define PIN_A9 25
#define PIN_A10 24
#define PIN_A11 23
#define PIN_A12 22
#define PIN_B1 40
#define PIN_B2 39
#define PIN_B3 38
#define PIN_B4 37
#define PIN_B5 36
#define PIN_B6 35
#define PIN_B7 34
#define PIN_C1 45
#define PIN_C2 44
#define PIN_C3 43
#define PIN_C4 42
#define PIN_C5 41
byte output_pins[] = {
PIN_B6, //C0
PIN_B6,
PIN_B7,
PIN_B7,
PIN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8,
PIN_A7,
PIN_A3,
PIN_A4,
PIN_A1,
PIN_A2,
PIN_A11,
PIN_A12,
PIN_A11,
PIN_A12,
PIN_A1,
PIN_A2,
PIN_A3,
PIN_A4,
PIN_A8,
PIN_A7,
PIN_A6,
PIN_A5,
PIN_A9, //C1
PIN_A10,
PIN_A9,
PIN_A10,
PIN_A6,
PIN_A5,
PIN_A8,
PIN_A7,
PIN_A3,
PIN_A4,
PIN_A1,
PIN_A2,
PIN_A11,
PIN_A12,
PIN_A11,
PIN_A12,
PIN_A1,
PIN_A2,
PIN_A3,
PIN_A4,
PIN_A8,
PIN_A7,
PIN_A6,
PIN_A5,
PIN_A9, //C2
PIN_A10,
PIN_A9,
PIN_A10,
PIN_A1,
PIN_A2,
PIN_A6,
PIN_A5,
PIN_A8,
PIN_A7,
PIN_A3,
PIN_A4,
PIN_A11,
PIN_A12,
PIN_A11,
PIN_A12,
PIN_A3,
PIN_A4,
PIN_A9,
PIN_A10,
PIN_A6,
PIN_A5,
PIN_A8,
PIN_A7,
PIN_A1, //C3
PIN_A2,
PIN_A1,
PIN_A2,
PIN_A6,
PIN_A5,
PIN_A8,
PIN_A7,
PIN_A3,
PIN_A4,
PIN_A9,
PIN_A10,
PIN_A11,
PIN_A12,
PIN_A11,
PIN_A12,
PIN_A1,
PIN_A2,
PIN_A3,
PIN_A4,
PIN_A8,
PIN_A7,
PIN_A6,
PIN_A5,
PIN_A9, //C4
PIN_A10,
PIN_A9,
PIN_A10,
PIN_A11,
PIN_A12,
PIN_A6,
PIN_A5,
PIN_A8,
PIN_A7,
PIN_A3,
PIN_A4,
PIN_A1,
PIN_A2,
PIN_A1,
PIN_A2,
PIN_A11,
PIN_A12,
PIN_A3,
PIN_A4,
PIN_A8,
PIN_A7,
PIN_A6,
PIN_A5,
PIN_A9, //C5
PIN_A10
};
//cheap keyboards often has the black keys softer or harder than the white ones
//uncomment the next line to allow a soft correction
//#define BLACK_KEYS_CORRECTION
#ifdef BLACK_KEYS_CORRECTION
#define MULTIPLIER 192 // 127 is the central value (corresponding to 1.0)
byte black_keys[] = {
0,1,0,1,0,0,1,0,1,0,1,0,
0,1,0,1,0,0,1,0,1,0,1,0,
0,1,0,1,0,0,1,0,1,0,1,0,
0,1,0,1,0,0,1,0,1,0,1,0,
0,1,0,1,0,0,1,0,1,0,1,0,
0
};
#endif
//uncomment the next line to inspect the number of scans per seconds
//#define DEBUG_SCANS_PER_SECOND
/*
426 cyles per second (2,35ms per cycle) using standard digitalWrite/digitalRead
896 cyles per second (1,11ms per cycle) using DIO2 digitalWrite2/digitalRead2
*/
//uncoment the next line to get text midi message at output
//#define DEBUG_MIDI_MESSAGE
byte keys_state[KEYS_NUMBER];
unsigned long keys_time[KEYS_NUMBER];
boolean signals[KEYS_NUMBER * 2];
boolean pedal_enabled;
void setup() {
Serial.begin(115200);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
int i;
for (i = 0; i < KEYS_NUMBER; i++)
{
keys_state[i] = KEY_OFF;
keys_time[i] = 0;
}
for (byte pin = 0; pin < sizeof(output_pins); pin++)
{
pinMode(output_pins[pin], OUTPUT);
}
for (byte pin = 0; pin < sizeof(input_pins); pin++)
{
pinMode(input_pins[pin], INPUT_PULLUP);
}
pinMode(PEDAL_PIN, INPUT_PULLUP);
pedal_enabled = digitalRead(PEDAL_PIN) != HIGH;
}
void send_midi_event(byte status_byte, byte key_index, unsigned long time)
{
unsigned long t = time;
#ifdef BLACK_KEYS_CORRECTION
if (black_keys[key_index])
{
t = (t * MULTIPLIER) >> 7;
}
#endif
if (t > MAX_TIME_MS)
t = MAX_TIME_MS;
if (t < MIN_TIME_MS)
t = MIN_TIME_MS;
t -= MIN_TIME_MS;
unsigned long velocity = 127 - (t * 127 / MAX_TIME_MS_N);
byte vel = (((velocity * velocity) >> 7) * velocity) >> 7;
byte key = 36 + key_index;
#ifdef DEBUG_MIDI_MESSAGE
char out[32];
sprintf(out, "%02X %02X %03d %d", status_byte, key, vel, time);
Serial.println(out);
#else
Serial.write(status_byte);
Serial.write(key);
Serial.write(vel);
#endif
}
void loop() {
#ifdef DEBUG_SCANS_PER_SECOND
static unsigned long cycles = 0;
static unsigned long start = 0;
static unsigned long current = 0;
cycles++;
current = millis();
if (current - start >= 1000)
{
Serial.println(cycles);
cycles = 0;
start = current;
}
#endif
byte pedal = LOW;
if (pedal_enabled)
{
pedal = digitalRead2(PEDAL_PIN);
}
boolean *s = signals;
for (byte i = 0; i < KEYS_NUMBER * 2; i++)
{
byte output_pin = output_pins[i];
byte input_pin = input_pins[i];
digitalWrite2(output_pin, LOW);
*(s++) = !digitalRead2(input_pin);
digitalWrite2(output_pin, HIGH);
}
byte *state = keys_state;
unsigned long *ktime = keys_time;
boolean *signal = signals;
for (byte key = 0; key < KEYS_NUMBER; key++)
{
for (byte state_index = 0; state_index < 2; state_index++)
{
switch (*state)
{
case KEY_OFF:
if (state_index == 0 && *signal)
{
*state = KEY_START;
*ktime = millis();
}
break;
case KEY_START:
if (state_index == 0 && !*signal)
{
*state = KEY_OFF;
break;
}
if (state_index == 1 && *signal)
{
*state = KEY_ON;
send_midi_event(0x90, key, millis() - *ktime);
}
break;
case KEY_ON:
if (state_index == 1 && !*signal)
{
*state = KEY_RELEASED;
*ktime = millis();
}
break;
case KEY_RELEASED:
if (state_index == 0 && !*signal)
{
if (pedal)
{
*state = KEY_SUSTAINED;
break;
}
*state = KEY_OFF;
send_midi_event(0x80, key, millis() - *ktime);
}
break;
case KEY_SUSTAINED:
if (!pedal)
{
*state = KEY_OFF;
send_midi_event(0x80, key, MAX_TIME_MS);
}
if (state_index == 0 && *signal)
{
*state = KEY_SUSTAINED_RESTART;
*ktime = millis();
}
break;
case KEY_SUSTAINED_RESTART:
if (state_index == 0 && !*signal)
{
*state = KEY_SUSTAINED;
digitalWrite(13, HIGH);
break;
}
if (state_index == 1 && *signal)
{
*state = KEY_ON;
send_midi_event(0x80, key, MAX_TIME_MS);
send_midi_event(0x90, key, millis() - *ktime);
}
break;
}
signal++;
}
state++;
ktime++;
}
}