diff --git a/src/GasolineGenerator.cpp b/src/GasolineGenerator.cpp index f72c8e2..7d506b1 100644 --- a/src/GasolineGenerator.cpp +++ b/src/GasolineGenerator.cpp @@ -22,14 +22,18 @@ void GasolineGenerator::initialize(DigitalInputCard *inputCard, DigitalOutputCar // Initialize all outputs to safe state setIgnitionSystem(false); setStarter(false); - setCarburetorValve(false); // Close for startup - - // Set debounce time for power output sensor if enabled - if (config.usePowerOutputSensor && config.powerOutputSensorPin >= 0) + setCarburetorValve(false); // Close for startup // Set debounce time for power output sensor if enabled + if (config.usePowerOutputSensor) { this->inputCard->setDebounceTime(config.powerOutputSensorPin, POWER_SENSOR_DEBOUNCE_MS); } + // Set debounce time for toggle pin if enabled + if (config.useTogglePin) + { + this->inputCard->setDebounceTime(config.togglePin, POWER_SENSOR_DEBOUNCE_MS); + } + // Register input change callback this->inputCard->registerCallback([this](uint8_t pin, bool state) { this->handleInputChanges(pin, state); }); @@ -51,9 +55,8 @@ void GasolineGenerator::loop() handleStartupSequence(currentTime); break; - case GeneratorState::RUNNING: - // Monitor engine status and handle any issues - if (config->usePowerOutputSensor && config->powerOutputSensorPin >= 0) + case GeneratorState::RUNNING: // Monitor engine status and handle any issues + if (config->usePowerOutputSensor) { bool hasPowerOutput = inputCard->digitalRead(config->powerOutputSensorPin); if (!hasPowerOutput) @@ -172,10 +175,8 @@ void GasolineGenerator::handleStartupSequence(unsigned long currentTime) { setStarter(true); - } - - // Check if we have power output AND starter is off (engine is running independently) - if (config->usePowerOutputSensor && config->powerOutputSensorPin >= 0) + } // Check if we have power output AND starter is off (engine is running independently) + if (config->usePowerOutputSensor) { bool hasPowerOutput = inputCard->digitalRead(config->powerOutputSensorPin); if (hasPowerOutput && !starterEngaged && !isEngineRunning) @@ -228,10 +229,11 @@ void GasolineGenerator::onEngineStarted() /** * @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. - * + * + * This function processes changes in the state of input pins, particularly for the power output sensor + * and toggle switch. It checks if the engine has started or stopped based on the power output sensor state, + * and handles toggle switch presses to start/stop the generator. + * * @param pin The pin number that changed state. * @param state The new state of the pin (true for HIGH, false for LOW). */ @@ -252,4 +254,23 @@ void GasolineGenerator::handleInputChanges(uint8_t pin, bool state) shutdown(); } } + + // Handle toggle pin changes + if (pin == config->togglePin && config->useTogglePin) + { + if (state) // Toggle switch pressed (rising edge) + { + if (this->state == GeneratorState::STOPPED) + { + // Start the generator + start(); + } + else if (this->state == GeneratorState::RUNNING) + { + // Stop the generator + shutdown(); + } + // Ignore toggle during STARTING and SHUTTING_DOWN states + } + } } \ No newline at end of file diff --git a/src/GasolineGenerator.hpp b/src/GasolineGenerator.hpp index 8dd3dab..e236843 100644 --- a/src/GasolineGenerator.hpp +++ b/src/GasolineGenerator.hpp @@ -14,12 +14,14 @@ struct GasolineGeneratorConfig { - int8_t ignitionSystemPowerRelayPin; // Pin for ignition system power relay - int8_t starterRelayPin; // Pin for starter relay - int8_t carburetorValvePin; // Pin for carburetor control - bool carburetorValveReverse; // If true, 0 means open, 1 means closed else 0 means closed, 1 means open - int8_t powerOutputSensorPin = -1; // Pin for monitoring generator power output (-1 to disable) - bool usePowerOutputSensor = false; // Enable/disable power output sensor monitoring + uint8_t ignitionSystemPowerRelayPin; // Pin for ignition system power relay + uint8_t starterRelayPin; // Pin for starter relay + uint8_t carburetorValvePin; // Pin for carburetor control + bool carburetorValveReverse; // If true, 0 means open, 1 means closed else 0 means closed, 1 means open + uint8_t powerOutputSensorPin; // Pin for monitoring generator power output + bool usePowerOutputSensor = false; // Enable/disable power output sensor monitoring + uint8_t togglePin; // Pin for toggle switch to turn generator on/off + bool useTogglePin = false; // Enable/disable toggle pin functionality }; /** diff --git a/src/config.hpp b/src/config.hpp index 4a2f3e2..0d23e97 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -6,4 +6,6 @@ #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 +#define GENERATOR_USE_POWER_OUTPUT_SENSOR false +#define GENERATOR_TOGGLE_PIN -1 +#define GENERATOR_USE_TOGGLE_PIN false \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 31e13f1..b40e4ad 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -56,10 +56,11 @@ void setup() GasolineGeneratorConfig generatorConfig; 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.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; + generatorConfig.togglePin = GENERATOR_TOGGLE_PIN; + generatorConfig.useTogglePin = GENERATOR_USE_TOGGLE_PIN; generator.initialize(&espmega.inputs, &espmega.outputs, generatorConfig);