Fix bug: Only 1 EffectEngine allowed; SetColors implemented for EffectEngine

Former-commit-id: ae8141ebd530b7735ffa7818dc3c690913769070
This commit is contained in:
johan
2013-11-29 23:22:49 +01:00
parent 1b18c7151f
commit 3bd1789c42
8 changed files with 157 additions and 59 deletions

View File

@@ -43,7 +43,7 @@ void Effect::run()
// add methods extra builtin methods to the interpreter
PyObject * thisCapsule = PyCapsule_New(this, nullptr, nullptr);
PyObject * module = Py_InitModule4("hyperion", effectMethods, nullptr, thisCapsule, PYTHON_API_VERSION);
// add ledCount variable to the interpreter
PyObject_SetAttrString(module, "ledCount", Py_BuildValue("i", _imageProcessor->getLedCount()));
@@ -81,14 +81,106 @@ void Effect::effectFinished()
PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args)
{
// get the effect
Effect * effect = getEffect(self);
return Py_BuildValue("i", 42);
// check if we have aborted already
if (effect->_abortRequested)
{
return Py_BuildValue("");
}
// determine the timeout
int timeout = effect->_timeout;
if (timeout > 0)
{
timeout = effect->_endTime - QDateTime::currentMSecsSinceEpoch();
// we are done if the time has passed
if (timeout <= 0)
{
return Py_BuildValue("");
}
}
// check the number of arguments
int argCount = PyTuple_Size(args);
if (argCount == 3)
{
// three seperate arguments for red, green, and blue
ColorRgb color;
if (PyArg_ParseTuple(args, "bbb", &color.red, &color.green, &color.blue))
{
effect->setColors(effect->_priority, std::vector<ColorRgb>(effect->_imageProcessor->getLedCount(), color), timeout);
return Py_BuildValue("");
}
else
{
return nullptr;
}
}
else if (argCount == 1)
{
// bytearray of values
PyObject * bytearray = nullptr;
if (PyArg_ParseTuple(args, "O", &bytearray))
{
if (PyByteArray_Check(bytearray))
{
size_t length = PyByteArray_Size(bytearray);
if (length == 3 * effect->_imageProcessor->getLedCount())
{
std::vector<ColorRgb> colors(effect->_imageProcessor->getLedCount());
char * data = PyByteArray_AS_STRING(bytearray);
for (size_t i = 0; i < colors.size(); ++i)
{
ColorRgb & color = colors[i];
color.red = data [3*i];
color.green = data [3*i+1];
color.blue = data [3*i+2];
}
effect->setColors(effect->_priority, colors, timeout);
return Py_BuildValue("");
}
else
{
PyErr_SetString(PyExc_RuntimeError, "Length of bytearray argument should be 3*ledCount");
return nullptr;
}
}
else
{
PyErr_SetString(PyExc_RuntimeError, "Argument is not a bytearray");
return nullptr;
}
}
else
{
return nullptr;
}
}
else
{
PyErr_SetString(PyExc_RuntimeError, "Function expect 1 or 3 arguments");
return nullptr;
}
// error
PyErr_SetString(PyExc_RuntimeError, "Unknown error");
return nullptr;
}
PyObject* Effect::wrapSetImage(PyObject *self, PyObject *args)
{
Effect * effect = getEffect(self);
return Py_BuildValue("i", 42);
// check if we have aborted already
if (effect->_abortRequested)
{
return Py_BuildValue("");
}
return Py_BuildValue("");
}
PyObject* Effect::wrapAbort(PyObject *self, PyObject *)
@@ -101,13 +193,13 @@ Effect * Effect::getEffect(PyObject *self)
{
// Get the effect from the capsule in the self pointer
Effect * effect = reinterpret_cast<Effect *>(PyCapsule_GetPointer(self, nullptr));
// Test if the effect has reached it end time
if (effect->_timeout > 0 && QDateTime::currentMSecsSinceEpoch() > effect->_endTime)
{
effect->_abortRequested = true;
}
// return the effect
return effect;
}

View File

@@ -27,6 +27,8 @@ public slots:
signals:
void effectFinished(Effect * effect);
void setColors(int priority, const std::vector<ColorRgb> &ledColors, const int timeout_ms);
private slots:
void effectFinished();
@@ -42,7 +44,7 @@ private:
const int _priority;
const int _timeout;
int64_t _endTime;
PyThreadState * _interpreterThreadState;

View File

@@ -1,3 +1,6 @@
// Qt includes
#include <QMetaType>
// Python includes
#include <Python.h>
@@ -10,8 +13,11 @@
EffectEngine::EffectEngine(Hyperion * hyperion) :
_hyperion(hyperion),
_availableEffects(),
_activeEffects()
_activeEffects(),
_mainThreadState(nullptr)
{
qRegisterMetaType<std::vector<ColorRgb>>("std::vector<ColorRgb>");
// connect the Hyperion channel clear feedback
connect(_hyperion, SIGNAL(channelCleared(int)), this, SLOT(channelCleared(int)));
connect(_hyperion, SIGNAL(allChannelsCleared()), this, SLOT(allChannelsCleared()));
@@ -23,17 +29,16 @@ EffectEngine::EffectEngine(Hyperion * hyperion) :
std::cout << "Initializing Python interpreter" << std::endl;
Py_InitializeEx(0);
PyEval_InitThreads(); // Create the GIL
//_mainThreadState = PyEval_SaveThread();
PyEval_ReleaseLock(); // Release the GIL
PyRun_SimpleString("print 'test'");
_mainThreadState = PyEval_SaveThread();
}
EffectEngine::~EffectEngine()
{
// clean up the Python interpreter
std::cout << "Cleaning up Python interpreter" << std::endl;
PyEval_AcquireLock(); // Get the Gil
//PyEval_RestoreThread(_mainThreadState);
//Py_Finalize();
PyEval_RestoreThread(_mainThreadState);
Py_Finalize();
}
std::list<std::string> EffectEngine::getEffects() const
@@ -54,6 +59,7 @@ int EffectEngine::runEffect(const std::string &effectName, int priority, int tim
// create the effect
Effect * effect = new Effect(priority, timeout);
connect(effect, SIGNAL(setColors(int,std::vector<ColorRgb>,int)), _hyperion, SLOT(setColors(int,std::vector<ColorRgb>,int)), Qt::QueuedConnection);
connect(effect, SIGNAL(effectFinished(Effect*)), this, SLOT(effectFinished(Effect*)));
_activeEffects.push_back(effect);