2013-11-26 21:38:24 +01:00
|
|
|
// stl includes
|
2013-11-27 20:15:29 +01:00
|
|
|
#include <iostream>
|
2013-11-26 21:38:24 +01:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
// effect engin eincludes
|
2013-11-24 16:10:48 +01:00
|
|
|
#include "Effect.h"
|
|
|
|
|
2013-11-26 21:38:24 +01:00
|
|
|
// Effect wrapper methods for Python interpreter extra build in methods
|
2013-11-27 20:15:29 +01:00
|
|
|
static PyObject* Effect_setColor(PyObject *self, PyObject *args)
|
2013-11-26 21:38:24 +01:00
|
|
|
{
|
|
|
|
return Py_BuildValue("i", 42);
|
|
|
|
}
|
|
|
|
|
2013-11-27 20:15:29 +01:00
|
|
|
static PyObject* Effect_setImage(PyObject *self, PyObject *args)
|
2013-11-26 21:38:24 +01:00
|
|
|
{
|
|
|
|
return Py_BuildValue("i", 42);
|
|
|
|
}
|
|
|
|
|
2013-11-27 20:15:29 +01:00
|
|
|
static PyObject* Effect_getLedCount(PyObject *self, PyObject *args)
|
2013-11-26 21:38:24 +01:00
|
|
|
{
|
|
|
|
return Py_BuildValue("i", 42);
|
|
|
|
}
|
|
|
|
|
2013-11-27 20:15:29 +01:00
|
|
|
static PyObject* Effect_abort(PyObject *self, PyObject *)
|
2013-11-26 21:38:24 +01:00
|
|
|
{
|
2013-11-27 20:15:29 +01:00
|
|
|
Effect * effect = reinterpret_cast<Effect *>(PyCapsule_GetPointer(self, nullptr));
|
|
|
|
bool abort = effect->isAbortedRequested();
|
|
|
|
return Py_BuildValue("i", abort ? 1 : 0);
|
2013-11-26 21:38:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef effectMethods[] = {
|
2013-11-27 20:15:29 +01:00
|
|
|
{"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."},
|
|
|
|
{"abort", Effect_abort, METH_NOARGS , "Check if the effect should abort execution."},
|
2013-11-26 21:38:24 +01:00
|
|
|
{NULL, NULL, 0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Effect::Effect(int priority, int timeout) :
|
|
|
|
QThread(),
|
|
|
|
_priority(priority),
|
|
|
|
_timeout(timeout),
|
|
|
|
_interpreterThreadState(nullptr),
|
|
|
|
_abortRequested(false)
|
2013-11-24 16:10:48 +01:00
|
|
|
{
|
2013-11-26 21:38:24 +01:00
|
|
|
// connect the finished signal
|
|
|
|
connect(this, SIGNAL(finished()), this, SLOT(effectFinished()));
|
2013-11-24 16:10:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Effect::~Effect()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-11-26 21:38:24 +01:00
|
|
|
void Effect::run()
|
|
|
|
{
|
|
|
|
// Initialize a new thread state
|
|
|
|
PyEval_AcquireLock(); // Get the GIL
|
|
|
|
_interpreterThreadState = Py_NewInterpreter();
|
2013-11-27 20:15:29 +01:00
|
|
|
|
|
|
|
// add methods extra builtin methods to the interpreter
|
|
|
|
PyObject * thisCapsule = PyCapsule_New(this, nullptr, nullptr);
|
|
|
|
Py_InitModule4("hyperion", effectMethods, nullptr, thisCapsule, PYTHON_API_VERSION);
|
|
|
|
std::cout << this << std::endl;
|
2013-11-26 21:38:24 +01:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2013-11-27 20:15:29 +01:00
|
|
|
bool Effect::isAbortedRequested() const
|
|
|
|
{
|
|
|
|
return _abortRequested;
|
|
|
|
}
|
|
|
|
|
2013-11-26 21:38:24 +01:00
|
|
|
int Effect::getPriority() const
|
|
|
|
{
|
|
|
|
return _priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Effect::abort()
|
|
|
|
{
|
|
|
|
_abortRequested = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Effect::effectFinished()
|
|
|
|
{
|
|
|
|
emit effectFinished(this);
|
|
|
|
}
|