-
Notifications
You must be signed in to change notification settings - Fork 3
/
bletx.js
160 lines (140 loc) · 4.6 KB
/
bletx.js
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
/** Global Variables */
var midi, data;
var synth;
var bluetoothDevice;
var bleDevice;
var readyBLEpacket = [];
var timer = setInterval(bleSend, 10);
/** Function to run upon window load. */
async function Init(){
kb=document.getElementById("keyboard");
kb.addEventListener("change",KeyIn);
}
/** Function for connecting to MIDIBLE Device */
function txConnect() {
printToConsole('Searching for bluetooth devices...');
console.log('Requesting Bluetooth Device with MIDI UUID...');
bluetoothDevice = null;
console.log('Searching for ' + getUrlVars()["demo"] + getUrlVars()["id"] + "...");
navigator.bluetooth.requestDevice({
filters: [{
services: [MIDI_SERVICE_UID],
name: bleDevice
}]
})
.then(device =>{
console.log('Connecting to GATT server of ' + device.name);
printToConsole('Connecting to bluetooth device '+ device.name + '...');
bluetoothDevice = device;
return device.gatt.connect();
})
.then(server => {
console.log('Getting Service...');
return server.getPrimaryService(MIDI_SERVICE_UID);
})
.then(service => {
console.log('Getting Characteristic...');
return service.getCharacteristic(MIDI_IO_CHARACTERISTIC_UID);
})
.then(characteristic => {
console.log('Found Characteristic...');
printToConsole('Ready to rock!');
bleConnected();
midiChar = characteristic;
})
.catch(error => {
printToConsole(error);
console.log('Argh! ' + error);
})
}
function txDisconnect() {
if (!bluetoothDevice) {
return;
}
console.log('Disconnecting from Bluetooth Device...');
if (bluetoothDevice.gatt.connected) {
bluetoothDevice.gatt.disconnect();
bleDisconnect();
printToConsole('Disconnecting from Bluetooth Device...');
} else {
console.log('> Bluetooth Device is already disconnected');
printToConsole('Bluetooth Device is already disconnected.');
}
}
function onDisconnected(event) {
let device = event.target;
console.log('Device ' + device.name + ' is disconnected.');
printToConsole('Device ' + device.name + ' is disconnected.');
bleDisconnect();
}
function midiEncoder(midiData) {
let midiBLEmessage = [];
let pos = 0;
let len = midiData.length;
console.log('Encoding: ' + midiData);
midiBLEmessage.push(timestampGenerator()[0]);
for (pos = 0; pos < len;pos++) {
if ((midiData[pos] >>> 7) === 1) {
midiBLEmessage.push(timestampGenerator()[1]);
}
midiBLEmessage.push(midiData[pos]);
}
console.log('Encoded: ' + midiBLEmessage);
printToConsole('Encoded: ' + midiBLEmessage);
return midiBLEmessage;
}
function timestampGenerator() {
let localTime = performance.now() & 8191;
let timestamp = [((localTime >> 7) | 0x80) & 0xBF,(localTime & 0x7F) | 0x80];
return timestamp;
}
function KeyIn(e){
let i = 0;
midiMessage = [0x90,e.note[1],e.note[0]?100:0];
let l = midiMessage.length;
printToConsole('MIDI-message: ' + midiMessage);
for (i = 0; i < l; i++) {
readyBLEpacket.push(midiMessage[i]);
}
}
window.onload=function() {
console.log('Service: ' + MIDI_SERVICE_UID);
console.log('Char: ' + MIDI_IO_CHARACTERISTIC_UID);
document.getElementById('ikeys').style.display = 'none';
Init();
let norurl;
if (getUrlVars()["demo"] !== undefined && getUrlVars()["id"] !== undefined) {
norurl = "./multitx.html?demo=" + getUrlVars()["demo"] + "&id=" + getUrlVars()["id"];
console.log(norurl);
document.getElementById("link").href = norurl;
bleDevice = getUrlVars()["demo"] + " " + getUrlVars()["id"];
}else{
console.log("BLE Device: " + bleDevice);
}
}
function bleConnected() {
document.getElementById('ikeys').style.display = 'block';
document.getElementById('hide').style.display = 'none';
document.getElementById('ibutton').innerHTML = 'Disconnect';
document.getElementById('midi-data').style.height = '45vh';
document.getElementById("ibutton").onclick = txDisconnect;
document.getElementById("info").style.fontSize = '1.5em';
document.getElementById('midi-data').style.background = '#ECEFF1'
}
function bleSend(){
let packet = [];
if(readyBLEpacket.length > 0) {
packet = new Uint8Array(midiEncoder(readyBLEpacket));
console.log('BLE-packet sent: ' + packet);
midiChar.writeValue(packet);
readyBLEpacket = [];
}
}
function bleDisconnect() {
document.getElementById('ikeys').style.display = 'none';
document.getElementById('hide').style.display = 'block';
document.getElementById('ibutton').innerHTML = 'Connect';
document.getElementById('midi-data').style.height = '10vh';
document.getElementById("ibutton").onclick = txConnect;
document.getElementById("info").style.fontSize = '2em';
}