Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Harp.Toolkit/ListCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.CommandLine;
using System.IO.Ports;

namespace Harp.Toolkit;

public class ListCommand : Command
{
public ListCommand()
: base("list", "List all available system serial ports.")
{
SetAction(parseResult =>
{
var portNames = SerialPort.GetPortNames();
Console.WriteLine($"PortNames: [{string.Join(", ", portNames)}]");
});
}
}
13 changes: 13 additions & 0 deletions Harp.Toolkit/PortNameOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.CommandLine;

namespace Harp.Toolkit;

public class PortNameOption : Option<string>
{
public PortNameOption()
: base("--port")
{
Description = "Specifies the name of the serial port used to communicate with the device.";
Required = true;
}
}
12 changes: 12 additions & 0 deletions Harp.Toolkit/PortTimeoutOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.CommandLine;

namespace Harp.Toolkit;

public class PortTimeoutOption : Option<int?>
{
public PortTimeoutOption()
: base("--timeout")
{
Description = "Specifies an optional timeout, in milliseconds, to receive a response from the device.";
}
}
58 changes: 5 additions & 53 deletions Harp.Toolkit/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,13 @@ internal class Program
{
static async Task Main(string[] args)
{
Option<string> portNameOption = new("--port")
{
Description = "Specifies the name of the serial port used to communicate with the device.",
Required = true
};

Option<int?> portTimeoutOption = new("--timeout")
{
Description = "Specifies an optional timeout, in milliseconds, to receive a response from the device."
};

Option<FileInfo> firmwarePathOption = new("--path")
{
Description = "Specifies the path of the firmware file to write to the device.",
Required = true
};

Option<bool> forceUpdateOption = new("--force")
{
Description = "Indicates whether to force a firmware update on the device regardless of compatibility."
};

var listCommand = new Command("list", description: "Lists all available system serial ports.");
listCommand.SetAction(parseResult =>
{
var portNames = SerialPort.GetPortNames();
Console.WriteLine($"PortNames: [{string.Join(", ", portNames)}]");
});

var updateCommand = new Command("update", description: "Update the device firmware from a local HEX file.");
updateCommand.Options.Add(portNameOption);
updateCommand.Options.Add(firmwarePathOption);
updateCommand.Options.Add(forceUpdateOption);
updateCommand.SetAction(async parseResult =>
{
var firmwarePath = parseResult.GetRequiredValue(firmwarePathOption);
var portName = parseResult.GetRequiredValue(portNameOption);
var forceUpdate = parseResult.GetValue(forceUpdateOption);

var firmware = DeviceFirmware.FromFile(firmwarePath.FullName);
Console.WriteLine($"{firmware.Metadata}");
ProgressBar.Write(0);
try
{
var progress = new Progress<int>(ProgressBar.Update);
await Bootloader.UpdateFirmwareAsync(portName, firmware, forceUpdate, progress);
}
finally { Console.WriteLine(); }
});

var rootCommand = new RootCommand("Tool for inspecting, updating and interfacing with Harp devices.");
RootCommand rootCommand = new("Tool for inspecting, updating and interfacing with Harp devices.");
PortNameOption portNameOption = new();
PortTimeoutOption portTimeoutOption = new();
rootCommand.Options.Add(portNameOption);
rootCommand.Options.Add(portTimeoutOption);
rootCommand.Subcommands.Add(listCommand);
rootCommand.Subcommands.Add(updateCommand);
rootCommand.Subcommands.Add(new ListCommand());
rootCommand.Subcommands.Add(new UpdateFirmwareCommand());
rootCommand.SetAction(async parseResult =>
{
var portName = parseResult.GetRequiredValue(portNameOption);
Expand Down
43 changes: 43 additions & 0 deletions Harp.Toolkit/UpdateFirmwareCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.CommandLine;
using Bonsai.Harp;

namespace Harp.Toolkit;

public class UpdateFirmwareCommand : Command
{
public UpdateFirmwareCommand()
: base("update", "Update the device firmware from a local HEX file.")
{
PortNameOption portNameOption = new();
Option<FileInfo> firmwarePathOption = new("--path")
{
Description = "Specifies the path of the firmware file to write to the device.",
Required = true
};

Option<bool> forceUpdateOption = new("--force")
{
Description = "Indicates whether to force a firmware update on the device regardless of compatibility."
};

Options.Add(portNameOption);
Options.Add(firmwarePathOption);
Options.Add(forceUpdateOption);
SetAction(async parseResult =>
{
var firmwarePath = parseResult.GetRequiredValue(firmwarePathOption);
var portName = parseResult.GetRequiredValue(portNameOption);
var forceUpdate = parseResult.GetValue(forceUpdateOption);

var firmware = DeviceFirmware.FromFile(firmwarePath.FullName);
Console.WriteLine($"{firmware.Metadata}");
ProgressBar.Write(0);
try
{
var progress = new Progress<int>(ProgressBar.Update);
await Bootloader.UpdateFirmwareAsync(portName, firmware, forceUpdate, progress);
}
finally { Console.WriteLine(); }
});
}
}