Newer
Older
PrismSoftware / ECTrainer2 / Worker.cpp
#include "common.h"

Worker::Worker(ECTrainer* pEct) 
	: BaseProcess(pEct)
	, LOG_FILENAME(_T("log.txt"))
	, _AppStatus(APP_STATUS::BOOT)
	, _fpLog(NULL) {

}

bool Worker::Init() {
	if (_tfopen_s(&_fpLog, LOG_FILENAME, _T("w")) != 0) {
		::MessageBox(_pEct->PECTrainerGUI()->GetMainHWnd(), _T("Can't open log file."), _T("Error"), 0);
		return false;
	}
	_ftprintf(_fpLog, _T("time,gazeVx,gazeVy,RR\n"));

	return true;
}

bool Worker::MainLoop() {
	int64 start = cv::getTickCount();
	_AppStatus = APP_STATUS::IDLE;

	while (_pEct->IsAppRun()) {
		// 視線情報更新を待つ
		Sleep(0);
		if (!_pEct->PEyeTrack()->IsNewGazeV()) continue;

		// 状態更新
		if (_AppStatus == APP_STATUS::CALIB) {
			switch (_pEct->PTobiiREST()->GetCalibStatus()) {
			case CALIB_STATUS::DONE:
				_pEct->PStimulus()->SetPage(STIM_PAGE::CALIB_COMPLETE);
				_AppStatus = APP_STATUS::IDLE;
				break;
			case CALIB_STATUS::FAIL:
			case CALIB_STATUS::ERR:
				_pEct->PStimulus()->SetPage(STIM_PAGE::CALIB_FAILED);
				_AppStatus = APP_STATUS::IDLE;
				break;
			}
		}

		// ログ出力
		auto elapse = (cv::getTickCount() - start) * 1000. / cv::getTickFrequency();
		_ftprintf(_fpLog, _T("%.1f"), elapse);
		cv::Point2f gazeV = (_pEct->GetGazeV());
		_ftprintf(_fpLog, _T(",%.1f,%.1f"), gazeV.x, gazeV.y);
		_ftprintf(_fpLog, _T(",%d"), _pEct->PBitalMonitor()->GetRR());
		_ftprintf(_fpLog, _T("\n"));
	}

	fclose(_fpLog);
	return true;
}

bool Worker::StartCalibration() {
	if (_AppStatus != APP_STATUS::IDLE) return false;
	_pEct->PStimulus()->SetPage(STIM_PAGE::CALIB);
	_pEct->PTobiiREST()->StartCalibration();
	_AppStatus = APP_STATUS::CALIB;
	return true;
}