diff --git a/.gitignore b/.gitignore index 268f074..abfc536 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ log/ -ECTrainer2/log.txt movies/ voices/ packages/ +ECTrainer2/Log*.txt +ECTrainer2/Log*.csv diff --git a/ECTrainer2/ECTrainer.cpp b/ECTrainer2/ECTrainer.cpp index 8a9461e..bedcf71 100644 --- a/ECTrainer2/ECTrainer.cpp +++ b/ECTrainer2/ECTrainer.cpp @@ -10,6 +10,7 @@ #include "BitalMonitor.h" #include "Worker.h" #include "KeepAlive.h" +#include "myWinUtils.h" // �R���X�g���N�^ diff --git a/ECTrainer2/ECTrainer.h b/ECTrainer2/ECTrainer.h index d31b960..2335e33 100644 --- a/ECTrainer2/ECTrainer.h +++ b/ECTrainer2/ECTrainer.h @@ -2,7 +2,8 @@ #include #include -#include "myOpenCV.h" +#include +//#include "myOpenCV.h" #define ADDR "192.168.71.50" //#define EYEDEVICE_GLASS2 @@ -24,17 +25,16 @@ enum class ECTMSG { QUIT = WM_QUIT, CALIB_START = WM_USER, - CALIB_END, + CALIB_OK, + CALIB_FAILED, + CALIB_ERR, + STIM_START, + STIM_STOP, }; // �A�v���P�[�V������� enum class APP_STATUS { - BOOT, IDLE, CALIB, EXEC -}; - -// �L�����u���[�V�������� -enum class CALIB_STATUS { - DONE, FAIL, ERR + BOOT, IDLE, CALIB, STIM }; // �h���y�[�W diff --git a/ECTrainer2/ECTrainerGUI.cpp b/ECTrainer2/ECTrainerGUI.cpp index b086ad8..6f4736d 100644 --- a/ECTrainer2/ECTrainerGUI.cpp +++ b/ECTrainer2/ECTrainerGUI.cpp @@ -158,14 +158,15 @@ //fStimulus = true; //fKeepContact = false; //if (!PlayMovie()) _pEct->Stop(); - _pEct->PWorker()->StartStim(); + ((BaseProcess*)_pEct->PWorker())->Tell(ECTMSG::STIM_START); } break; - case APP_STATUS::EXEC: + case APP_STATUS::STIM: if (cvui::button(140, 30, "STOP")) { if (!StopMovie()) _pEct->StopApp(); //fKeepContact = false; - _pEct->PWorker()->StopStim(); + ((BaseProcess*)_pEct->PWorker())->Tell(ECTMSG::STIM_STOP); + //_pEct->PWorker()->StopStim(); //fStimulus = false; } //cvui::trackbar(140, &targetSize, (float)0, (float)3.0); diff --git a/ECTrainer2/MyWinUtils.h b/ECTrainer2/MyWinUtils.h index 83523ea..70c35b5 100644 --- a/ECTrainer2/MyWinUtils.h +++ b/ECTrainer2/MyWinUtils.h @@ -5,9 +5,6 @@ #include #include -#define SAFE_DELETE(x) if (x) { delete x; x = NULL; } -#define SAFE_DELETE_ARRAY(x) if (x) { delete [] x; x = NULL; } - namespace mwut { typedef std::vector> STR_TABLE; diff --git a/ECTrainer2/Stimulus.cpp b/ECTrainer2/Stimulus.cpp index 7000490..651d6d3 100644 --- a/ECTrainer2/Stimulus.cpp +++ b/ECTrainer2/Stimulus.cpp @@ -1,14 +1,25 @@ -#include "common.h" -#include +//#include "common.h" +#include "BaseProcess.h" +#include "Stimulus.h" +#include "ECTrainer.h" +#include "ECTrainerGUI.h" // �폜�\�� +#include "MovieObject.h" +#include "myWinUtils.h" +//#include #include // �R���X�g���N�^ -Stimulus::Stimulus(ECTrainer* pEct, Marker* pMarker) - :BaseProcess(pEct) +Stimulus::Stimulus(ECTrainer* pEct, Marker* pMarker) : BaseProcess(pEct) , _pMarker(pMarker) , _StimNo(-1) , _StartTime(0) + , _pMovieObject(NULL) { + _pMovieObject = new MovieObject; +} + +Stimulus::~Stimulus() { + mwut::SafeDelete((void**)&_pMovieObject); } // ������ @@ -18,21 +29,21 @@ std::ifstream ifs(DATA_FILE); if (!ifs) { - OutputDebugString(_T("Can't open stim data file.")); + mwut::DebugPrintf(_T("Can't open stim data file.")); return false; } while (!ifs.eof()) { StimInfo st; ifs >> st.type >> st.filename >> st.dulation >> st.csvfile >> st.target; - std::cout << st.type << "," << st.filename << "," << st.dulation << "," - << st.csvfile << "," << st.target << std::endl; + //std::cout << st.type << "," << st.filename << "," << st.dulation << "," + // << st.csvfile << "," << st.target << std::endl; if (st.filename.size() > 0) _StimInfoSet.push_back(st); } return true; } -// ���[�v +// ��{���� bool Stimulus::Routine() { if (_StimNo >= 0) { @@ -54,6 +65,34 @@ return true; } +// �C�x���g���� +bool Stimulus::EventProc(MSG& msg) { + switch (msg.message) { + case (int)ECTMSG::CALIB_START: // �L�����u���[�V�����J�n + _DispBuffer = cv::imread(CALIB_FILE); + _pEct->PECTrainerGUI()->SetDispBuffer(_DispBuffer); + break; + + case (int)ECTMSG::CALIB_OK: // �L�����u���[�V�������� + _DispBuffer = cv::imread(CALIB_COMPLETE_FILE); + _pEct->PECTrainerGUI()->SetDispBuffer(_DispBuffer); + break; + + case (int)ECTMSG::CALIB_FAILED: // �L�����u���[�V�������s + case (int)ECTMSG::CALIB_ERR: // �L�����u���[�V�����G���[ + _DispBuffer = cv::imread(CALIB_FAILED_FILE); + _pEct->PECTrainerGUI()->SetDispBuffer(_DispBuffer); + break; + + case (int)ECTMSG::STIM_START: // �����J�n + break; + + case (int)ECTMSG::STIM_STOP: // ������~ + break; + } + return true; +} + // �h���� bool Stimulus::SetStimulus() { if (_StimNo < 0 || _StimNo >= _StimInfoSet.size()) return false; @@ -83,18 +122,18 @@ _DispBuffer = cv::imread(OPENING_FILE); _pEct->PECTrainerGUI()->SetDispBuffer(_DispBuffer); break; - case STIM_PAGE::CALIB: - _DispBuffer = cv::imread(CALIB_FILE); - _pEct->PECTrainerGUI()->SetDispBuffer(_DispBuffer); - break; - case STIM_PAGE::CALIB_COMPLETE: - _DispBuffer = cv::imread(CALIB_COMPLETE_FILE); - _pEct->PECTrainerGUI()->SetDispBuffer(_DispBuffer); - break; - case STIM_PAGE::CALIB_FAILED: - _DispBuffer = cv::imread(CALIB_FAILED_FILE); - _pEct->PECTrainerGUI()->SetDispBuffer(_DispBuffer); - break; + //case STIM_PAGE::CALIB: + // _DispBuffer = cv::imread(CALIB_FILE); + // _pEct->PECTrainerGUI()->SetDispBuffer(_DispBuffer); + // break; + //case STIM_PAGE::CALIB_COMPLETE: + // _DispBuffer = cv::imread(CALIB_COMPLETE_FILE); + // _pEct->PECTrainerGUI()->SetDispBuffer(_DispBuffer); + // break; + //case STIM_PAGE::CALIB_FAILED: + // _DispBuffer = cv::imread(CALIB_FAILED_FILE); + // _pEct->PECTrainerGUI()->SetDispBuffer(_DispBuffer); + // break; case STIM_PAGE::WHITE: _DispBuffer = cv::imread(WHITE_FILE); _pEct->PECTrainerGUI()->SetDispBuffer(_DispBuffer); @@ -125,8 +164,8 @@ // CSV�ǂݍ��� void Stimulus::ReadCsv() { - if (_MovieObject.GetNumFrames() > 0) return; // �ǂݍ��ݍς� - int nFrames = _MovieObject.SetData(_StimInfoSet[_StimNo].csvfile); + if (_pMovieObject->GetNumFrames() > 0) return; // �ǂݍ��ݍς� + int nFrames = _pMovieObject->SetData(_StimInfoSet[_StimNo].csvfile); mwut::DebugPrintf(_T("CSV file '%s' - %d frames read.\n"), mwut::Multi2Wide(_StimInfoSet[_StimNo].csvfile).c_str(), nFrames); diff --git a/ECTrainer2/Stimulus.h b/ECTrainer2/Stimulus.h index 5a18846..6e7776d 100644 --- a/ECTrainer2/Stimulus.h +++ b/ECTrainer2/Stimulus.h @@ -1,14 +1,15 @@ #pragma once -#include "myOpenCV.h" -#include "MovieObject.h" -#include "BaseProcess.h" #include #include #include #include +#include "myOpenCV.h" +class BaseProcess; class Marker; +class MovieObject; +enum class STIM_PAGE; struct StimInfo { int type; // 1:�摜 2:���� @@ -31,18 +32,25 @@ Marker* _pMarker; cv::Mat _DispBuffer; std::vector _StimInfoSet; - MovieObject _MovieObject; + MovieObject* _pMovieObject; int _StimNo; // �h���f�[�^�ԍ��@-1:��~ DWORD _StartTime; // �h���񎦊J�n���� //void StimWithMarker(); bool SetStimulus(); void ReadCsv(); + // ��{���� + bool Routine(); + // �C�x���g���� + bool EventProc(MSG& msg); public: + // �R���X�g���N�^ Stimulus(ECTrainer* pEct, Marker* pMarker); + // �f�X�g���N�^ + ~Stimulus(); + // ������ bool Init(); - bool Routine(); void SetPage(STIM_PAGE st); //cv::Point2f GetEyeR() { return _StimImages[_StimNo].eyes[0]; } //cv::Point2f GetEyeL() { return _StimImages[_StimNo].eyes[1]; } diff --git a/ECTrainer2/TobiiREST.cpp b/ECTrainer2/TobiiREST.cpp index 27c0734..e96edb6 100644 --- a/ECTrainer2/TobiiREST.cpp +++ b/ECTrainer2/TobiiREST.cpp @@ -121,14 +121,14 @@ values.push_back(_T("calibrated")); std::wstring status = REST_Handler::WaitForStatus(url.c_str(), _T("ca_state"), values); if (status == _T("failed")) { - ((BaseProcess*)_pEct->PWorker())->Tell(ECTMSG::CALIB_END, (int)CALIB_STATUS::FAIL); + ((BaseProcess*)_pEct->PWorker())->Tell(ECTMSG::CALIB_FAILED); mwut::DebugPrintf(_T("Calibration failed\n")); } else { - ((BaseProcess*)_pEct->PWorker())->Tell(ECTMSG::CALIB_END, (int)CALIB_STATUS::DONE); + ((BaseProcess*)_pEct->PWorker())->Tell(ECTMSG::CALIB_OK); mwut::DebugPrintf(_T("Calibration successful\n")); } #else - ((BaseProcess*)_pEct->PWorker())->Tell(ECTMSG::CALIB_END, (int)CALIB_STATUS::DONE); + ((BaseProcess*)_pEct->PWorker())->Tell(ECTMSG::CALIB_OK); mwut::DebugPrintf(_T("Calibration skipped\n")); #endif return true; @@ -136,7 +136,7 @@ catch (const std::exception & e) { UNREFERENCED_PARAMETER(e); // e.what() ���}���`�o�C�g������Ȃ̂Ŏg���Ȃ� - ((BaseProcess*)_pEct->PWorker())->Tell(ECTMSG::CALIB_END, (int)CALIB_STATUS::ERR); + ((BaseProcess*)_pEct->PWorker())->Tell(ECTMSG::CALIB_ERR); mwut::DebugPrintf(_T("Error in TobiiREST::QueryCalibResult()\n")); return false; } diff --git a/ECTrainer2/Worker.cpp b/ECTrainer2/Worker.cpp index e9a810e..f9f6cff 100644 --- a/ECTrainer2/Worker.cpp +++ b/ECTrainer2/Worker.cpp @@ -1,4 +1,4 @@ -//#include "common.h" +#include "BaseProcess.h" #include "Worker.h" #include "ECTrainer.h" #include "EyeTrack.h" @@ -10,7 +10,8 @@ // �R���X�g���N�^ Worker::Worker(ECTrainer* pEct) : BaseProcess(pEct) , _AppStatus(APP_STATUS::BOOT) - , _fpLog(NULL) + , _fpLogData(NULL) + , _fpLogEvent(NULL) , _pTimer(NULL) { @@ -18,16 +19,23 @@ // �f�X�g���N�^ Worker::~Worker() { - if (_fpLog) fclose(_fpLog); + if (_fpLogData) fclose(_fpLogData); + if (_fpLogEvent) fclose(_fpLogEvent); mwut::SafeDelete((void**)&_pTimer); } +// ������ bool Worker::Init() { - if (_tfopen_s(&_fpLog, LOG_FILENAME.c_str(), _T("w")) != 0) { - _pEct->MsgBox(_T("Can't open log file."), MB_ICONERROR); + if (_tfopen_s(&_fpLogData, DATA_LOG_FILE, _T("w")) != 0) { + _pEct->MsgBox(_T("Can't open data log file."), MB_ICONERROR); return false; } - _ftprintf(_fpLog, _T("time,gazeVx,gazeVy,H11,H12,H13,H21,H22,H23,H31,H32,H33,RR\n")); + _ftprintf(_fpLogData, _T("time,gazeVx,gazeVy,H11,H12,H13,H21,H22,H23,H31,H32,H33,RR\n")); + + if (_tfopen_s(&_fpLogEvent, EVENT_LOG_FILE, _T("w")) != 0) { + _pEct->MsgBox(_T("Can't open event log file."), MB_ICONERROR); + return false; + } _AppStatus = APP_STATUS::IDLE; _pTimer = new mwut::HPTimer(); @@ -42,7 +50,7 @@ Sleep(0); if (!_pEct->PEyeTrack()->IsNewGazeV()) return true; - this->WriteLog(); + this->DataLog(); return true; } @@ -50,67 +58,71 @@ // �C�x���g���� bool Worker::EventProc(MSG& msg) { switch (msg.message) { - case (int)ECTMSG::CALIB_START: + case (int)ECTMSG::CALIB_START: // �L�����u���[�V�����J�n if (_AppStatus == APP_STATUS::IDLE) { - _pEct->PStimulus()->SetPage(STIM_PAGE::CALIB); - ((BaseProcess*)_pEct->PTobiiREST())->Tell(ECTMSG::CALIB_START); _AppStatus = APP_STATUS::CALIB; - this->WriteLog(_T("Calibration start.")); + ((BaseProcess*)_pEct->PStimulus())->Tell(ECTMSG::CALIB_START); + //_pEct->PStimulus()->SetPage(STIM_PAGE::CALIB); + ((BaseProcess*)_pEct->PTobiiREST())->Tell(ECTMSG::CALIB_START); + this->EventLog(_T("Calibration start.")); } break; - case (int)ECTMSG::CALIB_END: - if (msg.wParam == (int)CALIB_STATUS::DONE) { - _pEct->PStimulus()->SetPage(STIM_PAGE::CALIB_COMPLETE); - this->WriteLog(_T("Calibration complete.")); - } else { - _pEct->PStimulus()->SetPage(STIM_PAGE::CALIB_FAILED); - this->WriteLog(_T("Calibration failed.")); - } + case (int)ECTMSG::CALIB_OK: // �L�����u���[�V�������� _AppStatus = APP_STATUS::IDLE; + ((BaseProcess*)_pEct->PStimulus())->Tell(ECTMSG::CALIB_OK); + //_pEct->PStimulus()->SetPage(STIM_PAGE::CALIB_COMPLETE); + this->EventLog(_T("Calibration complete.")); break; - } - return true; -} -// �^�X�N�J�n -bool Worker::StartStim() { - if (_AppStatus != APP_STATUS::IDLE) return false; - _AppStatus = APP_STATUS::EXEC; - _pEct->PStimulus()->SetPage(STIM_PAGE::START); - return true; -} + case (int)ECTMSG::CALIB_FAILED: // �L�����u���[�V�������s + case (int)ECTMSG::CALIB_ERR: // �L�����u���[�V�����G���[ + _AppStatus = APP_STATUS::IDLE; + ((BaseProcess*)_pEct->PStimulus())->Tell(ECTMSG::CALIB_FAILED); + //_pEct->PStimulus()->SetPage(STIM_PAGE::CALIB_FAILED); + this->EventLog(_T("Calibration failed.")); + break; -// �^�X�N��~ -bool Worker::StopStim() { - if (_AppStatus != APP_STATUS::EXEC) return false; - _pEct->PStimulus()->SetPage(STIM_PAGE::STOP); - _AppStatus = APP_STATUS::IDLE; - return true; -} - -// ���O�o�� -void Worker::WriteLog(const TCHAR* msg) { - _mtxLog.lock(); - - _ftprintf(_fpLog, _T("%.1f"), _pTimer->Elapse()); - - if (msg) { - _ftprintf(_fpLog, msg); - } else { - cv::Point2f gazeV = (_pEct->PEyeTrack()->GetGazeV()); - _ftprintf(_fpLog, _T(",%.1f,%.1f"), gazeV.x, gazeV.y); - cv::Mat h = _pEct->PMarker()->GetHomography(); - if (!h.empty() && h.rows == 3 && h.cols == 3) { - double* ptr = h.ptr(0); - for (int i = 0; i < 9; i++) { - _ftprintf(_fpLog, _T(",%lf"), *(ptr + i)); - } + case (int)ECTMSG::STIM_START: // �����J�n + if (_AppStatus == APP_STATUS::IDLE) { + _AppStatus = APP_STATUS::STIM; + _pEct->PStimulus()->SetPage(STIM_PAGE::START); + this->EventLog(_T("Stimulation start.")); } - - _ftprintf(_fpLog, _T(",%d"), _pEct->PBitalMonitor()->GetRR()); - _ftprintf(_fpLog, _T("\n")); + break; + + case (int)ECTMSG::STIM_STOP: // ������~ + if (_AppStatus == APP_STATUS::STIM) { + _pEct->PStimulus()->SetPage(STIM_PAGE::STOP); + _AppStatus = APP_STATUS::IDLE; + this->EventLog(_T("Stimulation stop.")); + } + break; + + } + return true; +} + +// �f�[�^���O�o�� +void Worker::DataLog() { + _ftprintf(_fpLogData, _T("%.1f"), _pTimer->Elapse()); + + cv::Point2f gazeV = (_pEct->PEyeTrack()->GetGazeV()); + _ftprintf(_fpLogData, _T(",%.1f,%.1f"), gazeV.x, gazeV.y); + cv::Mat h = _pEct->PMarker()->GetHomography(); + if (!h.empty() && h.rows == 3 && h.cols == 3) { + double* ptr = h.ptr(0); + for (int i = 0; i < 9; i++) { + _ftprintf(_fpLogData, _T(",%lf"), *(ptr + i)); + } } - _mtxLog.unlock(); + _ftprintf(_fpLogData, _T(",%d"), _pEct->PBitalMonitor()->GetRR()); + _ftprintf(_fpLogData, _T("\n")); +} + +// �C�x���g���O�o�� +void Worker::EventLog(const TCHAR* msg) { + _ftprintf(_fpLogEvent, _T("%.1f"), _pTimer->Elapse()); + _ftprintf(_fpLogEvent, _T(" %s\n"), msg); } diff --git a/ECTrainer2/Worker.h b/ECTrainer2/Worker.h index 31506c9..dad1e55 100644 --- a/ECTrainer2/Worker.h +++ b/ECTrainer2/Worker.h @@ -1,28 +1,37 @@ #pragma once -#include "BaseProcess.h" #include #include #include -#include -#include namespace mwut { class HPTimer; }; +class BaseProcess; +class ECTrainer; enum class APP_STATUS; +// ���[�J�[�N���X +// �A�v���P�[�V�����̏�ԊǗ� +// �A�C�R���^�N�g���� +// ���O�o�� class Worker : public BaseProcess { - const std::wstring LOG_FILENAME = _T("log.txt"); - APP_STATUS _AppStatus; - FILE* _fpLog; - mwut::HPTimer* _pTimer; - std::mutex _mtxLog; + const TCHAR* DATA_LOG_FILE = _T("LogData.csv"); + const TCHAR* EVENT_LOG_FILE = _T("LogEvent.txt"); + APP_STATUS _AppStatus; // �A�v���P�[�V������� + FILE* _fpLogData; // �f�[�^���O�t�@�C���|�C���^ + FILE* _fpLogEvent; // �C�x���g���O�t�@�C���|�C���^ + mwut::HPTimer* _pTimer; // ���O�p�̃^�C�}�[ - void WriteLog(const TCHAR* msg = NULL); + // �f�[�^���O�o�� + void DataLog(); + // �C�x���g���O�o�� + void EventLog(const TCHAR* msg); + // ��{���� bool Routine(); + // �C�x���g���� bool EventProc(MSG& msg); public: @@ -30,8 +39,8 @@ Worker(ECTrainer* pEct); // �f�X�g���N�^ ~Worker(); + // ������ bool Init(); - bool StartStim(); - bool StopStim(); + // �A�v���P�[�V������Ԃ̎擾 APP_STATUS GetAppStatus() { return _AppStatus; } };