I know there are many topics about updating the LVGL screen, but I wanted an opinion on the efficiency of the code below.
I have a product that reads the temperature and puts the values on the screen.
It is a simple temperature datalogger.
So I have a task that reads the temperature and another task that reads the time.
Then I have the routines (Tasks) that update the screen, I wanted to know if this way of writing is correct?
If I am efficient this way? It’s working well…But I would like to know if there is a more efficient way to do this?
I want to free up processing and RAM to implement other functions.
This Rotine take the temperature:
void v_LerSensor(void *parameters){
DS18B20.begin(); // initialize the DS18B20 sensor
float tempC;
float tempF;
while(1){
DS18B20.requestTemperatures(); // send the command to get temperatures
tempC = DS18B20.getTempCByIndex(0); // read temperature in °C
if (tempC!=-127.00){
tempF = tempC * 9 / 5 + 32; // convert °C to °F
xQueueSend(TempC_Queue, &tempC, pdMS_TO_TICKS(0));
}else{
Serial.println("[INFO] - Sensor desconectado");
xTaskCreate(v_DisplayTempDesc, "v_DisplayTempDesc", 4096, NULL, 3, NULL);
}
vTaskDelay(30 / portTICK_PERIOD_MS);
}
}
and rotine for date and hour:
void v_DataHora(void *parameters){
char data_formatadaZ[64];
while(1){
time_t t = now();
sprintf(datahora_formatadaX, "%02d/%02d/%04d %02d:%02d:%02d", day(t), month(t), year(t), hour(t), minute(t), second(t));
sprintf(data_formatadaX, "%04d-%02d-%02d", year(t), month(t), day(t));
strcpy(data_formatadaZ, datahora_formatadaX);
xQueueSend(datahora_formatadaX_Queue, &data_formatadaZ, pdMS_TO_TICKS(0));
v_RotinaEnviaMapa(datahora_formatadaX);
v_RotinaApagarLog(datahora_formatadaX);
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
The code below is actually my question:
Is it correct to do it this way?
//Funções que rodam o tempo todo para atualizar o display:
void v_DisplayTemp(void *parameters){
float tempReceived;
float tempReceivedAnt;
char tempC_char[20];
String systemUpTime;
while(1){
if (xQueueReceive(TempC_Queue, &tempReceived, portMAX_DELAY)){
if (tempReceived !=tempReceivedAnt){
sprintf(tempC_char, "%.2fºC", tempReceived);
tempReceivedAnt=tempReceived;
if (xSemaphoreTake(xMutex, portMAX_DELAY)) {
lv_meter_set_indicator_value(MyGauge[0], indic1[0], tempReceived);
lv_label_set_text(ui_lblTemp, tempC_char);
lv_timer_handler();
//vTaskDelay(10 / portTICK_PERIOD_MS);
xSemaphoreGive(xMutex);
}
}
}
}
}
void v_DisplayUptime(void *parameters){
String systemUpTime;
while(1){
uptime::calculateUptime();
if (xSemaphoreTake(xMutex, 300)) {
systemUpTime = String("Uptime: ") + String(uptime::getDays())+"d "+String(uptime::getHours())+":"+String(uptime::getMinutes())+":"+String(uptime::getSeconds());
lv_label_set_text(ui_lblUptime, systemUpTime.c_str());
lv_timer_handler();
//vTaskDelay(10 / portTICK_PERIOD_MS);
xSemaphoreGive(xMutex);
}
}
}
void v_DisplayHora(void *parameters){
char timeReceived_char[64];
while(1){
if (xQueueReceive(datahora_formatadaX_Queue, &timeReceived_char, portMAX_DELAY)){
if (xSemaphoreTake(xMutex, 100)) {
lv_label_set_text(ui_lblDataHora, timeReceived_char);
lv_timer_handler();
//vTaskDelay(10 / portTICK_PERIOD_MS);
xSemaphoreGive(xMutex);
}
}
}
}
//-----------------------------------------------------------------------------------------------------------------------------------------------
//Executa somente se ....Estiver habilitado, não é o caso no momento.
void StatusMQTT(bool status){
if (xSemaphoreTake(xMutex, 200)) {
if (status){
lv_obj_clear_flag(ui_imgMQTT, LV_OBJ_FLAG_HIDDEN);
}else{
lv_obj_add_flag(ui_imgMQTT, LV_OBJ_FLAG_HIDDEN);
}
lv_timer_handler();
xSemaphoreGive(xMutex);
}
}
void StatusSDCard(bool status){
if (status){
lv_obj_clear_flag(ui_imgSDCard, LV_OBJ_FLAG_HIDDEN);
}else{
lv_obj_add_flag(ui_imgSDCard, LV_OBJ_FLAG_HIDDEN);
}
lv_timer_handler();
}
//-----------------------------------------------------------------------------------------------------------------------------------------------
//Tarefas que mantem as imagens do LVGL atualizadas
void StatusWifi_img(bool status){
if (xSemaphoreTake(xMutex, portMAX_DELAY)) {
if (status){
lv_obj_clear_flag(ui_imgWifiOn, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(ui_imgWifiOff, LV_OBJ_FLAG_HIDDEN);
}else{
lv_obj_clear_flag(ui_imgWifiOff, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(ui_imgWifiOn, LV_OBJ_FLAG_HIDDEN);
}
lv_timer_handler();
xSemaphoreGive(xMutex);
}
}
void vStatusError(void *pvParameters){
bool status = (bool)pvParameters;
TickType_t now;
now = xTaskGetTickCount();
if (status){
xSemaphoreTake(xMutex, portMAX_DELAY);
lv_obj_clear_flag(ui_imgError, LV_OBJ_FLAG_HIDDEN);
lv_label_set_text(ui_lblMSGEmail, "Erro!");
lv_timer_handler();
xSemaphoreGive(xMutex);
vTaskDelayUntil(&now,pdMS_TO_TICKS(25000));
xSemaphoreTake(xMutex, portMAX_DELAY);
lv_obj_add_flag(ui_imgError, LV_OBJ_FLAG_HIDDEN);
lv_label_set_text(ui_lblMSGEmail, "Mapa Enviado por e-mail:");
lv_timer_handler();
xSemaphoreGive(xMutex);
}else{
xSemaphoreTake(xMutex, portMAX_DELAY);
lv_obj_add_flag(ui_imgError, LV_OBJ_FLAG_HIDDEN);
lv_label_set_text(ui_lblMSGEmail, "Mapa Enviado por e-mail:");
lv_timer_handler();
xSemaphoreGive(xMutex);
}
vTaskDelete(NULL);
}
void vStatusEmail(void *pvParameters){
bool status = (bool)pvParameters;
TickType_t now;
now = xTaskGetTickCount();
if (status){
xSemaphoreTake(xMutex, portMAX_DELAY);
lv_obj_add_flag(ui_imgEmailFalha, LV_OBJ_FLAG_HIDDEN);
lv_obj_clear_flag(ui_imgEmailEnviado, LV_OBJ_FLAG_HIDDEN);
lv_timer_handler();
xSemaphoreGive(xMutex);
vTaskDelayUntil(&now,pdMS_TO_TICKS(25000));
xSemaphoreTake(xMutex, portMAX_DELAY);
lv_obj_add_flag(ui_imgEmailEnviado, LV_OBJ_FLAG_HIDDEN);
lv_timer_handler();
xSemaphoreGive(xMutex);
}else{
xSemaphoreTake(xMutex, portMAX_DELAY);
lv_obj_add_flag(ui_imgEmailEnviado, LV_OBJ_FLAG_HIDDEN);
lv_obj_clear_flag(ui_imgEmailFalha, LV_OBJ_FLAG_HIDDEN);
lv_timer_handler();
xSemaphoreGive(xMutex);
vTaskDelayUntil(&now,pdMS_TO_TICKS(25000));
xSemaphoreTake(xMutex, portMAX_DELAY);
lv_obj_add_flag(ui_imgEmailFalha, LV_OBJ_FLAG_HIDDEN);
lv_timer_handler();
xSemaphoreGive(xMutex);
}
vTaskDelete(NULL);
}
void v_DisplaySinalWifi(void *parameters) {
int SinalWifi;
while(1) {
if (wifiConectado) {
if (xQueueReceive(SinalWifi_Queue, &SinalWifi, portMAX_DELAY)){
if (xSemaphoreTake(xMutex, portMAX_DELAY)) {
String sinalWifiStr = String(SinalWifi);
lv_label_set_text(ui_lblSinal, sinalWifiStr.c_str());
lv_timer_handler();
xSemaphoreGive(xMutex);
}
}
} else {
if (xSemaphoreTake(xMutex, portMAX_DELAY)) {
lv_label_set_text(ui_lblSinal, "DESCONECTADO");
lv_timer_handler();
xSemaphoreGive(xMutex);
}
}
vTaskDelay(1500 / portTICK_PERIOD_MS);
}
}
//-----------------------------------------------------------------------------------------------------------------------------------------------
// Essa tarefa abaixo é executada somente se o sensor desconectar.
void v_DisplayTempDesc(void *parameters){
if (xSemaphoreTake(xMutex, portMAX_DELAY)) {
lv_meter_set_indicator_value(MyGauge[0], indic1[0], -100);
lv_label_set_text(ui_lblTemp, "DESCON");
lv_timer_handler();
xSemaphoreGive(xMutex);
}
vTaskDelete(NULL);
}
Any suggestion ?