-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
dc_vmu.js
204 lines (190 loc) · 6.99 KB
/
dc_vmu.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
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
// Base on https://www.html5rocks.com/en/tutorials/file/dndfiles//
import { brUuid, vmuSize } 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 { dcWriteFile } from './utils/dcWriteFile.js';
import { dcReadFile } from './utils/dcReadFile.js';
var bluetoothDevice;
let brService = null;
var reader;
var progress = document.querySelector('.percent');
var cancel = 0;
var bdaddr = '';
var app_ver = '';
var latest_ver = '';
var name = '';
export function abortFileTransfer() {
cancel = 1;
}
function errorHandler(evt) {
switch(evt.target.error.code) {
case evt.target.error.NOT_FOUND_ERR:
log('File Not Found!');
break;
case evt.target.error.NOT_READABLE_ERR:
log('File is not readable');
break;
case evt.target.error.ABORT_ERR:
break; // noop
default:
log('An error occurred reading this file.');
};
}
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 => {
let view = new DataView(value.buffer);
for (let i = 0; i < vmuSize; i += 4) {
view.setUint32(i, view.getUint32(i), true)
}
downloadFile(new Blob([value.buffer], {type: "application/bin"}),
'vmu.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;
});
}
export function pakWrite(evt) {
// Reset progress indicator on new file selection.
progress.style.width = '0%';
progress.textContent = '0%';
reader = new FileReader();
reader.onerror = errorHandler;
reader.onabort = function(e) {
log('File read cancelled');
};
reader.onload = function(e) {
let data = reader.result.slice(0, vmuSize);
let view = new DataView(data);
for (let i = 0; i < vmuSize; i += 4) {
view.setUint32(i, view.getUint32(i), true)
}
writeFile(data);
}
// Read in the image file as a binary string.
reader.readAsArrayBuffer(document.getElementById("pakFile").files[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 writeFile(data) {
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';
dcWriteFile(brService, data, setProgress, cancel)
.then(_ => {
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 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);
});
}