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;
#endif
#if ESP32
static SemaphoreHandle_t xGuiSemaphore = NULL;
static TaskHandle_t g_lvgl_task_handle;
#endif
#define LVGL_TICK_PERIOD 20
#ifndef TFT_BCKL
@ -66,7 +71,7 @@ static inline void gui_init_lvgl()
#endif
/* Create the Virtual Device Buffers */
const size_t guiVDBsize = LV_VDB_SIZE / sizeof(lv_color_t);
const size_t guiVDBsize = LV_VDB_SIZE / sizeof(lv_color_t);
#ifdef ESP32
static lv_color_t* guiVdbBuffer1 =
(lv_color_t*)heap_caps_malloc(sizeof(lv_color_t) * guiVDBsize, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
@ -225,6 +230,15 @@ static inline void gui_init_filesystems()
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
gui_init_tft();
haspDevice.show_info(); // debug info + preload app flash size
@ -357,7 +371,14 @@ void guiSetup()
IRAM_ATTR void guiLoop(void)
{
#if ESP32
if(pdTRUE == xSemaphoreTake(xGuiSemaphore, portMAX_DELAY)) {
lv_task_handler();
xSemaphoreGive(xGuiSemaphore);
}
#else
lv_task_handler(); // process animations
#endif
#if defined(STM32F4xx)
// tick.update();
@ -373,6 +394,22 @@ void guiEverySecond(void)
// 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
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_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 ===== */
#if HASP_USE_CONFIG > 0
bool guiGetConfig(const JsonObject& settings);

View File

@ -16,22 +16,23 @@
#include "hal/hasp_hal.h"
#include "hasp_debug.h"
#include "hasp_config.h"
#include "hasp_gui.h"
#include "../hasp/hasp_dispatch.h"
#include "freertos/queue.h"
/* #include "freertos/queue.h"
QueueHandle_t queue;
typedef struct
{
char topic[64];
char payload[512];
} mqtt_message_t;
} mqtt_message_t; */
char mqttLwtTopic[28];
char mqttNodeTopic[24];
char mqttClientId[64];
char mqttGroupTopic[24];
bool mqttEnabled = false;
bool mqttEnabled = false;
bool mqttHAautodiscover = true;
uint32_t mqttPublishCount;
uint32_t mqttReceiveCount;
@ -56,22 +57,22 @@ uint16_t mqtt_reconnect_counter = 0;
void mqtt_run_scripts()
{
if(last_mqtt_state != current_mqtt_state) {
mqtt_message_t data;
snprintf(data.topic, sizeof(data.topic), "run");
// mqtt_message_t data;
// snprintf(data.topic, sizeof(data.topic), "run");
if(current_mqtt_state) {
snprintf(data.payload, sizeof(data.payload), "L:/mqtt_on.cmd");
// networkStart();
} else {
snprintf(data.payload, sizeof(data.payload), "L:/mqtt_off.cmd");
// networkStop();
}
// if(current_mqtt_state) {
// snprintf(data.payload, sizeof(data.payload), "L:/mqtt_on.cmd");
// // networkStart();
// } else {
// snprintf(data.payload, sizeof(data.payload), "L:/mqtt_off.cmd");
// // networkStop();
// }
size_t attempt = 0;
while(xQueueSend(queue, &data, (TickType_t)0) == errQUEUE_FULL && attempt < 100) {
vTaskDelay(5 / portTICK_PERIOD_MS);
attempt++;
};
// size_t attempt = 0;
// while(xQueueSend(queue, &data, (TickType_t)0) == errQUEUE_FULL && attempt < 100) {
// vTaskDelay(5 / portTICK_PERIOD_MS);
// attempt++;
// };
last_mqtt_state = current_mqtt_state;
}
@ -220,17 +221,24 @@ static void mqtt_message_cb(const char* topic, byte* payload, unsigned int lengt
}
}
else */
{
mqtt_message_t data;
snprintf(data.topic, sizeof(data.topic), topic);
snprintf(data.payload, sizeof(data.payload), (const char*)payload);
size_t attempt = 0;
while(xQueueSend(queue, &data, (TickType_t)0) == errQUEUE_FULL && attempt < 100) {
vTaskDelay(5 / portTICK_PERIOD_MS);
attempt++;
};
// dispatch_topic_payload(topic, (const char*)payload, length > 0, TAG_MQTT);
gui_acquire();
dispatch_topic_payload(topic, (const char*)payload, length > 0, TAG_MQTT);
gui_release();
}
/* {
mqtt_message_t data;
snprintf(data.topic, sizeof(data.topic), topic);
snprintf(data.payload, sizeof(data.payload), (const char*)payload);
size_t attempt = 0;
while(xQueueSend(queue, &data, (TickType_t)0) == errQUEUE_FULL && attempt < 100) {
vTaskDelay(5 / portTICK_PERIOD_MS);
attempt++;
};
// dispatch_topic_payload(topic, (const char*)payload, length > 0, TAG_MQTT);
} */
}
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()
{
queue = xQueueCreate(20, sizeof(mqtt_message_t));
/*queue = xQueueCreate(20, sizeof(mqtt_message_t)); */
esp_crt_bundle_set(rootca_crt_bundle_start);
mqttStart();
@ -386,6 +394,8 @@ void mqttSetup()
IRAM_ATTR void mqttLoop(void)
{
// mqttClient.loop();
/*
mqtt_message_t data;
while(xQueueReceive(queue, &data, (TickType_t)0)) {
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);
delay(1);
}
*/
}
void mqttEvery5Seconds(bool networkIsConnected)