2013-10-27 18:04:37 +01:00
|
|
|
// Qt includes
|
|
|
|
#include <QDateTime>
|
2018-12-27 23:11:32 +01:00
|
|
|
#include <QTimer>
|
2013-10-27 18:04:37 +01:00
|
|
|
|
|
|
|
#include "LinearColorSmoothing.h"
|
2016-09-07 20:10:37 +02:00
|
|
|
#include <hyperion/Hyperion.h>
|
2013-10-27 18:04:37 +01:00
|
|
|
|
2016-10-08 08:14:36 +02:00
|
|
|
#include <cmath>
|
|
|
|
|
2016-08-11 07:13:55 +02:00
|
|
|
using namespace hyperion;
|
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
const int64_t DEFAUL_SETTLINGTIME = 200; // settlingtime in ms
|
|
|
|
const double DEFAUL_UPDATEFREQUENCY = 25; // updatefrequncy in hz
|
|
|
|
const int64_t DEFAUL_UPDATEINTERVALL = static_cast<int64_t>(1000 / DEFAUL_UPDATEFREQUENCY); // updateintervall in ms
|
|
|
|
const unsigned DEFAUL_OUTPUTDEPLAY = 0; // outputdelay in ms
|
|
|
|
|
2019-01-01 19:47:07 +01:00
|
|
|
LinearColorSmoothing::LinearColorSmoothing(const QJsonDocument& config, Hyperion* hyperion)
|
2020-02-10 15:21:58 +01:00
|
|
|
: QObject(hyperion)
|
2018-12-27 23:11:32 +01:00
|
|
|
, _log(Logger::getInstance("SMOOTHING"))
|
|
|
|
, _hyperion(hyperion)
|
2020-02-10 15:21:58 +01:00
|
|
|
, _updateInterval(DEFAUL_UPDATEINTERVALL)
|
|
|
|
, _settlingTime(DEFAUL_SETTLINGTIME)
|
2018-12-27 23:11:32 +01:00
|
|
|
, _timer(new QTimer(this))
|
2020-02-10 15:21:58 +01:00
|
|
|
, _outputDelay(DEFAUL_OUTPUTDEPLAY)
|
|
|
|
, _writeToLedsEnable(false)
|
2018-12-27 23:11:32 +01:00
|
|
|
, _continuousOutput(false)
|
2017-03-30 06:20:20 +02:00
|
|
|
, _pause(false)
|
2017-08-04 12:01:45 +02:00
|
|
|
, _currentConfigId(0)
|
2020-02-16 18:05:50 +01:00
|
|
|
, _enabled(false)
|
2013-10-27 18:04:37 +01:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
// init cfg 0 (default)
|
2020-02-10 15:21:58 +01:00
|
|
|
addConfig(DEFAUL_SETTLINGTIME, DEFAUL_UPDATEFREQUENCY, DEFAUL_OUTPUTDEPLAY);
|
2018-12-27 23:11:32 +01:00
|
|
|
handleSettingsUpdate(settings::SMOOTHING, config);
|
2020-02-10 15:21:58 +01:00
|
|
|
selectConfig(0, true);
|
2013-10-27 18:04:37 +01:00
|
|
|
|
2017-08-04 12:01:45 +02:00
|
|
|
// add pause on cfg 1
|
2020-02-10 15:21:58 +01:00
|
|
|
SMOOTHING_CFG cfg = {true, 0, 0, 0};
|
2017-08-04 12:01:45 +02:00
|
|
|
_cfgList.append(cfg);
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
// listen for comp changes
|
2020-02-26 18:54:56 +01:00
|
|
|
connect(_hyperion, &Hyperion::compStateChangeRequest, this, &LinearColorSmoothing::componentStateChange);
|
2018-12-27 23:11:32 +01:00
|
|
|
// timer
|
2020-02-10 15:21:58 +01:00
|
|
|
connect(_timer, &QTimer::timeout, this, &LinearColorSmoothing::updateLeds);
|
2013-10-27 18:04:37 +01:00
|
|
|
}
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
void LinearColorSmoothing::handleSettingsUpdate(const settings::type& type, const QJsonDocument& config)
|
|
|
|
{
|
|
|
|
if(type == settings::SMOOTHING)
|
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
// std::cout << "LinearColorSmoothing::handleSettingsUpdate" << std::endl;
|
|
|
|
// std::cout << config.toJson().toStdString() << std::endl;
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
QJsonObject obj = config.object();
|
2020-02-10 15:21:58 +01:00
|
|
|
if(enabled() != obj["enable"].toBool(true))
|
|
|
|
setEnable(obj["enable"].toBool(true));
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
_continuousOutput = obj["continuousOutput"].toBool(true);
|
2020-02-10 15:21:58 +01:00
|
|
|
|
|
|
|
SMOOTHING_CFG cfg = {false,
|
|
|
|
static_cast<int64_t>(obj["time_ms"].toInt(DEFAUL_SETTLINGTIME)),
|
|
|
|
static_cast<int64_t>(1000.0/obj["updateFrequency"].toDouble(DEFAUL_UPDATEFREQUENCY)),
|
|
|
|
static_cast<unsigned>(obj["updateDelay"].toInt(DEFAUL_OUTPUTDEPLAY))
|
|
|
|
};
|
|
|
|
//Debug( _log, "smoothing cfg_id %d: pause: %d bool, settlingTime: %d ms, interval: %d ms (%u Hz), updateDelay: %u frames", _currentConfigId, cfg.pause, cfg.settlingTime, cfg.updateInterval, unsigned(1000.0/cfg.updateInterval), cfg.outputDelay );
|
2018-12-27 23:11:32 +01:00
|
|
|
_cfgList[0] = cfg;
|
2020-02-10 15:21:58 +01:00
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
// if current id is 0, we need to apply the settings (forced)
|
2020-02-10 15:21:58 +01:00
|
|
|
if( _currentConfigId == 0)
|
|
|
|
{
|
|
|
|
//Debug( _log, "_currentConfigId == 0");
|
2018-12-27 23:11:32 +01:00
|
|
|
selectConfig(0, true);
|
2020-02-10 15:21:58 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Debug( _log, "_currentConfigId != 0");
|
|
|
|
}
|
2018-12-27 23:11:32 +01:00
|
|
|
}
|
2013-10-27 18:04:37 +01:00
|
|
|
}
|
|
|
|
|
2013-11-11 10:00:37 +01:00
|
|
|
int LinearColorSmoothing::write(const std::vector<ColorRgb> &ledValues)
|
2013-10-27 18:04:37 +01:00
|
|
|
{
|
2020-07-12 09:20:58 +02:00
|
|
|
_targetTime = QDateTime::currentMSecsSinceEpoch() + _settlingTime;
|
|
|
|
_targetValues = ledValues;
|
|
|
|
|
2016-09-08 16:32:42 +02:00
|
|
|
// received a new target color
|
|
|
|
if (_previousValues.empty())
|
2013-10-27 18:04:37 +01:00
|
|
|
{
|
2016-09-08 16:32:42 +02:00
|
|
|
// not initialized yet
|
|
|
|
_previousTime = QDateTime::currentMSecsSinceEpoch();
|
|
|
|
_previousValues = ledValues;
|
2020-02-10 15:21:58 +01:00
|
|
|
|
|
|
|
//Debug( _log, "Start Smoothing timer: settlingTime: %d ms, interval: %d ms (%u Hz), updateDelay: %u frames", _settlingTime, _updateInterval, unsigned(1000.0/_updateInterval), _outputDelay );
|
2019-08-24 22:53:30 +02:00
|
|
|
QMetaObject::invokeMethod(_timer, "start", Qt::QueuedConnection, Q_ARG(int, _updateInterval));
|
2013-10-27 18:04:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
int LinearColorSmoothing::updateLedValues(const std::vector<ColorRgb>& ledValues)
|
2013-10-27 18:04:37 +01:00
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
int retval = 0;
|
|
|
|
if (!_enabled)
|
2014-09-22 21:28:38 +02:00
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
return -1;
|
2014-09-22 21:28:38 +02:00
|
|
|
}
|
2020-02-10 15:21:58 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
retval = write(ledValues);
|
|
|
|
}
|
|
|
|
return retval;
|
2013-10-27 18:04:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LinearColorSmoothing::updateLeds()
|
|
|
|
{
|
|
|
|
int64_t now = QDateTime::currentMSecsSinceEpoch();
|
2020-02-10 15:21:58 +01:00
|
|
|
int64_t deltaTime = _targetTime - now;
|
2013-10-27 18:04:37 +01:00
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
//Debug(_log, "elapsed Time [%d], _targetTime [%d] - now [%d], deltaTime [%d]", now -_previousTime, _targetTime, now, deltaTime);
|
2013-10-27 18:04:37 +01:00
|
|
|
if (deltaTime < 0)
|
|
|
|
{
|
2020-07-12 09:20:58 +02:00
|
|
|
_previousValues = _targetValues;
|
2013-10-27 18:04:37 +01:00
|
|
|
_previousTime = now;
|
|
|
|
|
2014-09-22 21:28:38 +02:00
|
|
|
queueColors(_previousValues);
|
2016-07-13 11:18:12 +02:00
|
|
|
_writeToLedsEnable = _continuousOutput;
|
2013-10-27 18:04:37 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-26 07:01:10 +02:00
|
|
|
_writeToLedsEnable = true;
|
2020-02-10 15:21:58 +01:00
|
|
|
|
|
|
|
//std::cout << "LinearColorSmoothing::updateLeds> _previousValues: "; LedDevice::printLedValues ( _previousValues );
|
|
|
|
|
2013-10-27 18:04:37 +01:00
|
|
|
float k = 1.0f - 1.0f * deltaTime / (_targetTime - _previousTime);
|
|
|
|
|
2016-10-08 08:14:36 +02:00
|
|
|
int reddif = 0, greendif = 0, bluedif = 0;
|
2013-10-27 18:04:37 +01:00
|
|
|
|
2016-10-08 08:14:36 +02:00
|
|
|
for (size_t i = 0; i < _previousValues.size(); ++i)
|
|
|
|
{
|
|
|
|
ColorRgb & prev = _previousValues[i];
|
|
|
|
ColorRgb & target = _targetValues[i];
|
2016-10-04 22:13:29 +02:00
|
|
|
|
2016-10-08 08:14:36 +02:00
|
|
|
reddif = target.red - prev.red;
|
|
|
|
greendif = target.green - prev.green;
|
|
|
|
bluedif = target.blue - prev.blue;
|
2016-10-04 22:13:29 +02:00
|
|
|
|
2016-10-08 08:14:36 +02:00
|
|
|
prev.red += (reddif < 0 ? -1:1) * std::ceil(k * std::abs(reddif));
|
|
|
|
prev.green += (greendif < 0 ? -1:1) * std::ceil(k * std::abs(greendif));
|
|
|
|
prev.blue += (bluedif < 0 ? -1:1) * std::ceil(k * std::abs(bluedif));
|
|
|
|
}
|
2013-10-27 18:04:37 +01:00
|
|
|
_previousTime = now;
|
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
//std::cout << "LinearColorSmoothing::updateLeds> _targetValues: "; LedDevice::printLedValues ( _targetValues );
|
|
|
|
|
2014-09-22 21:28:38 +02:00
|
|
|
queueColors(_previousValues);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinearColorSmoothing::queueColors(const std::vector<ColorRgb> & ledColors)
|
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
//Debug(_log, "queueColors - _outputDelay[%d] _outputQueue.size() [%d], _writeToLedsEnable[%d]", _outputDelay, _outputQueue.size(), _writeToLedsEnable);
|
2014-09-22 21:28:38 +02:00
|
|
|
if (_outputDelay == 0)
|
|
|
|
{
|
2014-09-25 22:05:01 +02:00
|
|
|
// No output delay => immediate write
|
2017-03-30 06:20:20 +02:00
|
|
|
if ( _writeToLedsEnable && !_pause)
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
|
|
|
// if ( ledColors.size() == 0 )
|
|
|
|
// qFatal ("No LedValues! - in LinearColorSmoothing::queueColors() - _outputDelay == 0");
|
|
|
|
// else
|
2019-01-01 19:47:07 +01:00
|
|
|
emit _hyperion->ledDeviceData(ledColors);
|
2020-02-10 15:21:58 +01:00
|
|
|
}
|
2014-09-22 21:28:38 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-13 11:18:12 +02:00
|
|
|
// Push new colors in the delay-buffer
|
|
|
|
if ( _writeToLedsEnable )
|
|
|
|
_outputQueue.push_back(ledColors);
|
|
|
|
|
2014-09-25 22:05:01 +02:00
|
|
|
// If the delay-buffer is filled pop the front and write to device
|
2016-07-13 11:18:12 +02:00
|
|
|
if (_outputQueue.size() > 0 )
|
2014-09-22 21:28:38 +02:00
|
|
|
{
|
2016-07-13 11:18:12 +02:00
|
|
|
if ( _outputQueue.size() > _outputDelay || !_writeToLedsEnable )
|
|
|
|
{
|
2017-03-30 06:20:20 +02:00
|
|
|
if (!_pause)
|
|
|
|
{
|
2019-01-01 19:47:07 +01:00
|
|
|
emit _hyperion->ledDeviceData(_outputQueue.front());
|
2017-03-30 06:20:20 +02:00
|
|
|
}
|
2016-07-13 11:18:12 +02:00
|
|
|
_outputQueue.pop_front();
|
|
|
|
}
|
2014-09-22 21:28:38 +02:00
|
|
|
}
|
2013-10-27 18:04:37 +01:00
|
|
|
}
|
|
|
|
}
|
2016-08-11 07:13:55 +02:00
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
void LinearColorSmoothing::clearQueuedColors()
|
|
|
|
{
|
|
|
|
QMetaObject::invokeMethod(_timer, "stop", Qt::QueuedConnection);
|
|
|
|
_previousValues.clear();
|
|
|
|
|
|
|
|
_targetValues.clear();
|
|
|
|
}
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
void LinearColorSmoothing::componentStateChange(const hyperion::Components component, const bool state)
|
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
_writeToLedsEnable = state;
|
2019-12-08 13:12:01 +01:00
|
|
|
if(component == hyperion::COMP_LEDDEVICE)
|
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
clearQueuedColors();
|
2019-12-08 13:12:01 +01:00
|
|
|
}
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
if(component == hyperion::COMP_SMOOTHING)
|
2019-12-08 13:12:01 +01:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
setEnable(state);
|
2019-12-08 13:12:01 +01:00
|
|
|
}
|
2018-12-27 23:11:32 +01:00
|
|
|
}
|
2016-09-08 16:32:42 +02:00
|
|
|
|
|
|
|
void LinearColorSmoothing::setEnable(bool enable)
|
2016-08-11 07:13:55 +02:00
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
_enabled = enable;
|
|
|
|
if (!_enabled)
|
2016-09-08 18:07:57 +02:00
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
clearQueuedColors();
|
2016-09-08 18:07:57 +02:00
|
|
|
}
|
2020-02-10 15:21:58 +01:00
|
|
|
// update comp register
|
2020-02-26 18:54:56 +01:00
|
|
|
_hyperion->setNewComponentState(hyperion::COMP_SMOOTHING, enable);
|
2016-08-11 07:13:55 +02:00
|
|
|
}
|
2017-03-30 06:20:20 +02:00
|
|
|
|
|
|
|
void LinearColorSmoothing::setPause(bool pause)
|
|
|
|
{
|
|
|
|
_pause = pause;
|
|
|
|
}
|
|
|
|
|
2017-08-04 12:01:45 +02:00
|
|
|
unsigned LinearColorSmoothing::addConfig(int settlingTime_ms, double ledUpdateFrequency_hz, unsigned updateDelay)
|
|
|
|
{
|
|
|
|
SMOOTHING_CFG cfg = {false, settlingTime_ms, int64_t(1000.0/ledUpdateFrequency_hz), updateDelay};
|
|
|
|
_cfgList.append(cfg);
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
//Debug( _log, "smoothing cfg %d: pause: %d bool, settlingTime: %d ms, interval: %d ms (%u Hz), updateDelay: %u frames", _cfgList.count()-1, cfg.pause, cfg.settlingTime, cfg.updateInterval, unsigned(1000.0/cfg.updateInterval), cfg.outputDelay );
|
2017-08-04 12:01:45 +02:00
|
|
|
return _cfgList.count() - 1;
|
|
|
|
}
|
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
unsigned LinearColorSmoothing::updateConfig(unsigned cfgID, int settlingTime_ms, double ledUpdateFrequency_hz, unsigned updateDelay)
|
|
|
|
{
|
|
|
|
unsigned updatedCfgID = cfgID;
|
|
|
|
if ( cfgID < static_cast<unsigned>(_cfgList.count()) )
|
|
|
|
{
|
|
|
|
SMOOTHING_CFG cfg = {false, settlingTime_ms, int64_t(1000.0/ledUpdateFrequency_hz), updateDelay};
|
|
|
|
_cfgList[updatedCfgID] = cfg;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
updatedCfgID = addConfig ( settlingTime_ms, ledUpdateFrequency_hz, updateDelay);
|
|
|
|
}
|
|
|
|
// Debug( _log, "smoothing updatedCfgID %u: settlingTime: %d ms, "
|
|
|
|
// "interval: %d ms (%u Hz), updateDelay: %u frames", cfgID, _settlingTime, int64_t(1000.0/ledUpdateFrequency_hz), unsigned(ledUpdateFrequency_hz), updateDelay );
|
|
|
|
return updatedCfgID;
|
|
|
|
}
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
bool LinearColorSmoothing::selectConfig(unsigned cfg, const bool& force)
|
2017-08-04 12:01:45 +02:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
if (_currentConfigId == cfg && !force)
|
2017-08-04 12:01:45 +02:00
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
//Debug( _log, "selectConfig SAME as before, not FORCED - _currentConfigId [%u], force [%d]", cfg, force);
|
|
|
|
//Debug( _log, "current smoothing cfg: %d, settlingTime: %d ms, interval: %d ms (%u Hz), updateDelay: %u frames", _currentConfigId, _settlingTime, _updateInterval, unsigned(1000.0/_updateInterval), _outputDelay );
|
2017-08-04 12:01:45 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
//Debug( _log, "selectConfig FORCED - _currentConfigId [%u], force [%d]", cfg, force);
|
2017-08-04 12:01:45 +02:00
|
|
|
if ( cfg < (unsigned)_cfgList.count())
|
|
|
|
{
|
|
|
|
_settlingTime = _cfgList[cfg].settlingTime;
|
|
|
|
_outputDelay = _cfgList[cfg].outputDelay;
|
|
|
|
_pause = _cfgList[cfg].pause;
|
|
|
|
|
|
|
|
if (_cfgList[cfg].updateInterval != _updateInterval)
|
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
|
2019-08-24 22:53:30 +02:00
|
|
|
QMetaObject::invokeMethod(_timer, "stop", Qt::QueuedConnection);
|
2017-08-04 12:01:45 +02:00
|
|
|
_updateInterval = _cfgList[cfg].updateInterval;
|
2020-02-10 15:21:58 +01:00
|
|
|
if ( this->enabled() && this->_writeToLedsEnable )
|
|
|
|
{
|
|
|
|
//Debug( _log, "_cfgList[cfg].updateInterval != _updateInterval - Restart timer - _updateInterval [%d]", _updateInterval);
|
|
|
|
QMetaObject::invokeMethod(_timer, "start", Qt::QueuedConnection, Q_ARG(int, _updateInterval));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Debug( _log, "Smoothing disabled, do NOT restart timer");
|
|
|
|
}
|
2017-08-04 12:01:45 +02:00
|
|
|
}
|
|
|
|
_currentConfigId = cfg;
|
2020-02-10 15:21:58 +01:00
|
|
|
// Debug( _log, "current smoothing cfg: %d, settlingTime: %d ms, interval: %d ms (%u Hz), updateDelay: %u frames", _currentConfigId, _settlingTime, _updateInterval, unsigned(1000.0/_updateInterval), _outputDelay );
|
|
|
|
// DebugIf( enabled() && !_pause, _log, "set smoothing cfg: %u settlingTime: %d ms, interval: %d ms, updateDelay: %u frames", _currentConfigId, _settlingTime, _updateInterval, _outputDelay );
|
|
|
|
// DebugIf( _pause, _log, "set smoothing cfg: %d, pause", _currentConfigId );
|
2017-08-04 12:01:45 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2017-08-04 12:01:45 +02:00
|
|
|
// reset to default
|
|
|
|
_currentConfigId = 0;
|
|
|
|
return false;
|
|
|
|
}
|