3
0
Fork 0

Enhance GasolineGenerator with toggle pin functionality and update configuration

This commit is contained in:
Siwat Sirichai 2025-06-09 00:26:25 +07:00
parent 863d9c1cf3
commit c7cfaeb98b
4 changed files with 46 additions and 20 deletions

View file

@ -23,14 +23,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); });
@ -52,9 +56,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)
@ -173,10 +176,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)
@ -230,8 +231,9 @@ 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).
@ -253,4 +255,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
}
}
}