Scope
scope.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 #include "resource.h"
3 #include "gui/MainDlgFrame.h"
4 #include "controllers/ScopeController.h"
5 #include "controllers/ScopeLogger.h"
6 #include "version.h"
7 
10 CAppModule _Module;
11 
12 uint8_t mylutfunc(const double& e) {
13  return (uint8_t)(255*std::sin(e));
14 }
15 
17 int Run(HINSTANCE hInstance) {
18  //LUT_function<> sinus(mylutfunc, M_PI/255);
19  int nRet = -1;
20  MSG msg;
21  // force message queue to be created
22  ::PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
23 
24  CMessageLoop theLoop;
25  _Module.AddMessageLoop(&theLoop);
26 
27  // See if default.xml exists
28  CString filepath = L"default.xml";
29  bool founddefault = false;
30  HANDLE defaultfile = CreateFile(filepath.GetString(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
31 
32  // If no default file found open a dialog for choosing an xml file
33  INT_PTR dlgret = IDCANCEL;
34  if ( defaultfile == INVALID_HANDLE_VALUE ) {
35  COMDLG_FILTERSPEC fileTypes[] = {{ L"Scope XML parameter file", L"*.xml" }};
36  CShellFileOpenDialog dlg(NULL, FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST | FOS_FILEMUSTEXIST, L"xml", fileTypes, 1);
37  dlg.GetPtr()->SetTitle(L"Select Scope XML parameter file");
38  IShellItem *psiFolder;
39  SHCreateItemFromParsingName(L"C:\\Temp\\", NULL, IID_PPV_ARGS(&psiFolder));
40  dlg.GetPtr()->SetFolder(psiFolder);
41  dlgret = dlg.DoModal();
42  if ( IDOK == dlgret )
43  dlg.GetFilePath(filepath);
44  psiFolder->Release();
45  }
46  // Convert file handle to path and close handle
47  else {
48  founddefault = true;
49  std::vector<wchar_t> buff(512);
50  GetFinalPathNameByHandle(defaultfile, buff.data(), 512, FILE_NAME_NORMALIZED);
51  filepath.SetString(buff.data());
52  CloseHandle(defaultfile);
53  }
54 
55  // If we found default.xml or choose a file in the dialog, proceed
56  if ( founddefault || (dlgret == IDOK) ) {
57  DBOUT(L"Filepath " << filepath.GetString());
58 
59  scope::ScopeController scope_controller;
60  scope_controller.Version();
61  // Loads initial parameter set into ScopeInterface's static GuiParameters
62  // Constructs the static ScopeController object, at this point there is only one thread (this one)
63  // that accesses ScopeController (important this instance() is not threadsafe)
64  scope_controller.LoadParameters(std::wstring(filepath.GetString()));
65 
66  // Set the loggers folder to the storage folder configured in the xml file
67 
68  // Sometimes, in debug build an mutex access violation pops up around here, do some cosmetic variations in the code and recompile
69  std::this_thread::sleep_for(std::chrono::milliseconds(20)); // Move this around on debug build mutex bug
70  scope::ScopeLogger logger;
71  logger.SetFilepath(scope_controller.GuiParameters.storage.folder()+L"\\"+GetCurrentDateString());
72 
73 
74  DBOUT(L"Sizeof parameters::Scope " << sizeof(scope_controller.GuiParameters));
75 
76  // Create the main window
78  RECT rec = {20,20,440,980}; // 262x403
79  if(wndMain.CreateEx(HWND(0), rec) == NULL)
80  throw (std::exception("Main window creation failed"));
81 
82  wndMain.ShowWindow(SW_SHOWDEFAULT);
83 
84  // Set main window title to current Scope git commit hash
85  std::wstring revstr = CA2W(STR(LASTGITCOMMIT));
86  revstr = L"Scope (Git commit " + revstr + L")";
87  wndMain.SetWindowText(revstr.c_str());
88 
89  // Log startup scope stuff
90  std::wstring msg(L"This is Scope (Git commit ");
91  msg += CA2W(STR(LASTGITCOMMIT));
92  msg += L")";
93  logger.Log(msg, scope::log_info);
94  msg = L"Using configuration file " + filepath;
95  logger.Log(msg, scope::log_info);
96 
97  // Run the main message loop
98  nRet = theLoop.Run();
99  }
100  // if we did not only display a message box and exit
101  else {
102  ::MessageBox(NULL, L"No default parameter file found and no parameter file choosen!", L"Choose wisely next time", MB_OK | MB_ICONWARNING | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
103  }
104 
105  _Module.RemoveMessageLoop();
106  scope::ScopeLogger log;
107  log.Shutdown();
108  return nRet;
109 }
110 
111 int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow) {
112  // Uncomment these to get a memory dump on exit
113 
114  // Enable CRT debug reporting on assertions
115  //_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
116 
117  // Enable CRT dump memory leaks on exit
118  //_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
119 
120  HRESULT hRes = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
121  // Be sure this succeeds, otherwise e.g. a library (Galil1.lib did this) could have called CoInitialize before!!
122  ATLASSERT(SUCCEEDED(hRes));
123 
124  // Just to be sure we do not have problems because someone tries to run this on XP...
125  assert( RunTimeHelper::IsWin7() );
126 
127  // this resolves ATL window thunking problem when Microsoft Layer for Unicode (MSLU) is used
128  ::DefWindowProc(HWND(NULL), 0, 0, 0L);
129 
130  // Initialize common controls (do not forget to link, see stdafx.h)
131  AtlInitCommonControls(ICC_COOL_CLASSES | ICC_BAR_CLASSES | ICC_USEREX_CLASSES | ICC_STANDARD_CLASSES); // add flags to support other controls
132 
133  hRes = _Module.Init(NULL, hInstance);
134  assert(SUCCEEDED(hRes));
135 
136  // Load library for RichEdit Control
137  HMODULE hInstRich = ::LoadLibrary(CRichEditCtrl::GetLibraryName());
138  assert(hInstRich != NULL);
139 
140  int32_t nRet = Run(hInstance);
141 
142  // Unload RichEdit library
143  ::FreeLibrary(hInstRich);
144 
145  //ReportLiveObjects();
146 
147  _Module.Term();
148  // Uninitialize COM
149  ::CoUninitialize();
150 
151  // And goodbye...
152  return nRet;
153 }
Main controller of microscope hardware and acquisition, also interface to the GUI.
The main application dialog frame.
Definition: MainDlgFrame.h:20
void SetFilepath(const std::wstring &_filepath=L"C:\\ScopeData")
Sets the filepath and creates logfile.
Definition: ScopeLogger.cpp:22
A logger class to log various messages and user comments.
Definition: ScopeLogger.h:24
int Run(HINSTANCE hInstance)
Run the main message loop.
Definition: scope.cpp:17
This is the include file for standard system include files, or project specific include files that ar...
void Log(const std::wstring &message, const log_message_type &msgtype)
Logs a message.
Definition: ScopeLogger.cpp:26
std::wstring GetCurrentDateString()
Definition: helpers.cpp:4
void Shutdown()
Calls ScopeLogger::Impl::Shutdown.
Definition: ScopeLogger.cpp:50
#define DBOUT(s)
A debug output to the debug console.
Definition: helpers.h:153
CAppModule _Module
The ATL application module.
Definition: scope.cpp:10
void Version()
Print the current ScopeController version to debug console.