hyperion.ng/libsrc/effectengine/Effect.cpp

119 lines
2.9 KiB
C++
Raw Normal View History

// stl includes
#include <iostream>
#include <sstream>
2016-10-14 20:59:03 +02:00
#include <cmath>
// Qt includes
#include <QDateTime>
#include <QFile>
#include <Qt>
#include <QLinearGradient>
#include <QConicalGradient>
#include <QRadialGradient>
#include <QRect>
#include <QImageReader>
#include <QResource>
// effect engin eincludes
2018-12-27 23:11:32 +01:00
#include <effectengine/Effect.h>
#include <effectengine/EffectModule.h>
#include <utils/Logger.h>
#include <hyperion/Hyperion.h>
2018-12-27 23:11:32 +01:00
// python utils/ global mainthread
#include <python/PythonProgram.h>
2018-12-27 23:11:32 +01:00
#include <python/PythonUtils.h>
//impl
PyThreadState* mainThreadState;
Effect::Effect(Hyperion *hyperion, int priority, int timeout, const QString &script, const QString &name, const QJsonObject &args, const QString &imageData)
: QThread()
2018-12-27 23:11:32 +01:00
, _hyperion(hyperion)
, _priority(priority)
, _timeout(timeout)
, _script(script)
, _name(name)
, _args(args)
, _imageData(imageData)
, _endTime(-1)
, _colors()
2018-12-27 23:11:32 +01:00
, _imageSize(hyperion->getLedGridSize())
, _image(_imageSize,QImage::Format_ARGB32_Premultiplied)
{
2018-12-27 23:11:32 +01:00
_colors.resize(_hyperion->getLedCount());
_colors.fill(ColorRgb::BLACK);
_log = Logger::getInstance("EFFECTENGINE");
// init effect image for image based effects, size is based on led layout
_image.fill(Qt::black);
_painter = new QPainter(&_image);
Q_INIT_RESOURCE(EffectEngine);
}
Effect::~Effect()
{
2020-07-22 16:43:24 +02:00
requestInterruption();
wait();
delete _painter;
_imageStack.clear();
}
void Effect::setModuleParameters()
{
// import the buildtin Hyperion module
PyObject * module = PyImport_ImportModule("hyperion");
// add a capsule containing 'this' to the module to be able to retrieve the effect from the callback function
PyModule_AddObject(module, "__effectObj", PyCapsule_New((void*)this, "hyperion.__effectObj", nullptr));
// 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));
// 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));
// add a args variable to the interpreter
2018-12-27 23:11:32 +01:00
PyObject_SetAttrString(module, "args", EffectModule::json2python(_args));
// decref the module
Py_XDECREF(module);
}
void Effect::run()
{
PythonProgram program(_name, _log);
setModuleParameters();
// Set the end time if applicable
if (_timeout > 0)
{
_endTime = QDateTime::currentMSecsSinceEpoch() + _timeout;
}
// Run the effect script
QFile file (_script);
QByteArray python_code;
if (file.open(QIODevice::ReadOnly))
{
python_code = file.readAll();
}
else
{
Error(_log, "Unable to open script file %s.", QSTRING_CSTR(_script));
}
file.close();
if (!python_code.isEmpty())
{
program.execute(python_code);
}
}