Skip to content

Commit b94f5bc

Browse files
committed
Convert BreakoutAnalogOutput data to offset binary
- The analog outputs on the breakout board expect 16-bit words in offset binary - We were using twos-complement. This fix converts all outgoing data to offset binary before the final calls to device.Write()
1 parent 7ac700f commit b94f5bc

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

OpenEphys.Onix1/BreakoutAnalogOutput.cs

Lines changed: 17 additions & 4 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,7 +87,14 @@ public override IObservable<Mat> Process(IObservable<Mat> source)
8787
}
8888

8989
var dataSize = outputBuffer.Step * outputBuffer.Rows;
90-
device.Write(outputBuffer.Data, dataSize);
90+
91+
// twos-complement to offset binary
92+
var dataPtr = (short *)outputBuffer.Data.ToPointer();
93+
const short Mask = -32768;
94+
for (int i = 0; i < dataSize / sizeof(short); i++)
95+
*(dataPtr + i) ^= Mask;
96+
97+
device.Write(outputBuffer.Data, dataSize);
9198
});
9299
});
93100
}
@@ -108,6 +115,12 @@ public IObservable<short[]> Process(IObservable<short[]> source)
108115
return source.Do(data =>
109116
{
110117
AssertChannelCount(data.Length);
118+
var samples = new ushort[data.Length];
119+
for (int i = 0; i < samples.Length; i++)
120+
{
121+
const short Mask = -32768;
122+
data[i] ^= Mask; // twos-complement to offset binary
123+
}
111124
device.Write(data);
112125
});
113126
});
@@ -130,10 +143,10 @@ public IObservable<float[]> Process(IObservable<float[]> source)
130143
return source.Do(data =>
131144
{
132145
AssertChannelCount(data.Length);
133-
var samples = new short[data.Length];
146+
var samples = new ushort[data.Length];
134147
for (int i = 0; i < samples.Length; i++)
135148
{
136-
samples[i] = (short)(data[i] * divisionsPerVolt);
149+
samples[i] = (ushort)(data[i] * divisionsPerVolt + BreakoutAnalogIO.DacMidScale);
137150
}
138151

139152
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)