random for Lua

This commit is contained in:
rusefi 2025-02-01 22:41:45 -05:00
parent 9b3895d6fa
commit bde8435b60
1 changed files with 10 additions and 0 deletions

View File

@ -12,6 +12,7 @@
#include "tunerstudio.h" #include "tunerstudio.h"
#include "lua_pid.h" #include "lua_pid.h"
#include "start_stop.h" #include "start_stop.h"
#include "tinymt32.h" // TL,DR: basic implementation of 'random'
#if EFI_PROD_CODE && HW_HELLEN #if EFI_PROD_CODE && HW_HELLEN
#include "hellen_meta.h" #include "hellen_meta.h"
@ -634,9 +635,18 @@ int lua_canRxAddMask(lua_State* l) {
PUBLIC_API_WEAK void boardConfigureLuaHooks(lua_State* lState) { } PUBLIC_API_WEAK void boardConfigureLuaHooks(lua_State* lState) { }
static tinymt32_t tinymt;
void configureRusefiLuaHooks(lua_State* lState) { void configureRusefiLuaHooks(lua_State* lState) {
boardConfigureLuaHooks(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<Timer> luaTimer(lState, "Timer"); LuaClass<Timer> luaTimer(lState, "Timer");
luaTimer luaTimer
.ctor() .ctor()