Python interpreter added to EffectEngine

Former-commit-id: f721f5952efe305d66347d9074ff760baabd2f18
This commit is contained in:
johan 2013-11-26 21:38:24 +01:00
parent 72acdd655d
commit d24fddaf21
4 changed files with 182 additions and 5 deletions

View File

@ -2,6 +2,9 @@
#include <hyperion/Hyperion.h> #include <hyperion/Hyperion.h>
// pre-declarioation
class Effect;
class EffectEngine : public QObject class EffectEngine : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -22,8 +25,13 @@ public slots:
/// Clear all effects /// Clear all effects
void allChannelsCleared(); void allChannelsCleared();
private slots:
void effectFinished(Effect * effect);
private: private:
Hyperion * _hyperion; Hyperion * _hyperion;
std::map<std::string, std::string> _availableEffects; std::map<std::string, std::string> _availableEffects;
std::list<Effect *> _activeEffects;
}; };

View File

@ -1,10 +1,98 @@
// stl includes
#include <sstream>
// effect engin eincludes
#include "Effect.h" #include "Effect.h"
Effect::Effect() // Effect wrapper methods for Python interpreter extra build in methods
static PyObject* Effect_SetColor(PyObject *self, PyObject *args)
{ {
return Py_BuildValue("i", 42);
}
static PyObject* Effect_SetImage(PyObject *self, PyObject *args)
{
return Py_BuildValue("i", 42);
}
static PyObject* Effect_GetLedCount(PyObject *self, PyObject *args)
{
return Py_BuildValue("i", 42);
}
static PyObject* Effect_IsAbortRequested(PyObject *self, PyObject *args)
{
return Py_BuildValue("i", 42);
}
static PyMethodDef effectMethods[] = {
{"setColor", Effect_SetColor, METH_VARARGS, "Set a new color for the leds."},
{"setImage", Effect_SetImage, METH_VARARGS, "Set a new image to process and determine new led colors."},
{"getLedCount", Effect_GetLedCount, METH_VARARGS, "Get the number of avaliable led channels."},
{"isAbortRequested", Effect_IsAbortRequested, METH_VARARGS, "Check if the effect should abort execution."},
{NULL, NULL, 0, NULL}
};
Effect::Effect(int priority, int timeout) :
QThread(),
_priority(priority),
_timeout(timeout),
_interpreterThreadState(nullptr),
_abortRequested(false)
{
// connect the finished signal
connect(this, SIGNAL(finished()), this, SLOT(effectFinished()));
} }
Effect::~Effect() Effect::~Effect()
{ {
} }
void Effect::run()
{
// Initialize a new thread state
PyEval_AcquireLock(); // Get the GIL
_interpreterThreadState = Py_NewInterpreter();
Py_InitModule("hyperiond", effectMethods);
// Create hyperion instance in the new interpreter
std::ostringstream oss;
oss << "import hyperiond" << std::endl;
oss << "class Hyperion:" << std::endl;
oss << " def setColor(self):" << std::endl;
oss << " return hyperiond.setColor()" << std::endl;
oss << " def setImage(self):" << std::endl;
oss << " return hyperiond.setImage()" << std::endl;
oss << " def getLedCount(self):" << std::endl;
oss << " return hyperiond.getLedCount()" << std::endl;
oss << " def isAbortRequested(self):" << std::endl;
oss << " return hyperiond.isAbortRequested()" << std::endl;
oss << "hyperion = Hyperion()" << std::endl;
PyRun_SimpleString(oss.str().c_str());
// Run the effect script
std::string script = "test.py";
FILE* file = fopen(script.c_str(), "r");
PyRun_SimpleFile(file, script.c_str());
// Clean up the thread state
Py_EndInterpreter(_interpreterThreadState);
_interpreterThreadState = nullptr;
PyEval_ReleaseLock();
}
int Effect::getPriority() const
{
return _priority;
}
void Effect::abort()
{
_abortRequested = true;
}
void Effect::effectFinished()
{
emit effectFinished(this);
}

View File

@ -1,12 +1,38 @@
#pragma once #pragma once
#include <QObject> // Qt includes
#include <QThread>
class Effect : public QObject // Python includes
#include <Python.h>
class Effect : public QThread
{ {
Q_OBJECT Q_OBJECT
public: public:
Effect(); Effect(int priority, int timeout);
virtual ~Effect(); virtual ~Effect();
virtual void run();
int getPriority() const;
public slots:
void abort();
signals:
void effectFinished(Effect * effect);
private slots:
void effectFinished();
private:
const int _priority;
const int _timeout;
PyThreadState * _interpreterThreadState;
bool _abortRequested;
}; };

View File

@ -1,8 +1,14 @@
// Python includes
#include <Python.h>
// effect engine includes
#include <effectengine/EffectEngine.h> #include <effectengine/EffectEngine.h>
#include "Effect.h"
EffectEngine::EffectEngine(Hyperion * hyperion) : EffectEngine::EffectEngine(Hyperion * hyperion) :
_hyperion(hyperion), _hyperion(hyperion),
_availableEffects() _availableEffects(),
_activeEffects()
{ {
// connect the Hyperion channel clear feedback // connect the Hyperion channel clear feedback
connect(_hyperion, SIGNAL(channelCleared(int)), this, SLOT(channelCleared(int))); connect(_hyperion, SIGNAL(channelCleared(int)), this, SLOT(channelCleared(int)));
@ -10,10 +16,20 @@ EffectEngine::EffectEngine(Hyperion * hyperion) :
// read all effects // read all effects
_availableEffects["test"] = "test.py"; _availableEffects["test"] = "test.py";
// initialize the python interpreter
std::cout << "Initializing Python interpreter" << std::endl;
Py_InitializeEx(0);
PyEval_InitThreads(); // Create the GIL
PyEval_ReleaseLock(); // Release the GIL
} }
EffectEngine::~EffectEngine() EffectEngine::~EffectEngine()
{ {
// clean up the Python interpreter
std::cout << "Cleaning up Python interpreter" << std::endl;
PyEval_AcquireLock(); // Get the Gil
Py_Finalize();
} }
std::list<std::string> EffectEngine::getEffects() const std::list<std::string> EffectEngine::getEffects() const
@ -28,15 +44,54 @@ std::list<std::string> EffectEngine::getEffects() const
int EffectEngine::runEffect(const std::string &effectName, int priority, int timeout) int EffectEngine::runEffect(const std::string &effectName, int priority, int timeout)
{ {
std::cout << "run effect " << effectName << " on channel " << priority << std::endl; std::cout << "run effect " << effectName << " on channel " << priority << std::endl;
// 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();
return 0; return 0;
} }
void EffectEngine::channelCleared(int priority) void EffectEngine::channelCleared(int priority)
{ {
std::cout << "clear effect on channel " << priority << std::endl; std::cout << "clear effect on channel " << priority << std::endl;
for (Effect * effect : _activeEffects)
{
if (effect->getPriority() == priority)
{
effect->abort();
}
}
} }
void EffectEngine::allChannelsCleared() void EffectEngine::allChannelsCleared()
{ {
std::cout << "clear effect on every channel" << std::endl; std::cout << "clear effect on every channel" << std::endl;
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();
} }