Lua: changing "table" API

This commit is contained in:
rusefi 2021-11-14 16:29:46 -05:00
parent 36d75fa6db
commit c8d666c5a1
2 changed files with 16 additions and 16 deletions

View File

@ -19,8 +19,6 @@ using namespace luaaa;
// Some functions lean on existing FSIO implementation // Some functions lean on existing FSIO implementation
#include "fsio_impl.h" #include "fsio_impl.h"
#define HUMAN_OFFSET 1
#if EFI_UNIT_TEST #if EFI_UNIT_TEST
Engine *engineForLuaUnitTests; Engine *engineForLuaUnitTests;
#endif #endif
@ -55,17 +53,17 @@ static int getSensor(lua_State* l, SensorType type) {
static int lua_getAuxAnalog(lua_State* l) { static int lua_getAuxAnalog(lua_State* l) {
// todo: shall we use HUMAN_INDEX since UI goes from 1 and Lua loves going from 1? // todo: shall we use HUMAN_INDEX since UI goes from 1 and Lua loves going from 1?
auto sensorIndex = luaL_checkinteger(l, 1); auto zeroBasedSensorIndex = luaL_checkinteger(l, 1);
auto type = static_cast<SensorType>(sensorIndex + static_cast<int>(SensorType::Aux1)); auto type = static_cast<SensorType>(zeroBasedSensorIndex + static_cast<int>(SensorType::Aux1));
return getSensor(l, type); return getSensor(l, type);
} }
static int lua_getSensorByIndex(lua_State* l) { static int lua_getSensorByIndex(lua_State* l) {
auto sensorIndex = luaL_checkinteger(l, 1); auto zeroBasedSensorIndex = luaL_checkinteger(l, 1);
return getSensor(l, static_cast<SensorType>(sensorIndex)); return getSensor(l, static_cast<SensorType>(zeroBasedSensorIndex));
} }
static int lua_getSensorByName(lua_State* l) { static int lua_getSensorByName(lua_State* l) {
@ -76,26 +74,26 @@ static int lua_getSensorByName(lua_State* l) {
} }
static int lua_getSensorRaw(lua_State* l) { static int lua_getSensorRaw(lua_State* l) {
auto sensorIndex = luaL_checkinteger(l, 1); auto zeroBasedSensorIndex = luaL_checkinteger(l, 1);
lua_pushnumber(l, Sensor::getRaw(static_cast<SensorType>(sensorIndex))); lua_pushnumber(l, Sensor::getRaw(static_cast<SensorType>(zeroBasedSensorIndex)));
return 1; return 1;
} }
static int lua_hasSensor(lua_State* l) { static int lua_hasSensor(lua_State* l) {
auto sensorIndex = luaL_checkinteger(l, 1); auto zeroBasedSensorIndex = luaL_checkinteger(l, 1);
lua_pushboolean(l, Sensor::hasSensor(static_cast<SensorType>(sensorIndex))); lua_pushboolean(l, Sensor::hasSensor(static_cast<SensorType>(zeroBasedSensorIndex)));
return 1; return 1;
} }
static int lua_table3d(lua_State* l) { static int lua_table3d(lua_State* l) {
auto tableIdx = luaL_checkinteger(l, 1); auto humanTableIdx = luaL_checkinteger(l, 1);
auto x = luaL_checknumber(l, 2); auto x = luaL_checknumber(l, 2);
auto y = luaL_checknumber(l, 3); auto y = luaL_checknumber(l, 3);
// index table, compute table lookup // index table, compute table lookup
auto result = getscriptTable(tableIdx)->getValue(x, y); auto result = getscriptTable(humanTableIdx - HUMAN_OFFSET)->getValue(x, y);
lua_pushnumber(l, result); lua_pushnumber(l, result);
return 1; return 1;
@ -103,7 +101,7 @@ static int lua_table3d(lua_State* l) {
static int lua_curve2d(lua_State* l) { static int lua_curve2d(lua_State* l) {
// index starting from 1 // index starting from 1
auto curveIdx = luaL_checkinteger(l, 1); auto humanCurveIdx = luaL_checkinteger(l, 1);
auto x = luaL_checknumber(l, 2); auto x = luaL_checknumber(l, 2);
#if EFI_UNIT_TEST #if EFI_UNIT_TEST
@ -111,7 +109,7 @@ static int lua_curve2d(lua_State* l) {
EXPAND_Engine; EXPAND_Engine;
#endif #endif
auto result = getCurveValue(curveIdx - HUMAN_OFFSET, x PASS_ENGINE_PARAMETER_SUFFIX); auto result = getCurveValue(humanCurveIdx - HUMAN_OFFSET, x PASS_ENGINE_PARAMETER_SUFFIX);
lua_pushnumber(l, result); lua_pushnumber(l, result);
return 1; return 1;

View File

@ -29,8 +29,10 @@ static inline uint32_t SWAP_UINT32(uint32_t x)
// we also have efi::size which probably does not work for C code // we also have efi::size which probably does not work for C code
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
// human-readable IDs start from 1 while computer-readbale indexes start from 0 #define HUMAN_OFFSET 1
#define ID2INDEX(id) ((id) - 1)
// human-readable IDs start from 1 while computer-readable indices start from 0
#define ID2INDEX(id) ((id) - HUMAN_OFFSET)
// number of milliseconds in one period of given frequency (per second) // number of milliseconds in one period of given frequency (per second)
#define frequency2periodMs(freq) ((1000.0f) / (freq)) #define frequency2periodMs(freq) ((1000.0f) / (freq))