Skip to content
Open
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
4 changes: 2 additions & 2 deletions Harp.Toolkit/Harp.Toolkit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bonsai.Harp" Version="3.5.2" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="Bonsai.Harp" Version="3.6.1" />
<PackageReference Include="System.CommandLine" Version="2.0.1" />
</ItemGroup>

</Project>
73 changes: 41 additions & 32 deletions Harp.Toolkit/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,45 @@ internal class Program
{
static async Task Main(string[] args)
{
var portName = new Option<string>(
name: "--port",
description: "Specifies the name of the serial port used to communicate with the device."
) { IsRequired = true };
portName.ArgumentHelpName = nameof(portName);
Option<string> portNameOption = new("--port")
{
Description = "Specifies the name of the serial port used to communicate with the device.",
Required = true
};

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

var firmwarePath = new Option<FileInfo>(
name: "--path",
description: "Specifies the path of the firmware file to write to the device."
) { IsRequired = true };
firmwarePath.ArgumentHelpName = nameof(firmwarePath);
Option<FileInfo> firmwarePathOption = new("--path")
{
Description = "Specifies the path of the firmware file to write to the device.",
Required = true
};

var forceUpdate = new Option<bool>(
name: "--force",
description: "Indicates whether to force a firmware update on the device regardless of compatibility."
);
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: "List all available system serial ports.");
listCommand.SetHandler(() =>
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.AddOption(portName);
updateCommand.AddOption(firmwarePath);
updateCommand.AddOption(forceUpdate);
updateCommand.SetHandler(async (portName, firmwarePath, forceUpdate) =>
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);
Expand All @@ -52,15 +56,18 @@ static async Task Main(string[] args)
await Bootloader.UpdateFirmwareAsync(portName, firmware, forceUpdate, progress);
}
finally { Console.WriteLine(); }
}, portName, firmwarePath, forceUpdate);
});

var rootCommand = new RootCommand("Tool for inspecting, updating and interfacing with Harp devices.");
rootCommand.AddOption(portName);
rootCommand.AddOption(portTimeout);
rootCommand.AddCommand(listCommand);
rootCommand.AddCommand(updateCommand);
rootCommand.SetHandler(async (portName, portTimeout) =>
rootCommand.Options.Add(portNameOption);
rootCommand.Options.Add(portTimeoutOption);
rootCommand.Subcommands.Add(listCommand);
rootCommand.Subcommands.Add(updateCommand);
rootCommand.SetAction(async parseResult =>
{
var portName = parseResult.GetRequiredValue(portNameOption);
var portTimeout = parseResult.GetValue(portTimeoutOption);

using var device = new AsyncDevice(portName);
var whoAmI = await device.ReadWhoAmIAsync().WithTimeout(portTimeout);
var hardwareVersion = await device.ReadHardwareVersionAsync();
Expand All @@ -74,7 +81,9 @@ static async Task Main(string[] args)
Console.WriteLine($"Fw: {firmwareVersion.Major}.{firmwareVersion.Minor}");
Console.WriteLine($"Timestamp (s): {timestamp}");
Console.WriteLine();
}, portName, portTimeout);
await rootCommand.InvokeAsync(args);
});

var parseResult = rootCommand.Parse(args);
await parseResult.InvokeAsync();
}
}
6 changes: 6 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"sdk": {
"version": "8.0.100",
"rollForward": "latestMinor"
}
}