Use semaphore instead of queue #174

This commit is contained in:
fvanroie 2022-11-28 17:30:54 +01:00
parent e30a572dcb
commit 42d179c0be
3 changed files with 80 additions and 28 deletions

View File

@ -24,6 +24,11 @@
File pFileOut; File pFileOut;
#endif #endif
#if ESP32
static SemaphoreHandle_t xGuiSemaphore = NULL;
static TaskHandle_t g_lvgl_task_handle;
#endif
#define LVGL_TICK_PERIOD 20 #define LVGL_TICK_PERIOD 20
#ifndef TFT_BCKL #ifndef TFT_BCKL
@ -225,6 +230,15 @@ static inline void gui_init_filesystems()
void guiSetup() void guiSetup()
{ {
#if ESP32
g_lvgl_task_handle = xTaskGetCurrentTaskHandle();
xGuiSemaphore = xSemaphoreCreateMutex();
if(!xGuiSemaphore) {
LOG_FATAL(TAG_GUI, "Create mutex for LVGL failed");
}
#endif
// Initialize hardware drivers // Initialize hardware drivers
gui_init_tft(); gui_init_tft();
haspDevice.show_info(); // debug info + preload app flash size haspDevice.show_info(); // debug info + preload app flash size
@ -357,7 +371,14 @@ void guiSetup()
IRAM_ATTR void guiLoop(void) IRAM_ATTR void guiLoop(void)
{ {
#if ESP32
if(pdTRUE == xSemaphoreTake(xGuiSemaphore, portMAX_DELAY)) {
lv_task_handler();
xSemaphoreGive(xGuiSemaphore);
}
#else
lv_task_handler(); // process animations lv_task_handler(); // process animations
#endif
#if defined(STM32F4xx) #if defined(STM32F4xx)
// tick.update(); // tick.update();
@ -373,6 +394,22 @@ void guiEverySecond(void)
// nothing // nothing
} }
void gui_acquire(void)
{
TaskHandle_t task = xTaskGetCurrentTaskHandle();
if(g_lvgl_task_handle != task) {
xSemaphoreTake(xGuiSemaphore, portMAX_DELAY);
}
}
void gui_release(void)
{
TaskHandle_t task = xTaskGetCurrentTaskHandle();
if(g_lvgl_task_handle != task) {
xSemaphoreGive(xGuiSemaphore);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
#if HASP_USE_CONFIG > 0 #if HASP_USE_CONFIG > 0
bool guiGetConfig(const JsonObject& settings) bool guiGetConfig(const JsonObject& settings)

View File

@ -60,6 +60,10 @@ uint32_t guiScreenshotEtag();
void gui_flush_cb(lv_disp_drv_t* disp, const lv_area_t* area, lv_color_t* color_p); void gui_flush_cb(lv_disp_drv_t* disp, const lv_area_t* area, lv_color_t* color_p);
void gui_antiburn_cb(lv_disp_drv_t* disp, const lv_area_t* area, lv_color_t* color_p); void gui_antiburn_cb(lv_disp_drv_t* disp, const lv_area_t* area, lv_color_t* color_p);
/* ===== Locks ===== */
void gui_acquire(void);
void gui_release(void);
/* ===== Read/Write Configuration ===== */ /* ===== Read/Write Configuration ===== */
#if HASP_USE_CONFIG > 0 #if HASP_USE_CONFIG > 0
bool guiGetConfig(const JsonObject& settings); bool guiGetConfig(const JsonObject& settings);

View File

@ -16,16 +16,17 @@
#include "hal/hasp_hal.h" #include "hal/hasp_hal.h"
#include "hasp_debug.h" #include "hasp_debug.h"
#include "hasp_config.h" #include "hasp_config.h"
#include "hasp_gui.h"
#include "../hasp/hasp_dispatch.h" #include "../hasp/hasp_dispatch.h"
#include "freertos/queue.h" /* #include "freertos/queue.h"
QueueHandle_t queue; QueueHandle_t queue;
typedef struct typedef struct
{ {
char topic[64]; char topic[64];
char payload[512]; char payload[512];
} mqtt_message_t; } mqtt_message_t; */
char mqttLwtTopic[28]; char mqttLwtTopic[28];
char mqttNodeTopic[24]; char mqttNodeTopic[24];
@ -56,22 +57,22 @@ uint16_t mqtt_reconnect_counter = 0;
void mqtt_run_scripts() void mqtt_run_scripts()
{ {
if(last_mqtt_state != current_mqtt_state) { if(last_mqtt_state != current_mqtt_state) {
mqtt_message_t data; // mqtt_message_t data;
snprintf(data.topic, sizeof(data.topic), "run"); // snprintf(data.topic, sizeof(data.topic), "run");
if(current_mqtt_state) { // if(current_mqtt_state) {
snprintf(data.payload, sizeof(data.payload), "L:/mqtt_on.cmd"); // snprintf(data.payload, sizeof(data.payload), "L:/mqtt_on.cmd");
// networkStart(); // // networkStart();
} else { // } else {
snprintf(data.payload, sizeof(data.payload), "L:/mqtt_off.cmd"); // snprintf(data.payload, sizeof(data.payload), "L:/mqtt_off.cmd");
// networkStop(); // // networkStop();
} // }
size_t attempt = 0; // size_t attempt = 0;
while(xQueueSend(queue, &data, (TickType_t)0) == errQUEUE_FULL && attempt < 100) { // while(xQueueSend(queue, &data, (TickType_t)0) == errQUEUE_FULL && attempt < 100) {
vTaskDelay(5 / portTICK_PERIOD_MS); // vTaskDelay(5 / portTICK_PERIOD_MS);
attempt++; // attempt++;
}; // };
last_mqtt_state = current_mqtt_state; last_mqtt_state = current_mqtt_state;
} }
@ -220,7 +221,14 @@ static void mqtt_message_cb(const char* topic, byte* payload, unsigned int lengt
} }
} }
else */ else */
{ {
gui_acquire();
dispatch_topic_payload(topic, (const char*)payload, length > 0, TAG_MQTT);
gui_release();
}
/* {
mqtt_message_t data; mqtt_message_t data;
snprintf(data.topic, sizeof(data.topic), topic); snprintf(data.topic, sizeof(data.topic), topic);
snprintf(data.payload, sizeof(data.payload), (const char*)payload); snprintf(data.payload, sizeof(data.payload), (const char*)payload);
@ -230,7 +238,7 @@ static void mqtt_message_cb(const char* topic, byte* payload, unsigned int lengt
attempt++; attempt++;
}; };
// dispatch_topic_payload(topic, (const char*)payload, length > 0, TAG_MQTT); // dispatch_topic_payload(topic, (const char*)payload, length > 0, TAG_MQTT);
} } */
} }
static void mqttSubscribeTo(const char* topic) static void mqttSubscribeTo(const char* topic)
@ -377,7 +385,7 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
void mqttSetup() void mqttSetup()
{ {
queue = xQueueCreate(20, sizeof(mqtt_message_t)); /*queue = xQueueCreate(20, sizeof(mqtt_message_t)); */
esp_crt_bundle_set(rootca_crt_bundle_start); esp_crt_bundle_set(rootca_crt_bundle_start);
mqttStart(); mqttStart();
@ -386,6 +394,8 @@ void mqttSetup()
IRAM_ATTR void mqttLoop(void) IRAM_ATTR void mqttLoop(void)
{ {
// mqttClient.loop(); // mqttClient.loop();
/*
mqtt_message_t data; mqtt_message_t data;
while(xQueueReceive(queue, &data, (TickType_t)0)) { while(xQueueReceive(queue, &data, (TickType_t)0)) {
LOG_DEBUG(TAG_MQTT, F("[%s] Received data from queue == %s\n"), pcTaskGetTaskName(NULL), data.topic); LOG_DEBUG(TAG_MQTT, F("[%s] Received data from queue == %s\n"), pcTaskGetTaskName(NULL), data.topic);
@ -393,6 +403,7 @@ IRAM_ATTR void mqttLoop(void)
dispatch_topic_payload(data.topic, data.payload, length > 0, TAG_MQTT); dispatch_topic_payload(data.topic, data.payload, length > 0, TAG_MQTT);
delay(1); delay(1);
} }
*/
} }
void mqttEvery5Seconds(bool networkIsConnected) void mqttEvery5Seconds(bool networkIsConnected)