mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
clear and clear all implemented
This commit is contained in:
@@ -76,74 +76,108 @@ LedString Hyperion::createLedString(const Json::Value& ledsConfig)
|
||||
}
|
||||
|
||||
Hyperion::Hyperion(const Json::Value &jsonConfig) :
|
||||
mLedString(createLedString(jsonConfig["leds"])),
|
||||
mRedTransform( createColorTransform(jsonConfig["color"]["red"])),
|
||||
mGreenTransform(createColorTransform(jsonConfig["color"]["green"])),
|
||||
mBlueTransform( createColorTransform(jsonConfig["color"]["blue"])),
|
||||
mDevice(constructDevice(jsonConfig["device"])),
|
||||
_ledString(createLedString(jsonConfig["leds"])),
|
||||
_muxer(_ledString.leds().size()),
|
||||
_redTransform(createColorTransform(jsonConfig["color"]["red"])),
|
||||
_greenTransform(createColorTransform(jsonConfig["color"]["green"])),
|
||||
_blueTransform(createColorTransform(jsonConfig["color"]["blue"])),
|
||||
_device(constructDevice(jsonConfig["device"])),
|
||||
_timer()
|
||||
{
|
||||
ImageProcessorFactory::getInstance().init(mLedString);
|
||||
ImageProcessorFactory::getInstance().init(_ledString);
|
||||
|
||||
_timer.setSingleShot(true);
|
||||
QObject::connect(&_timer, SIGNAL(timeout()), this, SLOT(update()));
|
||||
|
||||
// initialize the leds
|
||||
update();
|
||||
}
|
||||
|
||||
|
||||
Hyperion::~Hyperion()
|
||||
{
|
||||
// Delete the Led-String
|
||||
delete mDevice;
|
||||
delete _device;
|
||||
|
||||
// Delete the color-transform
|
||||
delete mBlueTransform;
|
||||
delete mGreenTransform;
|
||||
delete mRedTransform;
|
||||
delete _blueTransform;
|
||||
delete _greenTransform;
|
||||
delete _redTransform;
|
||||
}
|
||||
|
||||
unsigned Hyperion::getLedCount() const
|
||||
{
|
||||
return mLedString.leds().size();
|
||||
return _ledString.leds().size();
|
||||
}
|
||||
|
||||
void Hyperion::setValue(int priority, std::vector<RgbColor>& ledColors, const int timeout_ms)
|
||||
void Hyperion::setColor(int priority, RgbColor & color, const int timeout_ms)
|
||||
{
|
||||
// create led output
|
||||
std::vector<RgbColor> ledColors(_ledString.leds().size(), color);
|
||||
|
||||
// set colors
|
||||
setColors(priority, ledColors, timeout_ms);
|
||||
}
|
||||
|
||||
void Hyperion::setColors(int priority, std::vector<RgbColor>& ledColors, const int timeout_ms)
|
||||
{
|
||||
// Apply the transform to each led and color-channel
|
||||
for (RgbColor& color : ledColors)
|
||||
{
|
||||
color.red = mRedTransform->transform(color.red);
|
||||
color.green = mGreenTransform->transform(color.green);
|
||||
color.blue = mBlueTransform->transform(color.blue);
|
||||
color.red = _redTransform->transform(color.red);
|
||||
color.green = _greenTransform->transform(color.green);
|
||||
color.blue = _blueTransform->transform(color.blue);
|
||||
}
|
||||
|
||||
if (timeout_ms > 0)
|
||||
{
|
||||
const uint64_t timeoutTime = QDateTime::currentMSecsSinceEpoch() + timeout_ms;
|
||||
mMuxer.setInput(priority, ledColors, timeoutTime);
|
||||
_muxer.setInput(priority, ledColors, timeoutTime);
|
||||
}
|
||||
else
|
||||
{
|
||||
mMuxer.setInput(priority, ledColors);
|
||||
_muxer.setInput(priority, ledColors);
|
||||
}
|
||||
|
||||
if (priority == mMuxer.getCurrentPriority())
|
||||
if (priority == _muxer.getCurrentPriority())
|
||||
{
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void Hyperion::clear(int priority)
|
||||
{
|
||||
if (_muxer.hasPriority(priority))
|
||||
{
|
||||
_muxer.clearInput(priority);
|
||||
|
||||
// update leds if necessary
|
||||
if (priority < _muxer.getCurrentPriority());
|
||||
{
|
||||
update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Hyperion::clearall()
|
||||
{
|
||||
_muxer.clearAll();
|
||||
|
||||
// update leds
|
||||
update();
|
||||
}
|
||||
|
||||
void Hyperion::update()
|
||||
{
|
||||
// Update the muxer, cleaning obsolete priorities
|
||||
mMuxer.setCurrentTime(QDateTime::currentMSecsSinceEpoch());
|
||||
_muxer.setCurrentTime(QDateTime::currentMSecsSinceEpoch());
|
||||
|
||||
// Obtain the current priority channel
|
||||
int priority = mMuxer.getCurrentPriority();
|
||||
const PriorityMuxer::InputInfo & priorityInfo = mMuxer.getInputInfo(priority);
|
||||
int priority = _muxer.getCurrentPriority();
|
||||
const PriorityMuxer::InputInfo & priorityInfo = _muxer.getInputInfo(priority);
|
||||
|
||||
// Write the data to the device
|
||||
mDevice->write(priorityInfo.ledColors);
|
||||
_device->write(priorityInfo.ledColors);
|
||||
|
||||
// Start the timeout-timer
|
||||
if (priorityInfo.timeoutTime_ms == -1)
|
||||
|
@@ -6,10 +6,14 @@
|
||||
// Hyperion includes
|
||||
#include <hyperion/PriorityMuxer.h>
|
||||
|
||||
PriorityMuxer::PriorityMuxer() :
|
||||
mCurrentPriority(MAX_PRIORITY)
|
||||
PriorityMuxer::PriorityMuxer(int ledCount) :
|
||||
_currentPriority(LOWEST_PRIORITY),
|
||||
_activeInputs(),
|
||||
_lowestPriorityInfo()
|
||||
{
|
||||
// empty
|
||||
_lowestPriorityInfo.priority = LOWEST_PRIORITY;
|
||||
_lowestPriorityInfo.timeoutTime_ms = -1;
|
||||
_lowestPriorityInfo.ledColors = std::vector<RgbColor>(ledCount, {0, 0, 0});
|
||||
}
|
||||
|
||||
PriorityMuxer::~PriorityMuxer()
|
||||
@@ -19,23 +23,28 @@ PriorityMuxer::~PriorityMuxer()
|
||||
|
||||
int PriorityMuxer::getCurrentPriority() const
|
||||
{
|
||||
return mCurrentPriority;
|
||||
return _currentPriority;
|
||||
}
|
||||
|
||||
QList<int> PriorityMuxer::getPriorities() const
|
||||
{
|
||||
return mActiveInputs.keys();
|
||||
return _activeInputs.keys();
|
||||
}
|
||||
|
||||
bool PriorityMuxer::hasPriority(const int priority) const
|
||||
{
|
||||
return mActiveInputs.contains(priority);
|
||||
return _activeInputs.contains(priority);
|
||||
}
|
||||
|
||||
const PriorityMuxer::InputInfo& PriorityMuxer::getInputInfo(const int priority) const
|
||||
{
|
||||
auto elemIt = mActiveInputs.find(priority);
|
||||
if (elemIt == mActiveInputs.end())
|
||||
if (priority == LOWEST_PRIORITY)
|
||||
{
|
||||
return _lowestPriorityInfo;
|
||||
}
|
||||
|
||||
auto elemIt = _activeInputs.find(priority);
|
||||
if (elemIt == _activeInputs.end())
|
||||
{
|
||||
throw std::runtime_error("no such priority");
|
||||
}
|
||||
@@ -44,50 +53,50 @@ const PriorityMuxer::InputInfo& PriorityMuxer::getInputInfo(const int priority)
|
||||
|
||||
void PriorityMuxer::setInput(const int priority, const std::vector<RgbColor>& ledColors, const int64_t timeoutTime_ms)
|
||||
{
|
||||
InputInfo& input = mActiveInputs[priority];
|
||||
InputInfo& input = _activeInputs[priority];
|
||||
input.priority = priority;
|
||||
input.timeoutTime_ms = timeoutTime_ms;
|
||||
input.ledColors = ledColors;
|
||||
|
||||
mCurrentPriority = std::min(mCurrentPriority, priority);
|
||||
_currentPriority = std::min(_currentPriority, priority);
|
||||
}
|
||||
|
||||
void PriorityMuxer::clearInput(const int priority)
|
||||
{
|
||||
mActiveInputs.remove(priority);
|
||||
if (mCurrentPriority == priority)
|
||||
_activeInputs.remove(priority);
|
||||
if (_currentPriority == priority)
|
||||
{
|
||||
if (mActiveInputs.empty())
|
||||
if (_activeInputs.empty())
|
||||
{
|
||||
mCurrentPriority = MAX_PRIORITY;
|
||||
_currentPriority = LOWEST_PRIORITY;
|
||||
}
|
||||
else
|
||||
{
|
||||
QList<int> keys = mActiveInputs.keys();
|
||||
mCurrentPriority = *std::min_element(keys.begin(), keys.end());
|
||||
QList<int> keys = _activeInputs.keys();
|
||||
_currentPriority = *std::min_element(keys.begin(), keys.end());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PriorityMuxer::clearAll()
|
||||
{
|
||||
mActiveInputs.clear();
|
||||
mCurrentPriority = MAX_PRIORITY;
|
||||
_activeInputs.clear();
|
||||
_currentPriority = LOWEST_PRIORITY;
|
||||
}
|
||||
|
||||
void PriorityMuxer::setCurrentTime(const int64_t& now)
|
||||
{
|
||||
mCurrentPriority = MAX_PRIORITY;
|
||||
_currentPriority = LOWEST_PRIORITY;
|
||||
|
||||
for (auto infoIt = mActiveInputs.begin(); infoIt != mActiveInputs.end();)
|
||||
for (auto infoIt = _activeInputs.begin(); infoIt != _activeInputs.end();)
|
||||
{
|
||||
if (infoIt->timeoutTime_ms != -1 && infoIt->timeoutTime_ms <= now)
|
||||
{
|
||||
infoIt = mActiveInputs.erase(infoIt);
|
||||
infoIt = _activeInputs.erase(infoIt);
|
||||
}
|
||||
else
|
||||
{
|
||||
mCurrentPriority = std::min(mCurrentPriority, infoIt->priority);
|
||||
_currentPriority = std::min(_currentPriority, infoIt->priority);
|
||||
++infoIt;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user