178 lines
4.7 KiB
C++
178 lines
4.7 KiB
C++
#include "esp32-hal.h"
|
|
#pragma once
|
|
#include "program.h"
|
|
#include "shell.h"
|
|
#include "tft_handler.h"
|
|
#include <Arduino.h>
|
|
|
|
// Example Program 1: Simple counter
|
|
class CounterProgram : public Program {
|
|
private:
|
|
int counter = 0;
|
|
|
|
public:
|
|
void run() override {
|
|
Serial.println("Counter program started!");
|
|
|
|
WindowContentText text;
|
|
|
|
text.x = _window->x + 10;
|
|
text.y = _window->y + 50;
|
|
text.size = 2;
|
|
|
|
_window->window_content_text.push_back(text);
|
|
|
|
while (_running) {
|
|
counter++;
|
|
Serial.printf("Counter: %d\n", counter);
|
|
|
|
// You can modify the window title
|
|
if (_window) {
|
|
if (!_window->minimized) {
|
|
_window->title = "Counter: " + std::to_string(counter);
|
|
|
|
for (int i = 0; i < _window->window_content_text.size(); i++) {
|
|
_window->window_content_text[i].text = std::to_string(counter);
|
|
}
|
|
|
|
_display_state->update_display.store(true);
|
|
}
|
|
}
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
}
|
|
}
|
|
};
|
|
|
|
// Example Program 2: Text editor
|
|
class TextEditorProgram : public Program {
|
|
public:
|
|
void run() override {
|
|
Serial.println("Text editor started!");
|
|
|
|
// You have full access to the window and can draw to it
|
|
while (_running) {
|
|
// Handle text editing logic here
|
|
// Draw to the window content area using _tft
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(100));
|
|
}
|
|
}
|
|
};
|
|
|
|
// Example Program 3: Calculator
|
|
class CalculatorProgram : public Program {
|
|
public:
|
|
void run() override {
|
|
Serial.println("Calculator started!");
|
|
|
|
while (_running) {
|
|
// Calculator logic here
|
|
vTaskDelay(pdMS_TO_TICKS(100));
|
|
}
|
|
}
|
|
};
|
|
|
|
// Example Program 4: Game
|
|
class GameProgram : public Program {
|
|
public:
|
|
void run() override {
|
|
Serial.println("Game started!");
|
|
|
|
while (_running) {
|
|
// Game logic and rendering
|
|
vTaskDelay(pdMS_TO_TICKS(50)); // 20 FPS
|
|
}
|
|
}
|
|
};
|
|
|
|
// Example Program 5: Settings
|
|
class SettingsProgram : public Program {
|
|
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) {
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}; |