Skip to content

Commit

Permalink
Add Test0 device support
Browse files Browse the repository at this point in the history
- This device's frame size is determined by a register setting
- It is the type of device that is used by the onidriver_test hardware
emulator
  • Loading branch information
jonnew committed Jan 10, 2024
1 parent 2ce20a3 commit 4d8a517
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 7 deletions.
16 changes: 12 additions & 4 deletions Bonsai/Bonsai.config
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Packages>
<Package id="Bonsai" version="2.8.0" />
<Package id="Bonsai.Core" version="2.8.0" />
<Package id="Bonsai" version="2.8.1" />
<Package id="Bonsai.Core" version="2.8.1" />
<Package id="Bonsai.Design" version="2.8.0" />
<Package id="Bonsai.Design.Visualizers" version="2.8.0" />
<Package id="Bonsai.Dsp" version="2.8.0" />
<Package id="Bonsai.Dsp.Design" version="2.8.0" />
<Package id="Bonsai.Editor" version="2.8.0" />
<Package id="Bonsai.Scripting.Expressions" version="2.8.0" />
<Package id="Bonsai.Scripting.Expressions.Design" version="2.8.0" />
<Package id="Bonsai.System" version="2.8.0" />
<Package id="Bonsai.System.Design" version="2.8.0" />
<Package id="Bonsai.Vision" version="2.8.0" />
Expand All @@ -23,6 +25,7 @@
<Package id="Rx-Linq" version="2.2.5" />
<Package id="Rx-PlatformServices" version="2.2.5" />
<Package id="SvgNet" version="3.3.3" />
<Package id="System.Linq.Dynamic" version="1.0.7" />
<Package id="YamlDotNet" version="13.1.1" />
<Package id="ZedGraph" version="5.1.7" />
</Packages>
Expand All @@ -34,19 +37,23 @@
<AssemblyReference assemblyName="Bonsai.Dsp" />
<AssemblyReference assemblyName="Bonsai.Dsp.Design" />
<AssemblyReference assemblyName="Bonsai.Editor" />
<AssemblyReference assemblyName="Bonsai.Scripting.Expressions" />
<AssemblyReference assemblyName="Bonsai.Scripting.Expressions.Design" />
<AssemblyReference assemblyName="Bonsai.System" />
<AssemblyReference assemblyName="Bonsai.System.Design" />
<AssemblyReference assemblyName="Bonsai.Vision" />
<AssemblyReference assemblyName="Bonsai.Vision.Design" />
</AssemblyReferences>
<AssemblyLocations>
<AssemblyLocation assemblyName="Bonsai" processorArchitecture="MSIL" location="Packages\Bonsai.2.8.0\lib\net48\Bonsai.exe" />
<AssemblyLocation assemblyName="Bonsai.Core" processorArchitecture="MSIL" location="Packages\Bonsai.Core.2.8.0\lib\net462\Bonsai.Core.dll" />
<AssemblyLocation assemblyName="Bonsai" processorArchitecture="MSIL" location="Packages\Bonsai.2.8.1\lib\net48\Bonsai.exe" />
<AssemblyLocation assemblyName="Bonsai.Core" processorArchitecture="MSIL" location="Packages\Bonsai.Core.2.8.1\lib\net462\Bonsai.Core.dll" />
<AssemblyLocation assemblyName="Bonsai.Design" processorArchitecture="MSIL" location="Packages\Bonsai.Design.2.8.0\lib\net462\Bonsai.Design.dll" />
<AssemblyLocation assemblyName="Bonsai.Design.Visualizers" processorArchitecture="MSIL" location="Packages\Bonsai.Design.Visualizers.2.8.0\lib\net462\Bonsai.Design.Visualizers.dll" />
<AssemblyLocation assemblyName="Bonsai.Dsp" processorArchitecture="MSIL" location="Packages\Bonsai.Dsp.2.8.0\lib\net462\Bonsai.Dsp.dll" />
<AssemblyLocation assemblyName="Bonsai.Dsp.Design" processorArchitecture="MSIL" location="Packages\Bonsai.Dsp.Design.2.8.0\lib\net462\Bonsai.Dsp.Design.dll" />
<AssemblyLocation assemblyName="Bonsai.Editor" processorArchitecture="MSIL" location="Packages\Bonsai.Editor.2.8.0\lib\net472\Bonsai.Editor.dll" />
<AssemblyLocation assemblyName="Bonsai.Scripting.Expressions" processorArchitecture="MSIL" location="Packages\Bonsai.Scripting.Expressions.2.8.0\lib\net462\Bonsai.Scripting.Expressions.dll" />
<AssemblyLocation assemblyName="Bonsai.Scripting.Expressions.Design" processorArchitecture="MSIL" location="Packages\Bonsai.Scripting.Expressions.Design.2.8.0\lib\net462\Bonsai.Scripting.Expressions.Design.dll" />
<AssemblyLocation assemblyName="Bonsai.System" processorArchitecture="MSIL" location="Packages\Bonsai.System.2.8.0\lib\net462\Bonsai.System.dll" />
<AssemblyLocation assemblyName="Bonsai.System.Design" processorArchitecture="MSIL" location="Packages\Bonsai.System.Design.2.8.0\lib\net462\Bonsai.System.Design.dll" />
<AssemblyLocation assemblyName="Bonsai.Vision" processorArchitecture="MSIL" location="Packages\Bonsai.Vision.2.8.0\lib\net462\Bonsai.Vision.dll" />
Expand All @@ -60,6 +67,7 @@
<AssemblyLocation assemblyName="OpenTK.GLControl" processorArchitecture="MSIL" location="Packages\OpenTK.GLControl.3.1.0\lib\net20\OpenTK.GLControl.dll" />
<AssemblyLocation assemblyName="ScintillaNET" processorArchitecture="MSIL" location="Packages\jacobslusser.ScintillaNET.3.6.3\lib\net40\ScintillaNET.dll" />
<AssemblyLocation assemblyName="SVG" processorArchitecture="MSIL" location="Packages\SvgNet.3.3.3\lib\net462\SVG.dll" />
<AssemblyLocation assemblyName="System.Linq.Dynamic" processorArchitecture="MSIL" location="Packages\System.Linq.Dynamic.1.0.7\lib\net40\System.Linq.Dynamic.dll" />
<AssemblyLocation assemblyName="System.Reactive.Core" processorArchitecture="MSIL" location="Packages\Rx-Core.2.2.5\lib\net45\System.Reactive.Core.dll" />
<AssemblyLocation assemblyName="System.Reactive.Interfaces" processorArchitecture="MSIL" location="Packages\Rx-Interfaces.2.2.5\lib\net45\System.Reactive.Interfaces.dll" />
<AssemblyLocation assemblyName="System.Reactive.Linq" processorArchitecture="MSIL" location="Packages\Rx-Linq.2.2.5\lib\net45\System.Reactive.Linq.dll" />
Expand Down
6 changes: 6 additions & 0 deletions OpenEphys.Onix/OpenEphys.Onix.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenEphys.Onix", "OpenEphys
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|ARM64 = Debug|ARM64
Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU
Release|ARM64 = Release|ARM64
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{353B1EBC-F8EB-4D99-8331-9FF15EC17F38}.Debug|Any CPU.ActiveCfg = Debug|x64
{353B1EBC-F8EB-4D99-8331-9FF15EC17F38}.Debug|Any CPU.Build.0 = Debug|x64
{353B1EBC-F8EB-4D99-8331-9FF15EC17F38}.Debug|ARM64.ActiveCfg = Debug|x64
{353B1EBC-F8EB-4D99-8331-9FF15EC17F38}.Debug|ARM64.Build.0 = Debug|x64
{353B1EBC-F8EB-4D99-8331-9FF15EC17F38}.Debug|x64.ActiveCfg = Debug|x64
{353B1EBC-F8EB-4D99-8331-9FF15EC17F38}.Debug|x64.Build.0 = Debug|x64
{353B1EBC-F8EB-4D99-8331-9FF15EC17F38}.Release|Any CPU.ActiveCfg = Release|x64
{353B1EBC-F8EB-4D99-8331-9FF15EC17F38}.Release|Any CPU.Build.0 = Release|x64
{353B1EBC-F8EB-4D99-8331-9FF15EC17F38}.Release|ARM64.ActiveCfg = Release|ARM64
{353B1EBC-F8EB-4D99-8331-9FF15EC17F38}.Release|ARM64.Build.0 = Release|ARM64
{353B1EBC-F8EB-4D99-8331-9FF15EC17F38}.Release|x64.ActiveCfg = Release|x64
Expand Down
74 changes: 74 additions & 0 deletions OpenEphys.Onix/OpenEphys.Onix/ConfigureTest0.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.ComponentModel;
using System.Reactive.Subjects;
using System.Xml.Serialization;

namespace OpenEphys.Onix
{
public class ConfigureTest0 : SingleDeviceFactory
{
readonly BehaviorSubject<short> message = new(0);

public ConfigureTest0()
: base(typeof(Test0))
{
}

[Category(ConfigurationCategory)]
[Description("Specifies whether the Test0 device is enabled.")]
public bool Enable { get; set; } = true;

[Category(AcquisitionCategory)]
[Description("Specifies the first 16-bit word that appears in the device to host frame.")]
public short Message
{
get => message.Value;
set => message.OnNext(value);
}

[XmlIgnore]
[Category(ConfigurationCategory)]
[Description("Indicates the number of 16-bit numbers, 0 to PayloadWords - 1, that follow Message in each frame.")]
public uint DummyCount { get; private set; }

[XmlIgnore]
[Category(ConfigurationCategory)]
[Description("Indicates the rate at which frames are produced. 0 indicates that the frame rate is unspecified (variable or upstream controlled).")]
public uint FrameRateHz { get; private set; }

public override IObservable<ContextTask> Process(IObservable<ContextTask> source)
{
var deviceName = DeviceName;
var deviceAddress = DeviceAddress;
return source.ConfigureDevice(context =>
{
var device = context.GetDevice(deviceAddress, Test0.ID);
context.WriteRegister(deviceAddress, Test0.ENABLE, Enable ? 1u : 0);
FrameRateHz = context.ReadRegister(deviceAddress, Test0.FRAMERATE);
DummyCount = context.ReadRegister(deviceAddress, Test0.NUMTESTWORDS);

var deviceInfo = new DeviceInfo(context, DeviceType, deviceAddress);
var disposable = DeviceManager.RegisterDevice(deviceName, deviceInfo);
return disposable;
});
}
}

static class Test0
{
public const int ID = 10;

public const uint ENABLE = 0x0;
public const uint MESSAGE = 0x1;
public const uint NUMTESTWORDS = 0x2;
public const uint FRAMERATE = 0x3;

internal class NameConverter : DeviceNameConverter
{
public NameConverter()
: base(typeof(Test0))
{
}
}
}
}
4 changes: 2 additions & 2 deletions OpenEphys.Onix/OpenEphys.Onix/OpenEphys.Onix.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>9.0</LangVersion>
<Features>strict</Features>
<Version>0.1.0</Version>
<Version>0.1.1</Version>
<Platforms>x64</Platforms>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bonsai.Core" Version="2.8.0" />
<PackageReference Include="clroni" Version="6.1.0" />
<PackageReference Include="OpenCV.Net" Version="3.4.2" />
<PackageReference Include="clroni" Version="6.0.0-build230711" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"commandName": "Executable",
"executablePath": "$(SolutionDir)..\\Bonsai\\Bonsai.exe",
"commandLineArgs": "--lib:$(TargetDir).",
"nativeDebugging": true
"nativeDebugging": false
}
}
}
69 changes: 69 additions & 0 deletions OpenEphys.Onix/OpenEphys.Onix/Test0Data.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.ComponentModel;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Runtime.InteropServices;
using Bonsai;
using Bonsai.Reactive;
using OpenCV.Net;

namespace OpenEphys.Onix
{
public class Test0Data : Source<Test0DataFrame>
{
[TypeConverter(typeof(Test0.NameConverter))]
public string DeviceName { get; set; }

[Category(DeviceFactory.ConfigurationCategory)]
[Range(1, 1000000)]
[Description("The number of frames making up a single data block that is propagated in the observable sequence.")]
public int BufferSize { get; set; } = 100;

public unsafe override IObservable<Test0DataFrame> Generate()
{
var bufferSize = BufferSize; // TODO: Branch for bufferSize = 1?

return Observable.Using(
() => DeviceManager.ReserveDevice(DeviceName),
disposable => disposable.Subject.SelectMany(deviceInfo =>
Observable.Create<Test0DataFrame>(observer =>
{
// Find number of dummy words in the frame
var dummyWords = (int)deviceInfo.Context.ReadRegister(deviceInfo.DeviceAddress, Test0.NUMTESTWORDS);

var sampleIndex = 0;
var device = deviceInfo.GetDevice(typeof(Test0));
var dummyBuffer = new short[dummyWords * bufferSize];
var messageBuffer = new short[bufferSize];
var hubClockBuffer = new ulong[bufferSize];
var clockBuffer = new ulong[bufferSize];

var frameObserver = Observer.Create<oni.Frame>(
frame =>
{
var payload = (Test0PayloadHeader*)frame.Data.ToPointer();
Marshal.Copy(new IntPtr(payload + 1), dummyBuffer, sampleIndex * dummyWords, dummyWords);
messageBuffer[sampleIndex] = payload->Message;
hubClockBuffer[sampleIndex] = payload->HubClock;
clockBuffer[sampleIndex] = frame.Clock;
if (++sampleIndex >= bufferSize)
{
var dummy = BufferHelper.CopyBuffer(dummyBuffer, bufferSize, dummyWords, Depth.S16);
var message = BufferHelper.CopyBuffer(messageBuffer, bufferSize, 1, Depth.S16);
observer.OnNext(new Test0DataFrame(clockBuffer, hubClockBuffer, message, dummy));
hubClockBuffer = new ulong[bufferSize];
clockBuffer = new ulong[bufferSize];
sampleIndex = 0;
}
},
observer.OnError,
observer.OnCompleted);

return deviceInfo.Context.FrameReceived
.Where(frame => frame.DeviceAddress == device.Address)
.SubscribeSafe(frameObserver);
})));
}
}
}
32 changes: 32 additions & 0 deletions OpenEphys.Onix/OpenEphys.Onix/Test0DataFrame.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Runtime.InteropServices;
using OpenCV.Net;

namespace OpenEphys.Onix
{
public class Test0DataFrame
{
public Test0DataFrame(ulong[] clock, ulong[] hubClock, Mat message, Mat dummy)
{
Clock = clock;
HubClock = hubClock;
Message = message;
Dummy = dummy;
}

public ulong[] Clock { get; }

public ulong[] HubClock { get; }

public Mat Message { get; }

public Mat Dummy { get; }
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct Test0PayloadHeader
{
public ulong HubClock;
public short Message;
}
}

0 comments on commit 4d8a517

Please sign in to comment.