Skip to content

Commit

Permalink
lua stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
dzurikmiroslav committed Dec 13, 2023
1 parent 2b4137a commit dac545c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
3 changes: 2 additions & 1 deletion components/script/lib/lua/luaconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -787,8 +787,9 @@

void script_output_print(const char* buffer, size_t length);

#define lua_writestring(s,l) script_output_print(s, l)
#define lua_writestring(s,l) script_output_print(s, l)

#define lua_writestringerror(s,l) script_output_print(s, l)


#endif
Expand Down
33 changes: 33 additions & 0 deletions components/script/src/lua_evse_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,32 @@ static int l_get_high_temperature(lua_State* L)
return 1;
}

static int l_add_driver(lua_State* L)
{
luaL_argcheck(L, lua_isnumber(L, 1), 1, "Must be table");

ESP_LOGI("YOLO", "top %d", lua_gettop(L));

lua_getglobal(L, "evse");
lua_getfield(L, -1, "__drivers");
int len = lua_rawlen(L, -1);

// lua_insert(L, -3);
lua_pushstring(L, "aaa");
lua_rawseti(L, -2, len + 1);

// ESP_LOGI("YOLO", "top %d", lua_gettop(L));

// ESP_LOGI("YOLO", "val %d", (int)lua_tointeger(L, -1));

// lua_settable()


return 0;
}

static const luaL_Reg lib[] = {
//states
{"STATEA", NULL},
{"STATEB1", NULL},
{"STATEB2", NULL},
Expand All @@ -128,6 +153,7 @@ static const luaL_Reg lib[] = {
{"STATED2", NULL},
{"STATEE", NULL},
{"STATEF", NULL},
// error bits
{"ERRPILOTFAULTBIT", NULL},
{"ERRDIODESHORTBIT", NULL},
{"ERRLOCKFAULTBIT", NULL},
Expand All @@ -136,6 +162,7 @@ static const luaL_Reg lib[] = {
{"ERRRCMSELFTESTFAULTBIT", NULL},
{"ERRTEMPERATUREHIGHBIT", NULL},
{"ERRTEMPERATUREFAULTBIT", NULL},
// methods
{"getstate", l_get_state},
{"geterror", l_get_error},
{"getenabled", l_get_enabled},
Expand All @@ -152,6 +179,9 @@ static const luaL_Reg lib[] = {
{"getcurrent", l_get_current},
{"getlowtemperature", l_get_low_temperature},
{"gethightemperature", l_get_high_temperature},
{"adddriver", l_add_driver},
// private fields
{"__drivers", NULL},
{NULL, NULL}
};

Expand Down Expand Up @@ -210,5 +240,8 @@ int lua_open_evse(lua_State* L)
lua_pushinteger(L, EVSE_ERR_TEMPERATURE_FAULT_BIT);
lua_setfield(L, -2, "ERRTEMPERATUREFAULTBIT");

lua_newtable(L);
lua_setfield(L, -2, "__drivers");

return 1;
}
12 changes: 11 additions & 1 deletion components/script/src/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,17 @@ static void script_task_func(void* param)
luaL_requiref(L, "evse", lua_open_evse, 1);
lua_pop(L, 1);

luaL_dofile(L, "/data/main.lua");
xSemaphoreTake(output_mutex, portMAX_DELAY);
output_buffer_append_str(output_buffer, "loading file '/data/main.lua'...\n");
xSemaphoreGive(output_mutex);

if (luaL_dofile(L, "/data/main.lua")) {
xSemaphoreTake(output_mutex, portMAX_DELAY);
output_buffer_append_str(output_buffer, lua_tostring(L, lua_gettop(L)));
output_buffer_append_str(output_buffer, "\n");
lua_pop(L, 1);
xSemaphoreGive(output_mutex);
}

while (true) {
if (shutdown_sem != NULL) {
Expand Down

0 comments on commit dac545c

Please sign in to comment.