Changed hyperion args variable in module from string to dict

Former-commit-id: ae94dca70590e3ed15e991172b7458cf4298e655
This commit is contained in:
johan 2013-11-30 17:41:38 +01:00
parent 47eaf25904
commit 1ec84005c0
5 changed files with 59 additions and 9 deletions

View File

@ -1,5 +1,9 @@
#pragma once #pragma once
// Json includes
#include <json/value.h>
// Hyperion includes
#include <hyperion/Hyperion.h> #include <hyperion/Hyperion.h>
// pre-declarioation // pre-declarioation
@ -30,7 +34,7 @@ public:
struct EffectDefinition struct EffectDefinition
{ {
std::string script; std::string script;
std::string args; Json::Value args;
}; };
private slots: private slots:

View File

@ -32,5 +32,6 @@ add_library(effectengine
target_link_libraries(effectengine target_link_libraries(effectengine
hyperion hyperion
jsoncpp
${QT_LIBRARIES} ${QT_LIBRARIES}
${PYTHON_LIBRARIES}) ${PYTHON_LIBRARIES})

View File

@ -1,3 +1,6 @@
// Python include
#include <Python.h>
// stl includes // stl includes
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
@ -17,7 +20,7 @@ PyMethodDef Effect::effectMethods[] = {
}; };
Effect::Effect(int priority, int timeout, const std::string & script, const std::string & args) : Effect::Effect(int priority, int timeout, const std::string & script, const Json::Value & args) :
QThread(), QThread(),
_priority(priority), _priority(priority),
_timeout(timeout), _timeout(timeout),
@ -50,7 +53,8 @@ void Effect::run()
PyObject_SetAttrString(module, "ledCount", Py_BuildValue("i", _imageProcessor->getLedCount())); PyObject_SetAttrString(module, "ledCount", Py_BuildValue("i", _imageProcessor->getLedCount()));
// add a args variable to the interpreter // add a args variable to the interpreter
PyObject_SetAttrString(module, "args", Py_BuildValue("s", _args.c_str())); PyObject_SetAttrString(module, "args", json2python(_args));
//PyObject_SetAttrString(module, "args", Py_BuildValue("s", _args.c_str()));
// Set the end time if applicable // Set the end time if applicable
if (_timeout > 0) if (_timeout > 0)
@ -95,6 +99,45 @@ void Effect::effectFinished()
emit effectFinished(this); emit effectFinished(this);
} }
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:
{
PyObject * obj = PyDict_New();
for (Json::Value::iterator i = json.begin(); i != json.end(); ++i)
{
PyDict_SetItemString(obj, i.memberName(), json2python(*i));
}
return obj;
}
case Json::arrayValue:
{
PyObject * list = PyList_New(json.size());
for (Json::Value::iterator i = json.begin(); i != json.end(); ++i)
{
PyList_SetItem(list, i.index(), json2python(*i));
}
return list;
}
}
assert(false);
return nullptr;
}
PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args) PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args)
{ {
// get the effect // get the effect

View File

@ -1,11 +1,11 @@
#pragma once #pragma once
// Qt includes
#include <QThread>
// Python includes // Python includes
#include <Python.h> #include <Python.h>
// Qt includes
#include <QThread>
// Hyperion includes // Hyperion includes
#include <hyperion/ImageProcessor.h> #include <hyperion/ImageProcessor.h>
@ -14,7 +14,7 @@ class Effect : public QThread
Q_OBJECT Q_OBJECT
public: public:
Effect(int priority, int timeout, const std::string & script, const std::string & args = ""); Effect(int priority, int timeout, const std::string & script, const Json::Value & args = Json::Value());
virtual ~Effect(); virtual ~Effect();
virtual void run(); virtual void run();
@ -35,6 +35,8 @@ private slots:
void effectFinished(); void effectFinished();
private: private:
PyObject * json2python(const Json::Value & json) const;
// Wrapper methods for Python interpreter extra buildin methods // Wrapper methods for Python interpreter extra buildin methods
static PyMethodDef effectMethods[]; static PyMethodDef effectMethods[];
static PyObject* wrapSetColor(PyObject *self, PyObject *args); static PyObject* wrapSetColor(PyObject *self, PyObject *args);
@ -49,7 +51,7 @@ private:
const std::string & _script; const std::string & _script;
const std::string & _args; const Json::Value & _args;
int64_t _endTime; int64_t _endTime;

View File

@ -25,7 +25,7 @@ EffectEngine::EffectEngine(Hyperion * hyperion, const Json::Value & jsonEffectCo
for (const std::string & name : effectNames) for (const std::string & name : effectNames)
{ {
const Json::Value & info = jsonEffectConfig[name]; const Json::Value & info = jsonEffectConfig[name];
_availableEffects[name] = {info["script"].asString(), Json::FastWriter().write(info["args"])}; _availableEffects[name] = {info["script"].asString(), info["args"]};
} }
// initialize the python interpreter // initialize the python interpreter