System usage

This commit is contained in:
Rasmus Rasmussen 2026-01-06 19:50:28 +01:00
parent 36dd179def
commit 4b283e0139
5 changed files with 90 additions and 9 deletions

View File

@ -1,3 +1,4 @@
#include "esp32-hal.h"
#pragma once
#include "program.h"
#include "shell.h"
@ -17,6 +18,7 @@ public:
text.x = _window->x + 10;
text.y = _window->y + 50;
text.size = 2;
_window->window_content_text.push_back(text);
@ -90,9 +92,87 @@ public:
void run() override {
Serial.println("Settings started!");
WindowContentText ram_usage;
WindowContentText ram_usage_label;
WindowContentText cpu_usage;
WindowContentText cpu_usage_label;
ram_usage_label.size = 2;
ram_usage_label.text = "RAM usage";
ram_usage_label.x = _window->x + 10;
ram_usage_label.y = _window->y + 50;
ram_usage.size = 2;
ram_usage.x = _window->x + 10;
ram_usage.y = _window->y + 70;
cpu_usage_label.size = 2;
cpu_usage_label.text = "CPU usage:";
cpu_usage_label.x = _window->x + 10;
cpu_usage_label.y = _window->y + 90;
cpu_usage.size = 2;
cpu_usage.x = _window->x + 10;
cpu_usage.y = _window->y + 110;
_window->window_content_text.push_back(ram_usage_label);
_window->window_content_text.push_back(ram_usage);
_window->window_content_text.push_back(cpu_usage_label);
_window->window_content_text.push_back(cpu_usage);
// Variables for metrics
size_t total_ram;
size_t free_ram;
size_t used_ram;
float ram_percent;
float cpu_percent;
// For CPU calculation
uint32_t idle_count_last = 0;
uint32_t idle_count_current = 0;
uint counter = 0;
while (_running) {
// Settings UI logic
vTaskDelay(pdMS_TO_TICKS(100));
counter++;
// RAM Calculation (Internal + PSRAM)
size_t internal_total = heap_caps_get_total_size(MALLOC_CAP_INTERNAL);
size_t internal_free = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
size_t psram_total = heap_caps_get_total_size(MALLOC_CAP_SPIRAM);
size_t psram_free = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
total_ram = internal_total + psram_total;
free_ram = internal_free + psram_free;
used_ram = total_ram - free_ram;
ram_percent = (float)used_ram / (float)total_ram * 100.0f;
// CPU Usage - measure idle task iterations
idle_count_current = xTaskGetIdleRunTimeCounter();
if (idle_count_last > 0) {
uint32_t idle_delta = idle_count_current - idle_count_last;
// The more idle iterations, the less CPU is used
// Scale based on your measurement interval (100ms)
// Tune the divisor based on your specific system
cpu_percent = 100.0f - (idle_delta / 10000.0f);
if (cpu_percent < 0) cpu_percent = 0;
if (cpu_percent > 100) cpu_percent = 100;
} else {
cpu_percent = 0.0f;
}
idle_count_last = idle_count_current;
_window->window_content_text[1].text = std::to_string(used_ram);
_window->window_content_text[3].text = std::to_string(cpu_percent);
vTaskDelay(pdMS_TO_TICKS(5));
if (counter == 1000) {
_display_state->update_display.store(true);
counter = 0;
}
}
}
};

View File

@ -49,6 +49,7 @@ struct WindowDecoration {
struct WindowContentText {
int x;
int y;
int size;
std::string text;
};

View File

@ -220,13 +220,13 @@ void Shell::draw_window(Window window) {
}
// Window title
tft->draw_text(window.x + 6 + 65, window.y + 10, window.title, COL_WHITE);
tft->draw_text(window.x + 6 + 65, window.y + 10, window.title, COL_WHITE, 2);
// Window content
tft->draw_box(window.x + 3, window.y + 35, window.width - 6, window.height - 38, COL_WHITE);
for (WindowContentText text : window.window_content_text) {
tft->draw_text(text.x, text.y, text.text, COL_BLACK);
tft->draw_text(text.x, text.y, text.text, COL_BLACK, text.size);
}
}
@ -265,14 +265,14 @@ void Shell::draw_task_bar(int color) {
tft->draw_box(item.place_x, item.place_y, item.width + 2, item.height + 2, COL_WHITE);
tft->draw_box(item.place_x, item.place_y, item.width, item.height, COL_GREY);
tft->draw_text(item.place_x + 2, item.place_y + 2, item.name, COL_BLACK);
tft->draw_text(item.place_x + 2, item.place_y + 2, item.name, COL_BLACK, 1);
} else {
tft->draw_box(item.place_x - 1, item.place_y - 1, item.width + 1, item.height + 1, COL_WHITE);
tft->draw_box(item.place_x, item.place_y, item.width + 2, item.height + 2, COL_BLACK);
tft->draw_rect(item.place_x, item.place_y, item.width + 1, item.height + 1, 1, COL_DARK_GREY);
tft->draw_box(item.place_x, item.place_y, item.width, item.height, COL_GREY);
tft->draw_text(item.place_x + 2, item.place_y + 2, item.name, COL_BLACK);
tft->draw_text(item.place_x + 2, item.place_y + 2, item.name, COL_BLACK, 1);
}
}
}

View File

@ -31,9 +31,9 @@ void TFT_Handler::draw_line(int x1, int y1, int x2, int y2, int color) {
tft.drawLine(x1, y1, x2, y2, color);
}
void TFT_Handler::draw_text(int x, int y, std::string str, int color) {
void TFT_Handler::draw_text(int x, int y, std::string str, int color, int size) {
tft.setTextColor(color);
tft.setTextSize(1);
tft.setTextSize(size);
tft.drawString(str.c_str(), x, y);
}

View File

@ -62,7 +62,7 @@ public:
void draw_box(int x, int y, int size_x, int size_y, int color);
void draw_rect(int x, int y, int size_x, int size_y, int thickness, int color);
void draw_line(int x1, int y1, int x2, int y2, int color);
void draw_text(int x, int y, std::string str, int color);
void draw_text(int x, int y, std::string str, int color, int size);
void fill_screen(int color);
void draw_box_sprite(int x, int y, int size_x, int size_y, int color);