Simulation starts implementation of dynamic nodemcu tasks

This commit is contained in:
Ollo 2021-03-16 22:12:02 +01:00
parent cf74a0ae90
commit 87c157fd10
2 changed files with 71 additions and 2 deletions

View File

@ -18,7 +18,7 @@ public class LuaThreadTmr extends Thread {
private LuaValue code; private LuaValue code;
private final int delay; private int delay;
private final int timerNumber; private final int timerNumber;
@ -28,7 +28,7 @@ public class LuaThreadTmr extends Thread {
this.delay = delay; this.delay = delay;
this.timerNumber = timerNumber; this.timerNumber = timerNumber;
} }
@Override @Override
public void run() { public void run() {
try { try {

View File

@ -6,6 +6,7 @@ import org.luaj.vm2.Varargs;
import org.luaj.vm2.lib.OneArgFunction; import org.luaj.vm2.lib.OneArgFunction;
import org.luaj.vm2.lib.TwoArgFunction; import org.luaj.vm2.lib.TwoArgFunction;
import org.luaj.vm2.lib.VarArgFunction; import org.luaj.vm2.lib.VarArgFunction;
import org.luaj.vm2.lib.ZeroArgFunction;
import de.c3ma.ollo.LuaThreadTmr; import de.c3ma.ollo.LuaThreadTmr;
@ -21,6 +22,8 @@ public class ESP8266Tmr extends TwoArgFunction {
private static final int MAXTHREADS = 7; private static final int MAXTHREADS = 7;
private static LuaThreadTmr[] allThreads = new LuaThreadTmr[MAXTHREADS]; private static LuaThreadTmr[] allThreads = new LuaThreadTmr[MAXTHREADS];
private static LuaThreadTmr[] dynamicThreads = new LuaThreadTmr[MAXTHREADS];
private static int dynamicThreadCounter=0;
public static int gTimingFactor = 1; public static int gTimingFactor = 1;
@ -30,6 +33,7 @@ public class ESP8266Tmr extends TwoArgFunction {
final LuaTable tmr = new LuaTable(); final LuaTable tmr = new LuaTable();
tmr.set("stop", new stop()); tmr.set("stop", new stop());
tmr.set("alarm", new alarm()); tmr.set("alarm", new alarm());
tmr.set("create", new create());
env.set("tmr", tmr); env.set("tmr", tmr);
env.get("package").get("loaded").set("tmr", tmr); env.get("package").get("loaded").set("tmr", tmr);
@ -37,6 +41,9 @@ public class ESP8266Tmr extends TwoArgFunction {
for (Thread t : allThreads) { for (Thread t : allThreads) {
t = null; t = null;
} }
for (Thread t : dynamicThreads) {
t = null;
}
return tmr; return tmr;
} }
@ -87,6 +94,68 @@ public class ESP8266Tmr extends TwoArgFunction {
} }
} }
private class dynRegister extends VarArgFunction {
private final int dynIndex;
public dynRegister(int index) {
this.dynIndex = index;
}
public Varargs invoke(Varargs varargs) {
if (varargs.narg() == 4) {
final String endlessloop = varargs.arg(3).toString().toString();
final int delay = varargs.arg(2).toint();
final LuaValue code = varargs.arg(4);
dynamicThreads[dynIndex] = new LuaThreadTmr(dynIndex, code, (endlessloop.contains("AUTO")), Math.max(delay / gTimingFactor, 1));
System.out.println("[TMR] DynTimer" + dynamicThreadCounter + " registered");
}
return LuaValue.valueOf(true);
}
}
private class dynStart extends VarArgFunction {
private final int dynIndex;
public dynStart(int index) {
this.dynIndex = index;
}
public Varargs invoke(Varargs varargs) {
if (varargs.narg()== 0) {
if (dynamicThreads[dynIndex] != null) {
dynamicThreads[dynIndex].start();
System.out.println("[TMR] DynTimer" + dynIndex + " started");
}
}
return LuaValue.valueOf(true);
}
}
private class dynStop extends ZeroArgFunction {
private final int dynIndex;
public dynStop(int index) {
this.dynIndex = index;
}
public LuaValue call() {
if (dynamicThreads[dynIndex] != null) {
dynamicThreads[dynIndex].stopThread();
dynamicThreads[dynIndex] = null;
System.out.println("[TMR] DynTimer" + dynIndex + " stopped");
}
return LuaValue.valueOf(true);
}
}
private class create extends ZeroArgFunction {
public LuaValue call() {
if (dynamicThreadCounter >= MAXTHREADS) {
return LuaValue.error("[TMR] DynTimer" + dynamicThreadCounter + " exeeded maximum");
}
final LuaTable dynTimer = new LuaTable();
dynTimer.set("register", new dynRegister(dynamicThreadCounter));
dynTimer.set("start", new dynStart(dynamicThreadCounter));
dynTimer.set("unregister", new dynStop(dynamicThreadCounter));
dynamicThreadCounter++;
return dynTimer;
}
}
public void stopAllTimer() { public void stopAllTimer() {
for (int i = 0; i < allThreads.length; i++) { for (int i = 0; i < allThreads.length; i++) {
stopTmr(i); stopTmr(i);