-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
Showing
7 changed files
with
196 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
{ | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |