| Line | Branch | Decision | Exec | Source |
|---|---|---|---|---|
| 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 | ||||
| 12 | 32 | static int lua_efi_print(lua_State* l) { | ||
| 13 | 32 | auto msg = luaL_checkstring(l, 1); | ||
| 14 | ||||
| 15 | // we have somewhat similar debug code at serial_can.cpp | |||
| 16 | #if EFI_UNIT_TEST | |||
| 17 | 32 | printf("[LUA] %s\n", msg); | ||
| 18 | #endif | |||
| 19 | ||||
| 20 | 32 | efiPrintf("LUA: %s", msg); | ||
| 21 | ||||
| 22 | 32 | return 0; | ||
| 23 | } | |||
| 24 | ||||
| 25 | 1 | static int lua_interpolate(lua_State* l) { | ||
| 26 | 1 | auto x1 = luaL_checknumber(l, 1); | ||
| 27 | 1 | auto y1 = luaL_checknumber(l, 2); | ||
| 28 | 1 | auto x2 = luaL_checknumber(l, 3); | ||
| 29 | 1 | auto y2 = luaL_checknumber(l, 4); | ||
| 30 | 1 | auto x = luaL_checknumber(l, 5); | ||
| 31 | ||||
| 32 | 1 | auto result = interpolateMsg("lua", x1, y1, x2, y2, x); | ||
| 33 | ||||
| 34 | 1 | lua_pushnumber(l, result); | ||
| 35 | 1 | return 1; | ||
| 36 | } | |||
| 37 | ||||
| 38 | ||||
| 39 | 297 | void configureRusefiLuaUtilHooks(lua_State* lState) { | ||
| 40 | 297 | lua_register(lState, "print", lua_efi_print); | ||
| 41 | 297 | lua_register(lState, "interpolate", lua_interpolate); | ||
| 42 | ||||
| 43 |
6/8✓ Branch 3 taken 297 times.
✓ Branch 6 taken 297 times.
✓ Branch 11 taken 1 time.
✓ Branch 15 taken 1 time.
✗ Branch 18 not taken.
✓ Branch 19 taken 1 time.
✗ Branch 21 not taken.
✓ Branch 24 taken 1 time.
|
299 | 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 |
2/8✓ Branch 3 taken 297 times.
✓ Branch 6 taken 297 times.
✗ Branch 11 not taken.
✗ Branch 15 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 21 not taken.
✗ Branch 24 not taken.
|
297 | 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 |
2/9✓ Branch 3 taken 297 times.
✓ Branch 6 taken 297 times.
✗ Branch 11 not taken.
✗ Branch 14 not taken.
✗ Branch 18 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 24 not taken.
✗ Branch 28 not taken.
|
297 | 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 | } | |||
| 95 | onBoardStandBy(); | |||
| 96 | stm32_standby(); | |||
| 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 | 297 | } | ||
| 110 | ||||
| 111 | ✗ | void * hackEngineConfigurationPointer(void *ptr) { | ||
| 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 | } | |||
| 121 |