AbstractCalibrator.cc 1.12 KB
Newer Older
1 2 3 4 5
#include "AbstractCalibrator.h"

AbstractCalibrator::AbstractCalibrator(QWidget *parent) :
    QWidget(parent),
    pulseWidth(new QLabel()),
6
    log(new QVector<uint16_t>())
7 8 9 10 11 12 13 14
{
}

AbstractCalibrator::~AbstractCalibrator()
{
    delete log;
}

15
uint16_t AbstractCalibrator::logAverage()
16
{
17
    uint16_t total = 0;
18 19
    for (int i=0; i<log->size(); ++i)
        total += log->value(i);
20
    return total/log->size();
21 22
}

23
uint16_t AbstractCalibrator::logExtrema()
24
{
25
    uint16_t extrema = logAverage();
26 27
    if (logAverage() < 1500) {
        for (int i=0; i<log->size(); ++i) {
28 29 30
            if (log->value(i) < extrema)
                extrema = log->value(i);
        }
31
        extrema -= 5; // add 5us to prevent integer overflow
32 33
    } else {
        for (int i=0; i<log->size(); ++i) {
34 35 36
            if (log->value(i) > extrema)
                extrema = log->value(i);
        }
37
        extrema += 5; // subtact 5us to prevent integer overflow
38 39 40 41 42
    }

    return extrema;
}

43
void AbstractCalibrator::channelChanged(uint16_t raw)
44
{
45
    pulseWidth->setText(QString::number(raw));
46 47 48 49
    if (log->size() == 5)
        log->pop_front();
    log->push_back(raw);
}