15 #if EFI_PROD_CODE && HW_HELLEN
23 #if EFI_CAN_SUPPORT || EFI_UNIT_TEST
30 #define LUAAA_WITHOUT_CPP_STDLIB
33 using namespace luaaa;
47 auto zeroBasedCharIndex = luaL_checkinteger(l, 1);
48 if (zeroBasedCharIndex < 0 || zeroBasedCharIndex > VIN_NUMBER_SIZE) {
52 lua_pushnumber(l, value);
59 const char * msg = luaL_checkstring(l, 1);
62 efiPrintf(
"LUA: invalid pin [%s]", msg);
66 lua_pushnumber(l, physicalValue);
77 lua_pushnumber(l, result.Value);
88 auto zeroBasedSensorIndex = luaL_checkinteger(l, 1);
96 auto zeroBasedSensorIndex = luaL_checkinteger(l, 1);
105 luaL_error(l,
"Invalid sensor type: %s", name);
112 auto sensorName = luaL_checklstring(l, 1,
nullptr);
119 auto zeroBasedSensorIndex = luaL_checkinteger(l, 1);
126 auto zeroBasedSensorIndex = luaL_checkinteger(l, 1);
135 static uint32_t
getArray(lua_State* l,
int paramIndex, uint8_t *data, uint32_t
size) {
138 luaL_checktype(l, paramIndex, LUA_TTABLE);
140 lua_pushnumber(l, result + 1);
141 auto elementType = lua_gettable(l, paramIndex);
142 auto val = lua_tonumber(l, -1);
145 if (elementType == LUA_TNIL) {
150 if (elementType != LUA_TNUMBER) {
152 luaL_error(l,
"Unexpected data at position %d: %s", result, lua_tostring(l, -1));
159 luaL_error(l,
"Input array longer than buffer");
162 data[result - 1] = val;
167 #if EFI_CAN_SUPPORT || EFI_UNIT_TEST
170 lua_Integer
channel = luaL_checkinteger(l, 1);
172 luaL_argcheck(l,
channel == 1 ||
channel == 2, 1,
"only buses 1 and 2 currently supported");
182 if (lua_gettop(l) == 2) {
184 id = luaL_checkinteger(l, 1);
189 id = luaL_checkinteger(l, 2);
190 ext = luaL_checkinteger(l, 3);
196 luaL_argcheck(l,
id <= 0x7FF, 2,
"ID specified is greater than max std ID");
198 luaL_argcheck(l,
id <= 0x1FFF'FFFF, 2,
"ID specified is greater than max ext ID");
209 luaL_checktype(l, dataIndex, LUA_TTABLE);
211 lua_pushnumber(l, dlc + 1);
212 auto elementType = lua_gettable(l, dataIndex);
213 auto val = lua_tonumber(l, -1);
216 if (elementType == LUA_TNIL) {
221 if (elementType != LUA_TNUMBER) {
223 luaL_error(l,
"Unexpected CAN data at position %d: %s", dlc, lua_tostring(l, -1));
230 luaL_error(l,
"CAN frame length cannot be longer than 8");
258 auto channel = luaL_checkinteger(l, pos);
262 if (channel < 0 || channel >= LUA_PWM_COUNT) {
263 luaL_error(l,
"setPwmDuty invalid channel %d",
channel);
270 #define PWM_FREQ_PWM 1000
275 freq = clampF(1, freq, 1000);
285 efiPrintf(
"LUA PWM on %s at %f initial duty",
287 PERCENT_MULT *
duty);
292 auto freq = luaL_checknumber(l, 2);
293 auto duty = luaL_checknumber(l, 3);
295 if (duty < 0 || duty > PWM_MAX_DUTY) {
296 luaL_error(l,
"Duty parameter should be from 0 to 1 got %f",
duty);
321 auto duty = luaL_checknumber(l, 2);
329 auto freq = luaL_checknumber(l, 2);
332 freq = clampF(1, freq, 1000);
334 p.pwm.setFrequency(freq);
345 auto idx = luaL_checkinteger(l, 1);
360 lua_pushboolean(l,
state);
373 auto idx = luaL_checkinteger(l, 1);
374 if (idx < 0 || idx >= LUA_DIGITAL_INPUT_COUNT) {
388 lua_pushboolean(l,
state);
400 auto idx = luaL_checkinteger(l, 1);
401 auto val = luaL_checknumber(l, 2);
404 if (idx < 1 || idx > 7) {
409 firstDebugField[idx - 1] = val;
414 #if EFI_ENGINE_CONTROL
416 if (lua_gettop(l) == 0) {
429 return luaL_error(l,
"null airmass");
433 auto result = airmass->getAirmass(rpm,
false).CylinderAirmass;
435 lua_pushnumber(l, result);
440 float airmass = luaL_checknumber(l, 1);
441 float engineLoadPercent = luaL_checknumber(l, 2);
443 airmass = clampF(0, airmass, 10);
444 engineLoadPercent = clampF(0, engineLoadPercent, 1000);
456 template<
typename TCLASS,
typename ...ARGS>
457 struct PlacementConstructorCaller<TCLASS, lua_State*, ARGS...>
462 static TCLASS * Invoke(lua_State *
state,
void * mem)
464 return InvokeImpl(
state, mem,
typename make_indices<
sizeof...(ARGS)>::type());
468 template<std::size_t ...Ns>
469 static TCLASS * InvokeImpl(lua_State *
state,
void * mem, indices<Ns...>)
472 return new(mem) TCLASS(
state, LuaStack<ARGS>::get(
state, Ns + 1)...);
478 LuaSensor() : LuaSensor(nullptr,
"Invalid") { }
484 LuaSensor(lua_State* l,
const char* name)
489 luaL_error(l,
"Tried to create a Lua sensor of type %s, but one was already registered.", getSensorName());
492 efiPrintf(
"LUA registered sensor of type %s", getSensorName());
496 bool isRedundant()
const override {
497 return m_isRedundant;
501 void setTimeout(
int timeoutMs)
override {
505 void setRedundant(
bool value) {
506 m_isRedundant = value;
509 void set(
float value) {
517 void showInfo(
const char* sensorName)
const override {
518 const auto value = get();
519 efiPrintf(
"Sensor \"%s\": Lua sensor: Valid: %s Converted value %.2f", sensorName,
boolToString(value.Valid), value.Value);
523 bool m_isRedundant =
false;
527 return lua_type(l, idx) == LUA_TFUNCTION;
532 return luaL_error(l,
"expected function");
534 return luaL_ref(l, LUA_REGISTRYINDEX);
544 int callback = NO_CALLBACK;
546 switch (lua_gettop(l)) {
549 eid = luaL_checkinteger(l, 1);
555 eid = luaL_checkinteger(l, 1);
561 eid = luaL_checkinteger(l, 2);
568 eid = luaL_checkinteger(l, 2);
574 return luaL_error(l,
"Wrong number of arguments to canRxAdd. Got %d, expected 1, 2, or 3.");
588 int callback = NO_CALLBACK;
590 switch (lua_gettop(l)) {
593 eid = luaL_checkinteger(l, 1);
594 mask = luaL_checkinteger(l, 2);
600 eid = luaL_checkinteger(l, 1);
601 mask = luaL_checkinteger(l, 2);
608 eid = luaL_checkinteger(l, 2);
609 mask = luaL_checkinteger(l, 3);
616 eid = luaL_checkinteger(l, 2);
617 mask = luaL_checkinteger(l, 3);
624 return luaL_error(l,
"Wrong number of arguments to canRxAddMask. Got %d, expected 2, 3, or 4.");
634 luaL_checktype(l, 1, LUA_TTABLE);
635 size_t sourceIndex = luaL_checknumber(l, 2);
636 size_t destinationIndex = luaL_checknumber(l, 3);
637 size_t size = luaL_checknumber(l, 4);
638 for (
size_t i = 0;i<
size;i++) {
640 lua_rawseti(l, 1, destinationIndex + i);
650 LuaClass<LuaBiQuad> biQuard(lState,
"Biquad");
656 LuaClass<Timer> luaTimer(lState,
"Timer");
659 .fun(
"reset",
static_cast<void (Timer::*)()
>(&Timer::reset ))
660 .fun(
"getElapsedSeconds",
static_cast<float(Timer::*)()const
>(&Timer::getElapsedSeconds));
662 LuaClass<LuaSensor> luaSensor(lState,
"Sensor");
664 .ctor<lua_State*,
const char*>()
665 .fun(
"set", &LuaSensor::set)
666 .fun(
"setRedundant", &LuaSensor::setRedundant)
667 .fun(
"setTimeout", &LuaSensor::setTimeout)
668 .fun(
"invalidate", &LuaSensor::invalidate);
670 LuaClass<LuaPid> luaPid(lState,
"Pid");
672 .ctor<float, float, float, float,
float>()
677 LuaClass<LuaIndustrialPid> luaIndustrialPid(lState,
"IndustrialPid");
679 .ctor<float, float, float, float,
float>()
689 lua_register(lState,
"vin",
lua_vin);
696 lua_register(lState,
"table3d", [](lua_State* l) {
697 auto humanTableIdx = luaL_checkinteger(l, 1);
698 auto x = luaL_checknumber(l, 2);
699 auto y = luaL_checknumber(l, 3);
704 lua_pushnumber(l, result);
708 lua_register(lState,
"secondsSinceTsActivity", [](lua_State* l) {
713 lua_register(lState,
"curve", [](lua_State* l) {
715 auto humanCurveIdx = luaL_checkinteger(l, 1);
716 auto x = luaL_checknumber(l, 2);
718 auto result =
getCurveValue(humanCurveIdx - HUMAN_OFFSET, x);
720 lua_pushnumber(l, result);
725 lua_register(lState,
"getSentValue",
727 auto humanIndex = luaL_checkinteger(l, 1);
729 lua_pushnumber(l, value);
733 lua_register(lState,
"getSentValues",
737 auto humanIndex = luaL_checkinteger(l, 1);
739 lua_pushnumber(l, sig0);
740 lua_pushnumber(l, sig1);
745 #if EFI_LAUNCH_CONTROL
746 lua_register(lState,
"setSparkSkipRatio", [](lua_State* l) {
747 auto targetSkipRatio = luaL_checknumber(l, 1);
752 lua_register(lState,
"setSparkHardSkipRatio", [](lua_State* l) {
753 auto targetSkipRatio = luaL_checknumber(l, 1);
760 #if EFI_EMULATE_POSITION_SENSORS && !EFI_UNIT_TEST
761 lua_register(lState,
"selfStimulateRPM", [](lua_State* l) {
762 auto rpm = luaL_checkinteger(l, 1);
778 lua_register(lState,
"setLuaGauge", [](lua_State* l) {
779 auto index = luaL_checkinteger(l, 1) - 1;
780 auto value = luaL_checknumber(l, 2);
781 if (index < 0 || index >= LUA_GAUGE_COUNT)
788 lua_register(lState,
"enableCanTx", [](lua_State* l) {
793 #if EFI_ELECTRONIC_THROTTLE_BODY && EFI_PROD_CODE
794 lua_register(lState,
"getEtbTarget", [](lua_State* l) {
796 lua_pushnumber(l, result);
799 lua_register(lState,
"restartEtb", [](lua_State*) {
808 lua_register(lState,
"crc8_j1850", [](lua_State* l) {
810 uint32_t length =
getArray(l, 1, data,
sizeof(data));
811 auto trimLength = luaL_checkinteger(l, 2);
812 int crc = crc8(data, minI(length, trimLength));
814 lua_pushnumber(l, crc);
818 #if EFI_BOOST_CONTROL
819 lua_register(lState,
"setBoostTargetAdd", [](lua_State* l) {
823 lua_register(lState,
"setBoostTargetMult", [](lua_State* l) {
827 lua_register(lState,
"setBoostDutyAdd", [](lua_State* l) {
833 lua_register(lState,
"setIdleAdd", [](lua_State* l) {
838 lua_register(lState,
"setTimingAdd", [](lua_State* l) {
842 lua_register(lState,
"setTimingMult", [](lua_State* l) {
846 lua_register(lState,
"setFuelAdd", [](lua_State* l) {
850 lua_register(lState,
"setFuelMult", [](lua_State* l) {
854 #if EFI_ELECTRONIC_THROTTLE_BODY && EFI_PROD_CODE
855 lua_register(lState,
"setEtbAdd", [](lua_State* l) {
864 lua_register(lState,
"setEtbDisabled", [](lua_State* l) {
868 lua_register(lState,
"setIgnDisabled", [](lua_State* l) {
872 lua_register(lState,
"setFuelDisabled", [](lua_State* l) {
876 lua_register(lState,
"setDfcoDisabled", [](lua_State* l) {
882 lua_register(lState,
"setClutchUpState", [](lua_State* l) {
886 lua_register(lState,
"setClutchDownState", [](lua_State* l) {
890 lua_register(lState,
"setBrakePedalState", [](lua_State* l) {
895 lua_register(lState,
"setAcRequestState", [](lua_State* l) {
900 lua_register(lState,
"getCalibration", [](lua_State* l) {
901 auto propertyName = luaL_checklstring(l, 1,
nullptr);
903 lua_pushnumber(l, result);
907 #if EFI_TUNER_STUDIO && (EFI_PROD_CODE || EFI_SIMULATOR)
908 lua_register(lState,
"getOutput", [](lua_State* l) {
909 auto propertyName = luaL_checklstring(l, 1,
nullptr);
913 lua_pushnumber(l, result);
918 #if EFI_SHAFT_POSITION_INPUT
919 lua_register(lState,
"getEngineState", [](lua_State* l) {
930 lua_pushnumber(l, luaStateCode);
935 lua_register(lState,
"setCalibration", [](lua_State* l) {
936 auto propertyName = luaL_checklstring(l, 1,
nullptr);
937 auto value = luaL_checknumber(l, 2);
938 auto incrementVersion = lua_toboolean(l, 3);
941 efiPrintf(
"LUA: applying [%s][%f]", propertyName, value);
943 efiPrintf(
"LUA: invalid calibration key [%s]", propertyName);
945 if (incrementVersion) {
950 lua_register(lState, CMD_BURNCONFIG, [](lua_State* l) {
955 lua_register(lState,
"getGlobalConfigurationVersion", [](lua_State* l) {
960 lua_register(lState,
"setAcDisabled", [](lua_State* l) {
961 auto value = lua_toboolean(l, 1);
965 lua_register(lState,
"getTimeSinceAcToggleMs", [](lua_State* l) {
967 lua_pushnumber(l, result);
971 #if EFI_VEHICLE_SPEED
972 lua_register(lState,
"getCurrentGear", [](lua_State* l) {
977 lua_register(lState,
"getRpmInGear", [](lua_State* l) {
978 auto idx = luaL_checkinteger(l, 1);
989 lua_register(lState,
"getFan",
lua_fan);
993 #if EFI_ENGINE_CONTROL
998 lua_register(lState,
"isFirmwareError", [](lua_State* l) {
999 lua_pushboolean(l, hasFirmwareError());
1002 #if EFI_SHAFT_POSITION_INPUT
1003 lua_register(lState,
"stopEngine", [](lua_State*) {
1007 lua_register(lState,
"isEngineStopRequested", [](lua_State* l) {
1009 lua_pushboolean(l, result);
1012 lua_register(lState,
"getTimeSinceTriggerEventMs", [](lua_State* l) {
1014 lua_pushnumber(l, result);
1025 #if EFI_CAN_SUPPORT || EFI_UNIT_TEST
1026 lua_register(lState,
"txCan",
lua_txCan);
1029 #if EFI_VEHICLE_SPEED
1030 lua_register(lState,
"resetOdometer", [](lua_State*) {
1036 #if EFI_PROD_CODE && HW_HELLEN
1037 lua_register(lState,
"hellenEnablePower", [](lua_State*) {
1041 lua_register(lState,
"hellenDisablePower", [](lua_State*) {
1048 lua_register(lState,
"setDacVoltage", [](lua_State* l) {
1049 auto channel = luaL_checkinteger(l, 1);
1050 auto voltage = luaL_checknumber(l, 2);
void addLuaCanRxFilter(int32_t eid, uint32_t mask, int bus, int callback)
TriggerCentral triggerCentral
IgnitionState ignitionState
int getGlobalConfigurationVersion(void) const
RpmCalculator rpmCalculator
constexpr auto & module()
SingleTimerExecutor executor
IEtbController * etbControllers[ETB_COUNT]
TunerStudioOutputChannels outputChannels
RegisteredOutputPin fanRelay
OutputPin luaOutputPins[LUA_PWM_COUNT]
float getRpmInGear(size_t gear) const
virtual float getCurrentTarget() const =0
ShutdownController shutdownController
void setAirmass(AirmassResult airmass)
bool getLogicValue() const
spinning_state_e getState() const
virtual bool hasSensor() const
virtual SensorResult get() const =0
virtual float getRaw() const
static float getOrZero(SensorType type)
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 setValidValue(float value, efitick_t timestamp)
virtual void setTimeout(int timeoutMs)
bool directSelfStimulation
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)
efitimeus_t getTimeNowUs()
void setEtbLuaAdjustment(percent_t pos)
void doInitElectronicThrottle()
LimpManager * getLimpManager()
void incrementGlobalConfigurationVersion(const char *msg)
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)
BOARD_WEAK void boardConfigureLuaHooks(lua_State *lState)
int lua_canRxAddMask(lua_State *l)
static int lua_txCan(lua_State *l)
int getLuaFunc(lua_State *l)
AirmassModelBase & getLuaAirmassModel()
void setPwmDuty(int index, float duty)
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)
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 uint32_t getArray(lua_State *l, int paramIndex, uint8_t *data, uint32_t size)
static int lua_vincpy(lua_State *l)
static P luaL_checkPwmIndex(lua_State *l, int pos)
void configureRusefiLuaUtilHooks(lua_State *lState)
float getOutputValueByName(const char *name)
engine_configuration_s * engineConfiguration
const char * hwPortname(brain_pin_e brainPin)
bool isBrainPinValid(brain_pin_e brainPin)
void startSimplePwmExt(SimplePwm *state, const char *msg, ExecutorInterface *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, 1604, 1.0, 0.0, 3.0, "per")
int getSentValues(size_t index, uint16_t *sig0, uint16_t *sig1)
float getSentValue(size_t index)
This file is about configuring engine via the human-readable protocol.
void doScheduleStopEngine()
bool disableDecelerationFuelCutOff
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)
int acSwitchLastChangeTimeMs
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)