From e386ad4b38bdc1c67f1472da5994e9b949e764da Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Thu, 29 Apr 2021 19:22:31 -0700 Subject: [PATCH] Lua tick rate hook (#2606) * implement setTickRate * actually load the hook --- firmware/controllers/lua/lua.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/firmware/controllers/lua/lua.cpp b/firmware/controllers/lua/lua.cpp index 8f911f9cf6..8cd242fed9 100644 --- a/firmware/controllers/lua/lua.cpp +++ b/firmware/controllers/lua/lua.cpp @@ -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); } }