Skip to content

Commit

Permalink
Lua: changing "table" API
Browse files Browse the repository at this point in the history
  • Loading branch information
rusefillc committed Nov 14, 2021
1 parent 36d75fa commit c8d666c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
26 changes: 12 additions & 14 deletions firmware/controllers/lua/lua_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ using namespace luaaa;
// Some functions lean on existing FSIO implementation
#include "fsio_impl.h"

#define HUMAN_OFFSET 1

#if EFI_UNIT_TEST
Engine *engineForLuaUnitTests;
#endif
Expand Down Expand Up @@ -55,17 +53,17 @@ static int getSensor(lua_State* l, SensorType type) {

static int lua_getAuxAnalog(lua_State* l) {
// 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);
}

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) {
Expand All @@ -76,42 +74,42 @@ static int lua_getSensorByName(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;
}

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;
}

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 y = luaL_checknumber(l, 3);

// 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);
return 1;
}

static int lua_curve2d(lua_State* l) {
// index starting from 1
auto curveIdx = luaL_checkinteger(l, 1);
auto humanCurveIdx = luaL_checkinteger(l, 1);
auto x = luaL_checknumber(l, 2);

#if EFI_UNIT_TEST
Engine *engine = engineForLuaUnitTests;
EXPAND_Engine;
#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);
return 1;
Expand Down
6 changes: 4 additions & 2 deletions firmware/util/efilib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

// human-readable IDs start from 1 while computer-readbale indexes start from 0
#define ID2INDEX(id) ((id) - 1)
#define HUMAN_OFFSET 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)
#define frequency2periodMs(freq) ((1000.0f) / (freq))
Expand Down

0 comments on commit c8d666c

Please sign in to comment.