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>
|
2016-10-14 20:59:03 +02:00
|
|
|
#include <cmath>
|
2013-11-26 21:38:24 +01:00
|
|
|
|
2013-11-28 14:29:31 +01:00
|
|
|
// Qt includes
|
|
|
|
#include <QDateTime>
|
2016-09-13 11:51:16 +02:00
|
|
|
#include <QFile>
|
2016-09-21 22:01:50 +02:00
|
|
|
#include <Qt>
|
|
|
|
#include <QLinearGradient>
|
|
|
|
#include <QConicalGradient>
|
|
|
|
#include <QRadialGradient>
|
|
|
|
#include <QRect>
|
2017-10-12 12:00:32 +02:00
|
|
|
#include <QImageReader>
|
|
|
|
#include <QResource>
|
2013-11-28 14:29:31 +01:00
|
|
|
|
2013-11-26 21:38:24 +01:00
|
|
|
// effect engin eincludes
|
2018-12-27 23:11:32 +01:00
|
|
|
#include <effectengine/Effect.h>
|
|
|
|
#include <effectengine/EffectModule.h>
|
2016-06-23 13:48:49 +02:00
|
|
|
#include <utils/Logger.h>
|
2016-09-21 22:01:50 +02:00
|
|
|
#include <hyperion/Hyperion.h>
|
2013-11-24 16:10:48 +01:00
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
// python utils/ global mainthread
|
2020-08-04 19:30:16 +02:00
|
|
|
#include <python/PythonProgram.h>
|
2018-12-27 23:11:32 +01:00
|
|
|
#include <python/PythonUtils.h>
|
|
|
|
//impl
|
|
|
|
PyThreadState* mainThreadState;
|
2013-11-26 21:38:24 +01:00
|
|
|
|
2019-01-06 19:49:56 +01:00
|
|
|
Effect::Effect(Hyperion *hyperion, int priority, int timeout, const QString &script, const QString &name, const QJsonObject &args, const QString &imageData)
|
2016-08-08 00:17:00 +02:00
|
|
|
: QThread()
|
2018-12-27 23:11:32 +01:00
|
|
|
, _hyperion(hyperion)
|
2016-08-08 00:17:00 +02:00
|
|
|
, _priority(priority)
|
|
|
|
, _timeout(timeout)
|
|
|
|
, _script(script)
|
2016-09-13 11:51:16 +02:00
|
|
|
, _name(name)
|
2016-08-08 00:17:00 +02:00
|
|
|
, _args(args)
|
2019-01-06 19:49:56 +01:00
|
|
|
, _imageData(imageData)
|
2016-08-08 00:17:00 +02:00
|
|
|
, _endTime(-1)
|
|
|
|
, _colors()
|
2018-12-27 23:11:32 +01:00
|
|
|
, _imageSize(hyperion->getLedGridSize())
|
2017-08-04 12:01:45 +02:00
|
|
|
, _image(_imageSize,QImage::Format_ARGB32_Premultiplied)
|
2013-11-24 16:10:48 +01:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
_colors.resize(_hyperion->getLedCount());
|
2017-04-14 07:58:34 +02:00
|
|
|
_colors.fill(ColorRgb::BLACK);
|
2013-12-08 12:46:33 +01:00
|
|
|
|
2017-10-12 11:55:03 +02:00
|
|
|
_log = Logger::getInstance("EFFECTENGINE");
|
|
|
|
|
2016-09-21 22:01:50 +02:00
|
|
|
// init effect image for image based effects, size is based on led layout
|
2017-04-14 07:58:34 +02:00
|
|
|
_image.fill(Qt::black);
|
|
|
|
_painter = new QPainter(&_image);
|
2014-04-30 22:53:05 +02:00
|
|
|
|
2016-09-13 11:51:16 +02:00
|
|
|
Q_INIT_RESOURCE(EffectEngine);
|
2013-11-24 16:10:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Effect::~Effect()
|
|
|
|
{
|
2020-07-22 16:43:24 +02:00
|
|
|
requestInterruption();
|
|
|
|
wait();
|
|
|
|
|
2016-09-21 22:01:50 +02:00
|
|
|
delete _painter;
|
2017-04-14 07:58:34 +02:00
|
|
|
_imageStack.clear();
|
2013-11-24 16:10:48 +01:00
|
|
|
}
|
|
|
|
|
2020-08-04 19:30:16 +02:00
|
|
|
void Effect::setModuleParameters()
|
2013-11-26 21:38:24 +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
|
2019-08-03 15:11:28 +02:00
|
|
|
PyModule_AddObject(module, "__effectObj", PyCapsule_New((void*)this, "hyperion.__effectObj", nullptr));
|
2013-11-29 23:22:49 +01:00
|
|
|
|
2013-11-28 16:26:32 +01:00
|
|
|
// add ledCount variable to the interpreter
|
2020-07-22 16:43:24 +02:00
|
|
|
unsigned ledCount = 0;
|
|
|
|
QMetaObject::invokeMethod(_hyperion, "getLedCount", Qt::BlockingQueuedConnection, Q_RETURN_ARG(unsigned, ledCount));
|
|
|
|
PyObject_SetAttrString(module, "ledCount", Py_BuildValue("i", ledCount));
|
2013-11-26 21:38:24 +01:00
|
|
|
|
2017-04-09 22:28:32 +02:00
|
|
|
// add minimumWriteTime variable to the interpreter
|
2020-07-22 16:43:24 +02:00
|
|
|
int latchTime = 0;
|
|
|
|
QMetaObject::invokeMethod(_hyperion, "getLatchTime", Qt::BlockingQueuedConnection, Q_RETURN_ARG(int, latchTime));
|
|
|
|
PyObject_SetAttrString(module, "latchTime", Py_BuildValue("i", latchTime));
|
2017-04-09 22:28:32 +02:00
|
|
|
|
2013-11-30 00:28:04 +01:00
|
|
|
// add a args variable to the interpreter
|
2018-12-27 23:11:32 +01:00
|
|
|
PyObject_SetAttrString(module, "args", EffectModule::json2python(_args));
|
2013-11-26 21:38:24 +01:00
|
|
|
|
2014-03-01 19:28:57 +01:00
|
|
|
// decref the module
|
|
|
|
Py_XDECREF(module);
|
2020-08-04 19:30:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Effect::run()
|
|
|
|
{
|
|
|
|
PythonProgram program(_name, _log);
|
|
|
|
|
|
|
|
setModuleParameters();
|
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
|
2016-09-13 11:51:16 +02:00
|
|
|
QFile file (_script);
|
|
|
|
QByteArray python_code;
|
|
|
|
if (file.open(QIODevice::ReadOnly))
|
2013-11-30 00:28:04 +01:00
|
|
|
{
|
2016-09-13 11:51:16 +02:00
|
|
|
python_code = file.readAll();
|
2013-11-30 00:28:04 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-12 11:55:03 +02:00
|
|
|
Error(_log, "Unable to open script file %s.", QSTRING_CSTR(_script));
|
2016-09-13 11:51:16 +02:00
|
|
|
}
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
if (!python_code.isEmpty())
|
|
|
|
{
|
2020-08-04 19:30:16 +02:00
|
|
|
program.execute(python_code);
|
2013-11-30 00:28:04 +01:00
|
|
|
}
|
2013-11-26 21:38:24 +01:00
|
|
|
}
|