Lua tick rate hook (#2606)
* implement setTickRate * actually load the hook
This commit is contained in:
parent
65a8941286
commit
db8a4fbb6f
|
@ -72,6 +72,18 @@ private:
|
||||||
lua_State* m_ptr;
|
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() {
|
static LuaHandle setupLuaState() {
|
||||||
LuaHandle ls = lua_newstate(myAlloc, NULL);
|
LuaHandle ls = lua_newstate(myAlloc, NULL);
|
||||||
|
|
||||||
|
@ -86,6 +98,7 @@ static LuaHandle setupLuaState() {
|
||||||
luaopen_math(ls);
|
luaopen_math(ls);
|
||||||
|
|
||||||
// Load rusEFI hooks
|
// Load rusEFI hooks
|
||||||
|
lua_register(ls, "setTickRate", lua_setTickRate);
|
||||||
configureRusefiLuaHooks(ls);
|
configureRusefiLuaHooks(ls);
|
||||||
|
|
||||||
// run a GC cycle
|
// run a GC cycle
|
||||||
|
@ -130,6 +143,9 @@ void LuaThread::ThreadTask() {
|
||||||
return;
|
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 = "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";
|
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);
|
lua_settop(ls, 0);
|
||||||
|
|
||||||
|
chThdSleepMilliseconds(luaTickPeriodMs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue