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>
|
|
|
|
|
2013-11-28 14:29:31 +01:00
|
|
|
// Qt includes
|
|
|
|
#include <QDateTime>
|
|
|
|
|
2013-11-26 21:38:24 +01:00
|
|
|
// effect engin eincludes
|
2013-11-24 16:10:48 +01:00
|
|
|
#include "Effect.h"
|
|
|
|
|
2013-11-28 14:15:52 +01:00
|
|
|
// Python method table
|
|
|
|
PyMethodDef Effect::effectMethods[] = {
|
|
|
|
{"setColor", Effect::wrapSetColor, METH_VARARGS, "Set a new color for the leds."},
|
|
|
|
{"setImage", Effect::wrapSetImage, METH_VARARGS, "Set a new image to process and determine new led colors."},
|
2013-11-28 14:38:07 +01:00
|
|
|
{"abort", Effect::wrapAbort, 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),
|
2013-11-28 14:29:31 +01:00
|
|
|
_endTime(-1),
|
2013-11-26 21:38:24 +01:00
|
|
|
_interpreterThreadState(nullptr),
|
2013-11-28 14:38:07 +01:00
|
|
|
_abortRequested(false),
|
|
|
|
_imageProcessor(ImageProcessorFactory::getInstance().newImageProcessor())
|
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);
|
2013-11-28 16:26:32 +01:00
|
|
|
PyObject * module = Py_InitModule4("hyperion", effectMethods, nullptr, thisCapsule, PYTHON_API_VERSION);
|
2013-11-29 23:22:49 +01:00
|
|
|
|
2013-11-28 16:26:32 +01:00
|
|
|
// add ledCount variable to the interpreter
|
|
|
|
PyObject_SetAttrString(module, "ledCount", Py_BuildValue("i", _imageProcessor->getLedCount()));
|
2013-11-26 21:38:24 +01:00
|
|
|
|
2013-11-28 14:29:31 +01:00
|
|
|
// Set the end time if applicable
|
|
|
|
if (_timeout > 0)
|
|
|
|
{
|
|
|
|
_endTime = QDateTime::currentMSecsSinceEpoch() + _timeout;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Effect::getPriority() const
|
|
|
|
{
|
|
|
|
return _priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Effect::abort()
|
|
|
|
{
|
|
|
|
_abortRequested = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Effect::effectFinished()
|
|
|
|
{
|
|
|
|
emit effectFinished(this);
|
|
|
|
}
|
2013-11-28 14:15:52 +01:00
|
|
|
|
|
|
|
PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args)
|
|
|
|
{
|
2013-11-29 23:22:49 +01:00
|
|
|
// get the effect
|
2013-11-28 14:29:31 +01:00
|
|
|
Effect * effect = getEffect(self);
|
2013-11-29 23:22:49 +01:00
|
|
|
|
|
|
|
// 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;
|
2013-11-28 14:15:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PyObject* Effect::wrapSetImage(PyObject *self, PyObject *args)
|
|
|
|
{
|
2013-11-28 14:29:31 +01:00
|
|
|
Effect * effect = getEffect(self);
|
2013-11-29 23:22:49 +01:00
|
|
|
|
|
|
|
// check if we have aborted already
|
|
|
|
if (effect->_abortRequested)
|
|
|
|
{
|
|
|
|
return Py_BuildValue("");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Py_BuildValue("");
|
2013-11-28 14:15:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PyObject* Effect::wrapAbort(PyObject *self, PyObject *)
|
|
|
|
{
|
2013-11-28 14:29:31 +01:00
|
|
|
Effect * effect = getEffect(self);
|
2013-11-28 14:15:52 +01:00
|
|
|
return Py_BuildValue("i", effect->_abortRequested ? 1 : 0);
|
|
|
|
}
|
2013-11-28 14:29:31 +01:00
|
|
|
|
|
|
|
Effect * Effect::getEffect(PyObject *self)
|
|
|
|
{
|
|
|
|
// Get the effect from the capsule in the self pointer
|
|
|
|
Effect * effect = reinterpret_cast<Effect *>(PyCapsule_GetPointer(self, nullptr));
|
2013-11-29 23:22:49 +01:00
|
|
|
|
2013-11-28 14:29:31 +01:00
|
|
|
// Test if the effect has reached it end time
|
|
|
|
if (effect->_timeout > 0 && QDateTime::currentMSecsSinceEpoch() > effect->_endTime)
|
|
|
|
{
|
|
|
|
effect->_abortRequested = true;
|
|
|
|
}
|
2013-11-29 23:22:49 +01:00
|
|
|
|
2013-11-28 14:29:31 +01:00
|
|
|
// return the effect
|
|
|
|
return effect;
|
|
|
|
}
|