Skip to content

Commit a5abf5d

Browse files
refactor loader and write flash args (#101)
1 parent e107099 commit a5abf5d

File tree

3 files changed

+156
-136
lines changed

3 files changed

+156
-136
lines changed

index.js

+96-91
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
const baudrates = document.getElementById('baudrates');
2-
const connectButton = document.getElementById('connectButton');
3-
const disconnectButton = document.getElementById('disconnectButton');
4-
const resetButton = document.getElementById('resetButton');
5-
const consoleStartButton = document.getElementById('consoleStartButton');
6-
const consoleStopButton = document.getElementById('consoleStopButton');
7-
const eraseButton = document.getElementById('eraseButton');
8-
const programButton = document.getElementById('programButton');
9-
const filesDiv = document.getElementById('files');
10-
const terminal = document.getElementById('terminal');
11-
const programDiv = document.getElementById('program');
12-
const consoleDiv = document.getElementById('console');
13-
const lblBaudrate = document.getElementById('lblBaudrate');
14-
const lblConnTo = document.getElementById('lblConnTo');
15-
const table = document.getElementById('fileTable');
16-
const alertDiv = document.getElementById('alertDiv');
1+
const baudrates = document.getElementById("baudrates");
2+
const connectButton = document.getElementById("connectButton");
3+
const disconnectButton = document.getElementById("disconnectButton");
4+
const resetButton = document.getElementById("resetButton");
5+
const consoleStartButton = document.getElementById("consoleStartButton");
6+
const consoleStopButton = document.getElementById("consoleStopButton");
7+
const eraseButton = document.getElementById("eraseButton");
8+
const programButton = document.getElementById("programButton");
9+
const filesDiv = document.getElementById("files");
10+
const terminal = document.getElementById("terminal");
11+
const programDiv = document.getElementById("program");
12+
const consoleDiv = document.getElementById("console");
13+
const lblBaudrate = document.getElementById("lblBaudrate");
14+
const lblConnTo = document.getElementById("lblConnTo");
15+
const table = document.getElementById("fileTable");
16+
const alertDiv = document.getElementById("alertDiv");
1717

1818
// import { Transport } from './cp210x-webusb.js'
1919
import * as esptooljs from "./bundle.js";
@@ -30,11 +30,10 @@ let esploader;
3030
let file1 = null;
3131
let connected = false;
3232

33-
disconnectButton.style.display = 'none';
34-
eraseButton.style.display = 'none';
35-
consoleStopButton.style.display = 'none';
36-
filesDiv.style.display = 'none';
37-
33+
disconnectButton.style.display = "none";
34+
eraseButton.style.display = "none";
35+
consoleStopButton.style.display = "none";
36+
filesDiv.style.display = "none";
3837

3938
function handleFileSelect(evt) {
4039
var file = evt.target.files[0];
@@ -61,9 +60,9 @@ let espLoaderTerminal = {
6160
term.writeln(data);
6261
},
6362
write(data) {
64-
term.write(data)
65-
}
66-
}
63+
term.write(data);
64+
},
65+
};
6766

6867
connectButton.onclick = async () => {
6968
if (device === null) {
@@ -72,7 +71,12 @@ connectButton.onclick = async () => {
7271
}
7372

7473
try {
75-
esploader = new ESPLoader(transport, baudrates.value, espLoaderTerminal);
74+
const loaderOptions = {
75+
transport: transport,
76+
baudrate: baudrates.value,
77+
terminal: espLoaderTerminal
78+
};
79+
esploader = new ESPLoader(loaderOptions);
7680
connected = true;
7781

7882
chip = await esploader.main_fn();
@@ -84,16 +88,16 @@ connectButton.onclick = async () => {
8488
term.writeln(`Error: ${e.message}`);
8589
}
8690

87-
console.log('Settings done for :' + chip);
88-
lblBaudrate.style.display = 'none';
89-
lblConnTo.innerHTML = 'Connected to device: ' + chip;
90-
lblConnTo.style.display = 'block';
91-
baudrates.style.display = 'none';
92-
connectButton.style.display = 'none';
93-
disconnectButton.style.display = 'initial';
94-
eraseButton.style.display = 'initial';
95-
filesDiv.style.display = 'initial';
96-
consoleDiv.style.display = 'none';
91+
console.log("Settings done for :" + chip);
92+
lblBaudrate.style.display = "none";
93+
lblConnTo.innerHTML = "Connected to device: " + chip;
94+
lblConnTo.style.display = "block";
95+
baudrates.style.display = "none";
96+
connectButton.style.display = "none";
97+
disconnectButton.style.display = "initial";
98+
eraseButton.style.display = "initial";
99+
filesDiv.style.display = "initial";
100+
consoleDiv.style.display = "none";
97101
};
98102

99103
resetButton.onclick = async () => {
@@ -125,37 +129,37 @@ addFile.onclick = () => {
125129

126130
//Column 1 - Offset
127131
var cell1 = row.insertCell(0);
128-
var element1 = document.createElement('input');
129-
element1.type = 'text';
130-
element1.id = 'offset' + rowCount;
131-
element1.value = '0x1000';
132+
var element1 = document.createElement("input");
133+
element1.type = "text";
134+
element1.id = "offset" + rowCount;
135+
element1.value = "0x1000";
132136
cell1.appendChild(element1);
133137

134138
// Column 2 - File selector
135139
var cell2 = row.insertCell(1);
136-
var element2 = document.createElement('input');
137-
element2.type = 'file';
138-
element2.id = 'selectFile' + rowCount;
139-
element2.name = 'selected_File' + rowCount;
140-
element2.addEventListener('change', handleFileSelect, false);
140+
var element2 = document.createElement("input");
141+
element2.type = "file";
142+
element2.id = "selectFile" + rowCount;
143+
element2.name = "selected_File" + rowCount;
144+
element2.addEventListener("change", handleFileSelect, false);
141145
cell2.appendChild(element2);
142146

143147
// Column 3 - Progress
144148
var cell3 = row.insertCell(2);
145-
cell3.classList.add('progress-cell');
146-
cell3.style.display = 'none';
149+
cell3.classList.add("progress-cell");
150+
cell3.style.display = "none";
147151
cell3.innerHTML = `<progress value="0" max="100"></progress>`;
148152

149153
// Column 4 - Remove File
150154
var cell4 = row.insertCell(3);
151-
cell4.classList.add('action-cell');
155+
cell4.classList.add("action-cell");
152156
if (rowCount > 1) {
153-
var element4 = document.createElement('input');
154-
element4.type = 'button';
155-
var btnName = 'button' + rowCount;
157+
var element4 = document.createElement("input");
158+
element4.type = "button";
159+
var btnName = "button" + rowCount;
156160
element4.name = btnName;
157-
element4.setAttribute('class', 'btn');
158-
element4.setAttribute('value', 'Remove'); // or element1.value = "button";
161+
element4.setAttribute("class", "btn");
162+
element4.setAttribute("value", "Remove"); // or element1.value = "button";
159163
element4.onclick = function () {
160164
removeRow(row);
161165
};
@@ -180,14 +184,14 @@ disconnectButton.onclick = async () => {
180184

181185
term.clear();
182186
connected = false;
183-
baudrates.style.display = 'initial';
184-
connectButton.style.display = 'initial';
185-
disconnectButton.style.display = 'none';
186-
eraseButton.style.display = 'none';
187-
lblConnTo.style.display = 'none';
188-
filesDiv.style.display = 'none';
189-
alertDiv.style.display = 'none';
190-
consoleDiv.style.display = 'initial';
187+
baudrates.style.display = "initial";
188+
connectButton.style.display = "initial";
189+
disconnectButton.style.display = "none";
190+
eraseButton.style.display = "none";
191+
lblConnTo.style.display = "none";
192+
filesDiv.style.display = "none";
193+
alertDiv.style.display = "none";
194+
consoleDiv.style.display = "initial";
191195
cleanUp();
192196
};
193197

@@ -197,33 +201,33 @@ consoleStartButton.onclick = async () => {
197201
device = await navigator.serial.requestPort({});
198202
transport = new Transport(device);
199203
}
200-
lblConsoleFor.style.display = 'block';
201-
consoleStartButton.style.display = 'none';
202-
consoleStopButton.style.display = 'initial';
203-
programDiv.style.display = 'none';
204+
lblConsoleFor.style.display = "block";
205+
consoleStartButton.style.display = "none";
206+
consoleStopButton.style.display = "initial";
207+
programDiv.style.display = "none";
204208

205209
await transport.connect();
206210
isConsoleClosed = false;
207211

208212
while (true && !isConsoleClosed) {
209213
let val = await transport.rawRead();
210-
if (typeof val !== 'undefined') {
214+
if (typeof val !== "undefined") {
211215
term.write(val);
212216
} else {
213217
break;
214218
}
215219
}
216-
console.log('quitting console');
220+
console.log("quitting console");
217221
};
218222

219223
consoleStopButton.onclick = async () => {
220224
isConsoleClosed = true;
221225
await transport.disconnect();
222226
await transport.waitForUnlock(1500);
223227
term.clear();
224-
consoleStartButton.style.display = 'initial';
225-
consoleStopButton.style.display = 'none';
226-
programDiv.style.display = 'initial';
228+
consoleStartButton.style.display = "initial";
229+
consoleStopButton.style.display = "none";
230+
programDiv.style.display = "initial";
227231
};
228232

229233
function validate_program_inputs() {
@@ -242,30 +246,30 @@ function validate_program_inputs() {
242246
offset = parseInt(offSetObj.value);
243247

244248
// Non-numeric or blank offset
245-
if (Number.isNaN(offset)) return 'Offset field in row ' + index + ' is not a valid address!';
249+
if (Number.isNaN(offset)) return "Offset field in row " + index + " is not a valid address!";
246250
// Repeated offset used
247-
else if (offsetArr.includes(offset)) return 'Offset field in row ' + index + ' is already in use!';
251+
else if (offsetArr.includes(offset)) return "Offset field in row " + index + " is already in use!";
248252
else offsetArr.push(offset);
249253

250254
var fileObj = row.cells[1].childNodes[0];
251255
fileData = fileObj.data;
252-
if (fileData == null) return 'No file selected for row ' + index + '!';
256+
if (fileData == null) return "No file selected for row " + index + "!";
253257
}
254-
return 'success';
258+
return "success";
255259
}
256260

257261
programButton.onclick = async () => {
258-
const alertMsg = document.getElementById('alertmsg');
262+
const alertMsg = document.getElementById("alertmsg");
259263
const err = validate_program_inputs();
260264

261-
if (err != 'success') {
262-
alertMsg.innerHTML = '<strong>' + err + '</strong>';
263-
alertDiv.style.display = 'block';
265+
if (err != "success") {
266+
alertMsg.innerHTML = "<strong>" + err + "</strong>";
267+
alertDiv.style.display = "block";
264268
return;
265269
}
266270

267271
// Hide error message
268-
alertDiv.style.display = 'none';
272+
alertDiv.style.display = "none";
269273

270274
const fileArray = [];
271275
const progressBars = [];
@@ -282,33 +286,34 @@ programButton.onclick = async () => {
282286
progressBar.value = 0;
283287
progressBars.push(progressBar);
284288

285-
row.cells[2].style.display = 'initial';
286-
row.cells[3].style.display = 'none';
289+
row.cells[2].style.display = "initial";
290+
row.cells[3].style.display = "none";
287291

288292
fileArray.push({ data: fileObj.data, address: offset });
289293
}
290294

291295
try {
292-
await esploader.write_flash(
296+
const flashOptions = {
293297
fileArray,
294-
'keep',
295-
undefined,
296-
undefined,
297-
false,
298-
true,
299-
(fileIndex, written, total) => {
298+
flashSize: "keep",
299+
flashMode: undefined,
300+
flashFreq: undefined,
301+
eraseAll: false,
302+
compress: true,
303+
reportProgress: (fileIndex, written, total) => {
300304
progressBars[fileIndex].value = (written / total) * 100;
301305
},
302-
(image) => CryptoJS.MD5(CryptoJS.enc.Latin1.parse(image)),
303-
);
306+
calculateMD5Hash: (image) => CryptoJS.MD5(CryptoJS.enc.Latin1.parse(image)),
307+
};
308+
await esploader.write_flash(flashOptions);
304309
} catch (e) {
305310
console.error(e);
306311
term.writeln(`Error: ${e.message}`);
307312
} finally {
308313
// Hide progress bars and show erase buttons
309314
for (let index = 1; index < table.rows.length; index++) {
310-
table.rows[index].cells[2].style.display = 'none';
311-
table.rows[index].cells[3].style.display = 'initial';
315+
table.rows[index].cells[2].style.display = "none";
316+
table.rows[index].cells[3].style.display = "initial";
312317
}
313318
}
314319
};

0 commit comments

Comments
 (0)