2013-08-13 11:10:45 +02:00
|
|
|
// STL includes
|
|
|
|
#include <algorithm>
|
2017-09-01 08:50:37 +02:00
|
|
|
#include <limits>
|
2013-08-13 11:10:45 +02:00
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
// qt incl
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QTimer>
|
|
|
|
|
2013-08-13 11:10:45 +02:00
|
|
|
// Hyperion includes
|
|
|
|
#include <hyperion/PriorityMuxer.h>
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
// utils
|
|
|
|
#include <utils/Logger.h>
|
|
|
|
|
2017-09-01 08:50:37 +02:00
|
|
|
const int PriorityMuxer::LOWEST_PRIORITY = std::numeric_limits<uint8_t>::max();
|
|
|
|
|
2020-07-19 15:13:41 +02:00
|
|
|
PriorityMuxer::PriorityMuxer(int ledCount, QObject * parent)
|
|
|
|
: QObject(parent)
|
2018-12-27 23:11:32 +01:00
|
|
|
, _log(Logger::getInstance("HYPERION"))
|
|
|
|
, _currentPriority(PriorityMuxer::LOWEST_PRIORITY)
|
|
|
|
, _manualSelectedPriority(256)
|
2016-07-31 22:21:35 +02:00
|
|
|
, _activeInputs()
|
|
|
|
, _lowestPriorityInfo()
|
2018-12-27 23:11:32 +01:00
|
|
|
, _sourceAutoSelectEnabled(true)
|
2020-07-12 09:22:05 +02:00
|
|
|
, _updateTimer(new QTimer(this))
|
|
|
|
, _timer(new QTimer(this))
|
|
|
|
, _blockTimer(new QTimer(this))
|
2013-08-13 11:10:45 +02:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
// init lowest priority info
|
2017-09-01 08:50:37 +02:00
|
|
|
_lowestPriorityInfo.priority = PriorityMuxer::LOWEST_PRIORITY;
|
2018-12-27 23:11:32 +01:00
|
|
|
_lowestPriorityInfo.timeoutTime_ms = -1;
|
2017-03-21 17:55:46 +01:00
|
|
|
_lowestPriorityInfo.ledColors = std::vector<ColorRgb>(ledCount, {0, 0, 0});
|
|
|
|
_lowestPriorityInfo.componentId = hyperion::COMP_COLOR;
|
|
|
|
_lowestPriorityInfo.origin = "System";
|
2018-12-27 23:11:32 +01:00
|
|
|
_lowestPriorityInfo.owner = "";
|
|
|
|
|
|
|
|
_activeInputs[PriorityMuxer::LOWEST_PRIORITY] = _lowestPriorityInfo;
|
2017-03-21 17:55:46 +01:00
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
// adapt to 1s interval for COLOR and EFFECT timeouts > -1
|
|
|
|
connect(_timer, &QTimer::timeout, this, &PriorityMuxer::timeTrigger);
|
|
|
|
_timer->setSingleShot(true);
|
|
|
|
_blockTimer->setSingleShot(true);
|
|
|
|
// forward timeRunner signal to prioritiesChanged signal & threading workaround
|
|
|
|
connect(this, &PriorityMuxer::timeRunner, this, &PriorityMuxer::prioritiesChanged);
|
|
|
|
connect(this, &PriorityMuxer::signalTimeTrigger, this, &PriorityMuxer::timeTrigger);
|
2019-07-28 23:11:43 +02:00
|
|
|
connect(this, &PriorityMuxer::activeStateChanged, this, &PriorityMuxer::prioritiesChanged);
|
2017-06-17 23:29:04 +02:00
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
// start muxer timer
|
|
|
|
connect(_updateTimer, &QTimer::timeout, this, &PriorityMuxer::setCurrentTime);
|
|
|
|
_updateTimer->setInterval(250);
|
|
|
|
_updateTimer->start();
|
2013-08-13 11:10:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PriorityMuxer::~PriorityMuxer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
void PriorityMuxer::setEnable(const bool& enable)
|
|
|
|
{
|
|
|
|
enable ? _updateTimer->start() : _updateTimer->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PriorityMuxer::setSourceAutoSelectEnabled(const bool& enable, const bool& update)
|
|
|
|
{
|
|
|
|
if(_sourceAutoSelectEnabled != enable)
|
|
|
|
{
|
|
|
|
// on disable we need to make sure the last priority call to setPriority is still valid
|
|
|
|
if(!enable && !_activeInputs.contains(_manualSelectedPriority))
|
|
|
|
{
|
|
|
|
Warning(_log, "Can't disable auto selection, as the last manual selected priority (%d) is no longer available", _manualSelectedPriority);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_sourceAutoSelectEnabled = enable;
|
|
|
|
Debug(_log, "Source auto select is now %s", enable ? "enabled" : "disabled");
|
|
|
|
|
|
|
|
// update _currentPriority if called from external
|
|
|
|
if(update)
|
|
|
|
setCurrentTime();
|
|
|
|
|
|
|
|
emit autoSelectChanged(enable);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PriorityMuxer::setPriority(const uint8_t priority)
|
|
|
|
{
|
|
|
|
if(_activeInputs.contains(priority))
|
|
|
|
{
|
|
|
|
_manualSelectedPriority = priority;
|
|
|
|
// update auto select state -> update _currentPriority
|
|
|
|
setSourceAutoSelectEnabled(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PriorityMuxer::updateLedColorsLength(const int& ledCount)
|
|
|
|
{
|
|
|
|
for (auto infoIt = _activeInputs.begin(); infoIt != _activeInputs.end();)
|
|
|
|
{
|
|
|
|
if (infoIt->ledColors.size() >= 1)
|
|
|
|
{
|
|
|
|
infoIt->ledColors.resize(ledCount, infoIt->ledColors.at(0));
|
|
|
|
}
|
|
|
|
++infoIt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-13 11:10:45 +02:00
|
|
|
QList<int> PriorityMuxer::getPriorities() const
|
|
|
|
{
|
2013-08-18 13:33:56 +02:00
|
|
|
return _activeInputs.keys();
|
2013-08-13 11:10:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PriorityMuxer::hasPriority(const int priority) const
|
|
|
|
{
|
2017-09-01 08:50:37 +02:00
|
|
|
return (priority == PriorityMuxer::LOWEST_PRIORITY) ? true : _activeInputs.contains(priority);
|
2013-08-13 11:10:45 +02:00
|
|
|
}
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
const PriorityMuxer::InputInfo PriorityMuxer::getInputInfo(const int priority) const
|
2013-08-13 11:10:45 +02:00
|
|
|
{
|
2013-08-18 13:33:56 +02:00
|
|
|
auto elemIt = _activeInputs.find(priority);
|
|
|
|
if (elemIt == _activeInputs.end())
|
2013-08-13 11:10:45 +02:00
|
|
|
{
|
2017-09-01 08:50:37 +02:00
|
|
|
elemIt = _activeInputs.find(PriorityMuxer::LOWEST_PRIORITY);
|
|
|
|
if (elemIt == _activeInputs.end())
|
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
// fallback
|
|
|
|
return _lowestPriorityInfo;
|
2017-09-01 08:50:37 +02:00
|
|
|
}
|
2013-08-13 11:10:45 +02:00
|
|
|
}
|
|
|
|
return elemIt.value();
|
|
|
|
}
|
|
|
|
|
2020-02-15 22:47:27 +01:00
|
|
|
hyperion::Components PriorityMuxer::getComponentOfPriority(const int &priority)
|
|
|
|
{
|
|
|
|
return _activeInputs[priority].componentId;
|
|
|
|
}
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
void PriorityMuxer::registerInput(const int priority, const hyperion::Components& component, const QString& origin, const QString& owner, unsigned smooth_cfg)
|
2013-08-13 11:10:45 +02:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
// detect new registers
|
|
|
|
bool newInput = false;
|
|
|
|
if(!_activeInputs.contains(priority))
|
|
|
|
newInput = true;
|
|
|
|
|
2017-01-10 19:58:41 +01:00
|
|
|
InputInfo& input = _activeInputs[priority];
|
2013-08-13 11:10:45 +02:00
|
|
|
input.priority = priority;
|
2018-12-27 23:11:32 +01:00
|
|
|
input.timeoutTime_ms = newInput ? -100 : input.timeoutTime_ms;
|
2016-10-10 18:29:54 +02:00
|
|
|
input.componentId = component;
|
2017-02-28 17:53:41 +01:00
|
|
|
input.origin = origin;
|
2017-08-04 12:01:45 +02:00
|
|
|
input.smooth_cfg = smooth_cfg;
|
2018-12-27 23:11:32 +01:00
|
|
|
input.owner = owner;
|
|
|
|
|
|
|
|
if(newInput)
|
|
|
|
{
|
|
|
|
Debug(_log,"Register new input '%s/%s' with priority %d as inactive", QSTRING_CSTR(origin), hyperion::componentToIdString(component), priority);
|
|
|
|
emit priorityChanged(priority, true);
|
|
|
|
emit prioritiesChanged();
|
|
|
|
return;
|
|
|
|
}
|
2013-08-13 11:10:45 +02:00
|
|
|
}
|
|
|
|
|
2019-06-05 18:19:08 +02:00
|
|
|
bool PriorityMuxer::setInput(const int priority, const std::vector<ColorRgb>& ledColors, int64_t timeout_ms)
|
2013-08-13 11:10:45 +02:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
if(!_activeInputs.contains(priority))
|
2013-08-13 11:10:45 +02:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
Error(_log,"setInput() used without registerInput() for priority '%d', probably the priority reached timeout",priority);
|
|
|
|
return false;
|
2013-08-13 11:10:45 +02:00
|
|
|
}
|
2018-12-27 23:11:32 +01:00
|
|
|
|
|
|
|
// calc final timeout
|
|
|
|
if(timeout_ms > 0)
|
|
|
|
timeout_ms = QDateTime::currentMSecsSinceEpoch() + timeout_ms;
|
|
|
|
|
|
|
|
InputInfo& input = _activeInputs[priority];
|
|
|
|
// detect active <-> inactive changes
|
|
|
|
bool activeChange = false;
|
|
|
|
bool active = true;
|
|
|
|
if(input.timeoutTime_ms == -100 && timeout_ms != -100)
|
|
|
|
{
|
|
|
|
activeChange = true;
|
|
|
|
}
|
|
|
|
else if(timeout_ms == -100 && input.timeoutTime_ms != -100)
|
|
|
|
{
|
|
|
|
active = false;
|
|
|
|
activeChange = true;
|
|
|
|
}
|
|
|
|
// update input
|
|
|
|
input.timeoutTime_ms = timeout_ms;
|
|
|
|
input.ledColors = ledColors;
|
2019-08-24 22:53:30 +02:00
|
|
|
input.image.clear();
|
2018-12-27 23:11:32 +01:00
|
|
|
|
|
|
|
// emit active change
|
|
|
|
if(activeChange)
|
|
|
|
{
|
|
|
|
Debug(_log, "Priority %d is now %s", priority, active ? "active" : "inactive");
|
|
|
|
emit activeStateChanged(priority, active);
|
|
|
|
setCurrentTime();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-05 18:19:08 +02:00
|
|
|
bool PriorityMuxer::setInputImage(const int priority, const Image<ColorRgb>& image, int64_t timeout_ms)
|
2018-12-27 23:11:32 +01:00
|
|
|
{
|
|
|
|
if(!_activeInputs.contains(priority))
|
|
|
|
{
|
|
|
|
Error(_log,"setInputImage() used without registerInput() for priority '%d', probably the priority reached timeout",priority);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// calc final timeout
|
|
|
|
if(timeout_ms > 0)
|
|
|
|
timeout_ms = QDateTime::currentMSecsSinceEpoch() + timeout_ms;
|
|
|
|
|
|
|
|
InputInfo& input = _activeInputs[priority];
|
|
|
|
// detect active <-> inactive changes
|
|
|
|
bool activeChange = false;
|
|
|
|
bool active = true;
|
|
|
|
if(input.timeoutTime_ms == -100 && timeout_ms != -100)
|
|
|
|
{
|
|
|
|
activeChange = true;
|
|
|
|
}
|
|
|
|
else if(timeout_ms == -100 && input.timeoutTime_ms != -100)
|
|
|
|
{
|
|
|
|
active = false;
|
|
|
|
activeChange = true;
|
|
|
|
}
|
|
|
|
// update input
|
|
|
|
input.timeoutTime_ms = timeout_ms;
|
|
|
|
input.image = image;
|
2019-08-24 22:53:30 +02:00
|
|
|
input.ledColors.clear();
|
2018-12-27 23:11:32 +01:00
|
|
|
|
|
|
|
// emit active change
|
|
|
|
if(activeChange)
|
|
|
|
{
|
|
|
|
Debug(_log, "Priority %d is now %s", priority, active ? "active" : "inactive");
|
|
|
|
emit activeStateChanged(priority, active);
|
|
|
|
setCurrentTime();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-05 18:19:08 +02:00
|
|
|
bool PriorityMuxer::setInputInactive(const quint8& priority)
|
2018-12-28 18:12:45 +01:00
|
|
|
{
|
|
|
|
Image<ColorRgb> image;
|
|
|
|
return setInputImage(priority, image, -100);
|
|
|
|
}
|
|
|
|
|
2019-06-05 18:19:08 +02:00
|
|
|
bool PriorityMuxer::clearInput(const uint8_t priority)
|
2018-12-27 23:11:32 +01:00
|
|
|
{
|
|
|
|
if (priority < PriorityMuxer::LOWEST_PRIORITY && _activeInputs.remove(priority))
|
|
|
|
{
|
|
|
|
Debug(_log,"Removed source priority %d",priority);
|
|
|
|
// on clear success update _currentPriority
|
|
|
|
setCurrentTime();
|
|
|
|
emit priorityChanged(priority, false);
|
|
|
|
emit prioritiesChanged();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-08-13 11:10:45 +02:00
|
|
|
}
|
|
|
|
|
2017-08-12 07:55:32 +02:00
|
|
|
void PriorityMuxer::clearAll(bool forceClearAll)
|
2013-08-13 11:10:45 +02:00
|
|
|
{
|
2017-08-12 07:55:32 +02:00
|
|
|
if (forceClearAll)
|
2017-03-22 11:52:20 +01:00
|
|
|
{
|
2017-08-12 07:55:32 +02:00
|
|
|
_activeInputs.clear();
|
2017-09-01 08:50:37 +02:00
|
|
|
_currentPriority = PriorityMuxer::LOWEST_PRIORITY;
|
2017-08-12 07:55:32 +02:00
|
|
|
_activeInputs[_currentPriority] = _lowestPriorityInfo;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(auto key : _activeInputs.keys())
|
2017-03-22 11:52:20 +01:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
const InputInfo info = getInputInfo(key);
|
2019-08-21 16:10:35 +02:00
|
|
|
if ((info.componentId == hyperion::COMP_COLOR || info.componentId == hyperion::COMP_EFFECT || info.componentId == hyperion::COMP_IMAGE) && key < PriorityMuxer::LOWEST_PRIORITY-1)
|
2017-08-12 07:55:32 +02:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
clearInput(key);
|
2017-08-12 07:55:32 +02:00
|
|
|
}
|
2017-03-22 11:52:20 +01:00
|
|
|
}
|
|
|
|
}
|
2013-08-13 11:10:45 +02:00
|
|
|
}
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
void PriorityMuxer::setCurrentTime(void)
|
2013-08-13 11:10:45 +02:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
const int64_t now = QDateTime::currentMSecsSinceEpoch();
|
2019-07-20 11:28:16 +02:00
|
|
|
int newPriority;
|
|
|
|
_activeInputs.contains(0) ? newPriority = 0 : newPriority = PriorityMuxer::LOWEST_PRIORITY;
|
2013-08-13 11:10:45 +02:00
|
|
|
|
2013-08-18 13:33:56 +02:00
|
|
|
for (auto infoIt = _activeInputs.begin(); infoIt != _activeInputs.end();)
|
2013-08-13 11:10:45 +02:00
|
|
|
{
|
2017-04-03 05:19:05 +02:00
|
|
|
if (infoIt->timeoutTime_ms > 0 && infoIt->timeoutTime_ms <= now)
|
2013-08-13 11:10:45 +02:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
quint8 tPrio = infoIt->priority;
|
2013-08-18 13:33:56 +02:00
|
|
|
infoIt = _activeInputs.erase(infoIt);
|
2018-12-27 23:11:32 +01:00
|
|
|
Debug(_log,"Timeout clear for priority %d",tPrio);
|
|
|
|
emit priorityChanged(tPrio, false);
|
|
|
|
emit prioritiesChanged();
|
2013-08-13 11:10:45 +02:00
|
|
|
}
|
|
|
|
else
|
2017-08-04 12:01:45 +02:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
// timeoutTime of -100 is awaiting data (inactive); skip
|
2019-04-28 19:53:45 +02:00
|
|
|
if(infoIt->timeoutTime_ms > -100)
|
2018-12-27 23:11:32 +01:00
|
|
|
newPriority = qMin(newPriority, infoIt->priority);
|
|
|
|
|
2018-12-31 15:48:29 +01:00
|
|
|
// call timeTrigger when effect or color is running with timeout > 0, blacklist prio 255
|
2019-08-21 16:10:35 +02:00
|
|
|
if(infoIt->priority < 254 && infoIt->timeoutTime_ms > 0 && (infoIt->componentId == hyperion::COMP_EFFECT || infoIt->componentId == hyperion::COMP_COLOR || infoIt->componentId == hyperion::COMP_IMAGE))
|
2018-12-27 23:11:32 +01:00
|
|
|
emit signalTimeTrigger(); // as signal to prevent Threading issues
|
2018-12-31 15:48:29 +01:00
|
|
|
|
2013-08-13 11:10:45 +02:00
|
|
|
++infoIt;
|
|
|
|
}
|
|
|
|
}
|
2018-12-27 23:11:32 +01:00
|
|
|
// eval if manual selected prio is still available
|
|
|
|
if(!_sourceAutoSelectEnabled)
|
|
|
|
{
|
|
|
|
if(_activeInputs.contains(_manualSelectedPriority))
|
|
|
|
{
|
|
|
|
newPriority = _manualSelectedPriority;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Debug(_log, "The manual selected priority '%d' is no longer available, switching to auto selection", _manualSelectedPriority);
|
|
|
|
// update state, but no _currentPriority re-eval
|
|
|
|
setSourceAutoSelectEnabled(true, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// apply & emit on change (after apply!)
|
2020-02-15 22:47:27 +01:00
|
|
|
if (_currentPriority != newPriority)
|
2018-12-27 23:11:32 +01:00
|
|
|
{
|
2020-02-15 22:47:27 +01:00
|
|
|
_currentPriority = newPriority;
|
2018-12-27 23:11:32 +01:00
|
|
|
Debug(_log, "Set visible priority to %d", newPriority);
|
|
|
|
emit visiblePriorityChanged(newPriority);
|
2020-02-15 22:47:27 +01:00
|
|
|
// check for visible comp change
|
|
|
|
hyperion::Components comp = getComponentOfPriority(newPriority);
|
|
|
|
if (comp != _prevVisComp)
|
|
|
|
{
|
|
|
|
_prevVisComp = comp;
|
|
|
|
emit visibleComponentChanged(comp);
|
|
|
|
}
|
2018-12-27 23:11:32 +01:00
|
|
|
emit prioritiesChanged();
|
|
|
|
}
|
2013-08-13 11:10:45 +02:00
|
|
|
}
|
2017-06-17 23:29:04 +02:00
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
void PriorityMuxer::timeTrigger()
|
2017-06-17 23:29:04 +02:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
if(_blockTimer->isActive())
|
2017-06-17 23:29:04 +02:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
_timer->start(500);
|
2017-06-17 23:29:04 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
emit timeRunner();
|
|
|
|
_blockTimer->start(1000);
|
2017-06-17 23:29:04 +02:00
|
|
|
}
|
|
|
|
}
|