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;
|
|
|
|
|
2019-01-01 19:47:07 +01:00
|
|
|
LinearColorSmoothing::LinearColorSmoothing(const QJsonDocument& config, Hyperion* hyperion)
|
|
|
|
: LedDevice(QJsonObject(), hyperion)
|
2018-12-27 23:11:32 +01:00
|
|
|
, _log(Logger::getInstance("SMOOTHING"))
|
|
|
|
, _hyperion(hyperion)
|
|
|
|
, _updateInterval(1000)
|
|
|
|
, _settlingTime(200)
|
|
|
|
, _timer(new QTimer(this))
|
|
|
|
, _outputDelay(0)
|
2016-07-13 11:18:12 +02:00
|
|
|
, _writeToLedsEnable(true)
|
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)
|
2013-10-27 18:04:37 +01:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
// set initial state to true, as LedDevice::enabled() is true by default
|
|
|
|
_hyperion->getComponentRegister().componentStateChanged(hyperion::COMP_SMOOTHING, true);
|
|
|
|
|
|
|
|
// init cfg 0 (default)
|
|
|
|
_cfgList.append({false, 200, 25, 0});
|
|
|
|
handleSettingsUpdate(settings::SMOOTHING, config);
|
2013-10-27 18:04:37 +01:00
|
|
|
|
2017-08-04 12:01:45 +02:00
|
|
|
// add pause on cfg 1
|
2018-12-27 23:11:32 +01:00
|
|
|
SMOOTHING_CFG cfg = {true};
|
2017-08-04 12:01:45 +02:00
|
|
|
_cfgList.append(cfg);
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
// listen for comp changes
|
|
|
|
connect(_hyperion, &Hyperion::componentStateChanged, this, &LinearColorSmoothing::componentStateChange);
|
|
|
|
// timer
|
|
|
|
connect(_timer, SIGNAL(timeout()), this, SLOT(updateLeds()));
|
2013-10-27 18:04:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
LinearColorSmoothing::~LinearColorSmoothing()
|
|
|
|
{
|
2019-01-01 19:47:07 +01:00
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LinearColorSmoothing::handleSettingsUpdate(const settings::type& type, const QJsonDocument& config)
|
|
|
|
{
|
|
|
|
if(type == settings::SMOOTHING)
|
|
|
|
{
|
|
|
|
QJsonObject obj = config.object();
|
|
|
|
_continuousOutput = obj["continuousOutput"].toBool(true);
|
|
|
|
SMOOTHING_CFG cfg = {false, obj["time_ms"].toInt(200), unsigned(1000.0/obj["updateFrequency"].toDouble(25.0)), unsigned(obj["updateDelay"].toInt(0))};
|
|
|
|
_cfgList[0] = cfg;
|
|
|
|
// if current id is 0, we need to apply the settings (forced)
|
|
|
|
if(!_currentConfigId)
|
|
|
|
selectConfig(0, true);
|
|
|
|
|
|
|
|
if(enabled() != obj["enable"].toBool(true))
|
|
|
|
setEnable(obj["enable"].toBool(true));
|
|
|
|
}
|
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
|
|
|
{
|
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
|
|
|
|
_targetTime = QDateTime::currentMSecsSinceEpoch() + _settlingTime;
|
|
|
|
_targetValues = ledValues;
|
|
|
|
|
|
|
|
_previousTime = QDateTime::currentMSecsSinceEpoch();
|
|
|
|
_previousValues = ledValues;
|
2018-12-27 23:11:32 +01:00
|
|
|
_timer->start();
|
2013-10-27 18:04:37 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-08 16:32:42 +02:00
|
|
|
_targetTime = QDateTime::currentMSecsSinceEpoch() + _settlingTime;
|
|
|
|
memcpy(_targetValues.data(), ledValues.data(), ledValues.size() * sizeof(ColorRgb));
|
2013-10-27 18:04:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LinearColorSmoothing::switchOff()
|
|
|
|
{
|
2014-09-25 22:05:01 +02:00
|
|
|
// We will keep updating the leds (but with pure-black)
|
|
|
|
|
2014-09-22 21:28:38 +02:00
|
|
|
// Clear the smoothing parameters
|
|
|
|
std::fill(_targetValues.begin(), _targetValues.end(), ColorRgb::BLACK);
|
2013-10-27 18:04:37 +01:00
|
|
|
_targetTime = 0;
|
|
|
|
|
2014-09-22 21:28:38 +02:00
|
|
|
// Erase the output-queue
|
|
|
|
for (unsigned i=0; i<_outputQueue.size(); ++i)
|
|
|
|
{
|
|
|
|
_outputQueue.push_back(_targetValues);
|
|
|
|
_outputQueue.pop_front();
|
|
|
|
}
|
|
|
|
|
2019-04-19 08:22:23 +02:00
|
|
|
emit _hyperion->ledDeviceData(std::vector<ColorRgb>(_ledCount, ColorRgb::BLACK));
|
|
|
|
|
2014-09-22 21:28:38 +02:00
|
|
|
return 0;
|
2013-10-27 18:04:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LinearColorSmoothing::updateLeds()
|
|
|
|
{
|
|
|
|
int64_t now = QDateTime::currentMSecsSinceEpoch();
|
|
|
|
int deltaTime = _targetTime - now;
|
|
|
|
|
|
|
|
if (deltaTime < 0)
|
|
|
|
{
|
2013-11-11 10:00:37 +01:00
|
|
|
memcpy(_previousValues.data(), _targetValues.data(), _targetValues.size() * sizeof(ColorRgb));
|
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;
|
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;
|
|
|
|
|
2014-09-22 21:28:38 +02:00
|
|
|
queueColors(_previousValues);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinearColorSmoothing::queueColors(const std::vector<ColorRgb> & ledColors)
|
|
|
|
{
|
|
|
|
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)
|
2019-01-01 19:47:07 +01:00
|
|
|
emit _hyperion->ledDeviceData(ledColors);
|
|
|
|
|
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
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
void LinearColorSmoothing::componentStateChange(const hyperion::Components component, const bool state)
|
|
|
|
{
|
|
|
|
if(component == hyperion::COMP_SMOOTHING)
|
|
|
|
setEnable(state);
|
|
|
|
}
|
2016-09-08 16:32:42 +02:00
|
|
|
|
|
|
|
void LinearColorSmoothing::setEnable(bool enable)
|
2016-08-11 07:13:55 +02:00
|
|
|
{
|
2016-09-08 18:07:57 +02:00
|
|
|
if (!enable)
|
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
_timer->stop();
|
2016-09-08 18:07:57 +02:00
|
|
|
_previousValues.clear();
|
|
|
|
}
|
2018-12-27 23:11:32 +01:00
|
|
|
// update comp register
|
|
|
|
_hyperion->getComponentRegister().componentStateChanged(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
|
|
|
|
|
|
|
//Debug( _log, "smoothing cfg %d: interval: %d ms, settlingTime: %d ms, updateDelay: %d frames", _cfgList.count()-1, cfg.updateInterval, cfg.settlingTime, cfg.outputDelay );
|
2017-08-04 12:01:45 +02:00
|
|
|
return _cfgList.count() - 1;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( cfg < (unsigned)_cfgList.count())
|
|
|
|
{
|
|
|
|
_settlingTime = _cfgList[cfg].settlingTime;
|
|
|
|
_outputDelay = _cfgList[cfg].outputDelay;
|
|
|
|
_pause = _cfgList[cfg].pause;
|
|
|
|
|
|
|
|
if (_cfgList[cfg].updateInterval != _updateInterval)
|
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
_timer->stop();
|
2017-08-04 12:01:45 +02:00
|
|
|
_updateInterval = _cfgList[cfg].updateInterval;
|
2018-12-27 23:11:32 +01:00
|
|
|
_timer->setInterval(_updateInterval);
|
|
|
|
_timer->start();
|
2017-08-04 12:01:45 +02:00
|
|
|
}
|
|
|
|
_currentConfigId = cfg;
|
2018-12-27 23:11:32 +01:00
|
|
|
//DebugIf( enabled() && !_pause, _log, "set smoothing cfg: %d, interval: %d ms, settlingTime: %d ms, updateDelay: %d frames", _currentConfigId, _updateInterval, _settlingTime, _outputDelay );
|
2018-12-28 18:12:45 +01:00
|
|
|
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;
|
|
|
|
}
|