The time is also manipulated with the speedfactor

This commit is contained in:
ollo
2018-01-17 21:42:02 +01:00
parent 4522e40d52
commit fc81ac63f7
2 changed files with 27 additions and 15 deletions

View File

@@ -1,18 +1,12 @@
package de.c3ma.ollo.mockup;
import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaFunction;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;
import org.luaj.vm2.lib.OneArgFunction;
import org.luaj.vm2.lib.ThreeArgFunction;
import org.luaj.vm2.lib.TwoArgFunction;
import org.luaj.vm2.lib.VarArgFunction;
import org.luaj.vm2.lib.ZeroArgFunction;
import de.c3ma.ollo.LuaThreadTmr;
/**
* created at 29.12.2017 - 00:07:22<br />
* creator: ollo<br />
@@ -27,6 +21,8 @@ import de.c3ma.ollo.LuaThreadTmr;
*/
public class ESP8266Time extends TwoArgFunction {
private static long gSimulationStartTime = 0;
@Override
public LuaValue call(LuaValue modname, LuaValue env) {
env.checkglobals();
@@ -42,6 +38,23 @@ public class ESP8266Time extends TwoArgFunction {
return sntp;
}
/**
* Generate a time. If there is no speedup, it is simply the current system time.
* Otherwise the time is speedup by the given factor
* @return
*/
private long generateCurrenttime() {
if (gSimulationStartTime == 0) {
gSimulationStartTime = System.currentTimeMillis();
}
long time = System.currentTimeMillis();
if (ESP8266Tmr.gTimingFactor > 1) {
time = gSimulationStartTime + ((time - gSimulationStartTime) * ESP8266Tmr.gTimingFactor);
}
return time;
}
private class SyncFunction extends ThreeArgFunction {
@Override
@@ -49,12 +62,11 @@ public class ESP8266Time extends TwoArgFunction {
String serverName = server.checkjstring();
LuaFunction cb = callbackSuccess.checkfunction();
System.out.println("[SNTP] sync " + serverName);
/*FIXME also make it possible to simulate the time */
int seconds = (int) (System.currentTimeMillis() / 1000);
int useconds = (int) (System.currentTimeMillis() % 1000);
long time = generateCurrenttime();
int seconds = (int) (time / 1000);
int useconds = (int) (time % 1000);
cb.call(LuaValue.valueOf(seconds), LuaValue.valueOf(useconds), LuaValue.valueOf(serverName));
return LuaValue.valueOf(true);
}
@@ -64,10 +76,11 @@ public class ESP8266Time extends TwoArgFunction {
@Override
public LuaValue call() {
LuaValue[] v = new LuaValue[2];
LuaValue[] v = new LuaValue[2];
/*FIXME also make it possible to simulate the time */
int seconds = (int) (System.currentTimeMillis() / 1000);
int useconds = (int) (System.currentTimeMillis() % 1000);
long time = generateCurrenttime();
int seconds = (int) (time / 1000);
int useconds = (int) (time % 1000);
v[0] = LuaValue.valueOf(seconds);
v[1] = LuaValue.valueOf(useconds);
return LuaValue.varargsOf(v).arg1();