Files
JC3636W518EN/10-OBD-Display-2024/display_gif_embedded/src/main.cpp
T

216 lines
5.9 KiB
C++
Raw Normal View History

2026-10-23 00:20:33 +02:00
#include <Arduino.h>
#include "scr_st77916.h"
#include "SD_MMC.h"
// https://randomnerdtutorials.com/esp32-client-server-wi-fi/
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "ESP32-Access-Point";
const char* password = "123456789";
String httpGETRequest(const char* serverName);
2026-10-23 00:51:23 +02:00
// FPS measurement: LVGL calls this after every screen refresh (= one GIF frame here)
static volatile uint32_t refr_count = 0;
static volatile uint32_t refr_time_ms = 0;
static uint32_t busy_us = 0; // time spent inside lv_timer_handler()
static void refr_monitor_cb(lv_disp_drv_t* drv, uint32_t time, uint32_t px) {
refr_count++;
refr_time_ms += time;
}
void print_fps() {
static uint32_t last_ms = 0;
uint32_t now = millis();
if (now - last_ms < 1000) return;
// decode = busy time not spent refreshing the screen (mostly GIF decoding)
Serial.printf("FPS: %.1f avg refresh: %lu ms avg decode: %lu ms\n", refr_count * 1000.0f / (now - last_ms),
refr_count ? refr_time_ms / refr_count : 0,
refr_count ? (busy_us / 1000 - refr_time_ms) / refr_count : 0);
refr_count = 0;
refr_time_ms = 0;
busy_us = 0;
last_ms = now;
}
2026-10-23 00:20:33 +02:00
void my_print(const char* buf) {
Serial.println(buf); // Print logs to the serial monitor
}
void display_animation() {
lv_obj_t* img;
LV_IMG_DECLARE(loligod);
img = lv_gif_create(lv_scr_act());
Serial.print("Available Heap before: ");
Serial.println(esp_get_free_heap_size());
lv_mem_monitor_t mem_mon;
lv_mem_monitor(&mem_mon);
printf("Free memory: %d bytes\n", mem_mon.free_size);
// lv_fs_file_t file;
// if (lv_fs_open(&file, "A:wallpaper.gif", LV_FS_MODE_RD) == LV_FS_RES_OK) {
// lv_fs_close(&file); // File exists, proceed with loading
// lv_gif_set_src(img, "A:wallpaper.gif");
// Serial.println("GIF Loaded Successfully");
// } else {
// Serial.println("Failed to Load GIF: File not found or inaccessible");
// }
lv_gif_set_src(img, &loligod);
Serial.print("Available Heap after: ");
Serial.println(esp_get_free_heap_size());
lv_mem_monitor(&mem_mon);
printf("Free memory: %d bytes\n", mem_mon.free_size);
lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);
}
lv_obj_t* label1;
void display_text() {
// Create a style for the text
static lv_style_t style_text;
lv_style_init(&style_text);
// Set the text color to white
lv_style_set_text_color(&style_text, lv_color_white());
// // Set the shadow color to black (this will simulate the outline)
// lv_style_set_shadow_color(&style_text, lv_color_black());
// // Set the shadow width to create the outline effect
// lv_style_set_shadow_width(&style_text, 2); // Adjust for desired outline thickness
// // Set the shadow offset to 0 so it surrounds the text evenly
// lv_style_set_shadow_ofs_x(&style_text, 0);
// lv_style_set_shadow_ofs_y(&style_text, 0);
label1 = lv_label_create(lv_scr_act());
// Apply the style to the label
lv_obj_add_style(label1, &style_text, LV_PART_MAIN);
// lv_style_set_text_font(&label1, &Montserrat); /*Set a larger font*/
lv_label_set_long_mode(label1, LV_LABEL_LONG_WRAP); /*Break the long lines*/
lv_label_set_text(label1, "0");
lv_obj_set_width(label1, 150); /*Set smaller width to make the lines wrap*/
lv_obj_set_style_text_align(label1, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_align(label1, LV_ALIGN_CENTER, 0, 0);
// lv_obj_set_style_transform_zoom(label1, 256 * 2, 0); // Zoom: 256 = 1x scale (default)
}
void connectToWifi() {
WiFi.begin(ssid, password);
Serial.println("Connecting");
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Wifi not connected");
return;
}
int timeout = 0;
while (WiFi.status() != WL_CONNECTED && timeout <= 20) {
timeout++;
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
}
void setup() {
delay(200);
Serial.begin(115200);
scr_lvgl_init();
2026-10-23 00:51:23 +02:00
lv_disp_get_default()->driver->monitor_cb = refr_monitor_cb;
2026-10-23 00:20:33 +02:00
lv_init();
//lv_log_register_print_cb(my_print); // Register print function for LVGL logs
// Get the active screen (root object for the display)
lv_obj_t* screen = lv_scr_act();
// Set the background color of the entire screen to black
lv_obj_set_style_bg_color(screen, lv_color_black(), 0); // 0 is the part ID for the main part
lv_obj_set_style_bg_opa(screen, LV_OPA_COVER, 0); // Set opacity to fully opaque
// GIF is compiled into the firmware, so start it before (and independent of) the SD card
display_animation();
2026-10-23 00:20:33 +02:00
// Configure the SDMMC host
SD_MMC.setPins(SD_MMC_CLK_PIN, SD_MMC_CMD_PIN, SD_MMC_D0_PIN, SD_MMC_D1_PIN, SD_MMC_D2_PIN, SD_MMC_D3_PIN);
if (!SD_MMC.begin()) {
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD_MMC.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD card attached");
return;
}
Serial.println("SD Card initialized successfully.");
//connectToWifi();
//display_text();
}
void loop() {
2026-10-23 00:51:23 +02:00
uint32_t t0 = micros();
2026-10-23 00:20:33 +02:00
lv_timer_handler();
2026-10-23 00:51:23 +02:00
busy_us += micros() - t0;
vTaskDelay(1);
print_fps();
2026-10-23 00:20:33 +02:00
// if (WiFi.status() == WL_CONNECTED) {
// String speed = httpGETRequest("http://192.168.4.1/speed");
// Serial.print("Speed: ");
// Serial.println(speed);
// lv_label_set_text(label1, speed.c_str());
// } else {
// Serial.println("WiFi Disconnected");
// connectToWifi();
// }
}
String httpGETRequest(const char* serverName) {
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Send HTTP POST request
int httpResponseCode = http.GET();
String payload = "--";
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}