Skip to content

Commit

Permalink
Merge pull request #213 from open-ephys/issue-205
Browse files Browse the repository at this point in the history
Convert BreakoutAnalogOutput data to offset binary
  • Loading branch information
jonnew authored Aug 20, 2024
2 parents fd861a0 + 4011388 commit 25b7517
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
16 changes: 13 additions & 3 deletions OpenEphys.Onix1/BreakoutAnalogOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class BreakoutAnalogOutput : Sink<Mat>
/// </summary>
/// <param name="source"> A sequence of 12xN sample matrices containing the analog data to write to channels 0 to 11.</param>
/// <returns> A sequence of 12xN sample matrices containing the analog data that were written to channels 0 to 11.</returns>
public override IObservable<Mat> Process(IObservable<Mat> source)
public override unsafe IObservable<Mat> Process(IObservable<Mat> source)
{
var dataType = DataType;
return DeviceManager.GetDevice(DeviceName).SelectMany(deviceInfo =>
Expand Down Expand Up @@ -87,6 +87,10 @@ public override IObservable<Mat> Process(IObservable<Mat> source)
}

var dataSize = outputBuffer.Step * outputBuffer.Rows;

// twos-complement to offset binary
const short Mask = -32768;
CV.XorS(outputBuffer, new Scalar(Mask, 0, 0), outputBuffer);
device.Write(outputBuffer.Data, dataSize);
});
});
Expand All @@ -108,6 +112,12 @@ public IObservable<short[]> Process(IObservable<short[]> source)
return source.Do(data =>
{
AssertChannelCount(data.Length);
var samples = new ushort[data.Length];
for (int i = 0; i < samples.Length; i++)
{
const short Mask = -32768;
data[i] ^= Mask; // twos-complement to offset binary
}
device.Write(data);
});
});
Expand All @@ -130,10 +140,10 @@ public IObservable<float[]> Process(IObservable<float[]> source)
return source.Do(data =>
{
AssertChannelCount(data.Length);
var samples = new short[data.Length];
var samples = new ushort[data.Length];
for (int i = 0; i < samples.Length; i++)
{
samples[i] = (short)(data[i] * divisionsPerVolt);
samples[i] = (ushort)(data[i] * divisionsPerVolt + BreakoutAnalogIO.DacMidScale);
}

device.Write(samples);
Expand Down
1 change: 1 addition & 0 deletions OpenEphys.Onix1/ConfigureBreakoutAnalogIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ static class BreakoutAnalogIO
// constants
public const int ChannelCount = 12;
public const int NumberOfDivisions = 1 << 16;
public const int DacMidScale = 1 << 15;

// managed registers
public const uint ENABLE = 0;
Expand Down

0 comments on commit 25b7517

Please sign in to comment.