ParallelPort.cpp 1.62 KB
Newer Older
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
#include "ParallelPort.h"

ParallelPort::ParallelPort()
{
    QMutexLocker locker(&mutex);

    log = NULL;
    if ( settings.value("trigger/parallelPort").toBool()) {
        std::wstring tmp(gBinaryPath.toStdWString());
        tmp.append( L"/inpout32.dll" ) ;
        hInpOutDll = LoadLibrary ( tmp.c_str() ) ;

        if ( hInpOutDll != NULL ) {
            fpOut32 = (lpOut32)GetProcAddress(hInpOutDll, "Out32");
            fpInp32 = (lpInp32)GetProcAddress(hInpOutDll, "Inp32");
            fpIsInpOutDriverOpen = (lpIsInpOutDriverOpen)GetProcAddress(hInpOutDll, "IsInpOutDriverOpen");
        } else {
            qDebug() << "Failed to load inpout library.";
        }

        period = 1000/settings.value("trigger/parallelPortPollingFrequency").toDouble();

        log = new std::ofstream();
    }
}

ParallelPort::~ParallelPort()
{
    if (hInpOutDll)
        FreeLibrary ( hInpOutDll ) ;

    if (log) {
        closeLog();
        delete log;
    }
}

void ParallelPort::openLog()
{
    QMutexLocker locker(&mutex);

42 43 44
    if (!log)
        return;

45 46 47 48 49 50 51
    log->open(getNamePrefix().append(QString("parallelPort-%1.txt").arg(gFileIndexStr)).toStdString(), std::fstream::app);
}

void ParallelPort::closeLog()
{
    QMutexLocker locker(&mutex);

52 53 54
    if (!log)
        return;

55 56 57 58 59 60 61 62
    if (log->is_open())
        log->close();
}

void ParallelPort::poll()
{
    QMutexLocker locker(&mutex);

63 64 65
    if (!log)
        return;

66 67 68 69
    if (!fpIsInpOutDriverOpen)
        return;

    if (log->is_open()) {
70 71 72 73 74
        if (hInpOutDll) {
            WORD wData = fpInp32(888);
            *log << gTimer.timestamp() << "\t" << wData << std::endl;
        }

75 76 77
    }
}