Skip to content

Commit

Permalink
lua stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
dzurikmiroslav committed Dec 31, 2023
1 parent a785491 commit 8c1cbaa
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
1 change: 1 addition & 0 deletions components/script/src/l_evse_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ void l_evse_process(lua_State* L)
for (int i = 1; i <= len; i++) {
lua_rawgeti(L, -1, i);

call_field_event(L, "loop");
if (every_100ms) {
call_field_event(L, "every100ms");
}
Expand Down
4 changes: 2 additions & 2 deletions components/script/src/l_json_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ static int l_parse(lua_State* L)

parse_child(L, root);

cJSON_free(root);
cJSON_Delete(root);

return 1;
}
Expand All @@ -112,7 +112,7 @@ static int l_stringify(lua_State* L)
json = cJSON_PrintUnformatted(root);
}

cJSON_free(root);
cJSON_Delete(root);

lua_pushstring(L, json);

Expand Down
13 changes: 8 additions & 5 deletions components/script/src/l_mqtt_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "lauxlib.h"

#include "l_mqtt_lib.h"

#include "script_utils.h"
#include "esp_log.h"

static const char* TAG = "l_mqtt";
Expand All @@ -26,22 +26,25 @@ static void event_handler(void* handler_args, esp_event_base_t base, int32_t eve
case MQTT_EVENT_CONNECTED:
userdata->connected = true;
if (userdata->on_connect_ref != LUA_NOREF) {
lua_State* L = lua_newthread(userdata->L);
xSemaphoreTake(script_mutex, portMAX_DELAY);
lua_State* L = userdata->L;
lua_rawgeti(L, LUA_REGISTRYINDEX, userdata->on_connect_ref);
if (lua_pcall(L, 0, 0, 0) != LUA_OK) {
const char* err = lua_tostring(L, -1);
lua_writestring(err, strlen(err));
lua_writeline();
}
lua_closethread(L, userdata->L);
xSemaphoreGive(script_mutex);
}
break;
case MQTT_EVENT_DISCONNECTED:
userdata->connected = false;
break;
case MQTT_EVENT_DATA:
ESP_LOGW("LM", "data");
if (userdata->on_message_ref != LUA_NOREF) {
lua_State* L = lua_newthread(userdata->L);
xSemaphoreTake(script_mutex, portMAX_DELAY);
lua_State* L = userdata->L;
lua_rawgeti(L, LUA_REGISTRYINDEX, userdata->on_message_ref);
lua_pushlstring(L, event->topic, event->topic_len);
lua_pushlstring(L, event->data, event->data_len);
Expand All @@ -50,7 +53,7 @@ static void event_handler(void* handler_args, esp_event_base_t base, int32_t eve
lua_writestring(err, strlen(err));
lua_writeline();
}
lua_closethread(L, userdata->L);
xSemaphoreGive(script_mutex);
}
break;
default:
Expand Down
16 changes: 16 additions & 0 deletions components/script/src/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "lauxlib.h"

#include "script.h"
#include "script_utils.h"
#include "output_buffer.h"
#include "l_evse_lib.h"
#include "l_mqtt_lib.h"
Expand Down Expand Up @@ -39,12 +40,15 @@ static output_buffer_t* output_buffer = NULL;

static SemaphoreHandle_t output_mutex;

SemaphoreHandle_t script_mutex = NULL;

static void script_task_func(void* param)
{
lua_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT));
lua_writeline();

L = luaL_newstate();

luaL_openlibs(L);

luaL_requiref(L, "evse", luaopen_evse, 1);
Expand All @@ -62,6 +66,9 @@ static void script_task_func(void* param)
luaL_requiref(L, "boardconfig", luaopen_board_config, 0);
lua_pop(L, 1);

lua_gc(L, LUA_GCSETPAUSE, 110);
lua_gc(L, LUA_GCSETSTEPMUL, 200);

const char* loading_msg = "loading file '/data/init.lua'...";
lua_writestring(loading_msg, strlen(loading_msg));
lua_writeline();
Expand All @@ -77,7 +84,14 @@ static void script_task_func(void* param)
if (shutdown_sem != NULL) {
break;
}
xSemaphoreTake(script_mutex, portMAX_DELAY);
l_evse_process(L);
xSemaphoreGive(script_mutex);

int top = lua_gettop(L);
if (top != 0) {
ESP_LOGW(TAG, "top is %d, %d", top, lua_type(L, top));
}

vTaskDelay(pdMS_TO_TICKS(50));
}
Expand All @@ -101,6 +115,7 @@ static void script_stop(void)
ESP_LOGE(TAG, "Task stop timeout, will be force stoped");
vTaskDelete(script_task);
if (L != NULL) {
xSemaphoreGive(script_mutex);
lua_close(L);
L = NULL;
}
Expand All @@ -126,6 +141,7 @@ void script_init(void)

output_mutex = xSemaphoreCreateMutex();
output_buffer = output_buffer_create(OUTPUT_BUFFER_SIZE);
script_mutex = xSemaphoreCreateMutex();

if (script_is_enabled()) {
script_start();
Expand Down
9 changes: 9 additions & 0 deletions components/script/src/script_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef SCRIPT_UTILS_H_
#define SCRIPT_UTILS_H_

#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"

extern SemaphoreHandle_t script_mutex;

#endif /* SCRIPT_UTILS_H_ */

0 comments on commit 8c1cbaa

Please sign in to comment.