test_sdk/math/gtl_math_analog_value.cpp

153 lines
3.5 KiB
C++
Raw Normal View History

#include "gtl_math_analog_value.h"
#include <QDebug>
#include <QTime>
namespace gtl
{
namespace math
{
analog_value::analog_value(gtl::analog_data *data)
: QObject{/*data*/NULL}
, _avg_cnt(1)
, _value(0)
, _ad(data)
, _is_acq(true)
{
set_time(0.1);
connect(_ad, &gtl::analog_data::data_changed, this, &analog_value::data_changed);
}
analog_value::~analog_value()
{
}
QString analog_value::name() const
{
return _name;
}
qreal analog_value::value() const
{
return _value;
}
qreal analog_value::time() const
{
return _time;
}
quint32 analog_value::avg_cnt() const
{
return _avg_cnt;
}
qreal analog_value::avg_value() const
{
if(_values.empty())
return 0;
return std::accumulate(_values.begin(), _values.end(), 0.0)/_values.size();
}
QList<qreal> analog_value::values() const
{
return _values;
}
void analog_value::set_time(qreal value)
{
if(value != _time)
{
_ad->lock_device();
_time = value;
_data.resize(qRound(_time * _ad->get_rate()));
std::fill(_data.begin(), _data.end(), 0.0);
_data_ptr = 0;
_value = 0;
_values.clear();
_ad->unlock_device();
emit time_changed();
}
}
void analog_value::set_avg_cnt(quint32 value)
{
if(value != _avg_cnt)
{
_avg_cnt = value;
emit avg_cnt_changed();
}
}
void analog_value::stop_acq()
{
_is_acq = false;
}
void analog_value::before_copying_data(std::vector<qreal>::iterator begin, std::vector<qreal>::iterator end)
{
}
void analog_value::after_copying_data(std::vector<qreal>::iterator begin, std::vector<qreal>::iterator end)
{
}
void analog_value::set_name(QString value)
{
if(_name != value)
{
_name = value;
emit name_changed();
}
}
void analog_value::data_changed()
{
if(!_is_acq)
return;
_ad->lock_device();
int idx = /*std::max<int>(0, (int)_ad->size() - (int)_data.size())*/0;
while(idx != _ad->size())
{
int size = std::min<int>((int)_ad->size() - idx, (int)_data.size() - _data_ptr);
std::vector<qreal>::iterator begin = _data.begin() + _data_ptr;
std::vector<qreal>::iterator end = begin + size;
before_copying_data(begin, end);
std::copy(_ad->begin() + idx, _ad->begin() + idx + size, begin);
after_copying_data(begin, end);
if(end == _data.end())
{
_values.push_back(value());
while(_values.size() > _avg_cnt)
_values.pop_front();
}
_data_ptr = (_data_ptr + size) % _data.size();
idx += size;
}
_ad->unlock_device();
}
}
}