diff --git a/Harp.Toolkit/ListCommand.cs b/Harp.Toolkit/ListCommand.cs new file mode 100644 index 0000000..c8b60fa --- /dev/null +++ b/Harp.Toolkit/ListCommand.cs @@ -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)}]"); + }); + } +} diff --git a/Harp.Toolkit/PortNameOption.cs b/Harp.Toolkit/PortNameOption.cs new file mode 100644 index 0000000..b9168df --- /dev/null +++ b/Harp.Toolkit/PortNameOption.cs @@ -0,0 +1,13 @@ +using System.CommandLine; + +namespace Harp.Toolkit; + +public class PortNameOption : Option +{ + public PortNameOption() + : base("--port") + { + Description = "Specifies the name of the serial port used to communicate with the device."; + Required = true; + } +} diff --git a/Harp.Toolkit/PortTimeoutOption.cs b/Harp.Toolkit/PortTimeoutOption.cs new file mode 100644 index 0000000..c0d5ee3 --- /dev/null +++ b/Harp.Toolkit/PortTimeoutOption.cs @@ -0,0 +1,12 @@ +using System.CommandLine; + +namespace Harp.Toolkit; + +public class PortTimeoutOption : Option +{ + public PortTimeoutOption() + : base("--timeout") + { + Description = "Specifies an optional timeout, in milliseconds, to receive a response from the device."; + } +} diff --git a/Harp.Toolkit/Program.cs b/Harp.Toolkit/Program.cs index 502a73a..dbc7efe 100644 --- a/Harp.Toolkit/Program.cs +++ b/Harp.Toolkit/Program.cs @@ -8,61 +8,13 @@ internal class Program { static async Task Main(string[] args) { - Option portNameOption = new("--port") - { - Description = "Specifies the name of the serial port used to communicate with the device.", - Required = true - }; - - Option portTimeoutOption = new("--timeout") - { - Description = "Specifies an optional timeout, in milliseconds, to receive a response from the device." - }; - - Option firmwarePathOption = new("--path") - { - Description = "Specifies the path of the firmware file to write to the device.", - Required = true - }; - - Option 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(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); diff --git a/Harp.Toolkit/UpdateFirmwareCommand.cs b/Harp.Toolkit/UpdateFirmwareCommand.cs new file mode 100644 index 0000000..c346735 --- /dev/null +++ b/Harp.Toolkit/UpdateFirmwareCommand.cs @@ -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 firmwarePathOption = new("--path") + { + Description = "Specifies the path of the firmware file to write to the device.", + Required = true + }; + + Option 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(ProgressBar.Update); + await Bootloader.UpdateFirmwareAsync(portName, firmware, forceUpdate, progress); + } + finally { Console.WriteLine(); } + }); + } +}