Commit cba25557 authored by Lorenz Meier's avatar Lorenz Meier

Merge pull request #75 from Susurrus/issue_12

Added a check for a division by zero in AbstractCalibrator::logAverage()
parents dd011bc5 f548d299
...@@ -14,25 +14,42 @@ AbstractCalibrator::~AbstractCalibrator() ...@@ -14,25 +14,42 @@ AbstractCalibrator::~AbstractCalibrator()
uint16_t AbstractCalibrator::logAverage() uint16_t AbstractCalibrator::logAverage()
{ {
// Short-circuit here if the log is empty otherwise we get a div-by-0 error.
if (log->empty())
{
return 0;
}
uint16_t total = 0; uint16_t total = 0;
for (int i=0; i<log->size(); ++i) for (int i=0; i<log->size(); ++i)
{
total += log->value(i); total += log->value(i);
}
return total/log->size(); return total/log->size();
} }
uint16_t AbstractCalibrator::logExtrema() uint16_t AbstractCalibrator::logExtrema()
{ {
uint16_t extrema = logAverage(); uint16_t extrema = logAverage();
if (logAverage() < 1500) { if (logAverage() < 1500)
for (int i=0; i<log->size(); ++i) { {
for (int i=0; i<log->size(); ++i)
{
if (log->value(i) < extrema) if (log->value(i) < extrema)
extrema = log->value(i); {
extrema = log->value(i);
}
} }
extrema -= 5; // add 5us to prevent integer overflow extrema -= 5; // add 5us to prevent integer overflow
} else { }
for (int i=0; i<log->size(); ++i) { else
{
for (int i=0; i<log->size(); ++i)
{
if (log->value(i) > extrema) if (log->value(i) > extrema)
{
extrema = log->value(i); extrema = log->value(i);
}
} }
extrema += 5; // subtact 5us to prevent integer overflow extrema += 5; // subtact 5us to prevent integer overflow
} }
...@@ -44,6 +61,8 @@ void AbstractCalibrator::channelChanged(uint16_t raw) ...@@ -44,6 +61,8 @@ void AbstractCalibrator::channelChanged(uint16_t raw)
{ {
pulseWidth->setText(QString::number(raw)); pulseWidth->setText(QString::number(raw));
if (log->size() == 5) if (log->size() == 5)
{
log->pop_front(); log->pop_front();
}
log->push_back(raw); log->push_back(raw);
} }
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