PocketPutr/Desktop_Test/system_manager.cpp

61 lines
1.9 KiB
C++

#include "system_manager.h"
#include "shell.h"
#include "tft_handler.h"
#include "example_programs.h"
#include <Arduino.h>
void SystemManager::init(DISPLAY_STATE* display_state, Shell* shell, TFT_Handler* tft, CALIBRATION_IDLE calibration_idle) {
_display_state = display_state;
_shell = shell;
_tft = tft;
_calibration_idle = calibration_idle;
// Register all available programs
register_program<CounterProgram>("CounterProgram");
register_program<TextEditorProgram>("TextEditorProgram");
register_program<InternetTester>("InternetTester");
register_program<CalculatorProgram>("CalculatorProgram");
register_program<Chatty>("Chatty");
register_program<SettingsProgram>("SettingsProgram");
}
bool SystemManager::spawn_program(Window* window, const std::string& program_class) {
// Check if program with same name is already running
for (const auto& prog : _programs) {
if (prog->get_name() == window->title) {
return false;
}
}
// Check if the program class exists
auto it = _program_factories.find(program_class);
if (it == _program_factories.end()) {
Serial.printf("Program class '%s' not found!\n", program_class.c_str());
return false;
}
// Create the program instance using the factory
Program* program = it->second();
// Assign ID and initialize
window->id = _next_program_id;
program->init(_next_program_id, window->title, window, _shell, _tft, _display_state, _calibration_idle);
_next_program_id++;
_programs.push_back(program);
Serial.printf("Spawned program: %s (ID: %d)\n", window->title.c_str(), window->id);
return true;
}
void SystemManager::close_program(short int id) {
for (size_t i = 0; i < _programs.size(); ++i) {
if (_programs[i]->get_id() == id) {
Serial.printf("Closing program ID: %d\n", id);
delete _programs[i];
_programs.erase(_programs.begin() + i);
break;
}
}
}