Skip to content

Commit bb6d389

Browse files
authored
Merge pull request #50 from glopesdev/interface-gen
Add cancellation support to async device API
2 parents fa345ee + 982c23f commit bb6d389

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

interface/AsyncDevice.tt

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var publicRegisters = deviceMetadata.Registers.Where(register => register.Value.
1414
var deviceName = deviceMetadata.Device;
1515
#>
1616
using Bonsai.Harp;
17+
using System.Threading;
1718
using System.Threading.Tasks;
1819

1920
namespace <#= Namespace #>
@@ -69,26 +70,32 @@ foreach (var registerMetadata in publicRegisters)
6970
/// <summary>
7071
/// Asynchronously reads the contents of the <#= registerMetadata.Key #> register.
7172
/// </summary>
73+
/// <param name="cancellationToken">
74+
/// A <see cref="CancellationToken"/> which can be used to cancel the operation.
75+
/// </param>
7276
/// <returns>
7377
/// A task that represents the asynchronous read operation. The <see cref="Task{TResult}.Result"/>
7478
/// property contains the register payload.
7579
/// </returns>
76-
public async Task<<#= interfaceType #>> Read<#= registerMetadata.Key #>Async()
80+
public async Task<<#= interfaceType #>> Read<#= registerMetadata.Key #>Async(CancellationToken cancellationToken = default)
7781
{
78-
var reply = await CommandAsync(HarpCommand.Read<#= parsePayloadSuffix #>(<#= registerMetadata.Key #>.Address));
82+
var reply = await CommandAsync(HarpCommand.Read<#= parsePayloadSuffix #>(<#= registerMetadata.Key #>.Address), cancellationToken);
7983
return <#= registerMetadata.Key #>.GetPayload(reply);
8084
}
8185

8286
/// <summary>
8387
/// Asynchronously reads the timestamped contents of the <#= registerMetadata.Key #> register.
8488
/// </summary>
89+
/// <param name="cancellationToken">
90+
/// A <see cref="CancellationToken"/> which can be used to cancel the operation.
91+
/// </param>
8592
/// <returns>
8693
/// A task that represents the asynchronous read operation. The <see cref="Task{TResult}.Result"/>
8794
/// property contains the timestamped register payload.
8895
/// </returns>
89-
public async Task<Timestamped<<#= interfaceType #>>> ReadTimestamped<#= registerMetadata.Key #>Async()
96+
public async Task<Timestamped<<#= interfaceType #>>> ReadTimestamped<#= registerMetadata.Key #>Async(CancellationToken cancellationToken = default)
9097
{
91-
var reply = await CommandAsync(HarpCommand.Read<#= parsePayloadSuffix #>(<#= registerMetadata.Key #>.Address));
98+
var reply = await CommandAsync(HarpCommand.Read<#= parsePayloadSuffix #>(<#= registerMetadata.Key #>.Address), cancellationToken);
9299
return <#= registerMetadata.Key #>.GetTimestampedPayload(reply);
93100
}
94101
<#
@@ -100,11 +107,14 @@ foreach (var registerMetadata in publicRegisters)
100107
/// Asynchronously writes a value to the <#= registerMetadata.Key #> register.
101108
/// </summary>
102109
/// <param name="value">The value to be stored in the register.</param>
110+
/// <param name="cancellationToken">
111+
/// A <see cref="CancellationToken"/> which can be used to cancel the operation.
112+
/// </param>
103113
/// <returns>The task object representing the asynchronous write operation.</returns>
104-
public async Task Write<#= registerMetadata.Key #>Async(<#= interfaceType #> value)
114+
public async Task Write<#= registerMetadata.Key #>Async(<#= interfaceType #> value, CancellationToken cancellationToken = default)
105115
{
106116
var request = <#= registerMetadata.Key #>.FromPayload(MessageType.Write, value);
107-
await CommandAsync(request);
117+
await CommandAsync(request, cancellationToken);
108118
}
109119
<#
110120
}

interface/Harp.Generators.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<VersionPrefix>0.1.0</VersionPrefix>
5-
<VersionSuffix>build060202</VersionSuffix>
5+
<VersionSuffix>build062701</VersionSuffix>
66
<PackageId>Harp.Generators</PackageId>
77
<Title>Harp Generators</Title>
88
<Authors>harp-tech</Authors>
@@ -20,7 +20,7 @@
2020
</PropertyGroup>
2121

2222
<ItemGroup>
23-
<PackageReference Include="Bonsai.Harp" Version="3.5.0-build032701" />
23+
<PackageReference Include="Bonsai.Harp" Version="3.5.0-build062701" />
2424
<PackageReference Include="YamlDotNet" Version="13.0.2" />
2525
</ItemGroup>
2626

template/Bonsai.DeviceTemplate/Generators/Generators.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<FirmwarePath>..\Firmware\$projectname$</FirmwarePath>
1616
</PropertyGroup>
1717
<ItemGroup>
18-
<PackageReference Include="Harp.Generators" Version="0.1.0" GeneratePathProperty="true" />
18+
<PackageReference Include="Harp.Generators" Version="0.1.0-build062701" GeneratePathProperty="true" />
1919
</ItemGroup>
2020
<Target Name="TextTransform" BeforeTargets="AfterBuild">
2121
<PropertyGroup>

template/Harp.Templates.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<PackageType>Template</PackageType>
55
<VersionPrefix>0.1.0</VersionPrefix>
6-
<VersionSuffix>preview7</VersionSuffix>
6+
<VersionSuffix>preview10</VersionSuffix>
77
<PackageId>Harp.Templates</PackageId>
88
<Title>Harp Templates</Title>
99
<Authors>harp-tech</Authors>

0 commit comments

Comments
 (0)