-
Notifications
You must be signed in to change notification settings - Fork 5
/
webtool.html
265 lines (248 loc) · 9.11 KB
/
webtool.html
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
<!--
Based on the DAPJs web flash example:
https://github.com/ARMmbed/dapjs/blob/17e2b8a421c587349b3754189bbd44cb82c1dd0c/examples/daplink-flash/web.html
Copyright Arm Limited 2018
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->
<!DOCTYPE html>
<html>
<head>
<title>Universal Hex Creator</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<style>
html {
margin: 0;
padding: 0;
border: 0;
height: 100%;
}
body {
font: 400 16px/1.5 'Helvetica Neue', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
color: #ffffff;
text-align: center;
background-image: linear-gradient(135deg, #cd0365 -3%, #e7645c 70%);
background-attachment: fixed;
}
h1 {
font-weight: 600;
}
h2 {
font-weight: 500;
}
strong {
font-weight: 600;
}
.drop {
position: relative;
margin: 20px auto;
max-width: 500px;
background-color: rgba(255, 255, 255, 0.1);
padding: 40px 20px 65px 20px;
outline: 2px dashed #ffffff;
outline-offset: -10px;
}
.drop.hover {
outline-offset: -10px;
background-color: rgba(255, 255, 255, 0.2);
}
#icon-v1, #icon-v2 {
width: 100%;
fill: #ffffff;
margin-top: 26px;
margin-bottom: 30px;
}
.file {
width: 0.1px;
height: 0.1px;
opacity: 0;
z-index: -1;
}
.label {
cursor: pointer;
}
.label:hover strong {
color: #fccfe2;
}
#create {
position: absolute;
left: 0;
right: 0;
}
#button {
font-size: 16px;
color: inherit;
margin: 20px auto;
border: 1px solid #ffffff;
background-color: rgba(255, 255, 255, 0.1);
height: 48px;
padding: 0px 48px;
border-radius: 6px;
text-transform: uppercase;
cursor: pointer;
}
#button:hover {
background: rgba(255, 255, 255, 0.8);
color: #e6645b;
}
</style>
</head>
<body>
<h1>Universal Hex Creator</h1>
<div id="drop-v1" class="drop">
<h2>V1 Intel Hex</h2>
<svg id="icon-v1" xmlns="http://www.w3.org/2000/svg" width="50" height="43" viewBox="0 0 50 43">
<path d="M48.4 26.5c-.9 0-1.7.7-1.7 1.7v11.6h-43.3v-11.6c0-.9-.7-1.7-1.7-1.7s-1.7.7-1.7 1.7v13.2c0 .9.7 1.7 1.7 1.7h46.7c.9 0 1.7-.7 1.7-1.7v-13.2c0-1-.7-1.7-1.7-1.7zm-24.5 6.1c.3.3.8.5 1.2.5.4 0 .9-.2 1.2-.5l10-11.6c.7-.7.7-1.7 0-2.4s-1.7-.7-2.4 0l-7.1 8.3v-25.3c0-.9-.7-1.7-1.7-1.7s-1.7.7-1.7 1.7v25.3l-7.1-8.3c-.7-.7-1.7-.7-2.4 0s-.7 1.7 0 2.4l10 11.6z" />
</svg>
<input id="file-v1" type="file" class="file" />
<label id="label-v1" for="file-v1" class="label">
<strong>Select an Intel Hex file from your computer</strong><br />
<span>or drag and drop it here</span>
</label>
</div>
<div id="drop-v2" class="drop">
<h2>V2 Intel Hex</h2>
<svg id="icon-v2" xmlns="http://www.w3.org/2000/svg" width="50" height="43" viewBox="0 0 50 43">
<path d="M48.4 26.5c-.9 0-1.7.7-1.7 1.7v11.6h-43.3v-11.6c0-.9-.7-1.7-1.7-1.7s-1.7.7-1.7 1.7v13.2c0 .9.7 1.7 1.7 1.7h46.7c.9 0 1.7-.7 1.7-1.7v-13.2c0-1-.7-1.7-1.7-1.7zm-24.5 6.1c.3.3.8.5 1.2.5.4 0 .9-.2 1.2-.5l10-11.6c.7-.7.7-1.7 0-2.4s-1.7-.7-2.4 0l-7.1 8.3v-25.3c0-.9-.7-1.7-1.7-1.7s-1.7.7-1.7 1.7v25.3l-7.1-8.3c-.7-.7-1.7-.7-2.4 0s-.7 1.7 0 2.4l10 11.6z" />
</svg>
<input id="file-v2" type="file" class="file" />
<label id="label-v2" for="file-v2" class="label">
<strong>Select an Intel Hex file from your computer</strong><br />
<span> or drag and drop it here</span>
</label>
</div>
<div id="create">
<button id="button">Create Universal Hex</button>
</div>
<script type="text/javascript" src="microbit-uh.umd.min.js"></script>
<script>
let dropV1El = document.getElementById('drop-v1');
let dropV2El = document.getElementById('drop-v2');
let fileV1El = document.getElementById('file-v1');
let fileV2El = document.getElementById('file-v2');
// Load Intel Hex files
let v1HexStr = null;
let v2HexStr = null;
const setV1Hex = function (file) {
if (!file) return;
const reader = new FileReader();
reader.onloadend = function (evt) {
v1HexStr = evt.target.result;
console.log(v1HexStr);
document.getElementById('label-v1').textContent =
'File "' + file.name + '" loaded';
document.getElementById('icon-v1').style.display = 'none';
};
reader.readAsText(file);
};
const setV2Hex = function (file) {
if (!file) return;
const reader = new FileReader();
reader.onloadend = function (evt) {
v2HexStr = evt.target.result;
console.log(v2HexStr);
document.getElementById('label-v2').textContent =
'File "' + file.name + '" loaded';
document.getElementById('icon-v2').style.display = 'none';
};
reader.readAsText(file);
};
// Set up the file loading event listeners
fileV1El.addEventListener('change', function (event) {
setV1Hex(event.target.files[0]);
});
fileV2El.addEventListener('change', function (event) {
setV2Hex(event.target.files[0]);
});
dropV1El.addEventListener('drop', function (event) {
setV1Hex(event.dataTransfer.files[0]);
});
dropV2El.addEventListener('drop', function (event) {
setV2Hex(event.dataTransfer.files[0]);
});
[
'drag',
'dragstart',
'dragend',
'dragover',
'dragenter',
'dragleave',
'drop',
].forEach(function (eventName) {
dropV1El.addEventListener(eventName, function (event) {
event.preventDefault();
event.stopPropagation();
});
dropV2El.addEventListener(eventName, function (event) {
event.preventDefault();
event.stopPropagation();
});
});
// Set the the listeners to highlight the drop area
['dragover', 'dragenter'].forEach(function (eventName) {
dropV1El.addEventListener(eventName, function (event) {
dropV1El.classList.add('hover');
});
dropV2El.addEventListener(eventName, function (event) {
dropV2El.classList.add('hover');
});
});
['dragleave', 'dragend', 'drop'].forEach(function (eventName) {
dropV1El.addEventListener(eventName, function (event) {
dropV1El.classList.remove('hover');
});
dropV2El.addEventListener(eventName, function (event) {
dropV2El.classList.remove('hover');
});
});
// Function to trigger the browser to download a file
let downloadTextFile = function (filename, fileStr) {
let fileBlob = new Blob([fileStr], { type: 'text/plain' });
let tempLink = window.document.createElement('a');
tempLink.href = window.URL.createObjectURL(fileBlob);
tempLink.download = filename;
tempLink.style.display = 'none';
document.body.appendChild(tempLink);
tempLink.click();
document.body.removeChild(tempLink);
};
// Create the Universal Hex at button press
let createButton = document.getElementById('button');
createButton.addEventListener('click', function () {
// Create the hex file and download it
if (!v1HexStr || !v2HexStr) {
let errorMsg = 'Must have both hex files loaded first.';
console.error(errorMsg);
return alert(errorMsg);
}
try {
let universalHex = microbitUh.createUniversalHex([{
hex: v1HexStr,
boardId: 0x9900,
}, {
hex: v2HexStr,
boardId: 0x9903,
}]);
downloadTextFile('universal-hex.hex', universalHex);
} catch (e) {
alert(e);
}
});
</script>
</body>
</html>