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
53 changes: 50 additions & 3 deletions src/Device.tt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
var publicRegisters = DeviceMetadata.Registers.Where(register => register.Value.Visibility == RegisterVisibility.Public).ToList();
var deviceRegisters = DeviceMetadata.Registers;
var deviceName = DeviceMetadata.Device;
var isApplicationDevice = DeviceMetadata.IsApplicationDevice;
#>
<#
if (isApplicationDevice)
{
#>
using Bonsai;
using Bonsai.Harp;
Expand All @@ -21,9 +26,25 @@ using System.ComponentModel;
using System.Linq;
using System.Reactive.Linq;
using System.Xml.Serialization;
<#
}
else
{
#>
using Bonsai.Harp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
<#
} // isApplicationDevice
#>

namespace <#= Namespace #>
{
<#
if (isApplicationDevice)
{
#>
/// <summary>
/// Generates events and processes commands for the <#= deviceName #> device connected
/// at the specified serial port.
Expand Down Expand Up @@ -53,6 +74,19 @@ namespace <#= Namespace #>
(Bonsai.Harp.Device.RegisterMap.ToDictionary(entry => entry.Key, entry => entry.Value))
{
<#
}
else
{
#>
public partial class Device
{
/// <summary>
/// Gets a read-only mapping from address to register type.
/// </summary>
public static IReadOnlyDictionary<int, Type> RegisterMap { get; } = new Dictionary<int, Type>
{
<#
} // isApplicationDevice
int registerIndex = 0;
foreach (var register in deviceRegisters)
{
Expand All @@ -62,6 +96,10 @@ foreach (var register in deviceRegisters)
}
#>
};
<#
if (isApplicationDevice)
{
#>

/// <summary>
/// Gets the contents of the metadata file describing the <see cref="<#= deviceName #>"/>
Expand All @@ -76,7 +114,14 @@ foreach (var register in deviceRegisters)
using var streamReader = new System.IO.StreamReader(metadataStream);
return streamReader.ReadToEnd();
}
<#
} // isApplicationDevice
#>
}
<#
if (isApplicationDevice)
{
#>

/// <summary>
/// Represents an operator that returns the contents of the metadata file
Expand Down Expand Up @@ -388,6 +433,8 @@ foreach (var register in publicRegisters)
string INamedElement.Name => $"{nameof(<#= deviceName #>)}.{GetElementDisplayName(Register)}";
}
<#
} // deviceRegisters.Count > 0
} // isApplicationDevice
foreach (var registerMetadata in deviceRegisters)
{
var register = registerMetadata.Value;
Expand Down Expand Up @@ -647,6 +694,8 @@ if (isPrivate)
}
<#
}
if (isApplicationDevice && deviceRegisters.Count > 0)
{
#>

/// <summary>
Expand Down Expand Up @@ -691,6 +740,7 @@ foreach (var register in publicRegisters)
string INamedElement.Name => $"{nameof(<#= deviceName #>)}.{GetElementDisplayName(Payload)}";
}
<#
} // isApplicationDevice && deviceRegisters.Count > 0
foreach (var registerMetadata in publicRegisters)
{
var register = registerMetadata.Value;
Expand Down Expand Up @@ -824,9 +874,6 @@ foreach (var registerMetadata in publicRegisters)
}
#>
<#
} // deviceRegisters.Count > 0
#>
<#
var payloadTypes = new HashSet<string>();
foreach (var registerMetadata in deviceRegisters)
{
Expand Down
6 changes: 6 additions & 0 deletions src/Interface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public class DeviceMetadata
/// Specifies the collection of group masks available to be used with the different registers.
/// </summary>
public Dictionary<string, GroupMaskInfo> GroupMasks = [];

/// <summary>
/// Gets a value indicating whether the metadata describes an application device.
/// </summary>
[YamlIgnore]
public bool IsApplicationDevice => !string.IsNullOrEmpty(Device);
}

/// <summary>
Expand Down
7 changes: 5 additions & 2 deletions src/InterfaceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public sealed class InterfaceGenerator
readonly Device _deviceTemplate = new();
readonly AsyncDevice _asyncDeviceTemplate = new();
readonly CompilerErrorCollection errors = [];
readonly bool isApplicationDevice;

/// <summary>
/// Initializes a new instance of the <see cref="InterfaceGenerator"/> class with the
Expand All @@ -28,6 +29,7 @@ public InterfaceGenerator(DeviceMetadata deviceMetadata, string ns)
};
_deviceTemplate.Initialize(InterfaceImplementation.DeviceFileName, errors, session);
_asyncDeviceTemplate.Initialize(InterfaceImplementation.AsyncDeviceFileName, errors, session);
isApplicationDevice = deviceMetadata.IsApplicationDevice;
}

/// <summary>
Expand All @@ -41,7 +43,7 @@ public InterfaceGenerator(DeviceMetadata deviceMetadata, string ns)
/// <returns>The generated device interface implementation.</returns>
public InterfaceImplementation GenerateImplementation() =>
new(Device: _deviceTemplate.TransformText(),
AsyncDevice: _asyncDeviceTemplate.TransformText());
AsyncDevice: isApplicationDevice ? _asyncDeviceTemplate.TransformText() : string.Empty);
}

/// <summary>
Expand Down Expand Up @@ -72,7 +74,8 @@ public record struct InterfaceImplementation(string Device, string AsyncDevice)
public readonly IEnumerator<KeyValuePair<string, string>> GetEnumerator()
{
yield return new(DeviceFileName, Device);
yield return new(AsyncDeviceFileName, AsyncDevice);
if (!string.IsNullOrEmpty(AsyncDevice))
yield return new(AsyncDeviceFileName, AsyncDevice);
}

readonly IEnumerator IEnumerable.GetEnumerator()
Expand Down
4 changes: 1 addition & 3 deletions src/Python.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using Bonsai.Harp;

namespace Harp.Generators;
Expand Down Expand Up @@ -77,7 +76,6 @@ internal sealed class PythonRegister

internal static partial class TemplateHelper
{
const int CoreRegisterAddressLimit = 32;
const string CoreMetadataResourceName = "Harp.Generators.core.yml";

static readonly Dictionary<string, string> PrimitiveNumpyTypes = new()
Expand Down Expand Up @@ -215,7 +213,7 @@ public static PythonModule BuildPythonModule(DeviceMetadata deviceMetadata)
BuildPythonRegister(module, registerMetadata.Key, registerMetadata.Value, deviceMetadata);

module.ProtocolImports.Add("RegisterBase");
module.IsApplicationDevice = deviceMetadata.Registers.Values.All(register => register.Address >= CoreRegisterAddressLimit);
module.IsApplicationDevice = deviceMetadata.IsApplicationDevice;
return module;
}

Expand Down
1 change: 0 additions & 1 deletion src/core.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# yaml-language-server: $schema=https://harp-tech.org/draft-02/schema/registers.json
description: The core register set every Harp device carries, and its address space.
device: Tests
registers:
WhoAmI:
address: 0
Expand Down
Loading
Loading