From 863d9c1cf30215092bb4fbd081e81af3e0e06f42 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Sun, 8 Jun 2025 22:09:39 +0700 Subject: [PATCH] Refactor generator configuration to use defined constants --- src/GasolineGenerator.cpp | 65 +++++++++++++++++++++++++++++++++++++++ src/config.hpp | 9 ++++++ src/main.cpp | 12 ++++---- 3 files changed, 80 insertions(+), 6 deletions(-) diff --git a/src/GasolineGenerator.cpp b/src/GasolineGenerator.cpp index 6559b4c..ee03510 100644 --- a/src/GasolineGenerator.cpp +++ b/src/GasolineGenerator.cpp @@ -5,6 +5,15 @@ GasolineGenerator::GasolineGenerator() } +/** + * @brief Initialize the Gasoline Generator. + * + * @note This does not start the generator, it only initializes the input and output cards, + * + * @param inputCard The digital input card used for monitoring the generator state + * @param outputCard The digital output card used for controlling the generator components + * @param config The configuration settings for the generator, pins are of input and output cards specified earlier. + */ void GasolineGenerator::initialize(DigitalInputCard* inputCard, DigitalOutputCard* outputCard, const GasolineGeneratorConfig &config) { this->config = &config; @@ -29,6 +38,10 @@ void GasolineGenerator::initialize(DigitalInputCard* inputCard, DigitalOutputCar state = GeneratorState::STOPPED; } +/** + * @brief Main loop for the Gasoline Generator. + * * This function should be called periodically to handle the generator state machine. + */ void GasolineGenerator::loop() { unsigned long currentTime = millis(); @@ -65,6 +78,13 @@ void GasolineGenerator::loop() } } +/** + * @brief Start the gasoline generator. + * + * This function initiates the startup sequence for the generator. + * It will close the carburetor valve, power up the ignition system, + * and engage the starter relay to start the engine. + */ void GasolineGenerator::start() { if (state != GeneratorState::STOPPED) @@ -86,6 +106,12 @@ void GasolineGenerator::start() ignitionStartTime = millis(); } +/** + * @brief Shutdown the gasoline generator. + * + * This function stops the generator by turning off all systems, + * including the starter, ignition system, and carburetor valve. + */ void GasolineGenerator::shutdown() { // Turn off all systems @@ -97,23 +123,47 @@ void GasolineGenerator::shutdown() isEngineRunning = false; } +/** + * @brief Set the carburetor valve state. + * + * @param open If true, opens the carburetor valve; if false, closes it. + * The actual pin state is reversed based on the configuration. + */ void GasolineGenerator::setCarburetorValve(bool open) { bool pinState = config->carburetorValveReverse ? !open : open; outputCard->digitalWrite(config->carburetorValvePin, pinState); } +/** + * @brief Set the ignition system state. + * + * @param enable If true, powers the ignition system; if false, turns it off. + */ void GasolineGenerator::setIgnitionSystem(bool enable) { outputCard->digitalWrite(config->ignitionSystemPowerRelayPin, enable); } +/** + * @brief Set the starter relay state. + * + * @param enable If true, engages the starter; if false, disengages it. + */ void GasolineGenerator::setStarter(bool enable) { outputCard->digitalWrite(config->starterRelayPin, enable); starterEngaged = enable; } +/** + * @brief Handle the startup sequence of the gasoline generator. + * + * This function manages the timing and state transitions during the startup process, + * including engaging the starter and checking for successful engine start. + * + * @param currentTime The current time in milliseconds since system start. + */ void GasolineGenerator::handleStartupSequence(unsigned long currentTime) { // Step 3: After ignition delay, engage starter @@ -158,6 +208,12 @@ void GasolineGenerator::handleStartupSequence(unsigned long currentTime) } } +/** + * @brief Callback function called when the engine has successfully started. + * + * This function is called when the engine starts successfully after engaging the starter. + * It turns off the starter, opens the carburetor valve, and transitions to the RUNNING state. + */ void GasolineGenerator::onEngineStarted() { isEngineRunning = true; @@ -171,6 +227,15 @@ void GasolineGenerator::onEngineStarted() state = GeneratorState::RUNNING; } +/** + * @brief Handle input changes from the digital input card. + * + * This function processes changes in the state of input pins, particularly for the power output sensor. + * It checks if the engine has started or stopped based on the power output sensor state. + * + * @param pin The pin number that changed state. + * @param state The new state of the pin (true for HIGH, false for LOW). + */ void GasolineGenerator::handleInputChanges(uint8_t pin, bool state) { // Handle power output sensor changes diff --git a/src/config.hpp b/src/config.hpp index e69de29..4a2f3e2 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -0,0 +1,9 @@ +#pragma once + +// Generator Configuration +#define GENERATOR_IGNITION_SYSTEM_POWER_RELAY_PIN 0 +#define GENERATOR_STARTER_RELAY_PIN 1 +#define GENERATOR_CARBURETOR_VALVE_PIN 2 +#define GENERATOR_CARBURETOR_VALVE_REVERSE false +#define GENERATOR_POWER_OUTPUT_SENSOR_PIN -1 +#define GENERATOR_USE_POWER_OUTPUT_SENSOR false \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 234357b..31e13f1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,12 +54,12 @@ void setup() // Initialize generator GasolineGeneratorConfig generatorConfig; - generatorConfig.ignitionSystemPowerRelayPin = 0; - generatorConfig.starterRelayPin = 1; - generatorConfig.carburetorValvePin = 2; - generatorConfig.carburetorValveReverse = false; - generatorConfig.powerOutputSensorPin = -1; - generatorConfig.usePowerOutputSensor = false; + generatorConfig.ignitionSystemPowerRelayPin = GENERATOR_IGNITION_SYSTEM_POWER_RELAY_PIN; + generatorConfig.starterRelayPin = GENERATOR_STARTER_RELAY_PIN; + generatorConfig.carburetorValvePin = GENERATOR_CARBURETOR_VALVE_PIN; + generatorConfig.carburetorValveReverse = GENERATOR_CARBURETOR_VALVE_REVERSE; + generatorConfig.powerOutputSensorPin = GENERATOR_POWER_OUTPUT_SENSOR_PIN; + generatorConfig.usePowerOutputSensor = GENERATOR_USE_POWER_OUTPUT_SENSOR; generator.initialize(&espmega.inputs, &espmega.outputs, generatorConfig);