rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
lua_hooks_util.cpp
Go to the documentation of this file.
1/*
2 * lua_hooks_util.cpp
3 *
4 * Created on: Nov 2, 2021
5 * Author: rusefi
6 */
7
8#include "pch.h"
9#include "lua_hooks_util.h"
10#include "script_impl.h"
11
12static int lua_efi_print(lua_State* l) {
13 auto msg = luaL_checkstring(l, 1);
14
15 // we have somewhat similar debug code at serial_can.cpp
16#if EFI_UNIT_TEST
17 printf("[LUA] %s\n", msg);
18#endif
19
20 efiPrintf("LUA: %s", msg);
21
22 return 0;
23}
24
25static int lua_interpolate(lua_State* l) {
26 auto x1 = luaL_checknumber(l, 1);
27 auto y1 = luaL_checknumber(l, 2);
28 auto x2 = luaL_checknumber(l, 3);
29 auto y2 = luaL_checknumber(l, 4);
30 auto x = luaL_checknumber(l, 5);
31
32 auto result = interpolateMsg("lua", x1, y1, x2, y2, x);
33
34 lua_pushnumber(l, result);
35 return 1;
36}
37
38
39void configureRusefiLuaUtilHooks(lua_State* lState) {
40 lua_register(lState, "print", lua_efi_print);
41 lua_register(lState, "interpolate", lua_interpolate);
42
43 lua_register(lState, "findCurveIndex", [](lua_State* l) {
44 auto name = luaL_checklstring(l, 1, nullptr);
45 auto result = getCurveIndexByName(name);
46 if (!result) {
47 lua_pushnil(l);
48 } else {
49 // TS counts curve from 1 so convert indexing here
50 lua_pushnumber(l, result.Value + HUMAN_OFFSET);
51 }
52 return 1;
53 });
54
55 lua_register(lState, "findTableIndex",
56 [](lua_State* l) {
57 auto name = luaL_checklstring(l, 1, nullptr);
58 auto index = getTableIndexByName(name);
59 if (!index) {
60 lua_pushnil(l);
61 } else {
62 // TS counts curve from 1 so convert indexing here
63 lua_pushnumber(l, index.Value + HUMAN_OFFSET);
64 }
65 return 1;
66 });
67
68 lua_register(lState, "findSetting",
69 [](lua_State* l) {
70 auto name = luaL_checklstring(l, 1, nullptr);
71 auto defaultValue = luaL_checknumber(l, 2);
72
73 auto index = getSettingIndexByName(name);
74 if (!index) {
75 lua_pushnumber(l, defaultValue);
76 } else {
77 // TS counts curve from 1 so convert indexing here
78 lua_pushnumber(l, engineConfiguration->scriptSetting[index.Value]);
79 }
80 return 1;
81 });
82
83#ifndef STARTUP_STANDBY_PROHIBITED_PERIOD_SEC
84#define STARTUP_STANDBY_PROHIBITED_PERIOD_SEC 3
85#endif
86
87// works on STM32F4 and STM32F7 but only on specific boards
88// (it's all about PA0 being not used by ADC etc)
89#if defined(LUA_STM32_STANDBY)
90 lua_register(lState, "mcu_standby", [](lua_State*) {
91 if (getTimeNowS() < STARTUP_STANDBY_PROHIBITED_PERIOD_SEC) {
92 criticalError("mcu_standby invoked right on start");
93 return 0;
94 }
97 return 0;
98 });
99#endif
100
101/*
102 * todo: shall we? same for milliseconds?
103 lua_register(l, "getNowSeconds", [](lua_State* l) -> int {
104 int result = getTimeNowS();
105 lua_pushnumber(l, result);
106 return 1;
107 });
108*/
109}
110
112 // we know that 'engineConfiguration' was null at the time of initialization in unit tests
113#if EFI_UNIT_TEST
114 intptr_t offset = (intptr_t)ptr;
115 void * valuePtr = (void *)((char *)engineConfiguration + offset);
116 return valuePtr;
117#else
118 return ptr;
119#endif
120}
void stm32_standby()
Definition mpu_util.cpp:18
float interpolateMsg(const char *msg, float x1, float y1, float x2, float y2, float x)
Linear interpolation by two points.
efitimesec_t getTimeNowS()
Current system time in seconds (32 bits)
Definition efitime.cpp:42
static constexpr engine_configuration_s * engineConfiguration
void onBoardStandBy()
void * hackEngineConfigurationPointer(void *ptr)
static int lua_efi_print(lua_State *l)
void configureRusefiLuaUtilHooks(lua_State *lState)
static int lua_interpolate(lua_State *l)
expected< int > getSettingIndexByName(const char *name)
expected< int > getTableIndexByName(const char *name)
expected< int > getCurveIndexByName(const char *name)
uint16_t offset
Definition tunerstudio.h:0
printf("\n")