196 lines
5.8 KiB
C++
196 lines
5.8 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) {
|
|
vTaskDelay(pdMS_TO_TICKS(1));
|
|
}
|
|
}
|
|
};
|
|
|
|
// 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(1));
|
|
}
|
|
}
|
|
};
|
|
|
|
// 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(1)); // 20 FPS
|
|
}
|
|
}
|
|
};
|
|
|
|
// Example Program 5: Settings
|
|
class SettingsProgram : public Program {
|
|
private:
|
|
uint32_t max_idle_iterations;
|
|
|
|
public:
|
|
void run() override {
|
|
_window->x += 150;
|
|
_window->width -= 50;
|
|
_window->sprite.setPsram(false);
|
|
_window->sprite.createSprite(10, 10);
|
|
_window->sprite.setColorDepth(16);
|
|
_window->sprite.fillScreen(TFT_BLACK);
|
|
_window->sprite.fillCircle(50, 50, 40, TFT_RED);
|
|
_window->hasSprite = true;
|
|
//_window->sprite.pushSprite(0, 0);
|
|
|
|
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 = 0.0f;
|
|
|
|
// For CPU calculation
|
|
uint32_t idle_count_last = _calibration_idle.idle_count_last;
|
|
max_idle_iterations = _calibration_idle.max_idle_iterations;
|
|
Serial.printf("idle_count_last: %i\n", idle_count_last);
|
|
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 && max_idle_iterations > 0) {
|
|
uint32_t idle_delta = idle_count_current - idle_count_last;
|
|
|
|
// Calculate idle percentage based on baseline
|
|
// We're measuring over 1ms, so compare to max_idle_iterations
|
|
float idle_percent = ((float)idle_delta / (float)max_idle_iterations) * 100.0f;
|
|
|
|
// Cap at 100%
|
|
if (idle_percent > 100.0f) idle_percent = 100.0f;
|
|
|
|
// CPU usage is inverse of idle
|
|
cpu_percent = 100.0f - idle_percent;
|
|
if (cpu_percent < 0) cpu_percent = 0;
|
|
} else {
|
|
cpu_percent = 0.0f;
|
|
}
|
|
|
|
idle_count_last = idle_count_current;
|
|
|
|
_window->window_content_text[1].text = std::to_string(ram_percent);
|
|
_window->window_content_text[3].text = std::to_string((int)cpu_percent) + "%";
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(1));
|
|
if (counter == 1000) {
|
|
Serial.printf("internal_total %i\n", internal_total);
|
|
Serial.printf("internal_free %i\n", internal_free);
|
|
Serial.printf("psram_total %i\n", psram_total);
|
|
Serial.printf("psram_free %i\n", psram_free);
|
|
Serial.printf("total_ram %i\n", total_ram);
|
|
Serial.printf("free_ram %i\n", free_ram);
|
|
Serial.printf("used_ram %i\n", used_ram);
|
|
Serial.printf("ram_percent %f.2\n", ram_percent);
|
|
Serial.printf("cpu_percent %f.2\n", cpu_percent);
|
|
Serial.println("===============");
|
|
_display_state->update_display.store(true);
|
|
counter = 0;
|
|
}
|
|
}
|
|
}
|
|
}; |