-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFormMain.cs
More file actions
285 lines (249 loc) · 12 KB
/
FormMain.cs
File metadata and controls
285 lines (249 loc) · 12 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#if DEBUG
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LabNation.DeviceInterface.Devices;
using LabNation.DeviceInterface.Memories;
using LabNation.DeviceInterface.Hardware;
using LabNation.Common;
using System.Collections.Concurrent;
using System.Threading;
namespace ESuite
{
partial class FormMain : Form
{
private SmartScopeGui smartScopeApp;
private Thread appThread;
private IDevice device;
private FormLog logForm;
public FormMain()
{
appThread = new Thread(new ThreadStart(runApp));
appThread.Name = "SmartScope app thread";
System.Threading.Thread.CurrentThread.Name = "FormMainThread";
InitializeComponent(); //must be done before calling InitApp()
logForm = new FormLog();
this.WindowState = FormWindowState.Normal;
appThread.Start();
Logger.Info("Application initialized");
}
void runApp()
{
smartScopeApp = new SmartScopeGui(onDeviceConnect);
smartScopeApp.Exiting += (s, e) => {
try
{
if (this.InvokeRequired)
this.Invoke((MethodInvoker)delegate() { this.Close(); });
else
this.Close();
}
catch { }
};
smartScopeApp.Run();
}
private void btnJasper_click(object sender, EventArgs e)
{
if (!(device is SmartScope)) return;
SmartScope scope = (SmartScope)device;
bool running = scope.DataSourceScope.IsRunning;
scope.DataSourceScope.Stop();
List<SmartScope.GainCalibration> calibA = new List<SmartScope.GainCalibration>() {
new SmartScope.GainCalibration() { channel = AnalogChannel.ChA, divider = SmartScope.validDividers[0], multiplier = SmartScope.validMultipliers[0], coefficients = new double[] {0.0051, -0.0090, 0.3112} },
new SmartScope.GainCalibration() { channel = AnalogChannel.ChA, divider = SmartScope.validDividers[0], multiplier = SmartScope.validMultipliers[1], coefficients = new double[] {0.0027, -0.0048, 0.1630} },
new SmartScope.GainCalibration() { channel = AnalogChannel.ChA, divider = SmartScope.validDividers[0], multiplier = SmartScope.validMultipliers[2], coefficients = new double[] {0.0018, -0.0031, 0.1044} },
new SmartScope.GainCalibration() { channel = AnalogChannel.ChA, divider = SmartScope.validDividers[1], multiplier = SmartScope.validMultipliers[0], coefficients = new double[] {0.0320, -0.0559, 1.9276} },
new SmartScope.GainCalibration() { channel = AnalogChannel.ChA, divider = SmartScope.validDividers[1], multiplier = SmartScope.validMultipliers[1], coefficients = new double[] {0.0169, -0.0296, 1.0094} },
new SmartScope.GainCalibration() { channel = AnalogChannel.ChA, divider = SmartScope.validDividers[1], multiplier = SmartScope.validMultipliers[2], coefficients = new double[] {0.0109, -0.0190, 0.6446} },
new SmartScope.GainCalibration() { channel = AnalogChannel.ChA, divider = SmartScope.validDividers[2], multiplier = SmartScope.validMultipliers[0], coefficients = new double[] {0.1876, -0.3280, 11.2537} },
new SmartScope.GainCalibration() { channel = AnalogChannel.ChA, divider = SmartScope.validDividers[2], multiplier = SmartScope.validMultipliers[1], coefficients = new double[] {0.0995, -0.1739, 5.8836} },
new SmartScope.GainCalibration() { channel = AnalogChannel.ChA, divider = SmartScope.validDividers[2], multiplier = SmartScope.validMultipliers[2], coefficients = new double[] {0.0636, -0.1111, 3.7572} },
};
List<SmartScope.GainCalibration> calibB = calibA.Select(x => new SmartScope.GainCalibration() { channel = AnalogChannel.ChB, multiplier=x.multiplier,divider=x.divider, coefficients= x.coefficients }).ToList();
scope.rom.clearCalibration();
foreach (SmartScope.GainCalibration c in calibA.Concat(calibB))
scope.rom.setCalibration(c);
scope.rom.Upload();
if (running)
scope.DataSourceScope.Start();
}
private void onDeviceConnect(IDevice scope, bool connected)
{
//Only add memories here, since the scope device is only initialized after the initialization of
//the xnaControl
if (connected)
{
this.device = scope;
this.AddMemories(panelMemories);
}
else
{
this.device = null;
this.ClearMemories(panelMemories);
}
}
private void button1_Click(object sender, EventArgs e)
{
if (!(device is SmartScope)) return;
SmartScope s = (SmartScope)device;
string RomContents = "plug count: " + s.rom.plugCount + "\n\n";
RomContents += "ADC timing: " + s.rom.AdcTimingValue + "\n\n";
foreach(LabNation.DeviceInterface.Devices.SmartScope.GainCalibration c in s.rom.gainCalibration){
RomContents += String.Format("Calib [ch {0}:div {1:#0.0}:mul {2:0.0}] = [{3:0.0000}:{4:0.0000}:{5:0.0000}]\n", c.channel.Value, c.divider, c.multiplier, c.coefficients[0], c.coefficients[1], c.coefficients[2]);
}
MessageBox.Show(RomContents);
}
private void btnReadRom_Click(object sender, EventArgs e)
{
if (!(device is SmartScope)) return;
SmartScope s = (SmartScope)device;
ISmartScopeInterface h = s.HardwareInterface as ISmartScopeInterface;
byte[] result;
uint bytesPerRead = 8;
for(uint i = 0; i < 256; i+=bytesPerRead)
{
h.GetControllerRegister(ScopeController.ROM, i, bytesPerRead, out result);
Logger.Info(String.Format("@{0:X4} : {1}", i, String.Join(",", result.Select(x => x.ToString("X2")))));
}
}
private void btnReadFlash_Click(object sender, EventArgs e)
{
if (!(device is SmartScope)) return;
SmartScope s = (SmartScope)device;
ISmartScopeInterface h = s.HardwareInterface as ISmartScopeInterface;
byte[] result;
uint bytesPerRead = 8;
for (uint i = 0; i < 0x100; i += bytesPerRead)
{
h.GetControllerRegister(ScopeController.FLASH, i, bytesPerRead, out result);
if (result == null)
{
Logger.Error(String.Format("Read nothing @{0:X4}", i));
continue;
}
Logger.Info(String.Format("@{0:X4} : {1}", i, String.Join(",", result.Select(x => x.ToString("X2")))));
}
}
private void btnWriteFlash_Click(object sender, EventArgs e)
{
if (!(device is SmartScope)) return;
SmartScope s = (SmartScope)device;
ISmartScopeInterface h = s.HardwareInterface as ISmartScopeInterface;
uint bytesPerWrite = 11;
for (uint i = 0x0; i < 0x1000; i += bytesPerWrite)
{
byte[] data = new byte[bytesPerWrite];
for (int j = 0; j < data.Length; j++)
{
data[j] = (byte)(i + j);
}
h.SetControllerRegister(ScopeController.FLASH, i, data);
}
}
private void btnWriteRom_Click(object sender, EventArgs e)
{
if (!(device is SmartScope)) return;
SmartScope s = (SmartScope)device;
ISmartScopeInterface h = s.HardwareInterface as ISmartScopeInterface;
byte[] result = new byte[10] { 10,11,12,13,14,15,16,17,18,19};
h.SetControllerRegister(ScopeController.ROM, 10, result);
}
private void btnBootloader_Click(object sender, EventArgs e)
{
if (!(device is SmartScope)) return;
SmartScope s = (SmartScope)device;
s.LoadBootLoader();
}
private void btnReset_Click(object sender, EventArgs e)
{
if (!(device is SmartScope)) return;
SmartScope s = (SmartScope)device;
s.Reset();
}
private void btnFpgaFwVersion_Click(object sender, EventArgs e)
{
if (!(device is SmartScope)) return;
SmartScope s = (SmartScope)device;
MessageBox.Show(String.Format("FPGA FW version: 0x{0:X08}", s.GetFpgaFirmwareVersion()));
}
private void btnAwgTest_Click(object sender, EventArgs e)
{
if (!(device is IWaveGenerator)) return;
IWaveGenerator s = (IWaveGenerator)device;
double[] awgData = new double[2048];
s.GeneratorDataDouble = awgData;
for (int i = 0; i < awgData.Length; i++)
awgData[i] = (double)i / awgData.Length * 3.3;
s.GeneratorDataDouble = awgData;
}
private void txtBoxSerialClosetLocation_TextChanged(object sender, EventArgs e)
{
string serialPartBase36 = ((TextBox)sender).Text;
int serialPartBase10 = (int)Base36.Decode(serialPartBase36);
int boxesPerPigeonHole = 4;
int rows = 5;
int cols = 5;
int boxNr = serialPartBase10 % boxesPerPigeonHole;
int rowNr = (serialPartBase10 / boxesPerPigeonHole) % rows;
int colNr = (serialPartBase10 / (boxesPerPigeonHole * rows)) % cols;
string location = String.Format("Row {0} - Col {1} - Box {2}", rowNr + 1, colNr + 1, boxNr + 1);
labelClosetLocation.Text = location;
}
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
smartScopeApp.Exit();
}
catch { }
}
private void btnShowLog_Click(object sender, EventArgs e)
{
logForm.Show();
}
private void fullscreenify()
{
var s = Screen.AllScreens;
if (s.Count() > 1)
{
this.Location = s[0].WorkingArea.Location;
this.WindowState = FormWindowState.Maximized;
}
}
private void btnReposition_Click(object sender, EventArgs e)
{
fullscreenify();
}
private void FormMain_Shown(object sender, EventArgs e)
{
fullscreenify();
}
private void UpdateAdcTiming(object sender, EventArgs e)
{
int DataTermination = comboDataTermination.SelectedIndex;
int DclkTermination = comboDclkTermination.SelectedIndex;
int DataDelay = (int)numericDataTime.Value;
int DclkDelay = (int)numericDclkTime.Value;
byte timingRegister = (byte)((Math.Sign(DclkDelay) >= 0 ? 0 : 1) << 5);
timingRegister += (byte)((Math.Abs(DclkDelay) & 0x03) << 3);
timingRegister += (byte)((Math.Sign(DataDelay) >= 0 ? 0 : 1) << 2);
timingRegister += (byte)(Math.Abs(DataDelay) & 0x03);
byte terminationRegister = (byte)((DclkTermination & 0x07) << 3);
terminationRegister += (byte)((DataTermination & 0x07));
if (device is SmartScope)
{
SmartScope ss = device as SmartScope;
while(ss.AdcMemory[MAX19506.DATA_CLK_TIMING].Read().GetByte() != timingRegister)
ss.AdcMemory[MAX19506.DATA_CLK_TIMING].WriteImmediate(timingRegister);
while (ss.AdcMemory[MAX19506.CHA_TERMINATION].Read().GetByte() != terminationRegister)
ss.AdcMemory[MAX19506.CHA_TERMINATION].WriteImmediate(terminationRegister);
}
}
}
}
#endif