Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read device ID from type metadata #125

Merged
merged 1 commit into from
Jul 16, 2024
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
2 changes: 1 addition & 1 deletion OpenEphys.Onix/OpenEphys.Onix/ConfigureAnalogIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public override IObservable<ContextTask> Process(IObservable<ContextTask> source
var deviceAddress = DeviceAddress;
return source.ConfigureDevice(context =>
{
var device = context.GetDeviceContext(deviceAddress, AnalogIO.ID);
var device = context.GetDeviceContext(deviceAddress, DeviceType);
device.WriteRegister(AnalogIO.ENABLE, Enable ? 1u : 0u);
device.WriteRegister(AnalogIO.CH00INRANGE, (uint)InputRange0);
device.WriteRegister(AnalogIO.CH01INRANGE, (uint)InputRange1);
Expand Down
2 changes: 1 addition & 1 deletion OpenEphys.Onix/OpenEphys.Onix/ConfigureBno055.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override IObservable<ContextTask> Process(IObservable<ContextTask> source
var deviceAddress = DeviceAddress;
return source.ConfigureDevice(context =>
{
var device = context.GetDeviceContext(deviceAddress, Bno055.ID);
var device = context.GetDeviceContext(deviceAddress, DeviceType);
device.WriteRegister(Bno055.ENABLE, Enable ? 1u : 0);
return DeviceManager.RegisterDevice(deviceName, device, DeviceType);
});
Expand Down
2 changes: 1 addition & 1 deletion OpenEphys.Onix/OpenEphys.Onix/ConfigureDS90UB9x.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override IObservable<ContextTask> Process(IObservable<ContextTask> source
var deviceAddress = DeviceAddress;
return source.ConfigureDevice(context =>
{
var device = context.GetDeviceContext(deviceAddress, DS90UB9x.ID);
var device = context.GetDeviceContext(deviceAddress, DeviceType);
device.WriteRegister(DS90UB9x.ENABLE, enable ? 1u : 0);
return DeviceManager.RegisterDevice(deviceName, device, DeviceType);
});
Expand Down
2 changes: 1 addition & 1 deletion OpenEphys.Onix/OpenEphys.Onix/ConfigureDigitalIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override IObservable<ContextTask> Process(IObservable<ContextTask> source
var deviceAddress = DeviceAddress;
return source.ConfigureDevice(context =>
{
var device = context.GetDeviceContext(deviceAddress, DigitalIO.ID);
var device = context.GetDeviceContext(deviceAddress, DeviceType);
device.WriteRegister(DigitalIO.ENABLE, Enable ? 1u : 0);
return DeviceManager.RegisterDevice(deviceName, device, DeviceType);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override IObservable<ContextTask> Process(IObservable<ContextTask> source
})
.ConfigureLink(context =>
{
var device = context.GetDeviceContext(deviceAddress, FmcLinkController.ID);
var device = context.GetDeviceContext(deviceAddress, DeviceType);
void dispose() => device.WriteRegister(FmcLinkController.PORTVOLTAGE, 0);
device.WriteRegister(FmcLinkController.ENABLE, 1);

Expand Down
2 changes: 1 addition & 1 deletion OpenEphys.Onix/OpenEphys.Onix/ConfigureHarpSyncInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public override IObservable<ContextTask> Process(IObservable<ContextTask> source
var deviceAddress = DeviceAddress;
return source.ConfigureDevice(context =>
{
var device = context.GetDeviceContext(deviceAddress, HarpSyncInput.ID);
var device = context.GetDeviceContext(deviceAddress, DeviceType);
device.WriteRegister(HarpSyncInput.ENABLE, Enable ? 1u : 0);
device.WriteRegister(HarpSyncInput.SOURCE, (uint)Source);
return DeviceManager.RegisterDevice(deviceName, device, DeviceType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public override IObservable<ContextTask> Process(IObservable<ContextTask> source
var deviceAddress = DeviceAddress;
return source.ConfigureDevice(context =>
{
var device = context.GetDeviceContext(deviceAddress, Headstage64ElectricalStimulator.ID);
var device = context.GetDeviceContext(deviceAddress, DeviceType);
device.WriteRegister(Headstage64ElectricalStimulator.ENABLE, 0);
return DeviceManager.RegisterDevice(deviceName, device, DeviceType);
});
Expand Down
2 changes: 1 addition & 1 deletion OpenEphys.Onix/OpenEphys.Onix/ConfigureHeartbeat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public override IObservable<ContextTask> Process(IObservable<ContextTask> source
var deviceAddress = DeviceAddress;
return source.ConfigureDevice(context =>
{
var device = context.GetDeviceContext(deviceAddress, Heartbeat.ID);
var device = context.GetDeviceContext(deviceAddress, DeviceType);
device.WriteRegister(Heartbeat.ENABLE, 1);
var subscription = beatsPerSecond.Subscribe(newValue =>
{
Expand Down
2 changes: 1 addition & 1 deletion OpenEphys.Onix/OpenEphys.Onix/ConfigureLoadTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override IObservable<ContextTask> Process(IObservable<ContextTask> source
var transmittedWords = TransmittedWords;
return source.ConfigureDevice(context =>
{
var device = context.GetDeviceContext(deviceAddress, LoadTester.ID);
var device = context.GetDeviceContext(deviceAddress, DeviceType);
device.WriteRegister(LoadTester.ENABLE, 1);

var clk_hz = device.ReadRegister(LoadTester.CLK_HZ);
Expand Down
2 changes: 1 addition & 1 deletion OpenEphys.Onix/OpenEphys.Onix/ConfigureMemoryMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public override IObservable<ContextTask> Process(IObservable<ContextTask> source
var deviceAddress = DeviceAddress;
return source.ConfigureDevice(context =>
{
var device = context.GetDeviceContext(deviceAddress, MemoryMonitor.ID);
var device = context.GetDeviceContext(deviceAddress, DeviceType);
device.WriteRegister(MemoryMonitor.ENABLE, 1);
device.WriteRegister(MemoryMonitor.CLK_DIV, device.ReadRegister(MemoryMonitor.CLK_HZ) / SampleFrequency);

Expand Down
2 changes: 1 addition & 1 deletion OpenEphys.Onix/OpenEphys.Onix/ConfigureNeuropixelsV1e.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public override IObservable<ContextTask> Process(IObservable<ContextTask> source
return source.ConfigureDevice(context =>
{
// configure device via the DS90UB9x deserializer device
var device = context.GetPassthroughDeviceContext(deviceAddress, DS90UB9x.ID);
var device = context.GetPassthroughDeviceContext(deviceAddress, typeof(DS90UB9x));
device.WriteRegister(DS90UB9x.ENABLE, enable ? 1u : 0);

// configure deserializer aliases and serializer power supply
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override IObservable<ContextTask> Process(IObservable<ContextTask> source
return source.ConfigureDevice(context =>
{
// configure device via the DS90UB9x deserializer device
var device = context.GetPassthroughDeviceContext(deviceAddress, DS90UB9x.ID);
var device = context.GetPassthroughDeviceContext(deviceAddress, typeof(DS90UB9x));
ConfigureDeserializer(device);
ConfigureBno055(device);
var deviceInfo = new DeviceInfo(context, DeviceType, deviceAddress);
Expand Down
2 changes: 1 addition & 1 deletion OpenEphys.Onix/OpenEphys.Onix/ConfigureNeuropixelsV2e.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override IObservable<ContextTask> Process(IObservable<ContextTask> source
return source.ConfigureDevice(context =>
{
// configure device via the DS90UB9x deserializer device
var device = context.GetPassthroughDeviceContext(deviceAddress, DS90UB9x.ID);
var device = context.GetPassthroughDeviceContext(deviceAddress, typeof(DS90UB9x));
device.WriteRegister(DS90UB9x.ENABLE, enable ? 1u : 0);

// configure deserializer aliases and serializer power supply
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public override IObservable<ContextTask> Process(IObservable<ContextTask> source
return source.ConfigureDevice(context =>
{
// configure device via the DS90UB9x deserializer device
var device = context.GetPassthroughDeviceContext(deviceAddress, DS90UB9x.ID);
var device = context.GetPassthroughDeviceContext(deviceAddress, typeof(DS90UB9x));
device.WriteRegister(DS90UB9x.ENABLE, enable ? 1u : 0);

// configure deserializer aliases and serializer power supply
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override IObservable<ContextTask> Process(IObservable<ContextTask> source
return source.ConfigureDevice(context =>
{
// configure device via the DS90UB9x deserializer device
var device = context.GetPassthroughDeviceContext(deviceAddress, DS90UB9x.ID);
var device = context.GetPassthroughDeviceContext(deviceAddress, typeof(DS90UB9x));
ConfigureDeserializer(device);
ConfigureBno055(device);
var deviceInfo = new DeviceInfo(context, DeviceType, deviceAddress);
Expand Down
2 changes: 1 addition & 1 deletion OpenEphys.Onix/OpenEphys.Onix/ConfigureRhd2164.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public override IObservable<ContextTask> Process(IObservable<ContextTask> source
{
// config register format following RHD2164 datasheet
// https://intantech.com/files/Intan_RHD2000_series_datasheet.pdf
var device = context.GetDeviceContext(deviceAddress, Rhd2164.ID);
var device = context.GetDeviceContext(deviceAddress, DeviceType);

var format = device.ReadRegister(Rhd2164.FORMAT);
var amplifierDataFormat = AmplifierDataFormat;
Expand Down
2 changes: 1 addition & 1 deletion OpenEphys.Onix/OpenEphys.Onix/ConfigureTS4231.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override IObservable<ContextTask> Process(IObservable<ContextTask> source
var deviceAddress = DeviceAddress;
return source.ConfigureDevice(context =>
{
var device = context.GetDeviceContext(deviceAddress, TS4231.ID);
var device = context.GetDeviceContext(deviceAddress, DeviceType);
device.WriteRegister(TS4231.ENABLE, Enable ? 1u : 0);
return DeviceManager.RegisterDevice(deviceName, device, DeviceType);
});
Expand Down
2 changes: 1 addition & 1 deletion OpenEphys.Onix/OpenEphys.Onix/ConfigureTest0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override IObservable<ContextTask> Process(IObservable<ContextTask> source
var deviceAddress = DeviceAddress;
return source.ConfigureDevice(context =>
{
var device = context.GetDeviceContext(deviceAddress, Test0.ID);
var device = context.GetDeviceContext(deviceAddress, DeviceType);
device.WriteRegister(Test0.ENABLE, Enable ? 1u : 0);
FrameRateHz = device.ReadRegister(Test0.FRAMERATE);
DummyCount = device.ReadRegister(Test0.NUMTESTWORDS);
Expand Down
47 changes: 36 additions & 11 deletions OpenEphys.Onix/OpenEphys.Onix/ContextHelper.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
using System;
using System.Reflection;
using oni;

namespace OpenEphys.Onix
{
static class ContextHelper
{
public static DeviceContext GetDeviceContext(this ContextTask context, uint address, int id)
public static DeviceContext GetDeviceContext(this ContextTask context, uint address, Type expectedType)
{
if (!context.DeviceTable.TryGetValue(address, out Device device))
{
throw new InvalidOperationException($"The specified device '{id}:{address}' is not present in the device table.");
ThrowDeviceNotFoundException(expectedType, address);
}

if (device.ID != id)
if (device.ID != GetDeviceID(expectedType))
{
throw new InvalidOperationException($"The selected device is not a {id} device.");
ThrowInvalidDeviceException(expectedType, address);
}

return new DeviceContext(context, device);
Expand All @@ -25,23 +26,47 @@ public static DeviceContext GetDeviceContext(this DeviceInfo deviceInfo, Type ex
deviceInfo.AssertType(expectedType);
if (!deviceInfo.Context.DeviceTable.TryGetValue(deviceInfo.DeviceAddress, out Device device))
{
throw new InvalidOperationException(
$"The specified device '{expectedType}:{deviceInfo.DeviceAddress}' is not present in the device table."
);
ThrowDeviceNotFoundException(expectedType, deviceInfo.DeviceAddress);
}

return new DeviceContext(deviceInfo.Context, device);
}

public static DeviceContext GetPassthroughDeviceContext(this ContextTask context, uint address, int id)
public static DeviceContext GetPassthroughDeviceContext(this ContextTask context, uint address, Type expectedType)
{
var passthroughDeviceAddress = context.GetPassthroughDeviceAddress(address);
return GetDeviceContext(context, passthroughDeviceAddress, id);
return GetDeviceContext(context, passthroughDeviceAddress, expectedType);
}

public static DeviceContext GetPassthroughDeviceContext(this DeviceContext device, int id)
public static DeviceContext GetPassthroughDeviceContext(this DeviceContext device, Type expectedType)
{
return GetPassthroughDeviceContext(device.Context, device.Address, id);
return GetPassthroughDeviceContext(device.Context, device.Address, expectedType);
}

static int GetDeviceID(Type deviceType)
{
var fieldInfo = deviceType.GetField(
"ID",
BindingFlags.Static |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.IgnoreCase);
if (fieldInfo == null || !fieldInfo.IsLiteral)
{
throw new ArgumentException($"The specified device type {deviceType} does not have a const ID field.", nameof(deviceType));
}

return (int)fieldInfo.GetRawConstantValue();
}

static void ThrowDeviceNotFoundException(Type expectedType, uint address)
{
throw new InvalidOperationException($"Device '{expectedType.Name}' was not found in the device table at address {address}.");
}

static void ThrowInvalidDeviceException(Type expectedType, uint address)
{
throw new InvalidOperationException($"Invalid device ID. The device found at address {address} is not a '{expectedType.Name}' device.");
}
}
}
2 changes: 1 addition & 1 deletion OpenEphys.Onix/OpenEphys.Onix/DeviceInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void AssertType(Type expectedType)
if (DeviceType != expectedType)
{
throw new InvalidOperationException(
$"Expected device with register type {expectedType}. Actual type is {DeviceType}."
$"Expected device of type {expectedType.Name}. Actual type is {DeviceType.Name}."
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion OpenEphys.Onix/OpenEphys.Onix/NeuropixelsV1eBno055Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public unsafe IObservable<Bno055DataFrame> Generate<TSource>(IObservable<TSource
deviceInfo => Observable.Create<Bno055DataFrame>(observer =>
{
var device = deviceInfo.GetDeviceContext(typeof(NeuropixelsV1eBno055));
var passthrough = device.GetPassthroughDeviceContext(DS90UB9x.ID);
var passthrough = device.GetPassthroughDeviceContext(typeof(DS90UB9x));
var i2c = new I2CRegisterContext(passthrough, NeuropixelsV1eBno055.BNO055Address);

var pollingObserver = Observer.Create<TSource>(
Expand Down
2 changes: 1 addition & 1 deletion OpenEphys.Onix/OpenEphys.Onix/NeuropixelsV1eData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public unsafe override IObservable<NeuropixelsV1eDataFrame> Generate()
{
var info = (NeuropixelsV1eDeviceInfo)deviceInfo;
var device = info.GetDeviceContext(typeof(NeuropixelsV1e));
var passthrough = device.GetPassthroughDeviceContext(DS90UB9x.ID);
var passthrough = device.GetPassthroughDeviceContext(typeof(DS90UB9x));
var probeData = device.Context.FrameReceived.Where(frame => frame.DeviceAddress == passthrough.Address);

return Observable.Create<NeuropixelsV1eDataFrame>(observer =>
Expand Down
2 changes: 1 addition & 1 deletion OpenEphys.Onix/OpenEphys.Onix/NeuropixelsV2eBetaData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public unsafe override IObservable<NeuropixelsV2eBetaDataFrame> Generate()
{
var info = (NeuropixelsV2eDeviceInfo)deviceInfo;
var device = info.GetDeviceContext(typeof(NeuropixelsV2eBeta));
var passthrough = device.GetPassthroughDeviceContext(DS90UB9x.ID);
var passthrough = device.GetPassthroughDeviceContext(typeof(DS90UB9x));
var probeData = device.Context.FrameReceived.Where(frame =>
frame.DeviceAddress == passthrough.Address &&
NeuropixelsV2eBetaDataFrame.GetProbeIndex(frame) == (int)ProbeIndex);
Expand Down
2 changes: 1 addition & 1 deletion OpenEphys.Onix/OpenEphys.Onix/NeuropixelsV2eBno055Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public unsafe IObservable<Bno055DataFrame> Generate<TSource>(IObservable<TSource
deviceInfo => Observable.Create<Bno055DataFrame>(observer =>
{
var device = deviceInfo.GetDeviceContext(typeof(NeuropixelsV2eBno055));
var passthrough = device.GetPassthroughDeviceContext(DS90UB9x.ID);
var passthrough = device.GetPassthroughDeviceContext(typeof(DS90UB9x));
var i2c = new I2CRegisterContext(passthrough, NeuropixelsV2eBno055.BNO055Address);

var pollingObserver = Observer.Create<TSource>(
Expand Down
2 changes: 1 addition & 1 deletion OpenEphys.Onix/OpenEphys.Onix/NeuropixelsV2eData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public unsafe override IObservable<NeuropixelsV2eDataFrame> Generate()
{
var info = (NeuropixelsV2eDeviceInfo)deviceInfo;
var device = info.GetDeviceContext(typeof(NeuropixelsV2e));
var passthrough = device.GetPassthroughDeviceContext(DS90UB9x.ID);
var passthrough = device.GetPassthroughDeviceContext(typeof(DS90UB9x));
var probeData = device.Context.FrameReceived.Where(frame =>
frame.DeviceAddress == passthrough.Address &&
NeuropixelsV2eDataFrame.GetProbeIndex(frame) == (int)ProbeIndex);
Expand Down