Commit 9f072a03 authored by Thiago Santini's avatar Thiago Santini

Refactoring global variables to their own file

parent e6f80d2f
......@@ -57,7 +57,8 @@ SOURCES +=\
$${TOP}/src/pupil-tracking/PuReST.cpp \
$${TOP}/src/pupil-detection/PuRe.cpp \
$${TOP}/src/pupil-tracking/PupilTrackingMethod.cpp \
$${TOP}/src/ocv_utils.cpp
$${TOP}/src/ocv_utils.cpp \
$${TOP}/src/globals.cpp
HEADERS += \
$${TOP}/src/MainWindow.h\
......@@ -91,7 +92,8 @@ HEADERS += \
$${TOP}/src/pupil-tracking/PupilTrackingMethod.h \
$${TOP}/src/pupil-detection/PuRe.h \
$${TOP}/src/pupil-tracking/PuReST.h \
$${TOP}/src/ocv_utils.h
$${TOP}/src/ocv_utils.h \
$${TOP}/src/globals.h
FORMS += \
$${TOP}/src/MainWindow.ui \
......
#include "Camera.h"
#include <QFileInfo>
#include "globals.h"
static int gQCameraInfoMetaTypeId = qRegisterMetaType<QCameraInfo>("QCameraInfo");
static int gMatMetaTypeId = qRegisterMetaType<cv::Mat>("cv::Mat");
static int gTimestampMetaTypeId = qRegisterMetaType<Timestamp>("Timestamp");
......
......@@ -21,7 +21,6 @@
#include "FrameGrabber.h"
class CameraUI : public QDialog
{
Q_OBJECT
......
......@@ -3,6 +3,8 @@
#include <QSettings>
#include <QDebug>
#include "globals.h"
// TODO: make these keys configurable
CommandManager::CommandManager(QObject *parent)
......
......@@ -36,6 +36,7 @@
#include "CameraCalibration.h"
#include "globals.h"
#include "utils.h"
#include "ocv_utils.h"
......
......@@ -25,6 +25,7 @@
#include "InputWidget.h"
#include "globals.h"
#include "utils.h"
#include "ocv_utils.h"
......
#include "FrameGrabber.h"
#include "globals.h"
using namespace std;
using namespace cv;
......
......@@ -153,10 +153,10 @@ GazeEstimationWidget::GazeEstimationWidget(QString id, QWidget *parent) :
QMetaObject::invokeMethod(gazeEstimation, "updateConfig");
// Load sound effects
loadSoundEffect(startSound, "cal-start.wav");
loadSoundEffect(successSound, "cal-success.wav");
loadSoundEffect(failureSound, "cal-failure.wav");
loadSoundEffect(collectedSound, "cal-collected.wav");
loadSoundEffect("cal-start.wav", gExeDir, startSound);
loadSoundEffect("cal-success.wav", gExeDir, successSound);
loadSoundEffect("cal-failure.wav", gExeDir, failureSound);
loadSoundEffect("cal-collected.wav", gExeDir, collectedSound);
}
GazeEstimationWidget::~GazeEstimationWidget()
......
......@@ -133,8 +133,8 @@ MainWindow::MainWindow(QWidget *parent) :
connect(this, SIGNAL(stopRecording()),
journal, SIGNAL(stopRecording()) );
loadSoundEffect(recStartSound, "rec-start.wav");
loadSoundEffect(recStopSound, "rec-stop.wav");
loadSoundEffect("rec-start.wav", gExeDir, recStartSound);
loadSoundEffect("rec-stop.wav", gExeDir, recStopSound);
setupWidget(this, settings);
......
#include "PerformanceMonitorWidget.h"
#include "ui_PerformanceMonitorWidget.h"
#include "globals.h"
using namespace std;
PerformanceMonitorWidget::PerformanceMonitorWidget(QString id, QWidget *parent) :
......
#include "globals.h"
bool gCalibrating = false;
bool gFreezePreview = false;
LogWidget *gLogWidget = NULL;
// Initialized on startup
QElapsedTimer gTimer;
QString gExeDir;
QString gCfgDir;
PerformanceMonitor gPerformanceMonitor;
// Configurable
char gDataSeparator = '\t';
char gDataNewline = '\n';
#ifndef GLOBALS_H
#define GLOBALS_H
#include <QElapsedTimer>
#include <QMutex>
#include "LogWidget.h"
#include "PerformanceMonitor.h"
// Time reference
extern QElapsedTimer gTimer;
// Executable directory
extern QString gExeDir;
// Settings directory
extern QString gCfgDir;
// Separator and newline for data files
extern char gDataSeparator;
extern char gDataNewline;
// Logging
extern LogWidget *gLogWidget;
// Performance monitor
extern PerformanceMonitor gPerformanceMonitor;
// True when the gaze estimation is being calibrated -- e.g., to activate marker detection
extern bool gCalibrating;
// Freeze the previews, useful for debugging
extern bool gFreezePreview;
#endif // GLOBALS_H
#include "utils.h"
/*
* Global variables
*/
QElapsedTimer gTimer;
const Timestamp maxTimestamp = std::numeric_limits<qint64>::max();
QString gExeDir;
QString gCfgDir;
char gDataSeparator = '\t';
char gDataNewline = '\n';
QMutex gLogMutex;
QFile gLogFile;
QTextStream gLogStream;
LogWidget *gLogWidget = NULL;
std::vector<QString> gLogBuffer;
PerformanceMonitor gPerformanceMonitor;
bool gCalibrating = false;
bool gFreezePreview = false;
static QTextStream logStream;
/*
* Utility functions
......@@ -38,10 +16,11 @@ QDebug operator<<(QDebug dbg, const QCameraViewfinderSettings &p)
void logInitBanner()
{
if (!gLogFile.isOpen()) {
gLogFile.setFileName("EyeRecToo.log");
gLogFile.open(QIODevice::WriteOnly|QIODevice::Append);
gLogStream.setDevice(&gLogFile);
static QFile logFile;
if (!logFile.isOpen()) {
logFile.setFileName("EyeRecToo.log");
logFile.open(QIODevice::WriteOnly|QIODevice::Append);
logStream.setDevice(&logFile);
}
QDateTime utc = QDateTime::currentDateTimeUtc();
......@@ -61,33 +40,35 @@ void logExitBanner()
<< "\n######################################################################\n";
std::cout.flush();
gLogStream.flush();
logStream.flush();
}
extern QElapsedTimer gTimer;
extern LogWidget *gLogWidget;
void logMessages(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QMutexLocker lock(&gLogMutex);
static std::vector<QString> logBuffer;
std::string str = qFormatLogMessage(type, context, msg).prepend(
QString("[%1] ").arg(gTimer.elapsed(), 9, 'f', 0, ' ')
).append("\n").toStdString();
std::cout << str.c_str();
if (gLogStream.status() == QTextStream::Ok)
gLogStream << str.c_str();
if (logStream.status() == QTextStream::Ok)
logStream << str.c_str();
if (gLogWidget) {
if (gLogBuffer.size() > 0) {
for (auto s=gLogBuffer.begin(); s!=gLogBuffer.end(); s++)
if (logBuffer.size() > 0) {
for (auto s=logBuffer.begin(); s!=logBuffer.end(); s++)
QMetaObject::invokeMethod( gLogWidget, "appendMessage", Q_ARG( const QString&, *s ) );
gLogBuffer.clear();
logBuffer.clear();
}
QMetaObject::invokeMethod( gLogWidget, "appendMessage", Q_ARG( const QString&, QString(str.c_str())) );
} else
gLogBuffer.push_back( QString(str.c_str()) );
logBuffer.push_back( QString(str.c_str()) );
std::cout.flush();
gLogStream.flush();
logStream.flush();
}
QString toQString(QVideoFrame::PixelFormat format) {
......@@ -123,16 +104,16 @@ QString iniStr(QString str)
}
// CPU hoggers for testing
void delay(int thMs) { Timestamp cur = gTimer.elapsed(); volatile int a=0; while ( gTimer.elapsed() - cur < thMs ) a++; }
void delay(const int &thMs) { QElapsedTimer timer; Timestamp cur = timer.elapsed(); volatile int a=0; while ( timer.elapsed() - cur < thMs ) a++; }
void loadSoundEffect(QSoundEffect &effect, QString fileName)
void loadSoundEffect(const QString &fileName, const QString &exeDir, QSoundEffect &effect)
{
// TODO: clean this up; QSoundEffect doesn't work with resources currently so
// we search manually in case this is a release or a development environment.
// It assumes the build location for the development environment!
QStringList searchPaths;
searchPaths << gExeDir + "/../EyeRecToo/effects" << gExeDir + "/effects";
searchPaths << exeDir + "/../EyeRecToo/effects" << exeDir + "/effects";
bool loaded = false;
for (int i=0; i<searchPaths.size(); i++) {
QString target = searchPaths[i] + "/" + fileName;
......
......@@ -4,10 +4,6 @@
#include <iostream>
#include <atomic>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include <QElapsedTimer>
#include <QDebug>
#include <QDateTime>
......@@ -27,22 +23,10 @@ Q_DECLARE_METATYPE(QCameraInfo)
#include "PerformanceMonitor.h"
/*
* Global variables
*/
extern QElapsedTimer gTimer;
typedef qint64 Timestamp;
extern const Timestamp maxTimestamp;
Q_DECLARE_METATYPE(Timestamp)
extern QString gExeDir;
extern QString gCfgDir;
extern char gDataSeparator;
extern char gDataNewline;
extern QList<QPair<QString, QString> > gReferenceList;
/*
* Utility functions
*/
......@@ -58,7 +42,7 @@ QString toQString(QCameraViewfinderSettings setting);
QString iniStr(QString str);
void delay(int thMs);
void delay(const int &thMs);
template<typename T> void set(const QSettings *settings, const QString key, T &v)
{
......@@ -69,14 +53,6 @@ template<typename T> void set(const QSettings *settings, const QString key, T &v
v = qvariant_cast<T>(variant);
}
void loadSoundEffect(QSoundEffect &effect, QString fileName);
extern LogWidget *gLogWidget;
extern std::vector<QString> gLogBuffer;
extern PerformanceMonitor gPerformanceMonitor;
extern bool gCalibrating;
extern bool gFreezePreview;
void loadSoundEffect(const QString &fileName, const QString &exeDir, QSoundEffect &effect);
#endif // UTILS_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment