-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBreakoutAnalogOutput.cs
161 lines (146 loc) · 6.84 KB
/
BreakoutAnalogOutput.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.ComponentModel;
using System.Linq;
using System.Reactive.Linq;
using Bonsai;
using OpenCV.Net;
namespace OpenEphys.Onix1
{
/// <summary>
/// Sends analog output data to an ONIX breakout board.
/// </summary>
[Description("Sends analog output data to an ONIX breakout board.")]
public class BreakoutAnalogOutput : Sink<Mat>
{
const BreakoutAnalogIOVoltageRange OutputRange = BreakoutAnalogIOVoltageRange.TenVolts;
/// <inheritdoc cref = "SingleDeviceFactory.DeviceName"/>
[TypeConverter(typeof(BreakoutAnalogIO.NameConverter))]
[Description(SingleDeviceFactory.DeviceNameDescription)]
public string DeviceName { get; set; }
/// <summary>
/// Gets or sets the data type used to represent analog samples.
/// </summary>
/// <remarks>
/// If <see cref="BreakoutAnalogIODataType.S16"/> is selected, each DAC value is represented by a signed, twos-complement encoded
/// 16-bit integer. In this case, the output voltage always corresponds to <see cref="BreakoutAnalogIOVoltageRange.TenVolts"/>.
/// When <see cref="BreakoutAnalogIODataType.Volts"/> is selected, 32-bit floating point voltages between -10 and 10 volts are sent
/// directly to the DACs.
/// </remarks>
[Description("The data type used to represent analog samples.")]
public BreakoutAnalogIODataType DataType { get; set; } = BreakoutAnalogIODataType.S16;
/// <summary>
/// Send samples to analog outputs.
/// </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)
{
var dataType = DataType;
return DeviceManager.GetDevice(DeviceName).SelectMany(deviceInfo =>
{
var bufferSize = 0;
var scaleBuffer = default(Mat);
var transposeBuffer = default(Mat);
var sampleScale = dataType == BreakoutAnalogIODataType.Volts
? 1 / BreakoutAnalogIODeviceInfo.GetVoltsPerDivision(OutputRange)
: 1;
var device = deviceInfo.GetDeviceContext(typeof(BreakoutAnalogIO));
return source.Do(data =>
{
if (dataType == BreakoutAnalogIODataType.S16 && data.Depth != Depth.S16 ||
dataType == BreakoutAnalogIODataType.Volts && data.Depth != Depth.F32)
{
ThrowDataTypeException(data.Depth);
}
AssertChannelCount(data.Rows);
if (bufferSize != data.Cols)
{
bufferSize = data.Cols;
transposeBuffer = bufferSize > 1
? new Mat(data.Cols, data.Rows, data.Depth, 1)
: null;
if (sampleScale != 1)
{
scaleBuffer = transposeBuffer != null
? new Mat(data.Cols, data.Rows, Depth.S16, 1)
: new Mat(data.Rows, data.Cols, Depth.S16, 1);
}
else scaleBuffer = null;
}
var outputBuffer = data;
if (transposeBuffer != null)
{
CV.Transpose(outputBuffer, transposeBuffer);
outputBuffer = transposeBuffer;
}
if (scaleBuffer != null)
{
CV.ConvertScale(outputBuffer, scaleBuffer, sampleScale);
outputBuffer = scaleBuffer;
}
var dataSize = outputBuffer.Step * outputBuffer.Rows;
device.Write(outputBuffer.Data, dataSize);
});
});
}
/// <summary>
/// Send samples to analog outputs.
/// </summary>
/// <param name="source"> A sequence of 12x1 element arrays each containing the analog data to write to channels 0 to 11.</param>
/// <returns> A sequence of 12x1 element arrays each containing the analog data to write to channels 0 to 11.</returns>
public IObservable<short[]> Process(IObservable<short[]> source)
{
if (DataType != BreakoutAnalogIODataType.S16)
ThrowDataTypeException(Depth.S16);
return DeviceManager.GetDevice(DeviceName).SelectMany(deviceInfo =>
{
var device = deviceInfo.GetDeviceContext(typeof(BreakoutAnalogIO));
return source.Do(data =>
{
AssertChannelCount(data.Length);
device.Write(data);
});
});
}
/// <summary>
/// Send samples to analog outputs.
/// </summary>
/// <param name="source"> A sequence of 12x1 element arrays each containing the analog data to write to channels 0 to 11.</param>
/// <returns> A sequence of 12x1 element arrays each containing the analog data to write to channels 0 to 11.</returns>
public IObservable<float[]> Process(IObservable<float[]> source)
{
if (DataType != BreakoutAnalogIODataType.Volts)
ThrowDataTypeException(Depth.F32);
return DeviceManager.GetDevice(DeviceName).SelectMany(deviceInfo =>
{
var device = deviceInfo.GetDeviceContext(typeof(BreakoutAnalogIO));
var divisionsPerVolt = 1 / BreakoutAnalogIODeviceInfo.GetVoltsPerDivision(OutputRange);
return source.Do(data =>
{
AssertChannelCount(data.Length);
var samples = new short[data.Length];
for (int i = 0; i < samples.Length; i++)
{
samples[i] = (short)(data[i] * divisionsPerVolt);
}
device.Write(samples);
});
});
}
static void AssertChannelCount(int channels)
{
if (channels != BreakoutAnalogIO.ChannelCount)
{
throw new InvalidOperationException(
$"The input data must have exactly {BreakoutAnalogIO.ChannelCount} channels."
);
}
}
static void ThrowDataTypeException(Depth depth)
{
throw new InvalidOperationException(
$"Invalid input data type '{depth}' for the specified analog IO configuration."
);
}
}
}