2013-11-26 21:38:24 +01:00
|
|
|
// Python includes
|
|
|
|
#include <Python.h>
|
|
|
|
|
2013-11-30 12:42:08 +01:00
|
|
|
// Qt includes
|
|
|
|
#include <QMetaType>
|
|
|
|
|
2013-11-26 21:38:24 +01:00
|
|
|
// effect engine includes
|
2013-11-24 16:10:48 +01:00
|
|
|
#include <effectengine/EffectEngine.h>
|
2013-11-26 21:38:24 +01:00
|
|
|
#include "Effect.h"
|
2013-11-24 16:10:48 +01:00
|
|
|
|
2013-11-30 12:42:08 +01:00
|
|
|
EffectEngine::EffectEngine(Hyperion * hyperion, const Json::Value & jsonEffectConfig) :
|
2013-11-24 16:10:48 +01:00
|
|
|
_hyperion(hyperion),
|
2013-11-26 21:38:24 +01:00
|
|
|
_availableEffects(),
|
2013-11-29 23:22:49 +01:00
|
|
|
_activeEffects(),
|
|
|
|
_mainThreadState(nullptr)
|
2013-11-24 16:10:48 +01:00
|
|
|
{
|
2013-11-29 23:22:49 +01:00
|
|
|
qRegisterMetaType<std::vector<ColorRgb>>("std::vector<ColorRgb>");
|
|
|
|
|
2013-11-24 16:10:48 +01:00
|
|
|
// connect the Hyperion channel clear feedback
|
|
|
|
connect(_hyperion, SIGNAL(channelCleared(int)), this, SLOT(channelCleared(int)));
|
|
|
|
connect(_hyperion, SIGNAL(allChannelsCleared()), this, SLOT(allChannelsCleared()));
|
|
|
|
|
|
|
|
// read all effects
|
2013-11-30 12:42:08 +01:00
|
|
|
std::vector<std::string> effectNames = jsonEffectConfig.getMemberNames();
|
|
|
|
for (const std::string & name : effectNames)
|
|
|
|
{
|
|
|
|
const Json::Value & info = jsonEffectConfig[name];
|
2013-12-01 14:09:01 +01:00
|
|
|
_availableEffects.push_back({name, info["script"].asString(), info["args"]});
|
2013-11-30 12:42:08 +01:00
|
|
|
}
|
2013-11-26 21:38:24 +01:00
|
|
|
|
|
|
|
// initialize the python interpreter
|
|
|
|
std::cout << "Initializing Python interpreter" << std::endl;
|
|
|
|
Py_InitializeEx(0);
|
|
|
|
PyEval_InitThreads(); // Create the GIL
|
2013-11-29 23:22:49 +01:00
|
|
|
_mainThreadState = PyEval_SaveThread();
|
2013-11-24 16:10:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
EffectEngine::~EffectEngine()
|
|
|
|
{
|
2013-11-26 21:38:24 +01:00
|
|
|
// clean up the Python interpreter
|
|
|
|
std::cout << "Cleaning up Python interpreter" << std::endl;
|
2013-11-29 23:22:49 +01:00
|
|
|
PyEval_RestoreThread(_mainThreadState);
|
|
|
|
Py_Finalize();
|
2013-11-24 16:10:48 +01:00
|
|
|
}
|
|
|
|
|
2013-12-01 14:09:01 +01:00
|
|
|
const std::list<EffectDefinition> &EffectEngine::getEffects() const
|
2013-11-24 16:10:48 +01:00
|
|
|
{
|
2013-12-01 14:09:01 +01:00
|
|
|
return _availableEffects;
|
2013-11-24 16:10:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int EffectEngine::runEffect(const std::string &effectName, int priority, int timeout)
|
2013-12-01 14:09:01 +01:00
|
|
|
{
|
|
|
|
return runEffect(effectName, Json::Value(Json::nullValue), priority, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
int EffectEngine::runEffect(const std::string &effectName, const Json::Value &args, int priority, int timeout)
|
2013-11-24 16:10:48 +01:00
|
|
|
{
|
|
|
|
std::cout << "run effect " << effectName << " on channel " << priority << std::endl;
|
2013-11-26 21:38:24 +01:00
|
|
|
|
2013-12-01 14:09:01 +01:00
|
|
|
const EffectDefinition * effectDefinition = nullptr;
|
|
|
|
for (const EffectDefinition & e : _availableEffects)
|
|
|
|
{
|
|
|
|
if (e.name == effectName)
|
|
|
|
{
|
|
|
|
effectDefinition = &e;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (effectDefinition == nullptr)
|
2013-11-30 00:28:04 +01:00
|
|
|
{
|
|
|
|
// no such effect
|
2013-12-01 14:09:01 +01:00
|
|
|
std::cerr << "effect " << effectName << " not found" << std::endl;
|
2013-11-30 00:28:04 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-12-01 16:35:45 +01:00
|
|
|
return runEffectScript(effectDefinition->script, args.isNull() ? effectDefinition->args : args, priority, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
int EffectEngine::runEffectScript(const std::string &script, const Json::Value &args, int priority, int timeout)
|
|
|
|
{
|
2013-11-26 21:38:24 +01:00
|
|
|
// clear current effect on the channel
|
|
|
|
channelCleared(priority);
|
|
|
|
|
|
|
|
// create the effect
|
2013-12-01 16:35:45 +01:00
|
|
|
Effect * effect = new Effect(priority, timeout, script, args);
|
2013-11-29 23:22:49 +01:00
|
|
|
connect(effect, SIGNAL(setColors(int,std::vector<ColorRgb>,int)), _hyperion, SLOT(setColors(int,std::vector<ColorRgb>,int)), Qt::QueuedConnection);
|
2013-11-26 21:38:24 +01:00
|
|
|
connect(effect, SIGNAL(effectFinished(Effect*)), this, SLOT(effectFinished(Effect*)));
|
|
|
|
_activeEffects.push_back(effect);
|
|
|
|
|
|
|
|
// start the effect
|
|
|
|
effect->start();
|
|
|
|
|
2013-11-24 16:10:48 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EffectEngine::channelCleared(int priority)
|
|
|
|
{
|
2013-11-26 21:38:24 +01:00
|
|
|
for (Effect * effect : _activeEffects)
|
|
|
|
{
|
|
|
|
if (effect->getPriority() == priority)
|
|
|
|
{
|
|
|
|
effect->abort();
|
|
|
|
}
|
|
|
|
}
|
2013-11-24 16:10:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EffectEngine::allChannelsCleared()
|
|
|
|
{
|
2013-11-26 21:38:24 +01:00
|
|
|
for (Effect * effect : _activeEffects)
|
|
|
|
{
|
|
|
|
effect->abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EffectEngine::effectFinished(Effect *effect)
|
|
|
|
{
|
2013-11-30 15:54:47 +01:00
|
|
|
if (!effect->isAbortRequested())
|
|
|
|
{
|
|
|
|
// effect stopped by itself. Clear the channel
|
|
|
|
_hyperion->clear(effect->getPriority());
|
|
|
|
}
|
|
|
|
|
2013-11-26 21:38:24 +01:00
|
|
|
std::cout << "effect finished" << std::endl;
|
|
|
|
for (auto effectIt = _activeEffects.begin(); effectIt != _activeEffects.end(); ++effectIt)
|
|
|
|
{
|
|
|
|
if (*effectIt == effect)
|
|
|
|
{
|
|
|
|
_activeEffects.erase(effectIt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup the effect
|
|
|
|
effect->deleteLater();
|
2013-11-24 16:10:48 +01:00
|
|
|
}
|