/
Scan_channels.cpp
414 lines (295 loc) · 9.4 KB
/
Scan_channels.cpp
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Scan_channels.cpp : implementation file
//
#include "stdafx.h"
#include "slcontrol.h"
#include "Scan_channels.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
////////////////////////////
// Ken's code starts here //
////////////////////////////
#include "SLControlDlg.h"
#include "dapio32.h"
#include "global_functions.h"
////////////////////////////
// Ken's code ends here //
////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Scan_channels dialog
Scan_channels::Scan_channels(CWnd* pParent /*=NULL*/)
: CDialog(Scan_channels::IDD, pParent)
{
//{{AFX_DATA_INIT(Scan_channels)
force_value_volts = 0.0f;
fl_value_volts = 0.0f;
sl_int_value_volts = 0.0f;
sl_value_volts = 0.0f;
time_seconds = 0.0f;
//}}AFX_DATA_INIT
// Pointer to parent window
m_pParent=pParent;
m_nID=Scan_channels::IDD;
// Variables
char temp_string[200];
// Initialise record
p_Scan_channels_record =& Scan_channels_record;
// Initialise plots
no_of_plots=4;
p_FOR_plot =& FOR_plot;
p_SL_plot =& SL_plot;
p_FL_plot =& FL_plot;
p_SL_INT_plot =& SL_INT_plot;
pointers_array[0]=p_FOR_plot;
pointers_array[1]=p_SL_plot;
pointers_array[2]=p_FL_plot;
pointers_array[3]=p_SL_INT_plot;
// Set file strings
set_executable_directory_string(temp_string);
strcat(temp_string,"\\SCAN\\");
Scan_channels_path_string=temp_string;
dapl_source_file_string=Scan_channels_path_string+"dapl_source.txt";
// Window size and position
window_x_pos=(float)0.2;
window_y_pos=(float)0.2;
window_x_size=(float)0.35;
window_y_size=(float)0.5;
}
Scan_channels::~Scan_channels(void)
{
// Destructor
}
void Scan_channels::PostNcDestroy()
{
delete this;
}
BOOL Scan_channels::Create()
{
return CDialog::Create(m_nID, m_pParent);
}
void Scan_channels::remote_shut_down(void)
{
// Called by parent to shut down window
OnClose();
}
void Scan_channels::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(Scan_channels)
DDX_Text(pDX, IDC_FORCE_VALUE, force_value_volts);
DDX_Text(pDX, IDC_FL_VALUE, fl_value_volts);
DDX_Text(pDX, IDC_SL_INT_VALUE, sl_int_value_volts);
DDX_Text(pDX, IDC_SL_VALUE, sl_value_volts);
DDX_Text(pDX, IDC_TIME, time_seconds);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(Scan_channels, CDialog)
//{{AFX_MSG_MAP(Scan_channels)
ON_WM_PAINT()
ON_WM_TIMER()
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Scan_channels message handlers
BOOL Scan_channels::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
GetDlgItem(IDC_FORCE_VALUE)->EnableWindow(FALSE);
GetDlgItem(IDC_SL_VALUE)->EnableWindow(FALSE);
GetDlgItem(IDC_FL_VALUE)->EnableWindow(FALSE);
GetDlgItem(IDC_SL_INT_VALUE)->EnableWindow(FALSE);
GetDlgItem(IDC_TIME)->EnableWindow(FALSE);
// Install a timer
if (SetTimer(1,250,NULL)==NULL)
{
// Warn user if SetTimer fails
MessageBox("Scan_Channels dialog cannot initiate timer","Scan_channels::OnInitDialog");
}
else
{
// Set Scanning variable
scanning_status=TRUE;
record_point=1;
}
// Plots
CRect work_area;
CRect* p_work_area =& work_area;
SystemParametersInfo(SPI_GETWORKAREA,0,p_work_area,0);
SetWindowPos(NULL,(int)(window_x_pos*work_area.Width()),(int)(window_y_pos*work_area.Height()),
(int)(window_x_size*work_area.Width()),(int)(window_y_size*work_area.Height()),SWP_SHOWWINDOW);
// Create display dc
CClientDC dc(this);
p_display_dc =& dc;
// Calculate window dimensions and pass to plot
CRect current_window;
GetWindowRect(current_window);
current_window.NormalizeRect();
p_FOR_plot->initialise_plot(p_Scan_channels_record,TIME,FORCE,AUTO_SCALE_OFF,
CURSOR_DISPLAY_ON,TEXT_DISPLAY_OFF,
current_window.Width(),current_window.Height(),(float)0.2,(float)0.2,(float)0.5,(float)0.14,
"Scan Points","FOR (V)","Force v Time");
p_SL_plot->initialise_plot(p_Scan_channels_record,TIME,SL,AUTO_SCALE_OFF,
CURSOR_DISPLAY_ON,TEXT_DISPLAY_OFF,
current_window.Width(),current_window.Height(),(float)0.2,(float)0.4,(float)0.5,(float)0.14,
"Scan Points","SL (V)","SL v Time");
p_FL_plot->initialise_plot(p_Scan_channels_record,TIME,FL,AUTO_SCALE_OFF,
CURSOR_DISPLAY_ON,TEXT_DISPLAY_OFF,
current_window.Width(),current_window.Height(),(float)0.2,(float)0.6,(float)0.5,(float)0.14,
"Scan Points","FL (V)","FL v Time");
p_SL_INT_plot->initialise_plot(p_Scan_channels_record,TIME,INTENSITY,AUTO_SCALE_OFF,
CURSOR_DISPLAY_ON,TEXT_DISPLAY_OFF,
current_window.Width(),current_window.Height(),(float)0.2,(float)0.8,(float)0.5,(float)0.14,
"Scan Points","INT (V)","SL_INT v Time");
// Draw default plots
for (int i=0;i<=(no_of_plots-1);i++)
{
pointers_array[i]->draw_plot(p_display_dc,1,p_Scan_channels_record->no_of_points);
}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void Scan_channels::OnClose()
{
// Kill timer
if (KillTimer(1)==0)
{
MessageBox("Timer could not be destroyed","Scan_channels::~Scan_channels");
}
else
{
// MessageBox("Timer destroyed");
}
// Inform parent window
((CSLControlDlg*)m_pParent)->Scan_channels_Window_Done();
// Destroy Window
DestroyWindow();
}
void Scan_channels::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// MessageBox("Painting");
// Do not call CDialog::OnPaint() for painting messages
}
void Scan_channels::OnTimer(UINT nIDEvent)
{
// Code executes on each Timing Event
CDialog::OnTimer(nIDEvent);
// Run single scan if operating
if (scanning_status==TRUE)
{
single_scan();
// Create display dc
CClientDC dc(this);
p_display_dc =& dc;
// Update plots
clear_window();
for (int i=0;i<=(no_of_plots-1);i++)
{
pointers_array[i]->cursor_point[pointers_array[i]->active_cursor]=record_point;
pointers_array[i]->cursor_point[pointers_array[i]->inactive_cursor]=record_point;
pointers_array[i]->draw_plot(p_display_dc,1,p_Scan_channels_record->no_of_points);
}
// Update record point
if (record_point<=p_Scan_channels_record->no_of_points)
{
record_point++; // increment
}
else
{
record_point=1; // return to beginning of trace
}
}
}
void Scan_channels::single_scan(void)
{
// Variables
const int DataCount=4;
short int asiData[DataCount];
HDAP hdapSysGet,hdapSysPut,hdapBinGet; // DAPL communication pipes
int i;
char display_string[200];
// Code - extracts current channel values
// Start clock used to calculate how long scan takes
clock_t start_upload;
start_upload=clock();
// Open and flush DAPL communication pipes
hdapSysGet=DapHandleOpen("\\\\.\\Dap0\\$SysOut",DAPOPEN_READ);
hdapSysPut=DapHandleOpen("\\\\.\\Dap0\\$SysIn",DAPOPEN_WRITE);
hdapBinGet=DapHandleOpen("\\\\.\\Dap0\\$BinOut",DAPOPEN_READ);
// Flush pipes
DapInputFlush(hdapSysGet);
DapInputFlush(hdapBinGet);
DapInputFlush(hdapSysPut);
// Upload file
i=DapConfig(hdapSysPut,dapl_source_file_string);
if (i!=1) // DAPL file error
{
sprintf(display_string,"DAPL could not handle scan_file");
DapLastErrorTextGet(display_string,200);
MessageBox(display_string,"Scan_channels::single_scan()");
}
// Read data
i=DapBufferGet(hdapBinGet,sizeof(asiData),asiData);
if (i!=2*DataCount)
{
sprintf(display_string,"%i bytes read from DAPL",i);
MessageBox(display_string,"Scan_channels::single_scan()");
}
// Update window
p_Scan_channels_record->data[FORCE][record_point]=force_value_volts=convert_DAPL_units_to_volts(asiData[0]);
p_Scan_channels_record->data[SL][record_point]=sl_value_volts=convert_DAPL_units_to_volts(asiData[1]);
p_Scan_channels_record->data[FL][record_point]=fl_value_volts=convert_DAPL_units_to_volts(asiData[2]);
p_Scan_channels_record->data[INTENSITY][record_point]=sl_int_value_volts=convert_DAPL_units_to_volts(asiData[1]);
// Stops clock once scan is complete
time_seconds=((float)(clock()-start_upload)/CLOCKS_PER_SEC);
UpdateData(FALSE);
// Close communication pipes
DapHandleClose(hdapSysGet);
DapHandleClose(hdapSysPut);
DapHandleClose(hdapBinGet);
}
void Scan_channels::clear_window(void)
{
// Code clears plots by drawing over much of the window in the background colour
// Create display dc
CClientDC dc(this);
CClientDC* p_dc =& dc;
// Pen
CPen* pOriginalPen;
CPen BackgroundPen;
BackgroundPen.CreatePen(PS_SOLID,1,GetSysColor(COLOR_BTNFACE));
pOriginalPen = p_dc->SelectObject(&BackgroundPen);
// Background brush
CBrush* pOriginalBrush;
CBrush BackgroundBrush;
BackgroundBrush.CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
pOriginalBrush = p_dc->SelectObject(&BackgroundBrush);
// Overdraw screen
// Calculate window dimensions and pass to plot
CRect current_window;
GetWindowRect(current_window);
current_window.NormalizeRect();
p_dc->Rectangle(1,1,(int)(0.73*current_window.Width()),(int)(0.85*current_window.Height()));
// Return brush and pen
p_dc->SelectObject(pOriginalBrush);
p_dc->SelectObject(pOriginalPen);
}
void Scan_channels::OnCancel()
{
OnClose();
}
BOOL Scan_channels::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
// Traps Escape key presses to prevent dialog box closing
if (pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_ESCAPE)
{
return TRUE;
}
return CDialog::PreTranslateMessage(pMsg);
}