2013-11-26 21:38:24 +01:00
|
|
|
// Python includes
|
|
|
|
#include <Python.h>
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
EffectEngine::EffectEngine(Hyperion * hyperion) :
|
|
|
|
_hyperion(hyperion),
|
2013-11-26 21:38:24 +01:00
|
|
|
_availableEffects(),
|
|
|
|
_activeEffects()
|
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
|
|
|
|
_availableEffects["test"] = "test.py";
|
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
|
|
|
|
PyEval_ReleaseLock(); // Release the GIL
|
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;
|
|
|
|
PyEval_AcquireLock(); // Get the Gil
|
|
|
|
Py_Finalize();
|
2013-11-24 16:10:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::list<std::string> EffectEngine::getEffects() const
|
|
|
|
{
|
|
|
|
std::list<std::string> effectNames;
|
|
|
|
foreach (auto entry, _availableEffects) {
|
|
|
|
effectNames.push_back(entry.first);
|
|
|
|
}
|
|
|
|
return effectNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
int EffectEngine::runEffect(const std::string &effectName, int priority, int timeout)
|
|
|
|
{
|
|
|
|
std::cout << "run effect " << effectName << " on channel " << priority << std::endl;
|
2013-11-26 21:38:24 +01:00
|
|
|
|
|
|
|
// clear current effect on the channel
|
|
|
|
channelCleared(priority);
|
|
|
|
|
|
|
|
// create the effect
|
|
|
|
Effect * effect = new Effect(priority, timeout);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
std::cout << "clear effect on channel " << priority << std::endl;
|
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()
|
|
|
|
{
|
|
|
|
std::cout << "clear effect on every channel" << std::endl;
|
2013-11-26 21:38:24 +01:00
|
|
|
for (Effect * effect : _activeEffects)
|
|
|
|
{
|
|
|
|
effect->abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EffectEngine::effectFinished(Effect *effect)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|