-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdebug.js
137 lines (128 loc) · 4.61 KB
/
debug.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
// Base on https://www.html5rocks.com/en/tutorials/file/dndfiles//
import { brUuid } from './utils/constants.js';
import { downloadFile } from './utils/downloadFile.js';
import { getLatestRelease } from './utils/getLatestRelease.js';
import { getAppVersion } from './utils/getAppVersion.js';
import { getBdAddr } from './utils/getBdAddr.js';
import { dcReadFile } from './utils/dcReadFile.js';
var bluetoothDevice;
let brService = null;
var progress = document.querySelector('.percent');
var cancel = 0;
var bdaddr = '';
var app_ver = '';
var latest_ver = '';
var name = '';
export function abortFileTransfer() {
cancel = 1;
}
function setProgress(percent) {
progress.style.width = percent + '%';
progress.textContent = percent + '%';
}
export function pakRead(evt) {
// Reset progress indicator on new file selection.
progress.style.width = '0%';
progress.textContent = '0%';
readFile()
.then(value => {
downloadFile(new Blob([value.buffer], {type: "application/bin"}),
'br_debug_trace.bin');
document.getElementById("divBtConn").style.display = 'none';
document.getElementById("divInfo").style.display = 'block';
document.getElementById("divFileSelect").style.display = 'block';
document.getElementById("divFileTransfer").style.display = 'none';
})
.catch(error => {
log('Argh! ' + error);
document.getElementById("divBtConn").style.display = 'none';
document.getElementById("divInfo").style.display = 'block';
document.getElementById("divFileSelect").style.display = 'block';
document.getElementById("divFileTransfer").style.display = 'none';
cancel = 0;
});
}
function readFile() {
return new Promise(function(resolve, reject) {
document.getElementById('progress_bar').className = 'loading';
document.getElementById("divBtConn").style.display = 'none';
document.getElementById("divInfo").style.display = 'block';
document.getElementById("divFileSelect").style.display = 'none';
document.getElementById("divFileTransfer").style.display = 'block';
dcReadFile(brService, setProgress, cancel)
.then(data => {
resolve(data);
})
.catch(error => {
reject(error);
});
});
}
function onDisconnected() {
log('> Bluetooth Device disconnected');
cancel = 0;
document.getElementById("divBtConn").style.display = 'block';
document.getElementById("divInfo").style.display = 'none';
document.getElementById("divFileSelect").style.display = 'none';
document.getElementById("divFileTransfer").style.display = 'none';
}
export function btConn() {
log('Requesting Bluetooth Device...');
navigator.bluetooth.requestDevice(
{filters: [{namePrefix: 'BlueRetro'}],
optionalServices: [brUuid[0]]})
.then(device => {
log('Connecting to GATT Server...');
name = device.name;
bluetoothDevice = device;
bluetoothDevice.addEventListener('gattserverdisconnected', onDisconnected);
return bluetoothDevice.gatt.connect();
})
.then(server => {
log('Getting BlueRetro Service...');
return server.getPrimaryService(brUuid[0]);
})
.catch(error => {
log(error.name);
throw 'Couldn\'t connect to BlueRetro';
})
.then(service => {
brService = service;
return getBdAddr(brService);
})
.then(value => {
bdaddr = value;
return getLatestRelease();
})
.then(value => {
latest_ver = value
return getAppVersion(brService);
})
.catch(error => {
if (error.name == 'NotFoundError'
|| error.name == 'NotSupportedError') {
return '';
}
throw error;
})
.then(value => {
app_ver = value;
document.getElementById("divInfo").innerHTML = 'Connected to: ' + name + ' (' + bdaddr + ') [' + app_ver + ']';
try {
if (app_ver.indexOf(latest_ver) == -1) {
document.getElementById("divInfo").innerHTML += '<br><br>Download latest FW ' + latest_ver + ' from <a href=\'https://darthcloud.itch.io/blueretro\'>itch.io</a>';
}
}
catch (e) {
// Just move on
}
log('Init Cfg DOM...');
document.getElementById("divBtConn").style.display = 'none';
document.getElementById("divInfo").style.display = 'block';
document.getElementById("divFileSelect").style.display = 'block';
document.getElementById("divFileTransfer").style.display = 'none';
})
.catch(error => {
log('Argh! ' + error);
});
}