Commit 0ee4df18 authored by Leander Schulten's avatar Leander Schulten
Browse files

make it possible that Loopprograms can run multiple times. The start function...

make it possible that Loopprograms can run multiple times. The start function can not be used by the user anymore when writing own LoopPrograms
parent c35fc968
......@@ -510,8 +510,7 @@ QString generateProgrammCode(){
}
QString generateLoopProgrammCode(){
QString code = "int getProgrammLengthInMS()override{\n\treturn 50 * outputLength; // Program::Infinite \n}\n\n";
code += "void start()override{\n\t\n}\n\n";
QString code = "int getProgrammLengthInMS()override{\n\treturn 50 * outputLength; // or Program::Infinite \n}\n\n";
code += "void loopProgram()override{\n\tfor (int i = 0; i < outputLength; ++i){\n\t\t//output[i] = ... ;\n\t\twait(50);//waits 50 ms und shows the output\n\t}\n}\n\n";
return code;
}
......@@ -561,7 +560,7 @@ void CodeEditorHelper::typeChanged(){
// we only have to generate code if the current code dont have the right functions
if(replace)
module->setCode(generateLoopProgrammCode());
else if(text.indexOf("int getProgrammLengthInMS()") <0 || text.indexOf("void start()") <0 || text.indexOf("void loopProgram()") <0){
else if(text.indexOf("int getProgrammLengthInMS()") <0 || text.indexOf("void loopProgram()") <0){
emit insertText(generateLoopProgrammCode(),0);
qDebug () << "insert LoopProgrammCode" << generateLoopProgrammCode();
}
......@@ -808,6 +807,7 @@ void CodeEditorHelper::compile(){
return;
}
stream << "#include <programms/module.h>" << endl;
stream << "#include <iostream>" << endl;
stream << "#define _USE_MATH_DEFINES"<<endl;
stream << "#include <cmath>"<<endl;
stream << "" << endl;
......
......@@ -4,22 +4,35 @@
namespace Modules {
BoostLoopProgramContextSwitcher::BoostLoopProgramContextSwitcher():m_coro([this](Coro::push_type &y){
m_yield = &y;
y();
startLoopProgram();
}){
bool BoostLoopProgramContextSwitcher::resume(){
if(m_coro){
(*m_coro)();
return !(*m_coro);
}
return true;
}
bool BoostLoopProgramContextSwitcher::resume(){
m_coro();
return !m_coro;
}
void BoostLoopProgramContextSwitcher::yield(){
if(m_yield)
(*m_yield)();
}
void BoostLoopProgramContextSwitcher::yield(){
(*m_yield)();
}
void BoostLoopProgramContextSwitcher::start(){
if(m_coro){
delete m_coro;
m_coro = nullptr;
m_yield = nullptr;
}
m_coro = new Coro::pull_type([this](Coro::push_type &y){
m_yield = &y;
y();
startLoopProgram();
});
}
BoostLoopProgramContextSwitcher::~BoostLoopProgramContextSwitcher(){
if(m_coro)
delete m_coro;
}
}
......@@ -26,12 +26,13 @@ namespace Modules {
class BoostLoopProgramContextSwitcher : public LoopProgramContextSwitcher
{
using Coro = boost::coroutines2::coroutine<void>;
Coro::pull_type m_coro;
Coro::push_type *m_yield;
Coro::pull_type *m_coro = nullptr;
Coro::push_type *m_yield = nullptr;
public:
BoostLoopProgramContextSwitcher();
virtual void yield()override;
virtual bool resume()override;
virtual void start()override;
~BoostLoopProgramContextSwitcher() override;
};
}
......
......@@ -31,6 +31,11 @@ public:
* @return true if the startFunction is finished
*/
virtual bool resume()=0;
/**
* @brief start must be called before you can use the resume and yield functions. This function should reset and initialize
* the context switcher
*/
virtual void start()=0;
/**
* @brief setStartFunction set the function wich contain the aktual loopProgramm
* @param loopProgram the function that the contextSwitcher should call the first time when starts
......@@ -96,6 +101,14 @@ public:
}
return {false,false};
}
virtual void start()final override{
if(contextSwitcher){
contextSwitcher->start();
finished = false;
currentWaitTime = 0;
timeToWait = 0;
}
}
};
......
......@@ -26,7 +26,6 @@ public:
TestLoopProgramm() = default;
virtual const char * getName()const override{return "TestLoopProgramm";}
virtual int getProgrammLengthInMS() override{return outputLength*50;}
virtual void start() override{}
virtual ~TestLoopProgramm()override = default ;
};
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment