3
0
Fork 0
This commit is contained in:
Siwat Sirichai 2025-06-10 22:03:12 +07:00
commit 9df8df1956
4 changed files with 50 additions and 24 deletions

View file

@ -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
}
}
}