|
1 | 1 | import { saveAs } from "file-saver"; |
2 | | -import { WebDFUType, WebDFU } from "dfu"; |
| 2 | +import { WebDFUType, WebDFU } from "dfu/index"; |
3 | 3 |
|
4 | 4 | import { clearLog, logError, logInfo, logProgress, logWarning, setLogContext } from "./log"; |
5 | 5 |
|
@@ -317,15 +317,31 @@ uploadButton.addEventListener("click", async function (event) { |
317 | 317 | maxSize = parseInt(dfuseUploadSizeField.value); |
318 | 318 | } |
319 | 319 |
|
320 | | - try { |
321 | | - const blob = await webdfu.read(transferSize, maxSize); |
| 320 | + const process = webdfu.read(transferSize, maxSize); |
322 | 321 |
|
323 | | - saveAs(blob, "firmware.bin"); |
324 | | - } catch (error) { |
325 | | - logError(error); |
| 322 | + // after start |
| 323 | + if (webdfu?.type === WebDFUType.SDFUse) { |
| 324 | + logInfo(`Reading up to 0x${maxSize.toString(16)} bytes starting at 0x${webdfu.dfuseStartAddress.toString(16)}`); |
326 | 325 | } |
327 | 326 |
|
328 | | - setLogContext(null); |
| 327 | + logInfo("Copying data from DFU device to browser"); |
| 328 | + |
| 329 | + process.events.on("process", (done, total) => { |
| 330 | + logProgress(done, total); |
| 331 | + }); |
| 332 | + |
| 333 | + process.events.on("error", (error) => { |
| 334 | + logError(error); |
| 335 | + setLogContext(null); |
| 336 | + }); |
| 337 | + |
| 338 | + process.events.on("end", (blob) => { |
| 339 | + console.log("end?"); |
| 340 | + logInfo(`Read ${blob.size} bytes`); |
| 341 | + setLogContext(null); |
| 342 | + |
| 343 | + saveAs(blob, "firmware.bin"); |
| 344 | + }); |
329 | 345 | } |
330 | 346 |
|
331 | 347 | return false; |
@@ -363,27 +379,65 @@ async function download(): Promise<void> { |
363 | 379 | logWarning("Failed to clear status"); |
364 | 380 | } |
365 | 381 |
|
366 | | - try { |
367 | | - await webdfu.write(transferSize, firmwareFile, manifestationTolerant); |
| 382 | + let process = webdfu.write(transferSize, firmwareFile, manifestationTolerant); |
| 383 | + |
| 384 | + // Erase |
| 385 | + process.events.on("erase/start", () => { |
| 386 | + console.log("erase/start!"); |
| 387 | + logInfo("Erasing DFU device memory"); |
| 388 | + }); |
| 389 | + |
| 390 | + process.events.on("erase/process", (bytesSent, expectedSize) => { |
| 391 | + logProgress(bytesSent, expectedSize); |
| 392 | + }); |
368 | 393 |
|
| 394 | + process.events.on("erase/end", () => { |
| 395 | + logInfo("Success erased"); |
| 396 | + }); |
| 397 | + |
| 398 | + // Write firmware |
| 399 | + process.events.on("write/start", () => { |
| 400 | + logInfo("Copying data from browser to DFU device"); |
| 401 | + }); |
| 402 | + |
| 403 | + process.events.on("write/process", (bytesSent, expectedSize) => { |
| 404 | + logProgress(bytesSent, expectedSize); |
| 405 | + }); |
| 406 | + |
| 407 | + process.events.on("write/end", (bytes_sent: number) => { |
| 408 | + logInfo(`Wrote ${bytes_sent} bytes`); |
| 409 | + logInfo("Manifesting new firmware"); |
| 410 | + |
| 411 | + webdfu |
| 412 | + ?.getStatus() |
| 413 | + .then((status) => { |
| 414 | + logInfo(`Final DFU status: state=${status.state}, status=${status.status}`); |
| 415 | + }) |
| 416 | + .catch(() => {}); |
| 417 | + }); |
| 418 | + |
| 419 | + process.events.on("error", (error) => { |
| 420 | + logError(error); |
| 421 | + setLogContext(null); |
| 422 | + }); |
| 423 | + |
| 424 | + process.events.on("end", () => { |
369 | 425 | logInfo("Done!"); |
370 | 426 | setLogContext(null); |
371 | 427 |
|
372 | 428 | if (!manifestationTolerant) { |
373 | | - try { |
374 | | - await webdfu.waitDisconnected(5000); |
375 | | - |
376 | | - onDisconnect(); |
377 | | - webdfu = null; |
378 | | - } catch (error) { |
379 | | - // It didn't reset and disconnect for some reason... |
380 | | - console.log("Device unexpectedly tolerated manifestation."); |
381 | | - } |
| 429 | + webdfu |
| 430 | + ?.waitDisconnected(5000) |
| 431 | + .then(() => { |
| 432 | + onDisconnect(); |
| 433 | + webdfu = null; |
| 434 | + }) |
| 435 | + .catch(() => { |
| 436 | + // It didn't reset and disconnect for some reason... |
| 437 | + console.error("Device unexpectedly tolerated manifestation."); |
| 438 | + }); |
382 | 439 | } |
383 | | - } catch (error) { |
384 | | - logError(error); |
385 | | - setLogContext(null); |
386 | | - } |
| 440 | + }); |
387 | 441 | } |
388 | 442 | } |
389 | 443 |
|
|
0 commit comments