From 2b4137a1d7577a6abfbb315622d1278aef7d0f25 Mon Sep 17 00:00:00 2001 From: Miroslav Dzurik Date: Tue, 12 Dec 2023 21:55:38 +0100 Subject: [PATCH] lua stuff --- components/script/CMakeLists.txt | 2 +- components/script/src/lua_evse.c | 238 --------------------------- components/script/src/lua_evse.h | 7 - components/script/src/lua_evse_lib.c | 214 ++++++++++++++++++++++++ components/script/src/lua_evse_lib.h | 8 + components/script/src/script.c | 2 +- 6 files changed, 224 insertions(+), 247 deletions(-) delete mode 100644 components/script/src/lua_evse.c delete mode 100644 components/script/src/lua_evse.h create mode 100644 components/script/src/lua_evse_lib.c create mode 100644 components/script/src/lua_evse_lib.h diff --git a/components/script/CMakeLists.txt b/components/script/CMakeLists.txt index f782419..f0d2840 100644 --- a/components/script/CMakeLists.txt +++ b/components/script/CMakeLists.txt @@ -79,7 +79,7 @@ set(srcs "lib/lua/lstate.c" "lib/lua/lctype.c" "lib/lua/ldebug.c" - "src/lua_evse.c" + "src/lua_evse_lib.c" ) idf_component_register(SRCS "${srcs}" diff --git a/components/script/src/lua_evse.c b/components/script/src/lua_evse.c deleted file mode 100644 index 22876e0..0000000 --- a/components/script/src/lua_evse.c +++ /dev/null @@ -1,238 +0,0 @@ -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "lua.h" -#include "lauxlib.h" - -#include "lua_evse.h" -#include "evse.h" -#include "energy_meter.h" -#include "script_utils.h" -#include "temp_sensor.h" - -#include "esp_log.h" - -static int l_state(lua_State* L) -{ - lua_pushinteger(L, evse_get_state()); - return 1; -} - -static int l_error(lua_State* L) -{ - lua_pushinteger(L, evse_get_error()); - return 1; -} - -static int l_enabled(lua_State* L) -{ - lua_pushboolean(L, evse_is_enabled()); - return 1; -} - - -// static int l_set_enabled(lua_State* L) -// { -// int top = be_top(vm); -// if (top == 2 && be_isbool(vm, 2)) { -// evse_set_enabled(be_tobool(vm, 2)); -// } else { -// be_raise(vm, "type_error", NULL); -// } -// be_return(vm); -// } - -// static int l_available(lua_State* L) -// { -// be_pushbool(vm, evse_is_available()); -// be_return(vm); -// } - -// static int l_set_available(lua_State* L) -// { -// int top = be_top(vm); -// if (top == 2 && be_isbool(vm, 2)) { -// evse_set_available(be_tobool(vm, 2)); -// } else { -// be_raise(vm, "type_error", NULL); -// } -// be_return(vm); -// } - -// static int l_charging_current(lua_State* L) -// { -// be_pushreal(vm, evse_get_charging_current() / 10.0f); -// be_return(vm); -// } - -// static int l_set_charging_current(lua_State* L) -// { -// int top = be_top(vm); -// if (top == 2 && be_isnumber(vm, 2)) { -// uint16_t value = round(be_toreal(vm, 2) * 10); -// if (evse_set_charging_current(value) != ESP_OK) { -// be_raise(vm, "value_error", "invalid value"); -// } -// } else { -// be_raise(vm, "type_error", NULL); -// } -// be_return(vm); -// } - -// static int l_power(lua_State* L) -// { -// be_pushint(vm, energy_meter_get_power()); -// be_return(vm); -// } - -// static int l_charging_time(lua_State* L) -// { -// be_pushint(vm, energy_meter_get_charging_time()); -// be_return(vm); -// } - -// static int l_session_time(lua_State* L) -// { -// be_pushint(vm, energy_meter_get_session_time()); -// be_return(vm); -// } - -// static int l_consumption(lua_State* L) -// { -// be_pushint(vm, energy_meter_get_consumption()); -// be_return(vm); -// } - -// static int l_voltage(lua_State* L) -// { -// be_newobject(vm, "list"); - -// be_pushreal(vm, energy_meter_get_l1_voltage()); -// be_data_push(vm, -2); -// be_pop(vm, 1); - -// be_pushreal(vm, energy_meter_get_l2_voltage()); -// be_data_push(vm, -2); -// be_pop(vm, 1); - -// be_pushreal(vm, energy_meter_get_l3_voltage()); -// be_data_push(vm, -2); -// be_pop(vm, 1); - -// be_pop(vm, 1); -// be_return(vm); -// } - -// static int l_current(lua_State* L) -// { -// be_newobject(vm, "list"); - -// be_pushreal(vm, energy_meter_get_l1_current()); -// be_data_push(vm, -2); -// be_pop(vm, 1); - -// be_pushreal(vm, energy_meter_get_l2_current()); -// be_data_push(vm, -2); -// be_pop(vm, 1); - -// be_pushreal(vm, energy_meter_get_l3_current()); -// be_data_push(vm, -2); -// be_pop(vm, 1); - -// be_pop(vm, 1); -// be_return(vm); -// } - -// static int l_low_temperature(lua_State* L) -// { -// be_pushreal(vm, temp_sensor_get_low() / 100); -// be_return(vm); -// } - -// static int l_high_temperature(lua_State* L) -// { -// be_pushreal(vm, temp_sensor_get_high() / 100); -// be_return(vm); -// } - -static const luaL_Reg lib[] = { - {"STATE_A", NULL}, - {"STATE_B1", NULL}, - {"STATE_B2", NULL}, - {"STATE_C1", NULL}, - {"STATE_C2", NULL}, - {"STATE_D1", NULL}, - {"STATE_D2", NULL}, - {"STATE_E", NULL}, - {"STATE_F", NULL}, - {"ERR_PILOT_FAULT_BIT", NULL}, - {"ERR_DIODE_SHORT_BIT", NULL}, - {"ERR_LOCK_FAULT_BIT", NULL}, - {"ERR_UNLOCK_FAULT_BIT", NULL}, - {"ERR_RCM_TRIGGERED_BIT", NULL}, - {"ERR_RCM_SELFTEST_FAULT_BIT", NULL}, - {"ERR_TEMPERATURE_HIGH_BIT", NULL}, - {"ERR_TEMPERATURE_FAULT_BIT", NULL}, - {"state", l_state}, - {"error", l_error}, - {"enabled", l_enabled}, - {NULL, NULL} -}; - -int lua_open_evse(lua_State* L) -{ - luaL_newlib(L, lib); - - lua_pushinteger(L, EVSE_STATE_A); - lua_setfield(L, -2, "STATE_A"); - - lua_pushinteger(L, EVSE_STATE_B1); - lua_setfield(L, -2, "STATE_B1"); - - lua_pushinteger(L, EVSE_STATE_B2); - lua_setfield(L, -2, "STATE_B2"); - - lua_pushinteger(L, EVSE_STATE_C1); - lua_setfield(L, -2, "STATE_C1"); - - lua_pushinteger(L, EVSE_STATE_C2); - lua_setfield(L, -2, "STATE_C2"); - - lua_pushinteger(L, EVSE_STATE_D1); - lua_setfield(L, -2, "STATE_D1"); - - lua_pushinteger(L, EVSE_STATE_D2); - lua_setfield(L, -2, "STATE_D2"); - - lua_pushinteger(L, EVSE_STATE_E); - lua_setfield(L, -2, "STATE_E"); - - lua_pushinteger(L, EVSE_STATE_F); - lua_setfield(L, -2, "STATE_F"); - - lua_pushinteger(L, EVSE_ERR_PILOT_FAULT_BIT); - lua_setfield(L, -2, "ERR_PILOT_FAULT_BIT"); - - lua_pushinteger(L, EVSE_ERR_DIODE_SHORT_BIT); - lua_setfield(L, -2, "ERR_DIODE_SHORT_BIT"); - - lua_pushinteger(L, EVSE_ERR_LOCK_FAULT_BIT); - lua_setfield(L, -2, "ERR_LOCK_FAULT_BIT"); - - lua_pushinteger(L, EVSE_ERR_UNLOCK_FAULT_BIT); - lua_setfield(L, -2, "ERR_UNLOCK_FAULT_BIT"); - - lua_pushinteger(L, EVSE_ERR_RCM_TRIGGERED_BIT); - lua_setfield(L, -2, "ERR_RCM_TRIGGERED_BIT"); - - lua_pushinteger(L, EVSE_ERR_RCM_SELFTEST_FAULT_BIT); - lua_setfield(L, -2, "ERR_RCM_SELFTEST_FAULT_BIT"); - - lua_pushinteger(L, EVSE_ERR_TEMPERATURE_HIGH_BIT); - lua_setfield(L, -2, "ERR_TEMPERATURE_HIGH_BIT"); - - lua_pushinteger(L, EVSE_ERR_TEMPERATURE_FAULT_BIT); - lua_setfield(L, -2, "ERR_TEMPERATURE_FAULT_BIT"); - - return 1; -} diff --git a/components/script/src/lua_evse.h b/components/script/src/lua_evse.h deleted file mode 100644 index 350d29d..0000000 --- a/components/script/src/lua_evse.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef LUA_EVSE_H_ - -#include "lua.h" - -int lua_open_evse(lua_State* L); - -#endif /* LUA_EVSE_H_ */ diff --git a/components/script/src/lua_evse_lib.c b/components/script/src/lua_evse_lib.c new file mode 100644 index 0000000..aa0dc52 --- /dev/null +++ b/components/script/src/lua_evse_lib.c @@ -0,0 +1,214 @@ +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "lua.h" +#include "lauxlib.h" + +#include "lua_evse_lib.h" +#include "evse.h" +#include "energy_meter.h" +#include "script_utils.h" +#include "temp_sensor.h" + +#include "esp_log.h" + +static int l_get_state(lua_State* L) +{ + lua_pushinteger(L, evse_get_state()); + return 1; +} + +static int l_get_error(lua_State* L) +{ + lua_pushinteger(L, evse_get_error()); + return 1; +} + +static int l_get_enabled(lua_State* L) +{ + lua_pushboolean(L, evse_is_enabled()); + return 1; +} + +static int l_set_enabled(lua_State* L) +{ + luaL_argcheck(L, lua_isboolean(L, 1), 1, "Must be boolean"); + evse_set_enabled(lua_toboolean(L, 1)); + return 0; +} + +static int l_get_available(lua_State* L) +{ + lua_pushboolean(L, evse_is_available()); + return 1; +} + +static int l_set_available(lua_State* L) +{ + luaL_argcheck(L, lua_isboolean(L, 1), 1, "Must be boolean"); + evse_set_available(lua_toboolean(L, 1)); + return 0; +} + +static int l_get_charging_current(lua_State* L) +{ + lua_pushnumber(L, evse_get_charging_current() / 10.0f); + return 1; +} + +static int l_set_charging_current(lua_State* L) +{ + luaL_argcheck(L, lua_isnumber(L, 1), 1, "Must be number"); + uint16_t value = round(lua_tonumber(L, 1) * 10); + if (evse_set_charging_current(value) != ESP_OK) { + luaL_argerror(L, 1, "Invalid value"); + } + return 0; +} + +static int l_get_power(lua_State* L) +{ + lua_pushinteger(L, energy_meter_get_power()); + return 1; +} + +static int l_get_charging_time(lua_State* L) +{ + lua_pushinteger(L, energy_meter_get_charging_time()); + return 1; +} + +static int l_get_session_time(lua_State* L) +{ + lua_pushinteger(L, energy_meter_get_session_time()); + return 1; +} + +static int l_get_consumption(lua_State* L) +{ + lua_pushinteger(L, energy_meter_get_consumption()); + return 1; +} + +static int l_get_voltage(lua_State* L) +{ + lua_pushnumber(L, energy_meter_get_l1_voltage()); + lua_pushnumber(L, energy_meter_get_l2_voltage()); + lua_pushnumber(L, energy_meter_get_l3_voltage()); + return 3; +} + +static int l_get_current(lua_State* L) +{ + lua_pushnumber(L, energy_meter_get_l1_current()); + lua_pushnumber(L, energy_meter_get_l2_current()); + lua_pushnumber(L, energy_meter_get_l3_current()); + return 3; +} + +static int l_get_low_temperature(lua_State* L) +{ + lua_pushnumber(L, temp_sensor_get_low() / 100); + return 1; +} + +static int l_get_high_temperature(lua_State* L) +{ + lua_pushnumber(L, temp_sensor_get_high() / 100); + return 1; +} + +static const luaL_Reg lib[] = { + {"STATEA", NULL}, + {"STATEB1", NULL}, + {"STATEB2", NULL}, + {"STATEC1", NULL}, + {"STATEC2", NULL}, + {"STATED1", NULL}, + {"STATED2", NULL}, + {"STATEE", NULL}, + {"STATEF", NULL}, + {"ERRPILOTFAULTBIT", NULL}, + {"ERRDIODESHORTBIT", NULL}, + {"ERRLOCKFAULTBIT", NULL}, + {"ERRUNLOCKFAULTBIT", NULL}, + {"ERRRCMTRIGGEREDBIT", NULL}, + {"ERRRCMSELFTESTFAULTBIT", NULL}, + {"ERRTEMPERATUREHIGHBIT", NULL}, + {"ERRTEMPERATUREFAULTBIT", NULL}, + {"getstate", l_get_state}, + {"geterror", l_get_error}, + {"getenabled", l_get_enabled}, + {"setenabled", l_set_enabled}, + {"getavailable", l_get_available}, + {"setavailable", l_set_available}, + {"getchargingcurrent", l_get_charging_current}, + {"setchargingcurrent", l_set_charging_current}, + {"getpower", l_get_power}, + {"getchargingtime", l_get_charging_time}, + {"getsessiontime", l_get_session_time}, + {"getconsumption", l_get_consumption}, + {"getvoltage", l_get_voltage}, + {"getcurrent", l_get_current}, + {"getlowtemperature", l_get_low_temperature}, + {"gethightemperature", l_get_high_temperature}, + {NULL, NULL} +}; + +int lua_open_evse(lua_State* L) +{ + luaL_newlib(L, lib); + + lua_pushinteger(L, EVSE_STATE_A); + lua_setfield(L, -2, "STATEA"); + + lua_pushinteger(L, EVSE_STATE_B1); + lua_setfield(L, -2, "STATEB1"); + + lua_pushinteger(L, EVSE_STATE_B2); + lua_setfield(L, -2, "STATEB2"); + + lua_pushinteger(L, EVSE_STATE_C1); + lua_setfield(L, -2, "STATEC1"); + + lua_pushinteger(L, EVSE_STATE_C2); + lua_setfield(L, -2, "STATEC2"); + + lua_pushinteger(L, EVSE_STATE_D1); + lua_setfield(L, -2, "STATED1"); + + lua_pushinteger(L, EVSE_STATE_D2); + lua_setfield(L, -2, "STATED2"); + + lua_pushinteger(L, EVSE_STATE_E); + lua_setfield(L, -2, "STATEE"); + + lua_pushinteger(L, EVSE_STATE_F); + lua_setfield(L, -2, "STATEF"); + + lua_pushinteger(L, EVSE_ERR_PILOT_FAULT_BIT); + lua_setfield(L, -2, "ERRPILOTFAULTBIT"); + + lua_pushinteger(L, EVSE_ERR_DIODE_SHORT_BIT); + lua_setfield(L, -2, "ERRDIODESHORTBIT"); + + lua_pushinteger(L, EVSE_ERR_LOCK_FAULT_BIT); + lua_setfield(L, -2, "ERRLOCKFAULTBIT"); + + lua_pushinteger(L, EVSE_ERR_UNLOCK_FAULT_BIT); + lua_setfield(L, -2, "ERRUNLOCKFAULTBIT"); + + lua_pushinteger(L, EVSE_ERR_RCM_TRIGGERED_BIT); + lua_setfield(L, -2, "ERRRCMTRIGGEREDBIT"); + + lua_pushinteger(L, EVSE_ERR_RCM_SELFTEST_FAULT_BIT); + lua_setfield(L, -2, "ERRRCMSELFTESTFAULTBIT"); + + lua_pushinteger(L, EVSE_ERR_TEMPERATURE_HIGH_BIT); + lua_setfield(L, -2, "ERRTEMPERATUREHIGHBIT"); + + lua_pushinteger(L, EVSE_ERR_TEMPERATURE_FAULT_BIT); + lua_setfield(L, -2, "ERRTEMPERATUREFAULTBIT"); + + return 1; +} diff --git a/components/script/src/lua_evse_lib.h b/components/script/src/lua_evse_lib.h new file mode 100644 index 0000000..5432f2c --- /dev/null +++ b/components/script/src/lua_evse_lib.h @@ -0,0 +1,8 @@ +#ifndef LUA_EVSE_LIB_H_ +#define LUA_EVSE_LIB_H_ + +#include "lua.h" + +int lua_open_evse(lua_State* L); + +#endif /* LUA_EVSE_LIB_H_ */ diff --git a/components/script/src/script.c b/components/script/src/script.c index a742340..12b4dab 100644 --- a/components/script/src/script.c +++ b/components/script/src/script.c @@ -15,7 +15,7 @@ #include "script.h" #include "script_utils.h" #include "output_buffer.h" -#include "lua_evse.h" +#include "lua_evse_lib.h" #define START_TIMEOUT 1000 #define SHUTDOWN_TIMEOUT 1000