110 lines
3.1 KiB
C++
110 lines
3.1 KiB
C++
/**
|
|
* @file main.cpp
|
|
* @brief Main application entry point for Locomotive Test Bench
|
|
*
|
|
* Orchestrates all system components:
|
|
* - Configuration management
|
|
* - Touchscreen UI
|
|
* - Motor control (DC analog)
|
|
* - DCC signal generation
|
|
* - Relay control for 2-rail/3-rail switching
|
|
*
|
|
* @author Locomotive Test Bench Project
|
|
* @date 2025
|
|
* @version 2.0
|
|
*/
|
|
|
|
#include <Arduino.h>
|
|
#include "Config.h"
|
|
#include "MotorController.h"
|
|
#include "DCCGenerator.h"
|
|
#include "RelayController.h"
|
|
#include "TouchscreenUI.h"
|
|
|
|
// Global objects
|
|
Config config;
|
|
MotorController motorController;
|
|
DCCGenerator dccGenerator;
|
|
RelayController relayController;
|
|
TouchscreenUI touchUI(&config, &motorController, &dccGenerator, &relayController);
|
|
|
|
/**
|
|
* @brief Setup function - runs once at startup
|
|
*
|
|
* Initializes all hardware and software components in correct order:
|
|
* 1. Serial communication
|
|
* 2. Configuration system
|
|
* 3. Relay controller
|
|
* 4. Motor controller
|
|
* 5. DCC generator
|
|
* 6. Touchscreen UI
|
|
*/
|
|
void setup() {
|
|
// Initialize serial communication
|
|
Serial.begin(115200);
|
|
delay(1000);
|
|
|
|
Serial.println("\n\n=================================");
|
|
Serial.println(" Locomotive Test Bench v2.0");
|
|
Serial.println(" ESP32-2432S028R Edition");
|
|
Serial.println("=================================\n");
|
|
|
|
// Load configuration
|
|
config.begin();
|
|
Serial.println("Configuration loaded");
|
|
|
|
// Initialize relay controller
|
|
relayController.begin();
|
|
relayController.setRailMode(config.system.is3Rail);
|
|
|
|
// Initialize motor controller
|
|
motorController.begin();
|
|
|
|
// Initialize DCC generator
|
|
dccGenerator.begin();
|
|
|
|
// Initialize touchscreen UI
|
|
touchUI.begin();
|
|
|
|
// Set initial mode (but power is off by default)
|
|
if (config.system.isDCCMode && config.system.powerOn) {
|
|
dccGenerator.enable();
|
|
dccGenerator.setLocoSpeed(
|
|
config.system.dccAddress,
|
|
config.system.speed,
|
|
config.system.direction
|
|
);
|
|
} else if (!config.system.isDCCMode && config.system.powerOn) {
|
|
motorController.setSpeed(
|
|
config.system.speed,
|
|
config.system.direction
|
|
);
|
|
}
|
|
|
|
Serial.println("\n=================================");
|
|
Serial.println("Setup complete!");
|
|
Serial.println("=================================");
|
|
Serial.print("Mode: ");
|
|
Serial.println(config.system.isDCCMode ? "DCC" : "DC Analog");
|
|
Serial.print("Rail Mode: ");
|
|
Serial.println(config.system.is3Rail ? "3-Rail" : "2-Rail");
|
|
Serial.print("Power: ");
|
|
Serial.println(config.system.powerOn ? "ON" : "OFF");
|
|
Serial.println("=================================\n");
|
|
}
|
|
|
|
void loop() {
|
|
// Update touchscreen UI (handles all user interactions)
|
|
touchUI.update();
|
|
|
|
// Update DCC signal generation (if enabled)
|
|
if (config.system.isDCCMode && touchUI.isPowerOn()) {
|
|
dccGenerator.update();
|
|
} else if (!config.system.isDCCMode && touchUI.isPowerOn()) {
|
|
motorController.update();
|
|
}
|
|
|
|
// Small delay to prevent watchdog issues
|
|
delay(1);
|
|
}
|