Skip to content

Commit 5d1371d

Browse files
author
Jasper van Bourgognie
committed
Extended wave generator with longer sample stretching and other small helper properties
1 parent 229b7f2 commit 5d1371d

File tree

5 files changed

+102
-34
lines changed

5 files changed

+102
-34
lines changed

Devices/DummyScope.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public enum WaveSource {
1414

1515
public class DummyScopeChannelConfig
1616
{
17-
public WaveForm waveform;
17+
public AnalogWaveForm waveform;
1818
public double amplitude;
1919
public Coupling coupling;
2020
public double dcOffset;
@@ -155,7 +155,7 @@ internal DummyScope () : base ()
155155
frequency = 10e3,
156156
phase = 0,
157157
dutyCycle = 0.5f,
158-
waveform = WaveForm.TRIANGLE,
158+
waveform = AnalogWaveForm.TRIANGLE,
159159
probeDivision = ProbeDivision.X1,
160160
}
161161
},
@@ -168,7 +168,7 @@ internal DummyScope () : base ()
168168
frequency = 10e3,
169169
phase = 0,
170170
dutyCycle = 0.5f,
171-
waveform = WaveForm.SINE,
171+
waveform = AnalogWaveForm.SINE,
172172
probeDivision = ProbeDivision.X1,
173173
}
174174
}
@@ -654,7 +654,7 @@ public void SetDummyWaveDutyCycle(AnalogChannel channel, double dc)
654654
{
655655
ChannelConfig[channel].dutyCycle = dc > 1 ? 1 : dc < 0 ? 0 : dc;
656656
}
657-
public void SetDummyWaveForm(AnalogChannel channel, WaveForm w)
657+
public void SetDummyWaveForm(AnalogChannel channel, AnalogWaveForm w)
658658
{
659659
ChannelConfig[channel].waveform = w;
660660
}

Devices/DummyScopeWaves.cs

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66

77
namespace LabNation.DeviceInterface.Devices
88
{
9-
public enum WaveForm { SINE, COSINE, SQUARE, SAWTOOTH, TRIANGLE, SAWTOOTH_SINE, MULTISINE,
9+
public enum AnalogWaveForm { SINE, COSINE, SQUARE, SAWTOOTH, TRIANGLE, SAWTOOTH_SINE, MULTISINE,
1010
#if DEBUG
1111
HALF_BIG_HALF_UGLY
1212
#endif
1313
};
14+
public enum DigitalWaveForm { Counter, OneHot, Marquee };
1415

1516
partial class DummyScope
1617
{
1718
public string Serial { get { return "DUMMY"; } }
1819
public static float[] GenerateWave(uint waveLength, double samplePeriod, double timeOffset, DummyScopeChannelConfig config )
1920
{
20-
WaveForm waveForm = config.waveform;
21+
AnalogWaveForm waveForm = config.waveform;
2122
double frequency = config.frequency;
2223
double amplitude = config.amplitude;
2324
double phase = config.phase;
@@ -26,29 +27,29 @@ public static float[] GenerateWave(uint waveLength, double samplePeriod, double
2627

2728
float[] wave = new float[waveLength];
2829
switch(waveForm) {
29-
case WaveForm.SINE:
30+
case AnalogWaveForm.SINE:
3031
wave = DummyScope.WaveSine(waveLength, samplePeriod, timeOffset, frequency, amplitude, phase);
3132
break;
32-
case WaveForm.COSINE:
33+
case AnalogWaveForm.COSINE:
3334
wave = DummyScope.WaveCosine(waveLength, samplePeriod, timeOffset, frequency, amplitude, phase);
3435
break;
35-
case WaveForm.SQUARE:
36+
case AnalogWaveForm.SQUARE:
3637
wave = DummyScope.WaveSquare(waveLength, samplePeriod, timeOffset, frequency, amplitude, phase, dutyCycle);
3738
break;
38-
case WaveForm.SAWTOOTH:
39+
case AnalogWaveForm.SAWTOOTH:
3940
wave = DummyScope.WaveSawTooth(waveLength, samplePeriod, timeOffset, frequency, amplitude, phase);
4041
break;
41-
case WaveForm.TRIANGLE:
42+
case AnalogWaveForm.TRIANGLE:
4243
wave = DummyScope.WaveTriangle(waveLength, samplePeriod, timeOffset, frequency, amplitude, phase);
4344
break;
44-
case WaveForm.SAWTOOTH_SINE:
45+
case AnalogWaveForm.SAWTOOTH_SINE:
4546
wave = DummyScope.WaveSawtoothSine(waveLength, samplePeriod, timeOffset, frequency, amplitude, phase);
4647
break;
47-
case WaveForm.MULTISINE:
48+
case AnalogWaveForm.MULTISINE:
4849
wave = DummyScope.WaveMultiCosine(waveLength, samplePeriod, timeOffset, frequency, amplitude, phase, new float[] {1, 2, 4, 8 });
4950
break;
5051
#if DEBUG
51-
case WaveForm.HALF_BIG_HALF_UGLY:
52+
case AnalogWaveForm.HALF_BIG_HALF_UGLY:
5253
wave = DummyScope.WaveHalfBigHalfUgly(waveLength, samplePeriod, timeOffset, frequency, amplitude, phase);
5354
break;
5455
#endif
@@ -144,7 +145,7 @@ public static float[] WaveSawtoothSine(uint nSamples, double samplePeriod, doubl
144145
float[] wave = Utils.CombineArrays(wave1, wave2, sumFloat);
145146
return wave;
146147
}
147-
148+
148149
public static float[] WaveHalfBigHalfUgly(uint awgPoints, double awgSamplePeriod, double timeOffset, double frequency, double amplitude, double phase)
149150
{
150151
float[] wave1 = DummyScope.WaveSine(awgPoints, awgSamplePeriod, timeOffset, frequency * 21f, amplitude, phase + 0 * 168);
@@ -191,6 +192,39 @@ public static float[] WaveHalfBigHalfUgly(uint awgPoints, double awgSamplePeriod
191192
return finalWave;
192193
}
193194

195+
public static byte[] WaveCounter(byte min, byte max)
196+
{
197+
byte[] counter = new byte[max - min + 1];
198+
byte count = min;
199+
for (int i = 0; i < counter.Length; i++)
200+
counter[i] = count++;
201+
return counter;
202+
}
203+
204+
public static byte[] WaveOneHot(int numberOfBits)
205+
{
206+
if (numberOfBits < 1 || numberOfBits > 8)
207+
throw new Exception("Number of bits out of range for one-hot digital sequence");
208+
209+
byte[] counter = new byte[numberOfBits];
210+
for (int i = 0; i < counter.Length; i++)
211+
counter[i] = (byte)(1 << i);
212+
return counter;
213+
}
214+
215+
public static byte[] WaveMarquee(int numberOfBits)
216+
{
217+
if (numberOfBits < 1 || numberOfBits > 8)
218+
throw new Exception("Number of bits out of range for one-hot digital sequence");
219+
220+
int mask = (int)Math.Pow(2, numberOfBits) - 1;
221+
int full = mask << numberOfBits;
222+
byte[] counter = new byte[numberOfBits * 2];
223+
for (int i = 0; i < counter.Length; i++)
224+
counter[i] = (byte)((full >> (numberOfBits * 2 - i)) & mask);
225+
return counter;
226+
}
227+
194228
public bool Ready { get { return true; } }
195229

196230
private static void AddNoise(float[] output, double noiseAmplitude)

Devices/IWaveGenerator.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ public interface IWaveGenerator : IDevice
1111
double[] GeneratorDataDouble { set; }
1212
int[] GeneratorDataInt { set; }
1313
byte[] GeneratorDataByte { set; }
14-
bool GeneratorToAnalogEnabled { set; }
15-
bool GeneratorToDigitalEnabled { set; }
16-
int GeneratorStretcherForFrequency(double frequency);
14+
bool GeneratorToAnalogEnabled { set; get; }
15+
bool GeneratorToDigitalEnabled { set; get; }
16+
UInt32 GeneratorStretcherForFrequency(double frequency);
1717
int GeneratorNumberOfSamplesForFrequency(double frequency);
1818
int GeneratorNumberOfSamples { set; get; }
19-
int GeneratorStretching { set; get; }
19+
UInt32 GeneratorStretching { set; get; }
2020
double GeneratorFrequencyMax { get; }
2121
double GeneratorFrequencyMin { get; }
2222
double GeneratorFrequency { get; set; }
23+
double GeneratorSamplePeriodMin { get; }
24+
double GeneratorSamplePeriodMax { get; }
25+
double GeneratorSamplePeriod { set; get; }
2326
}
2427
}

Devices/SmartScopeAwg.cs

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ partial class SmartScope
1212
{
1313
const double AWG_SAMPLE_PERIOD_0 = 10e-9; //10ns
1414
const int AWG_SAMPLES_MAX = 2048;
15-
const int AWG_SAMPLES_MIN = 20;
16-
const int AWG_STRETCHER_MAX = 255;
15+
const int AWG_SAMPLES_MIN = 1;
16+
const UInt32 AWG_STRETCHER_MAX = UInt32.MaxValue;
1717
public bool DataOutOfRange { get; private set; }
1818

1919
/// <summary>
@@ -83,19 +83,26 @@ public int GeneratorNumberOfSamples
8383
}
8484
}
8585

86-
public int GeneratorStretching
86+
public UInt32 GeneratorStretching
8787
{
8888
set
8989
{
90-
if (value > 255 || value < 0)
90+
if (value > AWG_STRETCHER_MAX || value < 0)
9191
{
92-
throw new ValidationException(String.Format("AWG stretching out of range [0,255] - got {0}", value));
92+
throw new ValidationException(String.Format("AWG stretching out of range [0,{0}] - got {1}", AWG_STRETCHER_MAX, value));
9393
}
94-
FpgaSettingsMemory[REG.GENERATOR_DECIMATION].Set((byte)value);
94+
FpgaSettingsMemory[REG.GENERATOR_DECIMATION_B0].Set((byte)(value & 0xFF));
95+
FpgaSettingsMemory[REG.GENERATOR_DECIMATION_B1].Set((byte)((value >> 8) & 0xFF));
96+
FpgaSettingsMemory[REG.GENERATOR_DECIMATION_B2].Set((byte)((value >> 16) & 0xFF));
97+
FpgaSettingsMemory[REG.GENERATOR_DECIMATION_B3].Set((byte)((value >> 24) & 0xFF));
9598
}
9699
get
97100
{
98-
return FpgaSettingsMemory[REG.GENERATOR_DECIMATION].GetByte();
101+
return
102+
(UInt32)(FpgaSettingsMemory[REG.GENERATOR_DECIMATION_B0].GetByte() ) +
103+
(UInt32)(FpgaSettingsMemory[REG.GENERATOR_DECIMATION_B1].GetByte() << 8) +
104+
(UInt32)(FpgaSettingsMemory[REG.GENERATOR_DECIMATION_B2].GetByte() << 16) +
105+
(UInt32)(FpgaSettingsMemory[REG.GENERATOR_DECIMATION_B3].GetByte() << 24);
99106
}
100107
}
101108

@@ -109,6 +116,10 @@ public bool GeneratorToAnalogEnabled
109116
StrobeMemory[STR.LA_ENABLE].WriteImmediate(false);
110117
StrobeMemory[STR.GENERATOR_TO_AWG].WriteImmediate(value);
111118
}
119+
get
120+
{
121+
return StrobeMemory[STR.GENERATOR_TO_AWG].GetBool();
122+
}
112123
}
113124

114125
public bool GeneratorToDigitalEnabled
@@ -119,6 +130,10 @@ public bool GeneratorToDigitalEnabled
119130
if (!Connected) return;
120131
StrobeMemory[STR.GENERATOR_TO_DIGITAL].WriteImmediate(value);
121132
}
133+
get
134+
{
135+
return StrobeMemory[STR.GENERATOR_TO_DIGITAL].GetBool();
136+
}
122137
}
123138

124139
public double GeneratorFrequencyMax
@@ -132,24 +147,24 @@ public double GeneratorFrequencyMin
132147
{
133148
get
134149
{
135-
return 1.0 / ((AWG_SAMPLES_MAX - 1) * AWG_SAMPLE_PERIOD_0 * (AWG_STRETCHER_MAX + 1));
150+
return 1.0 / ((AWG_SAMPLES_MAX - 1) * AWG_SAMPLE_PERIOD_0 * ((double)(AWG_STRETCHER_MAX) + 1));
136151
}
137152
}
138-
public int GeneratorStretcherForFrequency(double frequency)
153+
public UInt32 GeneratorStretcherForFrequency(double frequency)
139154
{
140155
if (frequency > GeneratorFrequencyMax || frequency < GeneratorFrequencyMin)
141156
throw new ValidationException(String.Format("AWG frequency {0} out of range [{1},{2}]", frequency, GeneratorFrequencyMin, GeneratorFrequencyMax));
142157

143158
double numberOfSamplesAtFullRate = Math.Floor(1 / (AWG_SAMPLE_PERIOD_0 * frequency));
144-
return (int)Math.Floor(numberOfSamplesAtFullRate / AWG_SAMPLES_MAX); ;
159+
return (UInt32)Math.Floor(numberOfSamplesAtFullRate / AWG_SAMPLES_MAX); ;
145160
}
146161
public int GeneratorNumberOfSamplesForFrequency(double frequency)
147162
{
148163
if (frequency > GeneratorFrequencyMax || frequency < GeneratorFrequencyMin)
149164
throw new ValidationException(String.Format("AWG frequency {0} out of range [{1},{2}]", frequency, GeneratorFrequencyMin, GeneratorFrequencyMax));
150165

151166
double numberOfSamplesAtFullRate = Math.Floor(1 / (AWG_SAMPLE_PERIOD_0 * frequency));
152-
int stretcher = GeneratorStretcherForFrequency(frequency);
167+
UInt32 stretcher = GeneratorStretcherForFrequency(frequency);
153168
return (int)Math.Floor(numberOfSamplesAtFullRate / (stretcher + 1));
154169
}
155170
public double GeneratorFrequency
@@ -164,5 +179,19 @@ public double GeneratorFrequency
164179
return 1 / (AWG_SAMPLE_PERIOD_0 * (GeneratorStretching + 1) * GeneratorNumberOfSamples);
165180
}
166181
}
182+
183+
public double GeneratorSamplePeriodMin { get { return AWG_SAMPLE_PERIOD_0; } }
184+
public double GeneratorSamplePeriodMax { get { return AWG_SAMPLE_PERIOD_0 * AWG_STRETCHER_MAX; } }
185+
public double GeneratorSamplePeriod
186+
{
187+
set {
188+
double samples = value / AWG_SAMPLE_PERIOD_0;
189+
UInt32 samplesRounded = (UInt32)Math.Floor(samples);
190+
GeneratorStretching = samplesRounded;
191+
}
192+
get {
193+
return GeneratorStretching * AWG_SAMPLE_PERIOD_0;
194+
}
195+
}
167196
}
168197
}

Memories/ScopeConstants_GEN.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ enum REG
4646
DIGITAL_TRIGGER_HIGH = 31,
4747
DIGITAL_TRIGGER_LOW = 32,
4848
DIGITAL_OUT = 33,
49-
GENERATOR_DECIMATION = 34,
50-
GENERATOR_SAMPLES_B0 = 35,
51-
GENERATOR_SAMPLES_B1 = 36,
49+
GENERATOR_DECIMATION_B0 = 34,
50+
GENERATOR_DECIMATION_B1 = 35,
51+
GENERATOR_DECIMATION_B2 = 36,
52+
GENERATOR_DECIMATION_B3 = 37,
53+
GENERATOR_SAMPLES_B0 = 38,
54+
GENERATOR_SAMPLES_B1 = 39,
5255
}
5356

5457
#if DEBUG
@@ -81,7 +84,6 @@ enum STR
8184
GENERATOR_TO_DIGITAL = 20,
8285
ROLL = 21,
8386
LA_CHANNEL = 22,
84-
GENERATOR_ENABLE = 23,
8587
}
8688

8789
#if DEBUG

0 commit comments

Comments
 (0)