16#if EFI_PROD_CODE && HW_HELLEN
24#if EFI_CAN_SUPPORT || EFI_UNIT_TEST
31#define LUAAA_WITHOUT_CPP_STDLIB
48 auto zeroBasedCharIndex = luaL_checkinteger(l, 1);
49 if (zeroBasedCharIndex < 0 || zeroBasedCharIndex > VIN_NUMBER_SIZE) {
53 lua_pushnumber(l, value);
60 const char * msg = luaL_checkstring(l, 1);
63 efiPrintf(
"LUA: invalid pin [%s]", msg);
67 lua_pushnumber(l, physicalValue);
78 lua_pushnumber(l, result.Value);
89 auto zeroBasedSensorIndex = luaL_checkinteger(l, 1);
97 auto zeroBasedSensorIndex = luaL_checkinteger(l, 1);
106 luaL_error(l,
"Invalid sensor type: %s", name);
113 auto sensorName = luaL_checklstring(l, 1,
nullptr);
120 auto zeroBasedSensorIndex = luaL_checkinteger(l, 1);
127 auto zeroBasedSensorIndex = luaL_checkinteger(l, 1);
139 luaL_checktype(l, paramIndex, LUA_TTABLE);
141 lua_pushnumber(l, result + 1);
142 auto elementType = lua_gettable(l, paramIndex);
143 auto val = lua_tonumber(l, -1);
146 if (elementType == LUA_TNIL) {
151 if (elementType != LUA_TNUMBER) {
153 luaL_error(l,
"Unexpected data at position %d: %s", result, lua_tostring(l, -1));
160 luaL_error(l,
"Input array longer than buffer");
163 data[result - 1] = val;
168#if EFI_CAN_SUPPORT || EFI_UNIT_TEST
171 lua_Integer
channel = luaL_checkinteger(l, 1);
173 luaL_argcheck(l,
channel == 1 ||
channel == 2, 1,
"only buses 1 and 2 currently supported");
183 if (lua_gettop(l) == 2) {
185 id = luaL_checkinteger(l, 1);
190 id = luaL_checkinteger(l, 2);
191 ext = luaL_checkinteger(l, 3);
197 luaL_argcheck(l,
id <= 0x7FF, 2,
"ID specified is greater than max std ID");
199 luaL_argcheck(l,
id <= 0x1FFF'FFFF, 2,
"ID specified is greater than max ext ID");
210 luaL_checktype(l, dataIndex, LUA_TTABLE);
212 lua_pushnumber(l, dlc + 1);
213 auto elementType = lua_gettable(l, dataIndex);
214 auto val = lua_tonumber(l, -1);
217 if (elementType == LUA_TNIL) {
222 if (elementType != LUA_TNUMBER) {
224 luaL_error(l,
"Unexpected CAN data at position %d: %s", dlc, lua_tostring(l, -1));
231 luaL_error(l,
"CAN frame length cannot be longer than 8");
259 auto channel = luaL_checkinteger(l, pos);
263 if (channel < 0 || channel >= LUA_PWM_COUNT) {
264 luaL_error(l,
"setPwmDuty invalid channel %d",
channel);
271#define PWM_FREQ_PWM 1000
276 freq = clampF(1, freq, 1000);
286 efiPrintf(
"LUA PWM on %s at %f initial duty",
288 PERCENT_MULT *
duty);
293 auto freq = luaL_checknumber(l, 2);
294 auto duty = luaL_checknumber(l, 3);
296 if (duty < 0 || duty > PWM_MAX_DUTY) {
297 luaL_error(l,
"Duty parameter should be from 0 to 1 got %f",
duty);
322 auto duty = luaL_checknumber(l, 2);
330 auto freq = luaL_checknumber(l, 2);
333 freq = clampF(1, freq, 1000);
335 p.pwm.setFrequency(freq);
346 auto idx = luaL_checkinteger(l, 1);
361 lua_pushboolean(l,
state);
374 auto idx = luaL_checkinteger(l, 1);
375 if (idx < 0 || idx >= LUA_DIGITAL_INPUT_COUNT) {
389 lua_pushboolean(l,
state);
401 auto idx = luaL_checkinteger(l, 1);
402 auto val = luaL_checknumber(l, 2);
405 if (idx < 1 || idx > 7) {
410 firstDebugField[idx - 1] = val;
415#if EFI_ENGINE_CONTROL
417 if (lua_gettop(l) == 0) {
430 return luaL_error(l,
"null airmass");
434 auto result = airmass->getAirmass(rpm,
false).CylinderAirmass;
436 lua_pushnumber(l, result);
441 float airmass = luaL_checknumber(l, 1);
442 float engineLoadPercent = luaL_checknumber(l, 2);
444 airmass = clampF(0, airmass, 10);
445 engineLoadPercent = clampF(0, engineLoadPercent, 1000);
457 template<
typename TCLASS,
typename ...ARGS>
458 struct PlacementConstructorCaller<TCLASS, lua_State*, ARGS...>
463 static TCLASS * Invoke(lua_State *
state,
void * mem)
465 return InvokeImpl(
state, mem,
typename make_indices<
sizeof...(ARGS)>::type());
469 template<std::size_t ...Ns>
470 static TCLASS * InvokeImpl(lua_State *
state,
void * mem, indices<Ns...>)
473 return new(mem) TCLASS(
state, LuaStack<ARGS>::get(
state, Ns + 1)...);
479 LuaSensor() : LuaSensor(nullptr,
"Invalid") { }
485 LuaSensor(lua_State* l,
const char* name)
490 luaL_error(l,
"Tried to create a Lua sensor of type %s, but one was already registered.",
getSensorName());
493 efiPrintf(
"LUA registered sensor of type %s",
getSensorName());
498 return m_isRedundant;
506 void setRedundant(
bool value) {
507 m_isRedundant = value;
510 void set(
float value) {
518 void showInfo(
const char* sensorName)
const override {
519 const auto value =
get();
520 efiPrintf(
"Sensor \"%s\": Lua sensor: Valid: %s Converted value %.2f", sensorName,
boolToString(value.Valid), value.Value);
524 bool m_isRedundant =
false;
528 return lua_type(l, idx) == LUA_TFUNCTION;
533 return luaL_error(l,
"expected function");
535 return luaL_ref(l, LUA_REGISTRYINDEX);
545 int callback = NO_CALLBACK;
547 switch (lua_gettop(l)) {
550 eid = luaL_checkinteger(l, 1);
556 eid = luaL_checkinteger(l, 1);
562 eid = luaL_checkinteger(l, 2);
569 eid = luaL_checkinteger(l, 2);
575 return luaL_error(l,
"Wrong number of arguments to canRxAdd. Got %d, expected 1, 2, or 3.");
589 int callback = NO_CALLBACK;
591 switch (lua_gettop(l)) {
594 eid = luaL_checkinteger(l, 1);
595 mask = luaL_checkinteger(l, 2);
601 eid = luaL_checkinteger(l, 1);
602 mask = luaL_checkinteger(l, 2);
609 eid = luaL_checkinteger(l, 2);
610 mask = luaL_checkinteger(l, 3);
617 eid = luaL_checkinteger(l, 2);
618 mask = luaL_checkinteger(l, 3);
625 return luaL_error(l,
"Wrong number of arguments to canRxAddMask. Got %d, expected 2, 3, or 4.");
639 LuaClass<Timer> luaTimer(lState,
"Timer");
642 .fun(
"reset",
static_cast<void (Timer::*)()
>(&Timer::reset ))
643 .fun(
"getElapsedSeconds",
static_cast<float(Timer::*)()const
>(&Timer::getElapsedSeconds));
645 LuaClass<LuaSensor> luaSensor(lState,
"Sensor");
647 .ctor<lua_State*,
const char*>()
648 .fun(
"set", &LuaSensor::set)
649 .fun(
"setRedundant", &LuaSensor::setRedundant)
650 .fun(
"setTimeout", &LuaSensor::setTimeout)
651 .fun(
"invalidate", &LuaSensor::invalidate);
654#define WITH_LUA_PID TRUE
658 LuaClass<LuaBiQuad> biQuard(lState,
"Biquad");
664 LuaClass<LuaPid> luaPid(lState,
"Pid");
671 LuaClass<LuaIndustrialPid> luaIndustrialPid(lState,
"IndustrialPid");
684#if EFI_PROD_CODE && EFI_SHAFT_POSITION_INPUT
685 lua_register(lState,
"startCrankingEngine", [](lua_State* l) {
690 lua_register(lState,
"vin",
lua_vin);
698#ifndef WITH_LUA_CONSUMPTION
699#define WITH_LUA_CONSUMPTION TRUE
702#if EFI_VEHICLE_SPEED && WITH_LUA_CONSUMPTION
703 lua_register(lState,
"getConsumedGrams", [](lua_State* l) {
707 lua_register(lState,
"getConsumedGramsRemainder", [](lua_State* l) {
711 lua_register(lState,
"getConsumptionGramPerSecond", [](lua_State* l) {
715 lua_register(lState,
"resetOdometer", [](lua_State*) {
720 lua_register(lState,
"table3d", [](lua_State* l) {
721 auto humanTableIdx = luaL_checkinteger(l, 1);
722 auto x = luaL_checknumber(l, 2);
723 auto y = luaL_checknumber(l, 3);
728 lua_pushnumber(l, result);
732 lua_register(lState,
"secondsSinceTsActivity", [](lua_State* l) {
737 lua_register(lState,
"curve", [](lua_State* l) {
739 auto humanCurveIdx = luaL_checkinteger(l, 1);
740 auto x = luaL_checknumber(l, 2);
742 auto result =
getCurveValue(humanCurveIdx - HUMAN_OFFSET, x);
744 lua_pushnumber(l, result);
748#if EFI_PROD_CODE && EFI_SENT_SUPPORT
749 lua_register(lState,
"getSentValue",
751 auto humanIndex = luaL_checkinteger(l, 1);
753 lua_pushnumber(l, value);
757 lua_register(lState,
"getSentValues",
761 auto humanIndex = luaL_checkinteger(l, 1);
763 lua_pushnumber(l, sig0);
764 lua_pushnumber(l, sig1);
769#if EFI_LAUNCH_CONTROL
770 lua_register(lState,
"setSparkSkipRatio", [](lua_State* l) {
771 auto targetSkipRatio = luaL_checknumber(l, 1);
776 lua_register(lState,
"setSparkHardSkipRatio", [](lua_State* l) {
777 auto targetSkipRatio = luaL_checknumber(l, 1);
784#if EFI_EMULATE_POSITION_SENSORS && !EFI_UNIT_TEST
785 lua_register(lState,
"selfStimulateRPM", [](lua_State* l) {
786 auto rpm = luaL_checkinteger(l, 1);
802 lua_register(lState,
"setLuaGauge", [](lua_State* l) {
803 auto index = luaL_checkinteger(l, 1) - 1;
804 auto value = luaL_checknumber(l, 2);
805 if (index < 0 || index >= LUA_GAUGE_COUNT)
812 lua_register(lState,
"enableCanTx", [](lua_State* l) {
817#if EFI_ELECTRONIC_THROTTLE_BODY && EFI_PROD_CODE
818 lua_register(lState,
"getEtbTarget", [](lua_State* l) {
820 assertNotNull(controller, 0);
821 auto result = controller->getCurrentTarget();
822 lua_pushnumber(l, result);
825 lua_register(lState,
"restartEtb", [](lua_State*) {
834 lua_register(lState,
"crc8_j1850", [](lua_State* l) {
836 uint32_t length =
getLuaArray(l, 1, data,
sizeof(data));
837 auto trimLength = luaL_checkinteger(l, 2);
838 int crc = crc8(data, minI(length, trimLength));
840 lua_pushnumber(l, crc);
845 lua_register(lState,
"setBoostTargetAdd", [](lua_State* l) {
849 lua_register(lState,
"setBoostTargetMult", [](lua_State* l) {
853 lua_register(lState,
"setBoostDutyAdd", [](lua_State* l) {
859 lua_register(lState,
"setIdleAdd", [](lua_State* l) {
864 lua_register(lState,
"setTimingAdd", [](lua_State* l) {
868 lua_register(lState,
"setTimingMult", [](lua_State* l) {
872 lua_register(lState,
"setFuelAdd", [](lua_State* l) {
876 lua_register(lState,
"setFuelMult", [](lua_State* l) {
880#if EFI_ELECTRONIC_THROTTLE_BODY && EFI_PROD_CODE
881 lua_register(lState,
"setEtbAdd", [](lua_State* l) {
888 lua_register(lState,
"setEtbDisabled", [](lua_State* l) {
894 lua_register(lState,
"setIgnDisabled", [](lua_State* l) {
898 lua_register(lState,
"setFuelDisabled", [](lua_State* l) {
902 lua_register(lState,
"setDfcoDisabled", [](lua_State* l) {
908 lua_register(lState,
"setClutchUpState", [](lua_State* l) {
912 lua_register(lState,
"setClutchDownState", [](lua_State* l) {
916 lua_register(lState,
"setBrakePedalState", [](lua_State* l) {
921 lua_register(lState,
"setAcRequestState", [](lua_State* l) {
926 lua_register(lState,
"setTorqueReductionState", [](lua_State* l) {
931 lua_register(lState,
"getCalibration", [](lua_State* l) {
932 auto propertyName = luaL_checklstring(l, 1,
nullptr);
934 lua_pushnumber(l, result);
938#if EFI_TUNER_STUDIO && (EFI_PROD_CODE || EFI_SIMULATOR)
939 lua_register(lState,
"getOutput", [](lua_State* l) {
940 auto propertyName = luaL_checklstring(l, 1,
nullptr);
944 lua_pushnumber(l, result);
949#if EFI_SHAFT_POSITION_INPUT
950 lua_register(lState,
"getEngineState", [](lua_State* l) {
961 lua_pushnumber(l, luaStateCode);
966 lua_register(lState,
"setCalibration", [](lua_State* l) {
967 auto propertyName = luaL_checklstring(l, 1,
nullptr);
968 auto value = luaL_checknumber(l, 2);
969 auto incrementVersion = lua_toboolean(l, 3);
972 efiPrintf(
"LUA: applying [%s][%f]", propertyName, value);
974 efiPrintf(
"LUA: invalid calibration key [%s]", propertyName);
976 if (incrementVersion) {
981 lua_register(lState, CMD_BURNCONFIG, [](lua_State* l) {
986 lua_register(lState,
"getGlobalConfigurationVersion", [](lua_State* l) {
991 lua_register(lState,
"setAcDisabled", [](lua_State* l) {
992 auto value = lua_toboolean(l, 1);
996 lua_register(lState,
"getTimeSinceAcToggleMs", [](lua_State* l) {
998 lua_pushnumber(l, result);
1007 lua_register(lState,
"getFan",
lua_fan);
1011#if EFI_ENGINE_CONTROL
1017#ifndef WITH_LUA_STOP_ENGINE
1018#define WITH_LUA_STOP_ENGINE TRUE
1021#if WITH_LUA_STOP_ENGINE
1022 lua_register(lState,
"isFirmwareError", [](lua_State* l) {
1023 lua_pushboolean(l, hasFirmwareError());
1026#if EFI_SHAFT_POSITION_INPUT
1027 lua_register(lState,
"stopEngine", [](lua_State*) {
1031 lua_register(lState,
"isEngineStopRequested", [](lua_State* l) {
1033 lua_pushboolean(l, result);
1036 lua_register(lState,
"getTimeSinceTriggerEventMs", [](lua_State* l) {
1038 lua_pushnumber(l, result);
1050#if EFI_CAN_SUPPORT || EFI_UNIT_TEST
1051 lua_register(lState,
"txCan",
lua_txCan);
1064#if EFI_PROD_CODE && HW_HELLEN
1065 lua_register(lState,
"hellenEnablePower", [](lua_State*) {
1069 lua_register(lState,
"hellenDisablePower", [](lua_State*) {
1076 lua_register(lState,
"setDacVoltage", [](lua_State* l) {
1077 auto channel = luaL_checkinteger(l, 1);
1078 auto voltage = luaL_checknumber(l, 2);
void addLuaCanRxFilter(int32_t eid, uint32_t mask, int bus, int callback)
Timer timeSinceStateChange
TriggerCentral triggerCentral
IgnitionState ignitionState
int getGlobalConfigurationVersion(void) const
SingleTimerExecutor scheduler
RpmCalculator rpmCalculator
IEtbController * etbControllers[ETB_COUNT]
TunerStudioOutputChannels outputChannels
constexpr auto & module()
RegisteredOutputPin fanRelay
OutputPin luaOutputPins[LUA_PWM_COUNT]
ShutdownController shutdownController
void setAirmass(AirmassResult airmass)
bool getLogicValue() const
spinning_state_e getState() const
virtual bool hasSensor() const
virtual SensorResult get() const =0
virtual bool isRedundant() const
virtual float getRaw() const
static float getOrZero(SensorType type)
const char * getSensorName() const
bool isEngineStop(efitick_t nowNt) const
void setSimplePwmDutyCycle(float dutyCycle) override
Base class for sensors that compute a value on one thread, and want to make it available to consumers...
void showInfo(const char *sensorName) const override
void setValidValue(float value, efitick_t timestamp)
virtual void setTimeout(int timeoutMs)
SensorResult get() const final override
bool directSelfStimulation
float getConsumedGramsRemainder() const
float getConsumptionGramPerSecond() const
uint32_t getConsumedGrams() const
virtual float getValue(float xColumn, float yRow) const =0
void setDacVoltage(int channel, float voltage)
ioportid_t getHwPort(const char *msg, brain_pin_e brainPin)
brain_pin_e parseBrainPin(const char *str)
ioportmask_t getHwPin(const char *msg, brain_pin_e brainPin)
const char * boolToString(bool value)
void setEtbLuaAdjustment(percent_t pos)
void doInitElectronicThrottle()
LimpManager * getLimpManager()
static Engine *const engine
void incrementGlobalConfigurationVersion(const char *msg)
static constexpr engine_configuration_s * engineConfiguration
AirmassModelBase * getAirmassModel(engine_load_mode_e mode)
void hellenEnableEn(const char *msg)
void hellenDisableEn(const char *msg)
StoredValueSensor luaGauges[]
bool efiReadPin(brain_pin_e pin)
static int lua_setPwmFreq(lua_State *l)
void startPwm(int index, float freq, float duty)
static auto lua_getAirmassResolveMode(lua_State *l)
static int lua_readpin(lua_State *l)
PUBLIC_API_WEAK void boardConfigureLuaHooks(lua_State *lState)
int lua_canRxAddMask(lua_State *l)
static int lua_txCan(lua_State *l)
int getLuaFunc(lua_State *l)
void setPwmDuty(int index, float duty)
uint32_t getLuaArray(lua_State *l, int paramIndex, uint8_t *data, uint32_t size)
static int lua_setAirmass(lua_State *l)
static SimplePwm pwms[LUA_PWM_COUNT]
static int lua_getDigital(lua_State *l)
bool getAuxDigital(int index)
static int lua_getAuxDigital(lua_State *l)
static SensorType findSensorByName(lua_State *l, const char *name)
static LuaAirmass luaAirmass
static int lua_getAuxAnalog(lua_State *l)
static int lua_getSensorByIndex(lua_State *l)
void configureRusefiLuaHooks(lua_State *lState)
static int validateCanChannelAndConvertFromHumanIntoZeroIndex(lua_State *l)
static int lua_fan(lua_State *l)
static int lua_hasSensor(lua_State *l)
static int lua_getSensorByName(lua_State *l)
AirmassModelBase & getLuaAirmassModel()
static int lua_getAirmass(lua_State *l)
static int lua_setPwmDuty(lua_State *l)
static int lua_getSensorRaw(lua_State *l)
static int getSensor(lua_State *l, SensorType type)
static bool isFunction(lua_State *l, int idx)
int lua_canRxAdd(lua_State *l)
static int lua_setDebug(lua_State *l)
static int lua_startPwm(lua_State *l)
static int lua_vin(lua_State *l)
static P luaL_checkPwmIndex(lua_State *l, int pos)
void configureRusefiLuaUtilHooks(lua_State *lState)
float getOutputValueByName(const char *name)
const char * hwPortname(brain_pin_e brainPin)
bool isBrainPinValid(brain_pin_e brainPin)
void startSimplePwmExt(SimplePwm *state, const char *msg, Scheduler *executor, brain_pin_e brainPin, OutputPin *output, float frequency, float dutyCycle, pwm_gen_callback *callback)
float getCurveValue(int index, float key)
ValueProvider3D * getscriptTable(int index)
SensorType findSensorTypeByName(const char *name)
luaAdjustment("ETB: luaAdjustment", SensorCategory.SENSOR_INPUTS, FieldType.INT, 1656, 1.0, 0.0, 3.0, "%")
float getSentValue(SentInput input)
int getSentValues(SentInput input, uint16_t *sig0, uint16_t *sig1)
This file is about configuring engine via the human-readable protocol.
void doScheduleStopEngine()
bool disableDecelerationFuelCutOff
bool torqueReductionState
void configureLowpass(float samplingFrequency, float cutoffFrequency)
float filter(float input)
void setOffset(float offset)
void setDerivativeFilterLoss(float derivativeFilterLoss)
void setAntiwindupFreq(float antiwindupFreq)
float get(float target, float input)
float get(float target, float input)
void setOffset(float offset)
scaled_channel< int16_t, 2, 1 > luaTargetAdd
engine_load_mode_e fuelAlgorithm
switch_input_pin_e luaDigitalInputPins[LUA_DIGITAL_INPUT_COUNT]
output_pin_e luaOutputPins[LUA_PWM_COUNT]
void disableTriggerStimulator()
void setTriggerEmulatorRPM(int rpm)
void enableTriggerStimulator(bool incGlobalConfiguration)
void updateTunerStudioState()
int getSecondsSinceChannelsRequest()
bool setConfigValueByName(const char *name, float value)
float getConfigValueByName(const char *name)