Skip to content

Commit 25b7517

Browse files
authored
Merge pull request #213 from open-ephys/issue-205
Convert BreakoutAnalogOutput data to offset binary
2 parents fd861a0 + 4011388 commit 25b7517

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

OpenEphys.Onix1/BreakoutAnalogOutput.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class BreakoutAnalogOutput : Sink<Mat>
3737
/// </summary>
3838
/// <param name="source"> A sequence of 12xN sample matrices containing the analog data to write to channels 0 to 11.</param>
3939
/// <returns> A sequence of 12xN sample matrices containing the analog data that were written to channels 0 to 11.</returns>
40-
public override IObservable<Mat> Process(IObservable<Mat> source)
40+
public override unsafe IObservable<Mat> Process(IObservable<Mat> source)
4141
{
4242
var dataType = DataType;
4343
return DeviceManager.GetDevice(DeviceName).SelectMany(deviceInfo =>
@@ -87,6 +87,10 @@ public override IObservable<Mat> Process(IObservable<Mat> source)
8787
}
8888

8989
var dataSize = outputBuffer.Step * outputBuffer.Rows;
90+
91+
// twos-complement to offset binary
92+
const short Mask = -32768;
93+
CV.XorS(outputBuffer, new Scalar(Mask, 0, 0), outputBuffer);
9094
device.Write(outputBuffer.Data, dataSize);
9195
});
9296
});
@@ -108,6 +112,12 @@ public IObservable<short[]> Process(IObservable<short[]> source)
108112
return source.Do(data =>
109113
{
110114
AssertChannelCount(data.Length);
115+
var samples = new ushort[data.Length];
116+
for (int i = 0; i < samples.Length; i++)
117+
{
118+
const short Mask = -32768;
119+
data[i] ^= Mask; // twos-complement to offset binary
120+
}
111121
device.Write(data);
112122
});
113123
});
@@ -130,10 +140,10 @@ public IObservable<float[]> Process(IObservable<float[]> source)
130140
return source.Do(data =>
131141
{
132142
AssertChannelCount(data.Length);
133-
var samples = new short[data.Length];
143+
var samples = new ushort[data.Length];
134144
for (int i = 0; i < samples.Length; i++)
135145
{
136-
samples[i] = (short)(data[i] * divisionsPerVolt);
146+
samples[i] = (ushort)(data[i] * divisionsPerVolt + BreakoutAnalogIO.DacMidScale);
137147
}
138148

139149
device.Write(samples);

OpenEphys.Onix1/ConfigureBreakoutAnalogIO.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ static class BreakoutAnalogIO
281281
// constants
282282
public const int ChannelCount = 12;
283283
public const int NumberOfDivisions = 1 << 16;
284+
public const int DacMidScale = 1 << 15;
284285

285286
// managed registers
286287
public const uint ENABLE = 0;

0 commit comments

Comments
 (0)