Skip to content

Instantly share code, notes, and snippets.

@JeanRibes
Created February 22, 2023 21:50
Show Gist options
  • Save JeanRibes/d70cd454dda54b276c6379891e066f61 to your computer and use it in GitHub Desktop.
Save JeanRibes/d70cd454dda54b276c6379891e066f61 to your computer and use it in GitHub Desktop.
0:0
1:1
2:2
3:3
4:-1
5:5
6:6
7:7
8:8
9:9
10:10
11:11
12:12
13:13
14:14
15:15
16:16
17:17
18:18
19:19
20:20
21:21
22:22
23:23
24:24
25:25
26:26
27:27
28:28
29:29
30:-1
31:-1
32:32
33:33
34:34
35:35
36:36
37:37
38:38
39:39
40:40
41:41
42:42
43:43
44:44
45:45
46:46
47:47
48:89
49:74
50:76
51:77
52:79
53:81
54:83
55:84
56:86
57:88
58:77
59:59
60:91
61:61
62:62
63:63
64:72
65:60
66:66
67:67
68:64
69:63
70:65
71:67
72:69
73:73
74:71
75:72
76:74
77:77
78:78
79:75
80:-1
81:-1
82:-4
83:62
84:66
85:70
86:86
87:61
88:88
89:68
90:90
91:76
92:79
93:-1
94:-1
95:93
96:96
97:73
98:75
99:78
100:80
101:82
102:-1
103:85
104:87
105:90
106:90
107:92
108:94
109:109
110:110
111:111
112:112
113:113
114:114
115:115
116:116
117:117
118:118
119:119
120:120
121:121
122:122
123:123
124:124
125:125
126:126
127:127
import serial
from alsa_midi import SequencerClient, READ_PORT, NoteOnEvent, NoteOffEvent
from time import sleep
from typing import Dict
CHAN = 0
VEL=64
keymap: Dict[int,int] = {i:i for i in range(128)}
with open('midi-keymap.txt', 'r') as f:
for line in f.readlines():
k,v = line.split(':')
key = int(k)
val = int(v)
keymap[key]=val
def save_keymap():
with open('midi-keymap.txt', 'w+') as f:
for key,value in keymap.items():
f.write(f"{key}:{value}\n")
ps2 = serial.Serial('/dev/ttyACM0', baudrate=115200)
client = SequencerClient("clavier PS/2")
port = client.create_port("output", caps=READ_PORT)
dest_port = client.list_ports(output=True)[0]
#port.connect_to(dest_port)
#print(ps2.readall())
#ps2.timeout = 1
state = [False] * 128
last_code=0
while True:
try:
status, code = ps2.read(2)
except (TypeError, ValueError):
continue
if code >= 128:
continue
noteOn = (status >> 7) == 0
if state[code] and noteOn and code not in (43,44,45):
continue
state[code] = noteOn
if code in (43,44,45,46) and status == 16: # press + caps lock
if not noteOn:
pass
elif code == 43:
save_keymap()
elif code == 44:
#print(f"set {last_code} to {keymap[last_code] + 1}")
keymap[last_code] = keymap[last_code] + 1
elif code == 45:
#print(f"set {last_code} to {keymap[last_code] - 1}")
keymap[last_code] = keymap[last_code] - 1
elif code==46:
keymap[last_code] = -1
code = last_code
elif noteOn:
last_code = code
note = keymap[code]
if note < 0 or note > 127:
continue
print(noteOn * 1, code, note, status, (status >> (4 & 0b00010000))== 1)
if (status >> 7) == 0: # 0: MAKE, 1: BREAK
client.event_output(NoteOnEvent(note=note, velocity=64, channel=CHAN))
else:
client.event_output(NoteOffEvent(note=note, velocity=64, channel=CHAN))
client.drain_output()
/*
blanc: DATA
vert: CLK / IRQ
rouge: VCC
noir: GND
*/
#include <PS2KeyAdvanced.h>
/* Keyboard constants Change to suit your Arduino
define pins used for data and clock from keyboard */
#define DATAPIN 4
#define IRQPIN 3
uint16_t c;
PS2KeyAdvanced keyboard;
void setup( )
{
// Configure the keyboard library
keyboard.begin( DATAPIN, IRQPIN );
Serial.begin( 115200 );
//Serial.println( "PS2 Advanced Key Simple Test:" );
pinMode(13, OUTPUT);
//keyboard.setLock(PS2_LOCK_NUM);
}
void loop( )
{
if ( keyboard.available( ) )
{
// read the next key
c = keyboard.read( );
if ( c > 0 )
{
Serial.write(c >> 8);
Serial.write(c & 0xFF);
}
}
if (((c >> 8) & 0b10000000) == 0)
digitalWrite(13, HIGH);
else
digitalWrite(13, LOW);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment