mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2023-10-10 13:36:59 +02:00
Add support for Python 3
Former-commit-id: b6aec954ba0e79ba5697ea8cc305eb9f7d29f332
This commit is contained in:
parent
1a92e6ccd2
commit
e0d405034f
@ -41,8 +41,8 @@ include_directories(${CMAKE_SOURCE_DIR}/include)
|
|||||||
# Prefer static linking over dynamic
|
# Prefer static linking over dynamic
|
||||||
#set(CMAKE_FIND_LIBRARY_SUFFIXES ".a;.so")
|
#set(CMAKE_FIND_LIBRARY_SUFFIXES ".a;.so")
|
||||||
|
|
||||||
#set(CMAKE_BUILD_TYPE "Debug")
|
set(CMAKE_BUILD_TYPE "Debug")
|
||||||
set(CMAKE_BUILD_TYPE "Release")
|
#set(CMAKE_BUILD_TYPE "Release")
|
||||||
|
|
||||||
# enable C++11
|
# enable C++11
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -Wall")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -Wall")
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
{
|
{
|
||||||
"name" : "MyPi",
|
"name" : "MyPi",
|
||||||
"type" : "adalight",
|
"type" : "adalight",
|
||||||
"output" : "/dev/ttyUSB0",
|
"output" : "/dev/ttyUSB0",
|
||||||
"rate" : 115200,
|
"rate" : 115200,
|
||||||
"colorOrder" : "rgb"
|
"colorOrder" : "rgb"
|
||||||
},
|
},
|
||||||
@ -363,7 +363,7 @@
|
|||||||
{
|
{
|
||||||
"paths" :
|
"paths" :
|
||||||
[
|
[
|
||||||
"/opt/hyperion/effects"
|
"/home/dincs/projects/hyperion/effects"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -280,7 +280,6 @@ void PresettableUniquelySwitchable::preset() {
|
|||||||
template<>
|
template<>
|
||||||
PODParameter<string>::PODParameter(char shortOption, const char *longOption,
|
PODParameter<string>::PODParameter(char shortOption, const char *longOption,
|
||||||
const char* description) : CommonParameter<PresettableUniquelySwitchable>(shortOption, longOption, description) {
|
const char* description) : CommonParameter<PresettableUniquelySwitchable>(shortOption, longOption, description) {
|
||||||
preset();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -55,5 +55,5 @@ private:
|
|||||||
|
|
||||||
std::list<Effect *> _activeEffects;
|
std::list<Effect *> _activeEffects;
|
||||||
|
|
||||||
PyThreadState * _mainThreadState;
|
PyThreadState * _mainThreadState;
|
||||||
};
|
};
|
||||||
|
@ -19,9 +19,39 @@ PyMethodDef Effect::effectMethods[] = {
|
|||||||
{NULL, NULL, 0, NULL}
|
{NULL, NULL, 0, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#if PY_MAJOR_VERSION >= 3
|
||||||
|
// create the hyperion module
|
||||||
|
struct PyModuleDef Effect::moduleDef = {
|
||||||
|
PyModuleDef_HEAD_INIT,
|
||||||
|
"hyperion", /* m_name */
|
||||||
|
"Hyperion module", /* m_doc */
|
||||||
|
-1, /* m_size */
|
||||||
|
Effect::effectMethods, /* m_methods */
|
||||||
|
NULL, /* m_reload */
|
||||||
|
NULL, /* m_traverse */
|
||||||
|
NULL, /* m_clear */
|
||||||
|
NULL, /* m_free */
|
||||||
|
};
|
||||||
|
|
||||||
Effect::Effect(int priority, int timeout, const std::string & script, const Json::Value & args) :
|
PyObject* Effect::PyInit_hyperion()
|
||||||
|
{
|
||||||
|
return PyModule_Create(&moduleDef);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
void Effect::PyInit_hyperion()
|
||||||
|
{
|
||||||
|
Py_InitModule("hyperion", effectMethods);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void Effect::registerHyperionExtensionModule()
|
||||||
|
{
|
||||||
|
PyImport_AppendInittab("hyperion", &PyInit_hyperion);
|
||||||
|
}
|
||||||
|
|
||||||
|
Effect::Effect(PyThreadState * mainThreadState, int priority, int timeout, const std::string & script, const Json::Value & args) :
|
||||||
QThread(),
|
QThread(),
|
||||||
|
_mainThreadState(mainThreadState),
|
||||||
_priority(priority),
|
_priority(priority),
|
||||||
_timeout(timeout),
|
_timeout(timeout),
|
||||||
_script(script),
|
_script(script),
|
||||||
@ -44,20 +74,26 @@ Effect::~Effect()
|
|||||||
|
|
||||||
void Effect::run()
|
void Effect::run()
|
||||||
{
|
{
|
||||||
|
// switch to the main thread state and acquire the GIL
|
||||||
|
PyEval_RestoreThread(_mainThreadState);
|
||||||
|
|
||||||
// Initialize a new thread state
|
// Initialize a new thread state
|
||||||
PyEval_AcquireLock(); // Get the GIL
|
_interpreterThreadState = Py_NewInterpreter();
|
||||||
_interpreterThreadState = Py_NewInterpreter();
|
|
||||||
|
|
||||||
// add methods extra builtin methods to the interpreter
|
// import the buildtin Hyperion module
|
||||||
PyObject * thisCapsule = PyCapsule_New(this, nullptr, nullptr);
|
PyObject * module = PyImport_ImportModule("hyperion");
|
||||||
PyObject * module = Py_InitModule4("hyperion", effectMethods, nullptr, thisCapsule, PYTHON_API_VERSION);
|
|
||||||
|
|
||||||
// add ledCount variable to the interpreter
|
// add a capsule containing 'this' to the module to be able to retrieve the effect from the callback function
|
||||||
PyObject_SetAttrString(module, "ledCount", Py_BuildValue("i", _imageProcessor->getLedCount()));
|
PyObject_SetAttrString(module, "__effectObj", PyCapsule_New(this, nullptr, nullptr));
|
||||||
|
|
||||||
// add a args variable to the interpreter
|
// add ledCount variable to the interpreter
|
||||||
PyObject_SetAttrString(module, "args", json2python(_args));
|
PyObject_SetAttrString(module, "ledCount", Py_BuildValue("i", _imageProcessor->getLedCount()));
|
||||||
//PyObject_SetAttrString(module, "args", Py_BuildValue("s", _args.c_str()));
|
|
||||||
|
// add a args variable to the interpreter
|
||||||
|
PyObject_SetAttrString(module, "args", json2python(_args));
|
||||||
|
|
||||||
|
// decref the module
|
||||||
|
Py_XDECREF(module);
|
||||||
|
|
||||||
// Set the end time if applicable
|
// Set the end time if applicable
|
||||||
if (_timeout > 0)
|
if (_timeout > 0)
|
||||||
@ -144,7 +180,7 @@ PyObject *Effect::json2python(const Json::Value &json) const
|
|||||||
PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args)
|
PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
// get the effect
|
// get the effect
|
||||||
Effect * effect = getEffect(self);
|
Effect * effect = getEffect();
|
||||||
|
|
||||||
// check if we have aborted already
|
// check if we have aborted already
|
||||||
if (effect->_abortRequested)
|
if (effect->_abortRequested)
|
||||||
@ -229,7 +265,7 @@ PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args)
|
|||||||
PyObject* Effect::wrapSetImage(PyObject *self, PyObject *args)
|
PyObject* Effect::wrapSetImage(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
// get the effect
|
// get the effect
|
||||||
Effect * effect = getEffect(self);
|
Effect * effect = getEffect();
|
||||||
|
|
||||||
// check if we have aborted already
|
// check if we have aborted already
|
||||||
if (effect->_abortRequested)
|
if (effect->_abortRequested)
|
||||||
@ -292,7 +328,7 @@ PyObject* Effect::wrapSetImage(PyObject *self, PyObject *args)
|
|||||||
|
|
||||||
PyObject* Effect::wrapAbort(PyObject *self, PyObject *)
|
PyObject* Effect::wrapAbort(PyObject *self, PyObject *)
|
||||||
{
|
{
|
||||||
Effect * effect = getEffect(self);
|
Effect * effect = getEffect();
|
||||||
|
|
||||||
// Test if the effect has reached it end time
|
// Test if the effect has reached it end time
|
||||||
if (effect->_timeout > 0 && QDateTime::currentMSecsSinceEpoch() > effect->_endTime)
|
if (effect->_timeout > 0 && QDateTime::currentMSecsSinceEpoch() > effect->_endTime)
|
||||||
@ -303,8 +339,24 @@ PyObject* Effect::wrapAbort(PyObject *self, PyObject *)
|
|||||||
return Py_BuildValue("i", effect->_abortRequested ? 1 : 0);
|
return Py_BuildValue("i", effect->_abortRequested ? 1 : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
Effect * Effect::getEffect(PyObject *self)
|
Effect * Effect::getEffect()
|
||||||
{
|
{
|
||||||
// Get the effect from the capsule in the self pointer
|
// extract the module from the runtime
|
||||||
return reinterpret_cast<Effect *>(PyCapsule_GetPointer(self, nullptr));
|
PyObject * module = PyObject_GetAttrString(PyImport_AddModule("__main__"), "hyperion");
|
||||||
|
|
||||||
|
if (PyModule_Check(module))
|
||||||
|
{
|
||||||
|
// retrieve the capsule with the effect
|
||||||
|
PyObject * effectCapsule = PyObject_GetAttrString(module, "__effectObj");
|
||||||
|
|
||||||
|
if (PyCapsule_CheckExact(effectCapsule))
|
||||||
|
{
|
||||||
|
// Get the effect from the capsule
|
||||||
|
return reinterpret_cast<Effect *>(PyCapsule_GetPointer(effectCapsule, nullptr));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// something is wrong
|
||||||
|
std::cerr << "Unable to retrieve the effect object from the Python runtime" << std::endl;
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ class Effect : public QThread
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Effect(int priority, int timeout, const std::string & script, const Json::Value & args = Json::Value());
|
Effect(PyThreadState * mainThreadState, int priority, int timeout, const std::string & script, const Json::Value & args = Json::Value());
|
||||||
virtual ~Effect();
|
virtual ~Effect();
|
||||||
|
|
||||||
virtual void run();
|
virtual void run();
|
||||||
@ -23,6 +23,9 @@ public:
|
|||||||
|
|
||||||
bool isAbortRequested() const;
|
bool isAbortRequested() const;
|
||||||
|
|
||||||
|
/// This function registers the extension module in Python
|
||||||
|
static void registerHyperionExtensionModule();
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void abort();
|
void abort();
|
||||||
|
|
||||||
@ -38,13 +41,22 @@ private:
|
|||||||
PyObject * json2python(const Json::Value & json) const;
|
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);
|
||||||
static PyObject* wrapSetImage(PyObject *self, PyObject *args);
|
static PyObject* wrapSetImage(PyObject *self, PyObject *args);
|
||||||
static PyObject* wrapAbort(PyObject *self, PyObject *args);
|
static PyObject* wrapAbort(PyObject *self, PyObject *args);
|
||||||
static Effect * getEffect(PyObject *self);
|
static Effect * getEffect();
|
||||||
|
|
||||||
|
#if PY_MAJOR_VERSION >= 3
|
||||||
|
static struct PyModuleDef moduleDef;
|
||||||
|
static PyObject* PyInit_hyperion();
|
||||||
|
#else
|
||||||
|
static void PyInit_hyperion();
|
||||||
|
#endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
PyThreadState * _mainThreadState;
|
||||||
|
|
||||||
const int _priority;
|
const int _priority;
|
||||||
|
|
||||||
const int _timeout;
|
const int _timeout;
|
||||||
|
@ -54,6 +54,7 @@ EffectEngine::EffectEngine(Hyperion * hyperion, const Json::Value & jsonEffectCo
|
|||||||
|
|
||||||
// initialize the python interpreter
|
// initialize the python interpreter
|
||||||
std::cout << "Initializing Python interpreter" << std::endl;
|
std::cout << "Initializing Python interpreter" << std::endl;
|
||||||
|
Effect::registerHyperionExtensionModule();
|
||||||
Py_InitializeEx(0);
|
Py_InitializeEx(0);
|
||||||
PyEval_InitThreads(); // Create the GIL
|
PyEval_InitThreads(); // Create the GIL
|
||||||
_mainThreadState = PyEval_SaveThread();
|
_mainThreadState = PyEval_SaveThread();
|
||||||
@ -151,7 +152,7 @@ int EffectEngine::runEffectScript(const std::string &script, const Json::Value &
|
|||||||
channelCleared(priority);
|
channelCleared(priority);
|
||||||
|
|
||||||
// create the effect
|
// create the effect
|
||||||
Effect * effect = new Effect(priority, timeout, script, args);
|
Effect * effect = new Effect(_mainThreadState, priority, timeout, script, args);
|
||||||
connect(effect, SIGNAL(setColors(int,std::vector<ColorRgb>,int,bool)), _hyperion, SLOT(setColors(int,std::vector<ColorRgb>,int,bool)), Qt::QueuedConnection);
|
connect(effect, SIGNAL(setColors(int,std::vector<ColorRgb>,int,bool)), _hyperion, SLOT(setColors(int,std::vector<ColorRgb>,int,bool)), 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);
|
||||||
|
@ -48,8 +48,8 @@ int main(int argc, char * argv[])
|
|||||||
IntParameter & argDuration = parameters.add<IntParameter> ('d', "duration" , "Specify how long the leds should be switched on in millseconds [default: infinity]");
|
IntParameter & argDuration = parameters.add<IntParameter> ('d', "duration" , "Specify how long the leds should be switched on in millseconds [default: infinity]");
|
||||||
ColorParameter & argColor = parameters.add<ColorParameter> ('c', "color" , "Set all leds to a constant color (either RRGGBB hex value or a color name. The color may be repeated multiple time like: RRGGBBRRGGBB)");
|
ColorParameter & argColor = parameters.add<ColorParameter> ('c', "color" , "Set all leds to a constant color (either RRGGBB hex value or a color name. The color may be repeated multiple time like: RRGGBBRRGGBB)");
|
||||||
ImageParameter & argImage = parameters.add<ImageParameter> ('i', "image" , "Set the leds to the colors according to the given image file");
|
ImageParameter & argImage = parameters.add<ImageParameter> ('i', "image" , "Set the leds to the colors according to the given image file");
|
||||||
StringParameter & argEffect = parameters.add<StringParameter> ('e', "effect" , "Enable the effect with the given name");
|
StringParameter & argEffect = parameters.add<StringParameter> ('e', "effect" , "Enable the effect with the given name");
|
||||||
StringParameter & argEffectArgs = parameters.add<StringParameter> (0x0, "effectArgs", "Arguments to use in combination with the specified effect. Should be a Json object string.");
|
StringParameter & argEffectArgs = parameters.add<StringParameter> (0x0, "effectArgs", "Arguments to use in combination with the specified effect. Should be a Json object string.");
|
||||||
SwitchParameter<> & argServerInfo = parameters.add<SwitchParameter<> >('l', "list" , "List server info");
|
SwitchParameter<> & argServerInfo = parameters.add<SwitchParameter<> >('l', "list" , "List server info");
|
||||||
SwitchParameter<> & argClear = parameters.add<SwitchParameter<> >('x', "clear" , "Clear data for the priority channel provided by the -p option");
|
SwitchParameter<> & argClear = parameters.add<SwitchParameter<> >('x', "clear" , "Clear data for the priority channel provided by the -p option");
|
||||||
SwitchParameter<> & argClearAll = parameters.add<SwitchParameter<> >(0x0, "clearall" , "Clear data for all active priority channels");
|
SwitchParameter<> & argClearAll = parameters.add<SwitchParameter<> >(0x0, "clearall" , "Clear data for all active priority channels");
|
||||||
@ -83,13 +83,13 @@ int main(int argc, char * argv[])
|
|||||||
bool colorTransform = argSaturation.isSet() || argValue.isSet() || argThreshold.isSet() || argGamma.isSet() || argBlacklevel.isSet() || argWhitelevel.isSet();
|
bool colorTransform = argSaturation.isSet() || argValue.isSet() || argThreshold.isSet() || argGamma.isSet() || argBlacklevel.isSet() || argWhitelevel.isSet();
|
||||||
|
|
||||||
// check that exactly one command was given
|
// check that exactly one command was given
|
||||||
int commandCount = count({argColor.isSet(), argImage.isSet(), argEffect.isSet(), argServerInfo.isSet(), argClear.isSet(), argClearAll.isSet(), colorTransform});
|
int commandCount = count({argColor.isSet(), argImage.isSet(), argEffect.isSet(), argServerInfo.isSet(), argClear.isSet(), argClearAll.isSet(), colorTransform});
|
||||||
if (commandCount != 1)
|
if (commandCount != 1)
|
||||||
{
|
{
|
||||||
std::cerr << (commandCount == 0 ? "No command found." : "Multiple commands found.") << " Provide exactly one of the following options:" << std::endl;
|
std::cerr << (commandCount == 0 ? "No command found." : "Multiple commands found.") << " Provide exactly one of the following options:" << std::endl;
|
||||||
std::cerr << " " << argColor.usageLine() << std::endl;
|
std::cerr << " " << argColor.usageLine() << std::endl;
|
||||||
std::cerr << " " << argImage.usageLine() << std::endl;
|
std::cerr << " " << argImage.usageLine() << std::endl;
|
||||||
std::cerr << " " << argEffect.usageLine() << std::endl;
|
std::cerr << " " << argEffect.usageLine() << std::endl;
|
||||||
std::cerr << " " << argServerInfo.usageLine() << std::endl;
|
std::cerr << " " << argServerInfo.usageLine() << std::endl;
|
||||||
std::cerr << " " << argClear.usageLine() << std::endl;
|
std::cerr << " " << argClear.usageLine() << std::endl;
|
||||||
std::cerr << " " << argClearAll.usageLine() << std::endl;
|
std::cerr << " " << argClearAll.usageLine() << std::endl;
|
||||||
@ -116,11 +116,11 @@ int main(int argc, char * argv[])
|
|||||||
{
|
{
|
||||||
connection.setImage(argImage.getValue(), argPriority.getValue(), argDuration.getValue());
|
connection.setImage(argImage.getValue(), argPriority.getValue(), argDuration.getValue());
|
||||||
}
|
}
|
||||||
else if (argEffect.isSet())
|
else if (argEffect.isSet())
|
||||||
{
|
{
|
||||||
connection.setEffect(argEffect.getValue(), argEffectArgs.getValue(), argPriority.getValue(), argDuration.getValue());
|
connection.setEffect(argEffect.getValue(), argEffectArgs.getValue(), argPriority.getValue(), argDuration.getValue());
|
||||||
}
|
}
|
||||||
else if (argServerInfo.isSet())
|
else if (argServerInfo.isSet())
|
||||||
{
|
{
|
||||||
QString info = connection.getServerInfo();
|
QString info = connection.getServerInfo();
|
||||||
std::cout << "Server info:\n" << info.toStdString() << std::endl;
|
std::cout << "Server info:\n" << info.toStdString() << std::endl;
|
||||||
|
Loading…
Reference in New Issue
Block a user