2013-11-30 17:41:38 +01:00
|
|
|
// Python include
|
|
|
|
#include <Python.h>
|
|
|
|
|
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}
|
|
|
|
};
|
|
|
|
|
2014-02-26 18:10:17 +01:00
|
|
|
#if PY_MAJOR_VERSION >= 3
|
|
|
|
// create the hyperion module
|
|
|
|
struct PyModuleDef Effect::moduleDef = {
|
2014-03-01 19:28:57 +01:00
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
"hyperion", /* m_name */
|
|
|
|
"Hyperion module", /* m_doc */
|
|
|
|
-1, /* m_size */
|
|
|
|
Effect::effectMethods, /* m_methods */
|
|
|
|
NULL, /* m_reload */
|
|
|
|
NULL, /* m_traverse */
|
|
|
|
NULL, /* m_clear */
|
|
|
|
NULL, /* m_free */
|
2014-02-26 18:10:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
PyObject* Effect::PyInit_hyperion()
|
|
|
|
{
|
2014-03-01 19:28:57 +01:00
|
|
|
return PyModule_Create(&moduleDef);
|
2014-02-26 18:10:17 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
void Effect::PyInit_hyperion()
|
|
|
|
{
|
2014-03-01 19:28:57 +01:00
|
|
|
Py_InitModule("hyperion", effectMethods);
|
2014-02-26 18:10:17 +01:00
|
|
|
}
|
|
|
|
#endif
|
2013-11-26 21:38:24 +01:00
|
|
|
|
2014-02-26 18:10:17 +01:00
|
|
|
void Effect::registerHyperionExtensionModule()
|
|
|
|
{
|
2014-03-01 19:28:57 +01:00
|
|
|
PyImport_AppendInittab("hyperion", &PyInit_hyperion);
|
2014-02-26 18:10:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Effect::Effect(PyThreadState * mainThreadState, int priority, int timeout, const std::string & script, const Json::Value & args) :
|
2013-11-26 21:38:24 +01:00
|
|
|
QThread(),
|
2014-03-01 19:28:57 +01:00
|
|
|
_mainThreadState(mainThreadState),
|
2013-11-26 21:38:24 +01:00
|
|
|
_priority(priority),
|
|
|
|
_timeout(timeout),
|
2013-11-30 00:28:04 +01:00
|
|
|
_script(script),
|
|
|
|
_args(args),
|
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),
|
2013-12-08 12:46:33 +01:00
|
|
|
_imageProcessor(ImageProcessorFactory::getInstance().newImageProcessor()),
|
|
|
|
_colors()
|
2013-11-24 16:10:48 +01:00
|
|
|
{
|
2013-12-08 12:46:33 +01:00
|
|
|
_colors.resize(_imageProcessor->getLedCount(), ColorRgb::BLACK);
|
|
|
|
|
2014-04-30 22:53:05 +02:00
|
|
|
// disable the black border detector for effects
|
|
|
|
_imageProcessor->enableBalckBorderDetector(false);
|
|
|
|
|
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()
|
|
|
|
{
|
2014-03-01 19:28:57 +01:00
|
|
|
// switch to the main thread state and acquire the GIL
|
|
|
|
PyEval_RestoreThread(_mainThreadState);
|
2014-02-26 18:10:17 +01:00
|
|
|
|
2013-11-26 21:38:24 +01:00
|
|
|
// Initialize a new thread state
|
2014-03-01 19:28:57 +01:00
|
|
|
_interpreterThreadState = Py_NewInterpreter();
|
2014-02-26 18:10:17 +01:00
|
|
|
|
2014-03-01 19:28:57 +01:00
|
|
|
// import the buildtin Hyperion module
|
|
|
|
PyObject * module = PyImport_ImportModule("hyperion");
|
2014-02-26 18:10:17 +01:00
|
|
|
|
2014-03-01 19:28:57 +01:00
|
|
|
// add a capsule containing 'this' to the module to be able to retrieve the effect from the callback function
|
|
|
|
PyObject_SetAttrString(module, "__effectObj", PyCapsule_New(this, nullptr, nullptr));
|
2013-11-27 20:15:29 +01:00
|
|
|
|
2014-03-01 19:28:57 +01:00
|
|
|
// add ledCount variable to the interpreter
|
|
|
|
PyObject_SetAttrString(module, "ledCount", Py_BuildValue("i", _imageProcessor->getLedCount()));
|
2013-11-29 23:22:49 +01:00
|
|
|
|
2014-03-01 19:28:57 +01:00
|
|
|
// add a args variable to the interpreter
|
|
|
|
PyObject_SetAttrString(module, "args", json2python(_args));
|
2013-11-26 21:38:24 +01:00
|
|
|
|
2014-03-01 19:28:57 +01:00
|
|
|
// decref the module
|
|
|
|
Py_XDECREF(module);
|
2013-11-30 00:28:04 +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
|
2013-11-30 00:28:04 +01:00
|
|
|
FILE* file = fopen(_script.c_str(), "r");
|
|
|
|
if (file != nullptr)
|
|
|
|
{
|
|
|
|
PyRun_SimpleFile(file, _script.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "Unable to open script file " << _script << std::endl;
|
|
|
|
}
|
2013-11-26 21:38:24 +01:00
|
|
|
|
|
|
|
// Clean up the thread state
|
|
|
|
Py_EndInterpreter(_interpreterThreadState);
|
|
|
|
_interpreterThreadState = nullptr;
|
|
|
|
PyEval_ReleaseLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Effect::getPriority() const
|
|
|
|
{
|
|
|
|
return _priority;
|
|
|
|
}
|
|
|
|
|
2013-11-30 15:54:47 +01:00
|
|
|
bool Effect::isAbortRequested() const
|
|
|
|
{
|
|
|
|
return _abortRequested;
|
|
|
|
}
|
|
|
|
|
2013-11-26 21:38:24 +01:00
|
|
|
void Effect::abort()
|
|
|
|
{
|
|
|
|
_abortRequested = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Effect::effectFinished()
|
|
|
|
{
|
|
|
|
emit effectFinished(this);
|
|
|
|
}
|
2013-11-28 14:15:52 +01:00
|
|
|
|
2013-11-30 17:41:38 +01:00
|
|
|
PyObject *Effect::json2python(const Json::Value &json) const
|
|
|
|
{
|
|
|
|
switch (json.type())
|
|
|
|
{
|
|
|
|
case Json::nullValue:
|
|
|
|
return Py_BuildValue("");
|
|
|
|
case Json::realValue:
|
|
|
|
return Py_BuildValue("d", json.asDouble());
|
|
|
|
case Json::intValue:
|
|
|
|
case Json::uintValue:
|
|
|
|
return Py_BuildValue("i", json.asInt());
|
|
|
|
case Json::booleanValue:
|
|
|
|
return Py_BuildValue("i", json.asBool() ? 1 : 0);
|
|
|
|
case Json::stringValue:
|
|
|
|
return Py_BuildValue("s", json.asCString());
|
|
|
|
case Json::objectValue:
|
|
|
|
{
|
2014-03-01 19:28:57 +01:00
|
|
|
PyObject * dict= PyDict_New();
|
2013-11-30 17:41:38 +01:00
|
|
|
for (Json::Value::iterator i = json.begin(); i != json.end(); ++i)
|
|
|
|
{
|
2014-03-01 19:28:57 +01:00
|
|
|
PyObject * obj = json2python(*i);
|
|
|
|
PyDict_SetItemString(dict, i.memberName(), obj);
|
|
|
|
Py_XDECREF(obj);
|
2013-11-30 17:41:38 +01:00
|
|
|
}
|
2014-03-01 19:28:57 +01:00
|
|
|
return dict;
|
2013-11-30 17:41:38 +01:00
|
|
|
}
|
|
|
|
case Json::arrayValue:
|
|
|
|
{
|
|
|
|
PyObject * list = PyList_New(json.size());
|
|
|
|
for (Json::Value::iterator i = json.begin(); i != json.end(); ++i)
|
|
|
|
{
|
2014-03-01 19:28:57 +01:00
|
|
|
PyObject * obj = json2python(*i);
|
|
|
|
PyList_SetItem(list, i.index(), obj);
|
|
|
|
Py_XDECREF(obj);
|
2013-11-30 17:41:38 +01:00
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(false);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
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
|
2014-03-01 19:28:57 +01:00
|
|
|
Effect * effect = getEffect();
|
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))
|
|
|
|
{
|
2013-12-08 12:46:33 +01:00
|
|
|
std::fill(effect->_colors.begin(), effect->_colors.end(), color);
|
2013-12-08 17:45:26 +01:00
|
|
|
effect->setColors(effect->_priority, effect->_colors, timeout, false);
|
2013-11-29 23:22:49 +01:00
|
|
|
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())
|
|
|
|
{
|
|
|
|
char * data = PyByteArray_AS_STRING(bytearray);
|
2013-12-08 12:46:33 +01:00
|
|
|
memcpy(effect->_colors.data(), data, length);
|
2013-12-08 17:45:26 +01:00
|
|
|
effect->setColors(effect->_priority, effect->_colors, timeout, false);
|
2013-11-29 23:22:49 +01:00
|
|
|
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-29 23:53:58 +01:00
|
|
|
// get the effect
|
2014-03-01 19:28:57 +01:00
|
|
|
Effect * effect = getEffect();
|
2013-11-29 23:22:49 +01:00
|
|
|
|
|
|
|
// check if we have aborted already
|
|
|
|
if (effect->_abortRequested)
|
|
|
|
{
|
|
|
|
return Py_BuildValue("");
|
|
|
|
}
|
|
|
|
|
2013-11-29 23:53:58 +01:00
|
|
|
// 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("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// bytearray of values
|
|
|
|
int width, height;
|
|
|
|
PyObject * bytearray = nullptr;
|
|
|
|
if (PyArg_ParseTuple(args, "iiO", &width, &height, &bytearray))
|
|
|
|
{
|
|
|
|
if (PyByteArray_Check(bytearray))
|
|
|
|
{
|
|
|
|
int length = PyByteArray_Size(bytearray);
|
|
|
|
if (length == 3 * width * height)
|
|
|
|
{
|
|
|
|
Image<ColorRgb> image(width, height);
|
|
|
|
char * data = PyByteArray_AS_STRING(bytearray);
|
2013-12-08 12:46:33 +01:00
|
|
|
memcpy(image.memptr(), data, length);
|
2013-11-29 23:53:58 +01:00
|
|
|
|
2013-12-08 12:46:33 +01:00
|
|
|
effect->_imageProcessor->process(image, effect->_colors);
|
2013-12-08 17:45:26 +01:00
|
|
|
effect->setColors(effect->_priority, effect->_colors, timeout, false);
|
2013-11-29 23:53:58 +01:00
|
|
|
return Py_BuildValue("");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Length of bytearray argument should be 3*ledCount");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Argument 3 is not a bytearray");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// error
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Unknown error");
|
|
|
|
return nullptr;
|
2013-11-28 14:15:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PyObject* Effect::wrapAbort(PyObject *self, PyObject *)
|
|
|
|
{
|
2014-03-01 19:28:57 +01:00
|
|
|
Effect * effect = getEffect();
|
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-12-08 12:46:33 +01:00
|
|
|
return Py_BuildValue("i", effect->_abortRequested ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2014-02-26 18:10:17 +01:00
|
|
|
Effect * Effect::getEffect()
|
2013-12-08 12:46:33 +01:00
|
|
|
{
|
2014-03-01 19:28:57 +01:00
|
|
|
// extract the module from the runtime
|
|
|
|
PyObject * module = PyObject_GetAttrString(PyImport_AddModule("__main__"), "hyperion");
|
|
|
|
|
|
|
|
if (!PyModule_Check(module))
|
|
|
|
{
|
|
|
|
// something is wrong
|
|
|
|
Py_XDECREF(module);
|
|
|
|
std::cerr << "Unable to retrieve the effect object from the Python runtime" << std::endl;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve the capsule with the effect
|
|
|
|
PyObject * effectCapsule = PyObject_GetAttrString(module, "__effectObj");
|
|
|
|
Py_XDECREF(module);
|
|
|
|
|
|
|
|
if (!PyCapsule_CheckExact(effectCapsule))
|
|
|
|
{
|
|
|
|
// something is wrong
|
|
|
|
Py_XDECREF(effectCapsule);
|
|
|
|
std::cerr << "Unable to retrieve the effect object from the Python runtime" << std::endl;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the effect from the capsule
|
|
|
|
Effect * effect = reinterpret_cast<Effect *>(PyCapsule_GetPointer(effectCapsule, nullptr));
|
|
|
|
Py_XDECREF(effectCapsule);
|
|
|
|
return effect;
|
2013-11-28 14:29:31 +01:00
|
|
|
}
|