Added the possibility for scripts to have an argument string

Former-commit-id: 7c5cec34be56d926edda1c9955dc57de838622f9
This commit is contained in:
johan 2013-11-30 00:28:04 +01:00
parent 121f56ca32
commit 554bc30c35
4 changed files with 37 additions and 9 deletions

View File

@ -26,13 +26,20 @@ public slots:
/// Clear all effects
void allChannelsCleared();
public:
struct EffectDefinition
{
std::string script;
std::string args;
};
private slots:
void effectFinished(Effect * effect);
private:
Hyperion * _hyperion;
std::map<std::string, std::string> _availableEffects;
std::map<std::string, EffectDefinition> _availableEffects;
std::list<Effect *> _activeEffects;

View File

@ -17,10 +17,12 @@ PyMethodDef Effect::effectMethods[] = {
};
Effect::Effect(int priority, int timeout) :
Effect::Effect(int priority, int timeout, const std::string & script, const std::string & args) :
QThread(),
_priority(priority),
_timeout(timeout),
_script(script),
_args(args),
_endTime(-1),
_interpreterThreadState(nullptr),
_abortRequested(false),
@ -47,6 +49,9 @@ void Effect::run()
// add ledCount variable to the interpreter
PyObject_SetAttrString(module, "ledCount", Py_BuildValue("i", _imageProcessor->getLedCount()));
// add a args variable to the interpreter
PyObject_SetAttrString(module, "args", Py_BuildValue("s", _args.c_str()));
// Set the end time if applicable
if (_timeout > 0)
{
@ -54,9 +59,15 @@ void Effect::run()
}
// Run the effect script
std::string script = "test.py";
FILE* file = fopen(script.c_str(), "r");
PyRun_SimpleFile(file, script.c_str());
FILE* file = fopen(_script.c_str(), "r");
if (file != nullptr)
{
PyRun_SimpleFile(file, _script.c_str());
}
else
{
std::cerr << "Unable to open script file " << _script << std::endl;
}
// Clean up the thread state
Py_EndInterpreter(_interpreterThreadState);

View File

@ -14,7 +14,7 @@ class Effect : public QThread
Q_OBJECT
public:
Effect(int priority, int timeout);
Effect(int priority, int timeout, const std::string & script, const std::string & args = "");
virtual ~Effect();
virtual void run();
@ -45,6 +45,10 @@ private:
const int _timeout;
const std::string & _script;
const std::string & _args;
int64_t _endTime;
PyThreadState * _interpreterThreadState;

View File

@ -23,13 +23,12 @@ EffectEngine::EffectEngine(Hyperion * hyperion) :
connect(_hyperion, SIGNAL(allChannelsCleared()), this, SLOT(allChannelsCleared()));
// read all effects
_availableEffects["test"] = "test.py";
_availableEffects["test"] = {"test.py", "{\"speed\":0.2}"};
// initialize the python interpreter
std::cout << "Initializing Python interpreter" << std::endl;
Py_InitializeEx(0);
PyEval_InitThreads(); // Create the GIL
PyRun_SimpleString("print 'test'");
_mainThreadState = PyEval_SaveThread();
}
@ -54,11 +53,18 @@ int EffectEngine::runEffect(const std::string &effectName, int priority, int tim
{
std::cout << "run effect " << effectName << " on channel " << priority << std::endl;
if (_availableEffects.find(effectName) == _availableEffects.end())
{
// no such effect
return -1;
}
// clear current effect on the channel
channelCleared(priority);
// create the effect
Effect * effect = new Effect(priority, timeout);
const EffectDefinition & effectDefinition = _availableEffects[effectName];
Effect * effect = new Effect(priority, timeout, effectDefinition.script, effectDefinition.args);
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);