From bde8435b60bc17e8c58a49d05c4dfc838bb55a1b Mon Sep 17 00:00:00 2001 From: rusefi Date: Sat, 1 Feb 2025 22:41:45 -0500 Subject: [PATCH] random for Lua --- firmware/controllers/lua/lua_hooks.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/firmware/controllers/lua/lua_hooks.cpp b/firmware/controllers/lua/lua_hooks.cpp index 23ab53963a..a662382de2 100644 --- a/firmware/controllers/lua/lua_hooks.cpp +++ b/firmware/controllers/lua/lua_hooks.cpp @@ -12,6 +12,7 @@ #include "tunerstudio.h" #include "lua_pid.h" #include "start_stop.h" +#include "tinymt32.h" // TL,DR: basic implementation of 'random' #if EFI_PROD_CODE && HW_HELLEN #include "hellen_meta.h" @@ -634,9 +635,18 @@ int lua_canRxAddMask(lua_State* l) { PUBLIC_API_WEAK void boardConfigureLuaHooks(lua_State* lState) { } +static tinymt32_t tinymt; + void configureRusefiLuaHooks(lua_State* lState) { boardConfigureLuaHooks(lState); + tinymt32_init(&tinymt, 1534525); // todo: share instance with launch_control? probably not? + lua_register(lState, "random", [](lua_State* l) { + auto random = tinymt32_generate_float(&tinymt); + lua_pushnumber(l, random); + return 1; + }); + LuaClass luaTimer(lState, "Timer"); luaTimer .ctor()