Hi, Consciously, I don’t use lv timer_create() in my program and have tried to pass more variables of pointers and before lv_timer_handler(); its exist and after it, its are corrupted.
I do not control what lv_timer_handler() is for, I use it because it is indicated that it has to be used.
I don’t know if apart from it I need other instructions so that Lvgl works correctly and doesn’t corrupt my global variables. You can help?
I attach the setup and loop of my program.
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.hpp>
#include <ArduinoJson.h>
#include <esp_task_wdt.h>
#include "time.h"
#include <Preferences.h>
#include <lvgl.h>
#include <TFT_eSPI.h>
#include "ui.h"
#include "mi_EEPROM.h"
#include "config.h"
volatile unsigned long timing, dormir = 0;
#define WDT_TIMEOUT 10
// Pins para los led de estado
#define rojo 16
#define amar 17
#define verd 22
#define mosfet 14
static const uint16_t screenWidth = 240;
static const uint16_t screenHeight = 320;
static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[screenWidth * screenHeight / 10];
TFT_eSPI tft = TFT_eSPI(screenWidth, screenHeight); /* Instancio TFT */
#if LV_USE_LOG != 0
void my_print(const char *buf) /* Serial debugging */
{
Serial.printf(buf);
Serial.flush();
}
#endif
const int filas = 24;
const int colum = 4;
String tabla[filas][colum];
int estados[filas];
int estadop[filas];
String proxi[filas][colum];
int carga, cargap, dia_encurso = 0;
DynamicJsonDocument doc(2048);
int dia_guardado = 0;
String w_scro, x_scro;
double w_prec = 1.0000;
int w_hora, w_esta, f, c = 0;
int hora_encurso;
int buttonState = 1;
int TFT_On = 1;
int Wifi_On = 0;
const char *textos[19] = { "Ahora", "Mejor hoy", "Datos proporcionados por el\noperador del mercado disponibles\na partir de las 20:30h.\n El operador del mercado puede\nretrasarse en su publicacion.\n", "Ha conectado a", "Sin conesion a", "SSID Wifi:", "Contraseña", "Nombre red wifi", "Contraseña red wifi", "Pais:", "Idioma", "Guardar", "AVISO", "ERROR", "Registro Wifi OK.", "Registro rechazado.", "Ambos campos obligatorios.", "England\nSpain", "English\nSpanish" };
const char *L_textos[19] = { "Ahora", "Mejor hoy", "Datos proporcionados por el\noperador del mercado disponibles\na partir de las 20:30h.\n El operador del mercado puede\nretrasarse en su publicacion.\n", "Ha conectado a", "Sin conesion a", "SSID Wifi:", "Contraseña", "Nombre red wifi", "Contraseña red wifi", "Pais:", "Idioma", "Guardar", "AVISO", "ERROR", "Registro Wifi OK.", "Registro rechazado.", "Ambos campos obligatorios.", "England\nSpain", "English\nSpanish" };
/* Display flushing */
void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p) {
uint32_t w = (area->x2 - area->x1 + 1);
uint32_t h = (area->y2 - area->y1 + 1);
tft.startWrite();
tft.setAddrWindow(area->x1, area->y1, w, h);
tft.pushColors((uint16_t *)&color_p->full, w * h, true);
tft.endWrite();
lv_disp_flush_ready(disp);
}
/*Leo el touchpad*/
void my_touchpad_read(lv_indev_drv_t *indev_driver, lv_indev_data_t *data) {
uint16_t x = 0, y = 0;
bool pressed = tft.getTouch(&x, &y);
if (pressed) {
data->point.x = x;
data->point.y = y;
data->state = LV_INDEV_STATE_PR;
digitalWrite(mosfet, LOW);
dormir = millis();
} else {
data->state = LV_INDEV_STATE_REL;
}
}
void setup() {
Serial.begin(115200);
delay(1000);
lv_init();
#if LV_USE_LOG != 0
lv_log_register_print_cb(my_print);
#endif
// inicializamos los pin de estado.
pinMode(rojo, OUTPUT);
pinMode(verd, OUTPUT);
pinMode(amar, OUTPUT);
digitalWrite(rojo, LOW);
digitalWrite(verd, LOW);
digitalWrite(amar, LOW);
// Realizamos el control del TFT.
pinMode(mosfet, OUTPUT);
digitalWrite(mosfet, LOW);
// Estado del boton sleep
buttonState = 1;
// Leemos la EEPROM para saber el SSID y el password de la Wifi
pais = "0";
idioma = "0";
String qq_ssid, qq_pass, qq_pais, qq_idio;
Preferences preferences;
preferences.begin("mylogin", false);
if ( preferences.isKey("name_SSID") ) qq_ssid = preferences.getString("name_SSID");
if ( preferences.isKey("E_password") ) qq_pass = preferences.getString("E_password");
if ( preferences.isKey("E_pais") ) qq_pais = preferences.getString("E_pais");
if ( preferences.isKey("E_idioma") ) qq_idio = preferences.getString("E_idioma");
preferences.end();
ssid = qq_ssid.c_str();
password = qq_pass.c_str();
i_pais = qq_pais.toInt();
i_idio = qq_idio.toInt();
pais = qq_pais.c_str();
idioma = qq_idio.c_str();
if (ssid != "" && password != "") {
Wifi_On = 0;
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("Conectando a: " + String(ssid));
int ws = 0;
while (WiFi.status() != WL_CONNECTED && ws<50) {
ws +=1;
delay(500);
}
}
if ( WiFi.status() == WL_CONNECTED ) {
Wifi_On = 1;
StaticJsonDocument<1536> tex;
String Link = "https://alquimistec.com/econtrol/idioma/" + String(idioma) + "/textos.json";
String payload = datos_servidor(Link);
if ( !payload.isEmpty() ) {
DeserializationError error = deserializeJson(tex, payload);
if (error) Serial.println("ERROR al deserializar");
else {
int i = 0;
for (JsonObject item : tex.as<JsonArray>()) {
textos[i] = item["lit"];
i += 1;
if (i == 13) lit14 = item["lit"];
}
}
}
}
pinMode(GPIO_NUM_35, INPUT); // activo el GPIO35 para despertar externo del botón
esp_sleep_enable_ext0_wakeup(GPIO_NUM_35, 1);
eControl();
tft.begin();
tft.setRotation(1);
uint16_t calData[5] = { 185, 3534, 246, 3604, 1 };
tft.setTouch(calData);
lv_disp_draw_buf_init(&draw_buf, buf, NULL, screenWidth * screenHeight / 10);
static lv_disp_drv_t disp_drv; /*Inicializo el TFT*/
lv_disp_drv_init(&disp_drv);
disp_drv.hor_res = 320; /*Indico la resolucion de la pantalla*/
disp_drv.ver_res = 240;
disp_drv.flush_cb = my_disp_flush;
disp_drv.draw_buf = &draw_buf;
lv_disp_drv_register(&disp_drv);
static lv_indev_drv_t indev_drv; /*Inicializo el controlador del dispositivo de entrada (ficticio)*/
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read_cb = my_touchpad_read;
lv_indev_drv_register(&indev_drv);
ui_init();
esp_task_wdt_init(WDT_TIMEOUT, true); //Activamos perro para reiniciar ESP32
esp_task_wdt_add(NULL); //añadir subproceso actual al reloj WDT
}
void loop() {
lv_timer_handler(); /* El jefe */
delay(5);
if ((millis() - timing) > 300000) { // Ejecuto cada 5 mtos. eControl. 300.000 ms.
timing = millis();
eControl();
lv_event_send(ui_Inicio, LV_EVENT_REFRESH, NULL);
}
if ( (millis() - dormir) > 30000 && !TFT_On == 0) { // Apago el TFT si nadie hace nada en 30 seg. 30000
digitalWrite(mosfet, HIGH);
TFT_On = 0;
}
esp_task_wdt_reset(); //Tranquilizo al perro
buttonState = digitalRead(GPIO_NUM_35);
if( buttonState == LOW ) esp_deep_sleep_start();
}