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

@ -4,6 +4,7 @@
// pre-declarioation
class Effect;
typedef struct _ts PyThreadState;
class EffectEngine : public QObject
{
@ -34,4 +35,6 @@ private:
std::map<std::string, std::string> _availableEffects;
std::list<Effect *> _activeEffects;
PyThreadState * _mainThreadState;
};

View File

@ -70,6 +70,41 @@ public:
///
unsigned getLedCount() const;
///
/// Returns the value of a specific color transform
///
/// @param[in] transform The type of transform
/// @param[in] color The color channel to which the transform applies (only applicable for
/// Transform::THRESHOLD, Transform::GAMMA, Transform::BLACKLEVEL,
/// Transform::WHITELEVEL)
///
/// @return The value of the specified color transform
///
double getTransform(Transform transform, Color color) const;
///
/// Returns a list of active priorities
///
/// @return The list with priorities
///
QList<int> getActivePriorities() const;
///
/// Returns the information of a specific priorrity channel
///
/// @param[in] priority The priority channel
///
/// @return The information of the given
///
/// @throw std::runtime_error when the priority channel does not exist
///
const InputInfo& getPriorityInfo(const int priority) const;
/// Get the list of available effects
/// @return The list of available effects
std::list<std::string> getEffects() const;
public slots:
///
/// Writes a single color to all the leds for the given time and priority
///
@ -112,40 +147,6 @@ public:
///
void clearall();
///
/// Returns the value of a specific color transform
///
/// @param[in] transform The type of transform
/// @param[in] color The color channel to which the transform applies (only applicable for
/// Transform::THRESHOLD, Transform::GAMMA, Transform::BLACKLEVEL,
/// Transform::WHITELEVEL)
///
/// @return The value of the specified color transform
///
double getTransform(Transform transform, Color color) const;
///
/// Returns a list of active priorities
///
/// @return The list with priorities
///
QList<int> getActivePriorities() const;
///
/// Returns the information of a specific priorrity channel
///
/// @param[in] priority The priority channel
///
/// @return The information of the given
///
/// @throw std::runtime_error when the priority channel does not exist
///
const InputInfo& getPriorityInfo(const int priority) const;
/// Get the list of available effects
/// @return The list of available effects
std::list<std::string> getEffects() const;
/// Run the specified effect on the given priority channel and optionally specify a timeout
/// @param effectName Name of the effec to run
/// @param priority The priority channel of the effect

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);

View File

@ -4,6 +4,7 @@
// QT includes
#include <QDateTime>
#include <QThread>
// JsonSchema include
#include <utils/jsonschema/JsonFactory.h>
@ -392,10 +393,11 @@ void Hyperion::clear(int priority)
{
update();
}
// send clear signal to the effect engine
_effectEngine->channelCleared(priority);
}
// send clear signal to the effect engine
// (outside the check so the effect gets cleared even when the effect is not sending colors)
_effectEngine->channelCleared(priority);
}
void Hyperion::clearall()

View File

@ -164,8 +164,8 @@ void JsonSchemaChecker::checkType(const Json::Value & value, const Json::Value &
wrongType = !value.isNull();
else if (type == "any")
wrongType = false;
else
assert(false);
// else
// assert(false);
if (wrongType)
{

View File

@ -153,13 +153,6 @@ int main(int argc, char** argv)
}
#endif
// Create the effect engine
EffectEngine * effectEngine = nullptr;
if (true)
{
effectEngine = new EffectEngine(&hyperion);
}
// Create Json server if configuration is present
JsonServer * jsonServer = nullptr;
if (config.isMember("jsonServer"))
@ -197,7 +190,6 @@ int main(int argc, char** argv)
delete dispmanx;
#endif
delete xbmcVideoChecker;
delete effectEngine;
delete jsonServer;
delete protoServer;
delete boblightServer;