lua can set sensors (#3389)

* add sensor unsubscribe

* sensor hook

* test it!

* virtual destructor makes us sad

* ensure deinit
This commit is contained in:
Matthew Kennedy 2021-10-22 13:36:29 -07:00 committed by GitHub
parent 78e3c0c760
commit 8a841bb52c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 0 deletions

View File

@ -340,6 +340,30 @@ static int lua_setFuelMult(lua_State* l) {
} }
#endif // EFI_UNIT_TEST #endif // EFI_UNIT_TEST
struct LuaSensor : public StoredValueSensor {
LuaSensor() : LuaSensor("Invalid") { }
~LuaSensor() {
unregister();
}
LuaSensor(const char* name)
: StoredValueSensor(findSensorTypeByName(name), MS2NT(100))
{
Register();
}
void set(float value) {
setValidValue(value, getTimeNowNt());
}
void invalidate() {
StoredValueSensor::invalidate();
}
void showInfo(const char*) const {}
};
void configureRusefiLuaHooks(lua_State* l) { void configureRusefiLuaHooks(lua_State* l) {
LuaClass<Timer> luaTimer(l, "Timer"); LuaClass<Timer> luaTimer(l, "Timer");
@ -348,6 +372,12 @@ void configureRusefiLuaHooks(lua_State* l) {
.fun("reset", static_cast<void (Timer::*)() >(&Timer::reset )) .fun("reset", static_cast<void (Timer::*)() >(&Timer::reset ))
.fun("getElapsedSeconds", static_cast<float(Timer::*)()const>(&Timer::getElapsedSeconds)); .fun("getElapsedSeconds", static_cast<float(Timer::*)()const>(&Timer::getElapsedSeconds));
LuaClass<LuaSensor> luaSensor(l, "Sensor");
luaSensor
.ctor<const char*>()
.fun("set", &LuaSensor::set)
.fun("invalidate", &LuaSensor::invalidate);
lua_register(l, "print", lua_efi_print); lua_register(l, "print", lua_efi_print);
lua_register(l, "readPin", lua_readpin); lua_register(l, "readPin", lua_readpin);
lua_register(l, "getAuxAnalog", lua_getAuxAnalog); lua_register(l, "getAuxAnalog", lua_getAuxAnalog);

View File

@ -40,6 +40,10 @@ public:
} }
} }
void unregister() {
m_sensor = nullptr;
}
SensorResult get() const { SensorResult get() const {
// Check if mock // Check if mock
if (m_useMock) { if (m_useMock) {
@ -118,6 +122,10 @@ bool Sensor::Register() {
return s_sensorRegistry[getIndex()].Register(this); return s_sensorRegistry[getIndex()].Register(this);
} }
void Sensor::unregister() {
s_sensorRegistry[getIndex()].unregister();
}
/*static*/ void Sensor::resetRegistry() { /*static*/ void Sensor::resetRegistry() {
// Clear all entries // Clear all entries
for (size_t i = 0; i < efi::size(s_sensorRegistry); i++) { for (size_t i = 0; i < efi::size(s_sensorRegistry); i++) {

View File

@ -168,6 +168,8 @@ public:
return false; return false;
} }
void unregister();
protected: protected:
// Protected constructor - only subclasses call this // Protected constructor - only subclasses call this
explicit Sensor(SensorType type) explicit Sensor(SensorType type)

View File

@ -103,3 +103,30 @@ end
TEST(LuaHooks, TestLuaTimer) { TEST(LuaHooks, TestLuaTimer) {
EXPECT_EQ(testLuaReturnsNumber(timerTest), 0); EXPECT_EQ(testLuaReturnsNumber(timerTest), 0);
} }
static const char* sensorTest = R"(
function testFunc()
local sens = Sensor.new("CLT")
-- Check valid sensor
sens:set(33)
if getSensor("CLT") ~= 33 then
return 1
end
-- Check invalidation
sens:invalidate()
if getSensor("CLT") then
return 2
end
return 0
end
)";
TEST(LuaHooks, LuaSensor) {
EXPECT_EQ(testLuaReturnsNumber(sensorTest), 0);
// Ensure that the sensor got unregistered on teardown of the Lua interpreter
EXPECT_FALSE(Sensor::hasSensor(SensorType::Clt));
}