Skip to content

Commit

Permalink
RP2040 uploaded is now async and added progress bar to WinForms test UI.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Breitschopp committed Jul 28, 2024
1 parent 0a4a90b commit e0f2725
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
33 changes: 30 additions & 3 deletions OpenKNX.Toolbox.Lib/Rp2040UploadHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace OpenKNX.Toolbox.Lib
using System.Threading;
using System;

namespace OpenKNX.Toolbox.Lib
{
public static class Rp2040UploadHelper
{
Expand All @@ -25,14 +28,38 @@ public static List<string> GetCompatibleDrives()
/// </summary>
/// <param name="uploadDrive">The drive to upload to.</param>
/// <param name="filePathUf2">The file path to the UF2 file to upload.</param>
/// <param name="progress">An optional progress report.</param>
/// <param name="cancellationToken">An optional cancellation token.</param>
/// <returns>True, if success, False otherwise.</returns>
public static bool UploadFirmware(string uploadDrive, string filePathUf2)
public static async Task<bool> UploadFirmware(string uploadDrive, string filePathUf2, IProgress<KeyValuePair<long, long>>? progress = null, CancellationToken? cancellationToken = null)
{
try
{
var firmwareFileName = Path.GetFileName(filePathUf2);
var drivePath = Path.Combine(uploadDrive, firmwareFileName);
File.Copy(filePathUf2, drivePath);
var sourceFileInfo = new FileInfo(filePathUf2);

var bytesRead = -1;
var totalBytesCopied = 0L;
var buffer = new byte[16384];
var sourceLength = sourceFileInfo.Length;
using (var outStream = new FileStream(drivePath, FileMode.Create, FileAccess.Write, FileShare.Read))
{
using (var inStream = new FileStream(filePathUf2, FileMode.Open, FileAccess.Read, FileShare.Read))
{
while (bytesRead != 0 && (cancellationToken == null || !cancellationToken.Value.IsCancellationRequested))
{
bytesRead = await inStream.ReadAsync(buffer, 0, buffer.Length);
if (bytesRead == 0 || (cancellationToken != null && cancellationToken.Value.IsCancellationRequested))
break;

await outStream.WriteAsync(buffer, 0, buffer.Length);
totalBytesCopied += bytesRead;

progress?.Report(new KeyValuePair<long, long>(totalBytesCopied, sourceLength));
}
}
}

return true;
}
Expand Down
14 changes: 12 additions & 2 deletions OpenKNX.Toolbox.WinForms/Main.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 22 additions & 1 deletion OpenKNX.Toolbox.WinForms/Main.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System;
using System.Diagnostics;
using Microsoft.VisualBasic.Devices;
using OpenKNX.Toolbox.Lib;
using OpenKNX.Toolbox.Lib.Data;
using OpenKNX.Toolbox.WinForms.Properties;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace OpenKNX.Toolbox.WinForms
{
Expand Down Expand Up @@ -192,10 +196,27 @@ private void inFirmwareUpload_Click(object sender, EventArgs e)
var firmware = (ReleaseContentFirmware)inFirmwareVariant.SelectedItem;
var uploadDrive = (string)inFirmwareTarget.SelectedItem;

if (Rp2040UploadHelper.UploadFirmware(uploadDrive, firmware.FilePathUf2))
var progress = new Progress<KeyValuePair<long, long>>();
progress.ProgressChanged += FirmwareUploadProgress_ProgressChanged;

var task = Task.Run(() => Rp2040UploadHelper.UploadFirmware(uploadDrive, firmware.FilePathUf2, progress));
while (!task.IsCompleted)
{
task.Wait(100);
Application.DoEvents();
}

if (task.Result)
MessageBox.Show("Die Firmware wurde erfolgreich hochgeladen.", "Firmware-Upload erfolgreich", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("Die Firmware konnte nicht hochgeladen werden.", "Firmware-Upload fehlgeschlagen", MessageBoxButtons.OK, MessageBoxIcon.Error);

RefreshFirmwareTargets();
}

private void FirmwareUploadProgress_ProgressChanged(object? sender, KeyValuePair<long, long> e)
{
outFirmwareUploadProgress.Invoke(() => outFirmwareUploadProgress.Value = (int)(e.Key / (double)e.Value * 100));
}

private void inKnxprodPath_TextChanged(object sender, EventArgs e)
Expand Down

0 comments on commit e0f2725

Please sign in to comment.