From 6411ef2021190b1c2b78a7a5c24dd44f77650372 Mon Sep 17 00:00:00 2001 From: Andrey G Date: Wed, 26 Oct 2022 13:42:14 +0300 Subject: [PATCH] SENT glue to LUA (#4700) * use tabs * SENT: Lua glue: getSentValue returns ETB position now Can be tested with following LUA script: setTickRate(100) function onTick() pos = getSentValue(1) setLuaGauge(1, 10000 * pos) print(pos) end --- firmware/controllers/lua/lua_hooks.cpp | 2 +- firmware/hw_layer/drivers/sent/sent.cpp | 18 +++++++++++++++--- firmware/hw_layer/drivers/sent/sent.h | 2 +- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/firmware/controllers/lua/lua_hooks.cpp b/firmware/controllers/lua/lua_hooks.cpp index f38e45f983..774c10f6b7 100644 --- a/firmware/controllers/lua/lua_hooks.cpp +++ b/firmware/controllers/lua/lua_hooks.cpp @@ -676,7 +676,7 @@ void configureRusefiLuaHooks(lua_State* l) { #if EFI_SENT_SUPPORT lua_register(l, "getSentValue", [](lua_State* l) { - auto humanIndex = luaL_checkinteger(l, 1); + auto humanIndex = luaL_checkinteger(l, 1); auto value = getSentValue(humanIndex - 1); lua_pushnumber(l, value); return 1; diff --git a/firmware/hw_layer/drivers/sent/sent.cpp b/firmware/hw_layer/drivers/sent/sent.cpp index 04b8d8694c..120a775572 100644 --- a/firmware/hw_layer/drivers/sent/sent.cpp +++ b/firmware/hw_layer/drivers/sent/sent.cpp @@ -605,9 +605,21 @@ static void printSentInfo() } } -float getSentValue(int index) { - // todo: just return ETB 0 to 100% for now? - return 23; +/* Don't be confused: this actually returns throttle body position */ +float getSentValue(size_t index) { + if (index < SENT_CHANNELS_NUM) { + uint16_t sig0, sig1; + sent_channel &ch = channels[index]; + + if (ch.GetSignals(NULL, &sig0, &sig1) == 0) { + if (sig0 + sig1 == 0xfff) { + /* scale to 0.0 .. 1.0 */ + return (float)sig1 / 0xfff; + } + } + } + + return NAN; } /* Should be called once */ diff --git a/firmware/hw_layer/drivers/sent/sent.h b/firmware/hw_layer/drivers/sent/sent.h index b8ec3a9e2a..85ebf37b43 100644 --- a/firmware/hw_layer/drivers/sent/sent.h +++ b/firmware/hw_layer/drivers/sent/sent.h @@ -19,4 +19,4 @@ void SENT_ISR_Handler(uint8_t ch, uint16_t val_res); void startSent(void); void stopSent(void); -float getSentValue(int index); +float getSentValue(size_t index);