Repository navigation
Expand file tree
/
Copy pathOperatorConsole.cpp
More file actions
2306 lines (1941 loc) · 84 KB
/
Copy pathOperatorConsole.cpp
File metadata and controls
2306 lines (1941 loc) · 84 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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
* Operator Console - an extensible user interface for the Imatest IT *
* library *
* Copyright (C) 2013 Imatest LLC. *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************/
///
/// OperatorConsole.cpp : Defines the class behaviors for the application.
///
#include "stdafx.h"
#include "Config.h"
#include "imatest_library.h"
#include "imatest_acquisition.h"
#include "OperatorConsole.h"
#include "OperatorConsoleDlg.h"
#include "Setup.h"
#include "PassFail.h"
#include "PasswordDialog.h"
#include "ImatestSourceIDs.h"
#include <sstream>
#include <string>
#include "logger.h"
#include "logger_preferences.h"
#include <locale>
#include "ini_helpers.h"
// This REDIRECT_STDIO causes crashes on 'quit', and I'm not seeing what extra stdout data is displayed in the GUI when it is switched on.
// #define REDIRECT_STDIO // this redirects stdout and stderr to log window using pipes and threads
#define STDIO_DEBUG // turns on some calls printf and cout (for debugging)
#define STDOUT_BUFSIZE 32768 // this is the desired buffer size for the redirected stdout
#define START_TEST_FROM_FRAME_READY // test (blemish or sfrplus) will be started in OnFrameReady() rather than OnStart()
//#define NO_LOG // turns off log messages generated by the COperatorConsoleApp object
#define USE_RGB_DATA // if defined image data is in rgb format, otherwise it's in raw format
//TODO Rename "LOG_FILENAME"
#define LOG_FILENAME ".\\log.txt" // contents of log window will be saved to this file when the program exits
// This CLogger and its associated handler is meant for logging details about the Operator Console's operations, not Imatest IT per se.
static CLogger logger("OperatorConsole");
static CFileLogHandler fileLogHandler(LOGGER_LEVEL, LOG_FILE_NAME); // A file extension is not necessary; *.log is automatically appended.
// COperatorConsoleApp
BEGIN_MESSAGE_MAP(COperatorConsoleApp, CWinApp)
ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()
///
/// Converts the entry<T>.value into a std::string to facilitate writing to the pass/fail file
///
template <typename T> const std::string writeEntValueString(const entry<T>& ent)
{
stringstream ss;
ss << ent.value;
return ss.str();
}
///
/// Converts the entry<CString>.value into a std::string to facilitate writing to the pass/fail file
///
template <> const std::string writeEntValueString(const entry<CString>& ent)
{
stringstream ss;
ss << ent.value.GetString();
return ss.str();
}
///
/// Converts the minMaxEntry<CString>.min_val and .max_val into a std::string to facilitate writing to the pass/fail file
///
template <typename T> const std::string writeMinMaxEntValueString(const minMaxEntry<T>& ent)
{
stringstream ss;
ss << ent.min_val << " " << ent.max_val;
return ss.str();
}
///
/// Converts the vecEntry<CString>.value into a single std::string to facilitate writing to the pass/fail file
///
template <typename T> const std::string writeVecEntValueString(const vecEntry<T>& ent)
{
stringstream ss;
for (std::size_t idx = 0; idx < ent.value.size(); ++idx)
{
ss << ent.value[idx] << " ";
}
return ss.str();
}
///
/// Conditionally adds a particular key (variable name) and value to the std::vector<std::string> keys, vals if the particular key is in use by the pass/fail file
///
void addKeysAndValues(const std::string& candidateKey, const std::string& candidateVal, std::vector<std::string>& keys, std::vector<std::string>& vals, bool b_addKey)
{
if (b_addKey)
{
keys.push_back(candidateKey);
vals.push_back(candidateVal);
}
}
///
/// Adds a data_type string and key string to the std::vector<std::string> data_types and keys, respectively
///
void addDataTypeAndKey(const std::string& candidateDT, const CString& candidateKey, std::vector<std::string>& data_types, std::vector<CString>& keys)
{
data_types.push_back(candidateDT);
keys.push_back(candidateKey);
}
/// COperatorConsoleApp construction
COperatorConsoleApp::COperatorConsoleApp()
{
// support Restart Manager
m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;
// Place all significant initialization in InitInstance
m_acq = NULL;
m_testControl = NULL;
m_test = NULL;
m_fileImage = NULL;
m_cameraImage = NULL;
m_jsonDlgThread = NULL;
m_config = NULL;
memset(&m_flags, 0, sizeof(m_flags));
m_PFSettings.m_ini_file = _T(INI_FILENAME);
m_PFSettings.m_ini_file.Remove('\n');
m_PFSettings.m_ini_file.Remove('\r');
m_password = _T(ADMIN_PASSWORD);
#ifdef NO_AUTHORIZATION
m_passFailIsUnlocked = true; // Keep this initialized to false unless you want to completely disable password-protection for pass/fail settings!
#else
m_passFailIsUnlocked = false; // Keep this initialized to false unless you want to completely disable password-protection for pass/fail settings!
#endif
m_image_source = imatest_source;
m_logFileName = _T(LOG_FILENAME);
#if defined(STDIO_DEBUG)
printf("Hello from constructor [stdout]\n");
cout << "Hello from constructor [cout]" << endl;
#endif
}
COperatorConsoleApp::~COperatorConsoleApp()
{
if (m_fileImage != NULL)
{
delete[] m_fileImage;
}
if (m_cameraImage != NULL)
{
delete[] m_cameraImage;
}
}
/// The one and only COperatorConsoleApp object
COperatorConsoleApp theApp;
/// COperatorConsoleApp initialization
BOOL COperatorConsoleApp::InitInstance()
{
logger.AddHandler(&fileLogHandler);
#if defined(STDIO_DEBUG)
printf("Hello from %s [stdout]\n", __FUNCTION__);
cout << "Hello from" << __FUNCTION__ << "[cout]" << endl;
#endif
//TODO: call AfxInitRichEdit2() to initialize richedit2 library.
// InitCommonControlsEx() is required on Windows XP if an application
// manifest specifies use of ComCtl32.dll version 6 or later to enable
// visual styles. Otherwise, any window creation will fail.
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinApp::InitInstance();
AfxEnableControlContainer();
AfxInitRichEdit(); // this must be called once before displaying a dialog that uses RichEdit control
// Create the shell manager, in case the dialog contains
// any shell tree view or shell list view controls.
CShellManager *pShellManager = new CShellManager;
// Activate "Windows Native" visual manager for enabling themes in MFC controls
CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));
m_pMainWnd = NULL;
InitOutput(); // this sets up a pipe for handling stdout results from the DLLs
#if defined(STDIO_DEBUG)
fprintf(stdout, "Hello from stdout.\n"); // for debugging
fprintf(stderr, "Hello from stderr.\n"); // for debugging
cout << "Hello from cout." << endl; // for debugging
#endif
if (!InitLibs()) // InitLibs must be called before any calls to Imatest library functions
{
cerr << "Unable to initialize the Imatest library." << endl;
SEVERE_LOG(logger, "Unable to initialize the Imatest library.");
}
if (!ReadINISettings()) // if we're unable to read the ini file, we must generate image dimensions from user input before continuing
{
m_setup.width = 640;
m_setup.height = 480;
cout << "INI settings for Operator Console not found." << endl;
cout << "Please click the 'Setup' button and fill in the" << endl;
cout << "appropriate fields before proceeding." << endl;
WARNING_LOG(logger, "INI settings for Operator Console not found. Please click the 'Setup' button and fill in the appropriate fields before proceeding.");
}
if (!ReadPassFail())
{
WARNING_LOG(logger, "Unable to read the pass/fail file.")
}
if (!Init()) // do our application-specific initialization
{
cout << "Application Initialization Failed" << endl;
SEVERE_LOG(logger, "Application Initialization Failed");
Quit(); // if init failed, clean up anything that was done in Init()
}
else
{
COperatorConsoleDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
else if (nResponse == -1)
{
TRACE(traceAppMsg, 0, "Warning: dialog creation failed, so application is terminating unexpectedly.\n");
TRACE(traceAppMsg, 0, "Warning: if you are using MFC controls on the dialog, you cannot #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS.\n");
}
}
Quit(); // This was originally missing, but I think it should be here TDC
CloseLibs();
// Delete the shell manager created above.
if (pShellManager != NULL)
{
delete pShellManager;
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
//////////////////////////////////////////////
///
/// Init perform application-specific initialization
///
/// @params none
///
/// @return value true if successful, false otherwise
///
/// Description
/// Init does the following things:
/// It checks all of the hard coded filenames to be sure that the files exist
/// It initializes the blemish and sfrplus file acquisition objects (loads the raw files into buffers)
/// It initializes the camera object
/// Allocates a buffer to hold a single image frame
/// Creates and starts the blemish and sfrplus threads
/// Creates and starts the modeless dialog thread
/// Initializes the MATLAB and ImatestIT libraries
/// Initializes the blemish and sfrplus modules
///
/// If any of the initialization steps fails, an appropriate message is displayed in an Alert Box.
/////////////////////////////////////////////
bool COperatorConsoleApp::Init()
{
CString errMsg;
bool success = false;
FINEST_LOG(logger, "Entering COperatorConsoleApp::Init().");
#ifdef IMATEST_CAMERA
//m_width = CAMERA_WIDTH;
//m_height = CAMERA_HEIGHT;
#elif !defined FAKE_CAMERA
m_width = CAMERA_WIDTH;
m_height = CAMERA_HEIGHT;
#else
m_width = RAW_WIDTH_BLEMISH;
m_height = RAW_HEIGHT_BLEMISH;
#endif
//
// Initialize the Config object. These values are used in the calls to blemish_shell() and sfrplus_shell().
// Right now they're all hard coded (they're defined in OperatorConsole.h)
//
m_configRAW.Init(m_setup.ini_file, PROGRAMPATH, RAW_EXTENSION, FILE_ROOT, SERIAL_NUMBER, PART_NUMBER, 1);
m_configRGB.Init(m_setup.ini_file, PROGRAMPATH, RGB_EXTENSION, FILE_ROOT, SERIAL_NUMBER, PART_NUMBER, 3);
#if defined USE_RGB_DATA
m_config = &m_configRGB;
#else
m_config = &m_configRAW;
#endif
m_setup.program_path = m_config->m_programPath;
if (!CheckFiles(errMsg)) // make sure that all of the hard coded files exist
{
SEVERE_LOG(logger, "CheckFiles() failed.");
success = false;
}
else if (!InitBlemishAcq()) // loads the raw blemish file into a buffer
{
SEVERE_LOG(logger, "InitBlemishAcq() failed.");
errMsg = m_blemishAcq.GetInfo();
}
else if (!InitSFRplusAcq()) // loads the raw sfrplus file into a buffer
{
SEVERE_LOG(logger, "InitSFRplusAcq failed.");
errMsg = m_sfrPlusAcq.GetInfo();
}
else if (!InitCamera())
{
SEVERE_LOG(logger, "InitCamera() failed.");
errMsg = m_camera->GetInfo();
}
else if (!AllocateImageBuf()) // allocate a buffer large enough to hold an image [must be done after call to m_blemishAcq.Open()]
{
SEVERE_LOG(logger, "AllocateImageBuf() failed.");
errMsg = "Unable to allocate image buffers.";
}
else if (!InitBlemishThread())
{
SEVERE_LOG(logger, "InitBlemishThread() failed.");
errMsg = "Unable to create Blemish thread.";
}
else if (!InitSFRplusThread())
{
SEVERE_LOG(logger, "InitSFRplusThread() failed.");
errMsg = "Unable to create SFRplus thread.";
}
else if (!InitCameraThread())
{
SEVERE_LOG(logger, "InitCameraThread() failed.");
errMsg = "Unable to create Capture thread.";
}
else if (!InitModelessThread())
{
SEVERE_LOG(logger, "InitModelessThread() failed.")
errMsg = "Unable to create thread for modeless dialog.";
}
else
{
OnSetSFRplus(0, 0); // the default test is set to SFRplus
m_status = idle;
success = true;
}
if (!errMsg.IsEmpty())
{
SEVERE_LOG(logger, errMsg);
AfxMessageBox(errMsg);
}
FINEST_LOG(logger, "Exiting COperatorConsoleApp::Init().");
return success;
}
bool COperatorConsoleApp::InitBlemishAcq()
{
bool success = false;
FINEST_LOG(logger, "Entering COperatorConsoleApp::InitBlemishAcq()")
if (m_blemishAcq.Init(RAW_WIDTH_BLEMISH, RAW_HEIGHT_BLEMISH, RAW_BLEMISH_IMAGE))
{
FINE_LOG(logger, "m_blemishAcq.Init succeeded.")
success = m_blemishAcq.Open();
}
FINEST_LOG(logger, "Exiting COperatorConsoleApp::InitBlemishAcq()")
return success;
}
bool COperatorConsoleApp::InitSFRplusAcq()
{
bool success = false;
FINEST_LOG(logger, "Entering COperatorConsoleApp::InitSFRplusAcq()")
if (m_sfrPlusAcq.Init(RAW_WIDTH_SFRPLUS, RAW_HEIGHT_SFRPLUS, RAW_SFRPLUS_IMAGE))
{
success = m_sfrPlusAcq.Open();
}
FINEST_LOG(logger, "Exiting COperatorConsoleApp::InitSFRplusAcq()")
return success;
}
bool COperatorConsoleApp::InitCamera()
{
bool success = false;
FINEST_LOG(logger, "Entering COperatorConsoleApp::InitCamera()");
if (m_image_source == imatest_source)
{
FINE_LOG(logger, "imatest_source is selected.");
m_camera = &m_imatest_cam;
if (m_camera->Init(m_setup.width, m_setup.height, 4))
{
success = m_camera->Open();
m_camera->m_device_ID = m_setup.epiphan_deviceID;
m_camera->m_source_ID = m_setup.sourceID;
m_camera->m_ini_file.assign(m_setup.ini_file);
}
}
//else if (m_image_source == directshow_source)
//{
// FINE_LOG(logger, "directshow_source is selected.");
// m_camera = &m_directshow_cam;
// if (m_camera->Init(m_setup.width, m_setup.height, 4))
// {
// success = m_camera->Open();
// m_camera->m_device_ID = m_setup.epiphan_deviceID;
// m_camera->m_source_ID = m_setup.sourceID;
// m_camera->m_ini_file.assign(m_setup.ini_file);
// }
//}
else if (m_image_source == file_source)
{
// m_camera = &m_file_cam;
// m_camera->Init(IMAGE_NAME);
//success = m_camera->Open();
FINE_LOG(logger, "file_source is selected.");
cerr << "Error! Image file loading not yet implemented." << endl;
SEVERE_LOG(logger, "Error! Image file loading not yet implemented.");
}
//#ifdef IMATEST_CAMERA
// if (m_camera.Init(m_setup.width, m_setup.height, 4))
// {
// success = m_camera.Open();
// m_camera.m_device_ID = m_setup.epiphan_deviceID;
// m_camera.m_source_ID = m_setup.sourceID;
// m_camera.m_ini_file.assign(m_setup.ini_file);
// }
//#elif !defined FAKE_CAMERA
// if (m_camera.Init(CAMERA_WIDTH, CAMERA_HEIGHT, 4))
// {
// success = m_camera.Open();
// }
//#else
// m_camera.Init(IMAGE_NAME);
// success = m_camera.Open();
//#endif
FINEST_LOG(logger, "Exiting COperatorConsoleApp::InitCamera().");
return success;
}
bool COperatorConsoleApp::InitBlemishThread()
{
FINEST_LOG(logger, "Entering COperatorConsoleApp::InitBlemishThread()");
#if defined USE_RGB_DATA
FINE_LOG(logger, "Initializing m_blemish with m_cameraImage.");
m_blemish.Init(m_cameraImage, m_setup.width, m_setup.height, m_config);
#else
FINE_LOG(logger, "Initializing m_blemish with m_fileImage.");
m_blemish.Init(m_fileImage, m_blemishAcq.GetWidth(), m_blemishAcq.GetHeight(), m_config);
#endif
m_flags.blemishThread = m_blemishControl.Init(MSG_BLEMISH_DONE, m_nThreadID, m_blemish.ThreadProc, &m_blemish);
FINEST_LOG(logger, "Exiting COperatorConsoleApp::InitBlemishThread().");
return m_flags.blemishThread;
}
bool COperatorConsoleApp::InitSFRplusThread()
{
FINEST_LOG(logger, "Entering COperatorConsoleApp::InitSFRplusThread()");
#if defined USE_RGB_DATA
FINE_LOG(logger, "Initializing m_sfrPlus with m_cameraImage.");
m_sfrPlus.Init(m_cameraImage, m_setup.width, m_setup.height, m_config);
#else
FINE_LOG(logger, "Initializing m_sfrPlus with m_fileImage.");
m_sfrPlus.Init(m_fileImage, m_sfrPlusAcq.GetWidth(), m_sfrPlusAcq.GetHeight(), m_config);
#endif
m_flags.sfrplusThread = m_sfrPlusControl.Init(MSG_SFRPLUS_DONE, m_nThreadID, m_sfrPlus.ThreadProc, &m_sfrPlus);
FINEST_LOG(logger, "Exiting COperatorConsoleApp::InitSFRplusThread().");
return m_flags.sfrplusThread;
}
bool COperatorConsoleApp::InitCameraThread()
{
FINEST_LOG(logger, "Entering COperatorConsoleApp::InitCameraThread().");
//m_flags.cameraThread = m_cameraControl.Init(MSG_FRAME_READY, m_nThreadID, m_camera.ThreadProc, &m_camera);
m_flags.ImatestCameraThread = m_ImatestCameraControl.Init(MSG_FRAME_READY, m_nThreadID, m_imatest_cam.ThreadProc, &m_imatest_cam);
FINE_LOG(logger, "Sending message to declare imatest_source as the current images source.");
SendAppMessage(MSG_SET_IMATEST_CAM);
FINEST_LOG(logger, "Exiting COperatorConsoleApp::InitCameraThread().");
return m_flags.ImatestCameraThread;
}
bool COperatorConsoleApp::InitLibs()
{
FINEST_LOG(logger, "Entering COperatorConsoleApp::InitLibs().");
CString str;
m_flags.matlab = mclInitializeApplication(NULL, 0); // try to initialize the MATLAB library
if (!m_flags.matlab)
{
SEVERE_LOG(logger, "mclInitializeApplication() call failed.");
str = "Unable to initialize MATLAB library";
}
else
{
//
// Try to initialize Blemish and SFRplus libraries
//
FINE_LOG(logger, "mclInitializeApplication() call succeeded.");
m_flags.imatestIT = imatest_libraryInitialize();
if (!m_flags.imatestIT)
{
SEVERE_LOG(logger, "imatest_libraryInitialize() call failed.");
str = "Unable to initialize imatest library.";
}
else
{
FINE_LOG(logger, "imatest_libraryInitialize() call succeeded.");
}
m_flags.imatestAcq = imatest_acquisitionInitialize();
if (!m_flags.imatestAcq)
{
SEVERE_LOG(logger, "imatest_acquisitionInitialize() call failed.");
str = "Unable to initialize imatest acquisition library.";
}
else
{
FINE_LOG(logger, "imatest_acquisitionInitialize() call succeeded.");
}
}
if (!str.IsEmpty())
{
SEVERE_LOG(logger, str);
AfxMessageBox(str);
}
FINEST_LOG(logger, "Exiting COperatorConsoleApp::InitLibs().");
return m_flags.matlab && m_flags.imatestIT && m_flags.imatestAcq;
}
void COperatorConsoleApp::CloseLibs()
{
FINEST_LOG(logger, "Entering COperatorConsoleApp::CloseLibs().");
if (m_flags.imatestIT)
{
FINE_LOG(logger, "Calling imatest_libraryTerminate().");
imatest_libraryTerminate();
m_flags.imatestIT = false;
}
if (m_flags.imatestAcq)
{
FINE_LOG(logger, "Calling imatest_acquisitionTerminate().");
imatest_acquisitionTerminate();
m_flags.imatestAcq = false;
}
if (m_flags.matlab)
{
FINE_LOG(logger, "Calling mclTerminateApplication().");
mclTerminateApplication(); // terminate MATLAB runtime
m_flags.matlab = false;
}
FINEST_LOG(logger, "Exiting COperatorConsoleApp::CloseLibs().");
}
bool COperatorConsoleApp::InitModelessThread()
{
m_jsonDlgThread = (ModelessDialogThread *)AfxBeginThread(RUNTIME_CLASS(ModelessDialogThread));
m_flags.modelessThread = (m_jsonDlgThread != NULL);
return m_flags.modelessThread;
}
bool COperatorConsoleApp::InitOutput()
{
FINEST_LOG(logger, "Entering COperatorConsoleApp::InitOutput().");
m_flags.stdOut = false;
m_flags.stdErr = false;
#if defined REDIRECT_STDIO
m_stdoutThread.InitThread(STDOUT_BUFSIZE, MSG_STDOUT, m_nThreadID);
m_stderrThread.InitThread(STDOUT_BUFSIZE, MSG_STDERR, m_nThreadID);
m_flags.stdOut = true;
m_flags.stdErr = true;
FINE_LOG(logger, "STDIO redirection enabled.");
#else
FINE_LOG(logger, "STDIO redirection disabled.");
#endif
FINEST_LOG(logger, "Exiting COperatorConsoleApp::InitOutput().");
return m_flags.stdOut && m_flags.stdErr;
}
bool COperatorConsoleApp::AllocateImageBuf()
{
FINEST_LOG(logger, "Entering COperatorConsoleApp::AllocateImageBuf().");
bool fileBufSuccess = AllocateFileImageBuf(m_blemishAcq.BytesPerFrame());
bool imageBufSuccess = AllocateCameraImageBuf(m_camera->BytesPerFrame());
FINEST_LOG(logger, "Exiting COperatorConsoleApp::AllocateImageBuf().");
return fileBufSuccess && imageBufSuccess;
}
bool COperatorConsoleApp::AllocateCameraImageBuf(unsigned int numBytes)
{
FINEST_LOG(logger, "Entering COperatorConsoleApp::AllocateCameraImageBuf().");
m_cameraImageBufLen = numBytes;
if (m_cameraImage != NULL)
{
FINE_LOG(logger, "Deallocating m_cameraImage.");
delete[] m_cameraImage;
}
m_cameraImage = new char[m_cameraImageBufLen];
FINEST_LOG(logger, "Exiting COperatorConsoleApp::AllocateCameraImageBuf().");
return m_cameraImage != NULL;
}
bool COperatorConsoleApp::AllocateFileImageBuf(unsigned int numBytes)
{
FINEST_LOG(logger, "Entering COperatorConsoleApp::AllocateFileImageBuf().");
if (m_fileImage != NULL)
{
FINE_LOG(logger, "Deallocating m_fileImage.");
delete[] m_fileImage;
}
m_fileImage = new char[numBytes];
FINEST_LOG(logger, "Exiting COperatorConsoleApp::AllocateFileImageBuf().");
return m_fileImage != NULL;
}
////////////////////////////////////
///
/// The threads and dialog communicate with the application object
/// by sending messages. This function pulls out the messages of
/// interest to us before they get intercepted by Windows or MFC.
///
/// @return value of TRUE means that we've processed the message
/// @return values of FALSE means that Windows/MFC needs to deal with the message
///
//////////////////////////////////
BOOL COperatorConsoleApp::PreTranslateMessage(MSG* pMsg)
{
BOOL handled = FALSE;
FINEST_LOG(logger, "Entering COperatorConsoleApp::PreTranslateMessage().");
switch (pMsg->message)
{
case MSG_START:
FINE_LOG(logger, "PreTranslateMessage received MSG_START.");
OnStart((WPARAM)0, (LPARAM)0);
handled = TRUE;
break;
case MSG_STOP:
FINE_LOG(logger, "PreTranslateMessage received MSG_STOP.");
OnStop((WPARAM)0, (LPARAM)0);
handled = TRUE;
break;
case MSG_FRAME_READY:
FINE_LOG(logger, "PreTranslateMessage received MSG_FRAME_READY.");
OnFrameReady(pMsg->wParam, pMsg->lParam);
break;
case MSG_RUN_TEST:
FINE_LOG(logger, "PreTranslateMessage received MSG_RUN_TEST.");
OnRunTest((WPARAM)0, (LPARAM)0);
handled = TRUE;
break;
case MSG_BLEMISH_DONE:
FINE_LOG(logger, "PreTranslateMessage received MSG_BLEMISH_DONE.");
OnBlemishDone(pMsg->wParam, pMsg->lParam);
handled = TRUE;
break;
case MSG_SFRPLUS_DONE:
FINE_LOG(logger, "PreTranslateMessage received MSG_SFRPLUS_DONE.");
OnSFRplusDone(pMsg->wParam, pMsg->lParam);
handled = TRUE;
break;
case MSG_SET_BLEMISH:
FINE_LOG(logger, "PreTranslateMessage received MSG_SET_BLEMISH.");
OnSetBlemish(pMsg->wParam, pMsg->lParam);
handled = TRUE;
break;
case MSG_SET_SFRPLUS:
FINE_LOG(logger, "PreTranslateMessage received MSG_SET_SFRPLUS.");
OnSetSFRplus(pMsg->wParam, pMsg->lParam);
handled = TRUE;
break;
case MSG_JSON:
FINE_LOG(logger, "PreTranslateMessage received MSG_JSON.");
OnShowJSON(pMsg->wParam, pMsg->lParam);
handled = TRUE;
break;
case MSG_STDOUT:
FINE_LOG(logger, "PreTranslateMessage received MSG_STDOUT.");
OnUpdateStdout();
break;
case MSG_STDERR:
FINE_LOG(logger, "PreTranslateMessage received MSG_STDERR.");
OnUpdateStderr();
break;
case MSG_SETUP:
FINE_LOG(logger, "PreTranslateMessage received MSG_SETUP.");
OnSetup(pMsg->wParam, pMsg->lParam);
handled = TRUE;
break;
case MSG_PASS_FAIL:
FINE_LOG(logger, "PreTranslateMessage received MSG_PASS_FAIL.");
OnPassFail(pMsg->wParam, pMsg->lParam);
handled = TRUE;
break;
case MSG_SET_IMATEST_CAM:
FINE_LOG(logger, "PreTranslateMessage received MSG_SET_IMATEST_CAM.");
OnSetImatestCamera(pMsg->wParam, pMsg->lParam);
handled = TRUE;
break;
//case MSG_SET_DIRECTSHOW_CAM:
// FINE_LOG(logger, "PreTranslateMessage received MSG_SET_DIRECTSHOW_CAM.");
// OnSetDirectshowCamera(pMsg->wParam, pMsg->lParam);
// handled = TRUE;
// break;
}
FINEST_LOG(logger, "Exiting COperatorConsoleApp::PreTranslateMessage().");
return handled;
}
void COperatorConsoleApp::OnSetBlemish(WPARAM wParam, LPARAM lParam)
{
m_testControl = &m_blemishControl;
m_test = &m_blemish;
m_acq = &m_blemishAcq;
}
void COperatorConsoleApp::OnSetSFRplus(WPARAM wParam, LPARAM lParam)
{
m_testControl = &m_sfrPlusControl;
m_test = &m_sfrPlus;
m_acq = &m_sfrPlusAcq;
}
void COperatorConsoleApp::OnStart(WPARAM wParam, LPARAM lParam) // the Start button was pressed in the dialog
{
if (m_status != runningTest)
{
m_status = runningTest;
SendAppMessage(MSG_RUN_TEST); // send message to app to run test
}
}
void COperatorConsoleApp::OnStop(WPARAM wParam, LPARAM lParam)
{
m_status = idle; // any test in progress will finish, but a new one won't be started
}
void COperatorConsoleApp::OnShowJSON(WPARAM wParam, LPARAM lParam)
{
if (m_jsonDlgThread != NULL)
{
m_jsonDlgThread->SetText(m_results.json);
m_jsonDlgThread->SetTitle(m_results.name);
m_jsonDlgThread->Show(true);
}
}
void COperatorConsoleApp::Quit()
{
///
/// This function gets called when the user wants to Quit the application (either by closing the window or
/// pressing the Quit button). Before we quit, we need to wait for the blemish and sfrplus threads to
/// finish.
///
/// We tell the threads to finish by sending a Quit message to them. However, if a test is in progress,
/// that thread won't process the Quit message until the test finished. This can take a few seconds, so
/// we post a Log message and turn on the Wait cursor until the threads finish.
///
CWaitCursor waitcursor;
m_status = quitting; // this tells us not to run another test
if (m_flags.sfrplusThread)
{
m_sfrPlusControl.Quit(); // wait for the sfrplus thread to quit
m_flags.sfrplusThread = false;
}
if (m_flags.blemishThread)
{
m_blemishControl.Quit(); // wait for the blemish thread to quit
m_flags.blemishThread = false;
}
//if (m_flags.cameraThread)
//{
// m_cameraControl.Quit(); // wait for the camera thread to quit
// m_flags.cameraThread = false;
//}
if (m_flags.ImatestCameraThread)
{
m_ImatestCameraControl.Quit(); // wait for the Imatest library camera thread to quit
m_flags.ImatestCameraThread = false;
}
if (m_flags.modelessThread)
{
::PostThreadMessage(m_jsonDlgThread->m_nThreadID, MSG_MODELESS_QUIT, 0, 0); // tell the json dialog thread to quit
::WaitForSingleObject(m_jsonDlgThread->m_hThread, INFINITE); // wait for it to quit (thread gets deleted automatically)
m_flags.modelessThread = false;
}
#if defined(REDIRECT_STDIO)
if (m_flags.stdOut)
{
m_stdoutThread.Quit(); // close the stdout pipe and wait for the thread to quit
m_flags.stdOut = false;
}
if (m_flags.stdErr)
{
m_stderrThread.Quit(); // close the stderr pipe and wait for the thread to quit
m_flags.stdErr = false;
}
#endif
// TDC 2014/08/07 Calling SaveLog() anywhere in Quit() will NOT copy the contents of
// c_log as c_log.GetWindowText returns an empty string instead of the contents of c_log. Additionally,
// if you use SetSel to set to the entire edit box, the values it returns have nStart > nStop.
//SaveLog(LOG_FILENAME);
}
void COperatorConsoleApp::OnRunTest(WPARAM wParam, LPARAM lParam)
{
CString str;
#if !defined NO_LOG
str.Format("%s started\n", GetTestName());
((COperatorConsoleDlg *)m_pMainWnd)->LogMessage(str);
#endif
#if !defined USE_RGB_DATA
m_acq->GetFrame((void *)m_fileImage); // get the latest image from the Acquisition object (will eventually come from camera
#endif
#ifdef IMATEST_CAMERA
if (m_camera->BytesPerFrame() != m_cameraImageBufLen) {
AllocateCameraImageBuf(m_camera->BytesPerFrame());
m_test->Init(m_cameraImage, m_camera->GetWidth(), m_camera->GetHeight(), m_config);
}
m_camera->GetFrame(m_cameraImage);
m_cameraControl->Run(); // capture 1 frame from the camera (we'll run the test after the frame is captured)
#elif !defined START_TEST_FROM_FRAME_READY
m_testControl->Run(); // tell the thread to run 1 test (it will send our thread a message when it's done)
#else
m_cameraControl->Run(); // capture 1 frame from the camera (we'll run the test after the frame is captured)
#endif
}
void COperatorConsoleApp::OnFrameReady(WPARAM w, LPARAM l)
{
#if defined START_TEST_FROM_FRAME_READY
if (m_camera->BytesPerFrame() != m_cameraImageBufLen) {
AllocateCameraImageBuf(m_camera->BytesPerFrame());
m_test->Init(m_cameraImage, m_camera->GetWidth(), m_camera->GetHeight(), m_config);
}
m_camera->GetFrame(m_cameraImage);
m_testControl->Run(); // tell the thread to run 1 test (it will send our thread a message when it's done)
#else
int width = m_camera->GetWidth();
int height = m_camera->GetHeight();
((COperatorConsoleDlg *)m_pMainWnd)->UpdateImage(m_cameraImage, width, height); // display it in the dialog
m_cameraControl.Run(); // get another frame
#endif
}
void COperatorConsoleApp::OnBlemishDone(WPARAM wParam, LPARAM lParam)
{
if (m_status != quitting)
{
UpdateResults(&m_blemish); // right now blemish doesn't have any special results, so we can just call UpdateResults() directly
if (m_status == runningTest)
{
SendAppMessage(MSG_RUN_TEST); // send message to app to run again
}
}
}
void COperatorConsoleApp::OnSFRplusDone(WPARAM wParam, LPARAM lParam)
{
if (m_status != quitting)
{
UpdateResultsSFRplus(&m_sfrPlus);
if (m_status == runningTest)
{
SendAppMessage(MSG_RUN_TEST); // send message to app to run again
}
}
}
void COperatorConsoleApp::OnUpdateStdout()
{
GetStdoutMsg(m_stdoutThread, m_stdoutStr);
}
void COperatorConsoleApp::OnUpdateStderr()