Skip to content

Commit a1aebea

Browse files
Improve FW update progress reporting (#90)
Made the FW update experience more informative and visually appealing
1 parent 9d7162e commit a1aebea

File tree

2 files changed

+62
-19
lines changed

2 files changed

+62
-19
lines changed

β€Žsrc/components/FirmwareManager/FirmwareManager.tsxβ€Ž

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ export const FirmwareManager: React.FC<IFirmwareManager> = ({
8181
>
8282
Flash custom firmware
8383
</button>
84-
<p>{updateStatus}</p>
84+
<div className="space-y-2">
85+
{updateStatus.includes("Progress") && (
86+
<div className="w-full bg-gray-200 rounded-full h-2.5 dark:bg-gray-700">
87+
<div
88+
className="bg-blue-400 h-2.5 rounded-full transition-all duration-300"
89+
style={{
90+
width: `${updateStatus.split('Progress: ')[1]?.split('%')[0] || 0}%`
91+
}}
92+
></div>
93+
</div>
94+
)}
95+
<p className="whitespace-pre-wrap">{updateStatus}</p>
96+
</div>
8597
</div>
8698
);

β€Žsrc/utils/serialUtils.tsxβ€Ž

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,56 +84,87 @@ export const useWriteCommand = () => {
8484
bytes: Uint8Array,
8585
setUpdateStatus: Dispatch<SetStateAction<string>>
8686
) => {
87+
const formatTime = (seconds: number): string => {
88+
const mins = Math.floor(seconds / 60);
89+
const secs = Math.floor(seconds % 60);
90+
return `${mins}m ${secs}s`;
91+
};
92+
8793
setFileUploadBlocker(true);
8894
await write("fclose", false);
8995
await write(`fopen ${filePath}`, false);
90-
96+
9197
await write(`fseek 0`, false);
92-
98+
9399
let blob = new Blob([bytes]);
94100
const arrayBuffer = await blob.arrayBuffer();
95-
101+
96102
const chunkSize = 100000;
97-
103+
let successfulChunks = 0;
104+
let failedChunks = 0;
105+
98106
console.log("Total length: ", arrayBuffer.byteLength);
99-
107+
100108
let startTime = Date.now();
101109
let totalTime = 0;
102-
110+
let lastProgressUpdate = Date.now();
111+
103112
for (let i = 0; i < arrayBuffer.byteLength; i += chunkSize) {
104113
const chunk = arrayBuffer.slice(i, i + chunkSize);
105-
114+
106115
await write(`fwb ${chunk.byteLength}`, false, true);
107116
await serial.queueWriteAndResponseBinary(new Uint8Array(chunk));
108-
117+
successfulChunks++;
118+
109119
// calculate elapsed time and average time per chunk
110120
let elapsed = Date.now() - startTime;
111121
totalTime += elapsed;
112122
let avgTimePerChunk = totalTime / (i / chunkSize + 1);
113-
123+
114124
// estimate remaining time in seconds
115125
let remainingChunks = (arrayBuffer.byteLength - i) / chunkSize;
116126
let estRemainingTime = (remainingChunks * avgTimePerChunk) / 1000;
117-
127+
118128
console.log(
119129
"Chunk done",
120130
i,
121131
arrayBuffer.byteLength,
122132
((i / arrayBuffer.byteLength) * 100).toFixed(2) + "%",
123133
"Estimated time remaining: " + estRemainingTime.toFixed(0) + " seconds"
124134
);
125-
setUpdateStatus(
126-
`${((i / arrayBuffer.byteLength) * 100).toFixed(
127-
2
128-
)}% Estimated time remaining: ${estRemainingTime.toFixed(0)} seconds`
129-
);
130-
135+
136+
if (Date.now() - lastProgressUpdate > 500) {
137+
const percentComplete = (i / arrayBuffer.byteLength) * 100;
138+
const transferSpeed = (i / totalTime) * 1000 / 1024; // KB/s
139+
140+
setUpdateStatus(
141+
`πŸ“Š Upload Progress\n` +
142+
`━━━━━━━━━━━━━━━━━━━━━\n` +
143+
`⚑ Progress: ${percentComplete.toFixed(1)}%\n` +
144+
`πŸ“¦ Transferred: ${(i / 1024).toFixed(1)}KB / ${(arrayBuffer.byteLength / 1024).toFixed(1)}KB\n` +
145+
`πŸš€ Speed: ${transferSpeed.toFixed(1)} KB/s\n` +
146+
`⏱️ Time Elapsed: ${formatTime(totalTime/1000)}\n` +
147+
`βŒ› Time Remaining: ${formatTime(estRemainingTime)}\n` +
148+
`βœ… Chunks: ${successfulChunks}\n` +
149+
`❌ Retries: ${failedChunks}`
150+
);
151+
lastProgressUpdate = Date.now();
152+
}
153+
131154
// reset start time for next iteration
132155
startTime = Date.now();
133156
}
134157
console.log("FILE DONE");
135-
setUpdateStatus(`File upload complete!`);
136-
158+
159+
setUpdateStatus(
160+
`βœ… Upload Complete!\n` +
161+
`━━━━━━━━━━━━━━━━━━━━━\n` +
162+
`πŸ“¦ Total Size: ${(arrayBuffer.byteLength / 1024).toFixed(1)}KB\n` +
163+
`⏱️ Total Time: ${formatTime(totalTime/1000)}\n` +
164+
`βœ… Successful Chunks: ${successfulChunks}\n` +
165+
`❌ Failed Attempts: ${failedChunks}`
166+
);
167+
137168
await write("fclose", false);
138169
setFileUploadBlocker(false);
139170
};

0 commit comments

Comments
Β (0)