AbstractCalibrator.cc 1.27 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 18 19 20 21 22
	// Short-circuit here if the log is empty otherwise we get a div-by-0 error.
	if (log->empty())
	{
		return 0;
	}

23
    uint16_t total = 0;
24
    for (int i=0; i<log->size(); ++i)
25
	{
26
        total += log->value(i);
27
	}
28
    return total/log->size();
29 30
}

31
uint16_t AbstractCalibrator::logExtrema()
32
{
33
    uint16_t extrema = logAverage();
34 35 36 37
    if (logAverage() < 1500)
	{
        for (int i=0; i<log->size(); ++i)
		{
38
            if (log->value(i) < extrema)
39 40 41
			{
                extrema = log->value(i); 
			}
42
        }
43
        extrema -= 5; // add 5us to prevent integer overflow
44 45 46 47 48
    }
	else
	{
        for (int i=0; i<log->size(); ++i)
		{
49
            if (log->value(i) > extrema)
50
			{
51
                extrema = log->value(i);
52
			}
53
        }
54
        extrema += 5; // subtact 5us to prevent integer overflow
55 56 57 58 59
    }

    return extrema;
}

60
void AbstractCalibrator::channelChanged(uint16_t raw)
61
{
62
    pulseWidth->setText(QString::number(raw));
63
    if (log->size() == 5)
64
	{
65
        log->pop_front();
66
	}
67 68
    log->push_back(raw);
}