Lua tick rate hook (#2606)

* implement setTickRate

* actually load the hook
This commit is contained in:
Matthew Kennedy 2021-04-29 19:22:31 -07:00 committed by GitHub
parent a25773a11b
commit e386ad4b38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 0 deletions

View File

@ -72,6 +72,18 @@ private:
lua_State* m_ptr;
};
static int luaTickPeriodMs;
static int lua_setTickRate(lua_State* l) {
float freq = luaL_checknumber(l, 1);
// Limit to 1..100 hz
freq = clampF(1, freq, 100);
luaTickPeriodMs = 1000.0f / freq;
return 0;
}
static LuaHandle setupLuaState() {
LuaHandle ls = lua_newstate(myAlloc, NULL);
@ -86,6 +98,7 @@ static LuaHandle setupLuaState() {
luaopen_math(ls);
// Load rusEFI hooks
lua_register(ls, "setTickRate", lua_setTickRate);
configureRusefiLuaHooks(ls);
// run a GC cycle
@ -130,6 +143,9 @@ void LuaThread::ThreadTask() {
return;
}
// Reset default tick rate
luaTickPeriodMs = 100;
//auto scriptStr = "function onTick()\nlocal rpm = getSensor(3)\nif rpm ~= nil then\nprint('RPM: ' ..rpm)\nend\nend\n";
auto scriptStr = "n=0\nfunction onTick()\nprint('hello lua ' ..n)\nn=n+1\nend\n";
@ -161,6 +177,8 @@ void LuaThread::ThreadTask() {
}
lua_settop(ls, 0);
chThdSleepMilliseconds(luaTickPeriodMs);
}
}