hyperion.ng/libsrc/hyperion/PriorityMuxer.cpp

358 lines
9.7 KiB
C++
Raw Normal View History

// STL includes
#include <algorithm>
#include <limits>
2018-12-27 23:11:32 +01:00
// qt incl
#include <QDateTime>
#include <QTimer>
// Hyperion includes
#include <hyperion/PriorityMuxer.h>
2018-12-27 23:11:32 +01:00
// utils
#include <utils/Logger.h>
const int PriorityMuxer::LOWEST_PRIORITY = std::numeric_limits<uint8_t>::max();
PriorityMuxer::PriorityMuxer(int ledCount, QObject * parent)
: QObject(parent)
2018-12-27 23:11:32 +01:00
, _log(Logger::getInstance("HYPERION"))
, _currentPriority(PriorityMuxer::LOWEST_PRIORITY)
, _previousPriority(_currentPriority)
2018-12-27 23:11:32 +01:00
, _manualSelectedPriority(256)
, _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))
{
2018-12-27 23:11:32 +01:00
// init lowest priority info
_lowestPriorityInfo.priority = PriorityMuxer::LOWEST_PRIORITY;
2018-12-27 23:11:32 +01:00
_lowestPriorityInfo.timeoutTime_ms = -1;
_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;
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);
connect(this, &PriorityMuxer::activeStateChanged, this, &PriorityMuxer::prioritiesChanged);
2018-12-27 23:11:32 +01:00
// start muxer timer
connect(_updateTimer, &QTimer::timeout, this, &PriorityMuxer::setCurrentTime);
_updateTimer->setInterval(250);
_updateTimer->start();
}
PriorityMuxer::~PriorityMuxer()
{
}
2020-08-08 13:09:15 +02:00
void PriorityMuxer::setEnable(bool enable)
2018-12-27 23:11:32 +01:00
{
enable ? _updateTimer->start() : _updateTimer->stop();
}
2020-08-08 13:09:15 +02:00
bool PriorityMuxer::setSourceAutoSelectEnabled(bool enable, bool update)
2018-12-27 23:11:32 +01:00
{
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;
}
2020-08-08 13:09:15 +02:00
bool PriorityMuxer::setPriority(uint8_t priority)
2018-12-27 23:11:32 +01:00
{
if(_activeInputs.contains(priority))
{
_manualSelectedPriority = priority;
// update auto select state -> update _currentPriority
setSourceAutoSelectEnabled(false);
return true;
}
return false;
}
2020-08-08 13:09:15 +02:00
void PriorityMuxer::updateLedColorsLength(int ledCount)
2018-12-27 23:11:32 +01:00
{
for (auto infoIt = _activeInputs.begin(); infoIt != _activeInputs.end();)
{
if (infoIt->ledColors.size() >= 1)
{
infoIt->ledColors.resize(ledCount, infoIt->ledColors.at(0));
}
++infoIt;
}
}
QList<int> PriorityMuxer::getPriorities() const
{
2013-08-18 13:33:56 +02:00
return _activeInputs.keys();
}
2020-08-08 13:09:15 +02:00
bool PriorityMuxer::hasPriority(int priority) const
{
return (priority == PriorityMuxer::LOWEST_PRIORITY) ? true : _activeInputs.contains(priority);
}
2020-08-08 13:09:15 +02:00
PriorityMuxer::InputInfo PriorityMuxer::getInputInfo(int priority) const
{
2013-08-18 13:33:56 +02:00
auto elemIt = _activeInputs.find(priority);
if (elemIt == _activeInputs.end())
{
elemIt = _activeInputs.find(PriorityMuxer::LOWEST_PRIORITY);
if (elemIt == _activeInputs.end())
{
2018-12-27 23:11:32 +01:00
// fallback
return _lowestPriorityInfo;
}
}
return elemIt.value();
}
hyperion::Components PriorityMuxer::getComponentOfPriority(int priority) const
{
return _activeInputs[priority].componentId;
}
2020-08-08 13:09:15 +02:00
void PriorityMuxer::registerInput(int priority, hyperion::Components component, const QString& origin, const QString& owner, unsigned smooth_cfg)
{
2018-12-27 23:11:32 +01:00
// detect new registers
bool newInput = false;
if(!_activeInputs.contains(priority))
newInput = true;
InputInfo& input = _activeInputs[priority];
input.priority = priority;
2018-12-27 23:11:32 +01:00
input.timeoutTime_ms = newInput ? -100 : input.timeoutTime_ms;
input.componentId = component;
input.origin = origin;
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 prioritiesChanged();
return;
}
}
2020-08-08 13:09:15 +02:00
bool PriorityMuxer::setInput(int priority, const std::vector<ColorRgb>& ledColors, int64_t timeout_ms)
{
2018-12-27 23:11:32 +01:00
if(!_activeInputs.contains(priority))
{
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;
}
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;
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;
}
2020-08-08 13:09:15 +02:00
bool PriorityMuxer::setInputImage(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;
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;
}
2020-08-08 13:09:15 +02:00
bool PriorityMuxer::setInputInactive(quint8 priority)
{
Image<ColorRgb> image;
return setInputImage(priority, image, -100);
}
2020-08-08 13:09:15 +02:00
bool PriorityMuxer::clearInput(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 prioritiesChanged();
return true;
}
return false;
}
void PriorityMuxer::clearAll(bool forceClearAll)
{
if (forceClearAll)
{
_previousPriority = _currentPriority;
_activeInputs.clear();
_currentPriority = PriorityMuxer::LOWEST_PRIORITY;
_activeInputs[_currentPriority] = _lowestPriorityInfo;
}
else
{
for(auto key : _activeInputs.keys())
{
2018-12-27 23:11:32 +01:00
const InputInfo info = getInputInfo(key);
if ((info.componentId == hyperion::COMP_COLOR || info.componentId == hyperion::COMP_EFFECT || info.componentId == hyperion::COMP_IMAGE) && key < PriorityMuxer::LOWEST_PRIORITY-1)
{
2018-12-27 23:11:32 +01:00
clearInput(key);
}
}
}
}
2020-08-08 13:09:15 +02:00
void PriorityMuxer::setCurrentTime()
{
2018-12-27 23:11:32 +01:00
const int64_t now = QDateTime::currentMSecsSinceEpoch();
int newPriority;
_activeInputs.contains(0) ? newPriority = 0 : newPriority = PriorityMuxer::LOWEST_PRIORITY;
2013-08-18 13:33:56 +02:00
for (auto infoIt = _activeInputs.begin(); infoIt != _activeInputs.end();)
{
if (infoIt->timeoutTime_ms > 0 && infoIt->timeoutTime_ms <= now)
{
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 prioritiesChanged();
}
else
{
2018-12-27 23:11:32 +01:00
// timeoutTime of -100 is awaiting data (inactive); skip
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
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
++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!)
if (_currentPriority != newPriority)
2018-12-27 23:11:32 +01:00
{
_previousPriority = _currentPriority;
_currentPriority = newPriority;
2018-12-27 23:11:32 +01:00
Debug(_log, "Set visible priority to %d", newPriority);
emit visiblePriorityChanged(newPriority);
// 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();
}
}
2018-12-27 23:11:32 +01:00
void PriorityMuxer::timeTrigger()
{
2018-12-27 23:11:32 +01:00
if(_blockTimer->isActive())
{
2018-12-27 23:11:32 +01:00
_timer->start(500);
}
else
{
2018-12-27 23:11:32 +01:00
emit timeRunner();
_blockTimer->start(1000);
}
}