Now screen freezed on "Start Screen" until connecting to wifi is done.

This commit is contained in:
2025-11-05 19:16:26 +00:00
parent 17fd36ab4c
commit 2a18995886
30 changed files with 50365 additions and 49554 deletions

View File

@@ -1,10 +1,10 @@
#include <Arduino.h>
#include "main.h"
#define START_SCREEN_WAIT 4 * 1000 / 5 // First number is the time in seconds
#define START_SCREEN_WAIT 4 * 200 // First number is the time in seconds
const char *ssid = "CIA";
const char *password = "passespacarola";
const char *ssid = "";
const char *password = "";
// Touchscreen pinout
#define XPT2046_IRQ 36
@@ -17,7 +17,7 @@ int x, y, z;
#define SCREEN_HEIGHT 320
// MQTT settings
const char* mqtt_server_ip = "10.164.67.154";
const char* mqtt_server_ip = "10.164.67.98";
const int mqtt_port = 1883;
//const char* mqtt_user = "badge_device";
//const char* mqtt_password = "letmein";
@@ -32,7 +32,10 @@ unsigned long last_publish_time = 0;
bool ibvs_mode_done = false;
bool screenNeedsUpdate = true;
bool configDone = false;
bool get_imageDone = true;
bool winnerDone = false;
bool vote_end = false;
bool wifi_connected = false;
char current_screen = 'A';
char winner = '0';
char Badge_ID[9];
@@ -47,6 +50,8 @@ ShortText item3 = {"Maybe", 4, 140};
ShortText item4 = {"Abstain", 4, 170};
ShortText item5 = {"Null", 4, 200};
//lv_image_dsc_t dynamic_image;
#define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT / 10 * (LV_COLOR_DEPTH / 8))
uint32_t draw_buf[DRAW_BUF_SIZE / 4];
@@ -54,33 +59,15 @@ void setup() {
Serial.begin(115200);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
// Initialize SPIFFS
if (!SPIFFS.begin(true)) {
Serial.println("SPIFFS Mount Failed");
return;
}
Serial.println("");
Serial.println("WiFi connected");
// Print the IP address
Serial.print("My ip: ");
Serial.println(WiFi.localIP());
set_var_ui_ip(WiFi.localIP().toString().c_str());
// Create Badge ID from MAC address
String mac = WiFi.macAddress();
mac.replace(":", ""); // Remove all colons
strcpy(Badge_ID, mac.substring(0, 8).c_str()); // Get first 8 characters, 4 bytes
Serial.println("SPIFFS Mounted Successfully");
String LVGL_Arduino = String("LVGL Library Version: ") + lv_version_major() + "." + lv_version_minor() + "." + lv_version_patch();
Serial.println(LVGL_Arduino);
// Start LVGL
lv_init();
// Register print function for debugging
@@ -107,22 +94,31 @@ void setup() {
// Function to draw the GUI (text, buttons and sliders)
ui_init();
xTaskCreate(screen_manager, "Manage_screen", 8192, NULL, 1, NULL);
xTaskCreate(mqtt_manager, "Manage_mqtt", 8192, NULL, 1, NULL);
wifi_connected = connect_wifi();
}
void loop() {
if (ibvs_mode == IBVS_BADGE) {
if (!configDone) {
Serial.println("Entering badge mode...");
serial_config();
configDone = true;
Serial.println("Entering badge mode...");
serial_config();
configDone = true;
}
if (!get_imageDone) {
get_image();
get_imageDone = true;
}
get_image();
if (winner != '0' && !winnerDone) {
Winner_Screen();
winnerDone = true;
@@ -132,18 +128,23 @@ void loop() {
if (ibvs_mode == IBVS_PROCESSOR) {
if (!configDone) {
Serial.println("Entering processor mode...");
badge_registration();
configDone = true;
}
if (millis() - last_publish_time >= 60000) {
/* if (millis() - last_publish_time >= 60000) {
message = "MQTT server ping #" + String(ping_number++);
Serial.printf("Publishing message in topic '%s': %s\r\n", topic_test.c_str(), message.c_str());
mqtt_server.publish(topic_test, message);
last_publish_time = millis();
} */
if (vote_end) {
}
}
vTaskDelay(10 / portTICK_PERIOD_MS);
vTaskDelay(8000 / portTICK_PERIOD_MS);
}
void serial_config() {
@@ -301,7 +302,6 @@ void mqtt_subs() {
}
if (ibvs_mode == IBVS_PROCESSOR) {
mqtt_server.subscribe("#", [](const char * topic, const char * payload) {
// payload might be binary, but PicoMQTT guarantees that it's zero-terminated
Serial.printf("Received message in topic '%s': %s\r\n", topic, payload);
});
}
@@ -314,7 +314,6 @@ void mqtt_send_result(char result) {
mqtt_client.publish(topic, String(result).c_str());
Serial.printf("Published \'%c\' to topic : ", result);
Serial.println(topic);
// vote_end = true;
current_screen = 'E';
screenNeedsUpdate = true;
}
@@ -377,11 +376,14 @@ void touchscreen_read(lv_indev_t * indev, lv_indev_data_t * data) {
void screen_manager(void *pvParameters) {
while (1) {
if (start_screen < START_SCREEN_WAIT) {
start_screen++;
if (start_screen < START_SCREEN_WAIT) { // If not the wanted seconds keep counting
start_screen++;
// Serial.printf("Counting: %d\r\n", start_screen); // Debug
}
else if (start_screen == START_SCREEN_WAIT) {
else if (start_screen == START_SCREEN_WAIT && !wifi_connected) {
start_screen = START_SCREEN_WAIT - 600; // If wanted seconds but no wifi wait 1 second
}
else if (start_screen == START_SCREEN_WAIT && wifi_connected) {
Serial.println("Switching to screen G"); // Debug
current_screen = 'G';
screenNeedsUpdate = true;
@@ -438,9 +440,7 @@ void screen_manager(void *pvParameters) {
void mqtt_manager(void *pvParameters) {
while (1) {
if (ibvs_mode == IBVS_BADGE) {
if (!ibvs_mode_done){
mqtt_client.begin();
mqtt_subs();
@@ -463,6 +463,141 @@ void mqtt_manager(void *pvParameters) {
else {
vTaskDelay(5 / portTICK_PERIOD_MS);
}
}
}
void badge_registration() {
}
void get_image() {
Serial.println("DEBUG: get_image() called");
if (Serial.available() > 0) {
Serial.println("DEBUG: Data available!");
String command = Serial.readStringUntil('\n');
command.trim();
Serial.print("DEBUG: Command = '");
Serial.print(command);
Serial.println("'");
if (command.startsWith("UPLOAD_IMAGE:")) {
// Extract filename and size
int colonPos = command.indexOf(':', 13);
String filename = command.substring(13, colonPos);
int fileSize = command.substring(colonPos + 1).toInt();
Serial.println("Ready to receive image");
Serial.print("Filename: ");
Serial.println(filename);
Serial.print("Size: ");
Serial.println(fileSize);
// Read data as it arrives
Serial.println("DEBUG: Starting to receive data...");
fs::File file = SPIFFS.open("/" + filename, FILE_WRITE);
if (file) {
Serial.println("DEBUG: File opened");
byte buffer[128]; // Smaller buffer
int remaining = fileSize;
int bytesReceived = 0;
unsigned long startTime = millis();
while (remaining > 0 && (millis() - startTime < 30000)) {
int available = Serial.available();
if (available > 0) {
int toRead = min(remaining, min(128, available));
int actualRead = Serial.readBytes(buffer, toRead);
file.write(buffer, actualRead);
remaining -= actualRead;
bytesReceived += actualRead;
// Print progress every 10KB
if (bytesReceived % 10000 < 128) {
Serial.print("Received: ");
Serial.print(bytesReceived);
Serial.print(" / ");
Serial.println(fileSize);
}
}
yield(); // Feed watchdog
}
file.close();
Serial.print("DEBUG: File closed. Bytes received: ");
Serial.println(bytesReceived);
if (remaining == 0) {
Serial.println("Image uploaded successfully!");
String fullPath = "/" + filename;
Serial.println("DEBUG: Calling load_image...");
load_image(fullPath.c_str());
} else {
Serial.print("ERROR: Incomplete upload. Remaining: ");
Serial.println(remaining);
}
} else {
Serial.println("Failed to open file for writing");
}
}
}
}
bool load_image(const char* filename) {
fs::File file = SPIFFS.open(filename, FILE_READ);
if (!file) {
Serial.println("Failed to open image file");
return false;
}
size_t fileSize = file.size();
Serial.print("Image file size: ");
Serial.println(fileSize);
// Allocate memory for the image data
uint8_t* imageData = (uint8_t*)malloc(fileSize);
if (!imageData) {
Serial.println("Failed to allocate memory for image");
file.close();
return false;
}
// Read the entire file into memory
file.read(imageData, fileSize);
file.close();
// Set the image source to the loaded data
// LVGL will decode the BMP automatically
lv_image_set_src(objects.picture, imageData);
Serial.println("Image loaded successfully!");
return true;
}
bool connect_wifi() {
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Print the IP address
Serial.print("My ip: ");
Serial.println(WiFi.localIP());
set_var_ui_ip(WiFi.localIP().toString().c_str());
// Create Badge ID from MAC address
String mac = WiFi.macAddress();
mac.replace(":", ""); // Remove all colons
strcpy(Badge_ID, mac.substring(0, 8).c_str()); // Get first 8 characters, 4 bytes
return true;
}