hyperion.ng/libsrc/hyperion/PriorityMuxer.cpp

105 lines
2.2 KiB
C++
Raw Normal View History

// STL includes
#include <algorithm>
#include <stdexcept>
// Hyperion includes
#include <hyperion/PriorityMuxer.h>
PriorityMuxer::PriorityMuxer(int ledCount)
: _currentPriority(LOWEST_PRIORITY)
, _activeInputs()
, _lowestPriorityInfo()
{
2013-08-18 13:33:56 +02:00
_lowestPriorityInfo.priority = LOWEST_PRIORITY;
_lowestPriorityInfo.timeoutTime_ms = -1;
_lowestPriorityInfo.ledColors = std::vector<ColorRgb>(ledCount, {0, 0, 0});
}
PriorityMuxer::~PriorityMuxer()
{
// empty
}
int PriorityMuxer::getCurrentPriority() const
{
2013-08-18 13:33:56 +02:00
return _currentPriority;
}
QList<int> PriorityMuxer::getPriorities() const
{
2013-08-18 13:33:56 +02:00
return _activeInputs.keys();
}
bool PriorityMuxer::hasPriority(const int priority) const
{
2013-08-18 13:33:56 +02:00
return _activeInputs.contains(priority);
}
const PriorityMuxer::InputInfo& PriorityMuxer::getInputInfo(const int priority) const
{
2013-08-18 13:33:56 +02:00
if (priority == LOWEST_PRIORITY)
{
return _lowestPriorityInfo;
}
auto elemIt = _activeInputs.find(priority);
if (elemIt == _activeInputs.end())
{
std::cout << "error " << priority << std::endl;
throw std::runtime_error("HYPERION (prioritymux) ERROR: no such priority");
}
return elemIt.value();
}
void PriorityMuxer::setInput(const int priority, const std::vector<ColorRgb>& ledColors, const int64_t timeoutTime_ms)
{
2013-08-18 13:33:56 +02:00
InputInfo& input = _activeInputs[priority];
input.priority = priority;
input.timeoutTime_ms = timeoutTime_ms;
input.ledColors = ledColors;
2013-08-18 13:33:56 +02:00
_currentPriority = std::min(_currentPriority, priority);
}
void PriorityMuxer::clearInput(const int priority)
{
2013-08-18 13:33:56 +02:00
_activeInputs.remove(priority);
if (_currentPriority == priority)
{
2013-08-18 13:33:56 +02:00
if (_activeInputs.empty())
{
2013-08-18 13:33:56 +02:00
_currentPriority = LOWEST_PRIORITY;
}
else
{
2013-08-18 13:33:56 +02:00
QList<int> keys = _activeInputs.keys();
_currentPriority = *std::min_element(keys.begin(), keys.end());
}
}
}
void PriorityMuxer::clearAll()
{
2013-08-18 13:33:56 +02:00
_activeInputs.clear();
_currentPriority = LOWEST_PRIORITY;
}
void PriorityMuxer::setCurrentTime(const int64_t& now)
{
2013-08-18 13:33:56 +02:00
_currentPriority = LOWEST_PRIORITY;
2013-08-18 13:33:56 +02:00
for (auto infoIt = _activeInputs.begin(); infoIt != _activeInputs.end();)
{
if (infoIt->timeoutTime_ms != -1 && infoIt->timeoutTime_ms <= now)
{
2013-08-18 13:33:56 +02:00
infoIt = _activeInputs.erase(infoIt);
}
else
{
2013-08-18 13:33:56 +02:00
_currentPriority = std::min(_currentPriority, infoIt->priority);
++infoIt;
}
}
}