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

View File

@ -70,6 +70,41 @@ public:
/// ///
unsigned getLedCount() const; 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 /// Writes a single color to all the leds for the given time and priority
/// ///
@ -112,40 +147,6 @@ public:
/// ///
void clearall(); 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 /// Run the specified effect on the given priority channel and optionally specify a timeout
/// @param effectName Name of the effec to run /// @param effectName Name of the effec to run
/// @param priority The priority channel of the effect /// @param priority The priority channel of the effect

View File

@ -81,14 +81,106 @@ void Effect::effectFinished()
PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args) PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args)
{ {
// get the effect
Effect * effect = getEffect(self); 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) PyObject* Effect::wrapSetImage(PyObject *self, PyObject *args)
{ {
Effect * effect = getEffect(self); 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 *) PyObject* Effect::wrapAbort(PyObject *self, PyObject *)

View File

@ -27,6 +27,8 @@ public slots:
signals: signals:
void effectFinished(Effect * effect); void effectFinished(Effect * effect);
void setColors(int priority, const std::vector<ColorRgb> &ledColors, const int timeout_ms);
private slots: private slots:
void effectFinished(); void effectFinished();

View File

@ -1,3 +1,6 @@
// Qt includes
#include <QMetaType>
// Python includes // Python includes
#include <Python.h> #include <Python.h>
@ -10,8 +13,11 @@
EffectEngine::EffectEngine(Hyperion * hyperion) : EffectEngine::EffectEngine(Hyperion * hyperion) :
_hyperion(hyperion), _hyperion(hyperion),
_availableEffects(), _availableEffects(),
_activeEffects() _activeEffects(),
_mainThreadState(nullptr)
{ {
qRegisterMetaType<std::vector<ColorRgb>>("std::vector<ColorRgb>");
// connect the Hyperion channel clear feedback // connect the Hyperion channel clear feedback
connect(_hyperion, SIGNAL(channelCleared(int)), this, SLOT(channelCleared(int))); connect(_hyperion, SIGNAL(channelCleared(int)), this, SLOT(channelCleared(int)));
connect(_hyperion, SIGNAL(allChannelsCleared()), this, SLOT(allChannelsCleared())); connect(_hyperion, SIGNAL(allChannelsCleared()), this, SLOT(allChannelsCleared()));
@ -23,17 +29,16 @@ EffectEngine::EffectEngine(Hyperion * hyperion) :
std::cout << "Initializing Python interpreter" << std::endl; std::cout << "Initializing Python interpreter" << std::endl;
Py_InitializeEx(0); Py_InitializeEx(0);
PyEval_InitThreads(); // Create the GIL PyEval_InitThreads(); // Create the GIL
//_mainThreadState = PyEval_SaveThread(); PyRun_SimpleString("print 'test'");
PyEval_ReleaseLock(); // Release the GIL _mainThreadState = PyEval_SaveThread();
} }
EffectEngine::~EffectEngine() EffectEngine::~EffectEngine()
{ {
// clean up the Python interpreter // clean up the Python interpreter
std::cout << "Cleaning up Python interpreter" << std::endl; std::cout << "Cleaning up Python interpreter" << std::endl;
PyEval_AcquireLock(); // Get the Gil PyEval_RestoreThread(_mainThreadState);
//PyEval_RestoreThread(_mainThreadState); Py_Finalize();
//Py_Finalize();
} }
std::list<std::string> EffectEngine::getEffects() const 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 // create the effect
Effect * effect = new Effect(priority, timeout); 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*))); connect(effect, SIGNAL(effectFinished(Effect*)), this, SLOT(effectFinished(Effect*)));
_activeEffects.push_back(effect); _activeEffects.push_back(effect);

View File

@ -4,6 +4,7 @@
// QT includes // QT includes
#include <QDateTime> #include <QDateTime>
#include <QThread>
// JsonSchema include // JsonSchema include
#include <utils/jsonschema/JsonFactory.h> #include <utils/jsonschema/JsonFactory.h>
@ -392,10 +393,11 @@ void Hyperion::clear(int priority)
{ {
update(); 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() void Hyperion::clearall()

View File

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

View File

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