Enhance GasolineGenerator with toggle pin functionality and update configuration
This commit is contained in:
parent
863d9c1cf3
commit
c7cfaeb98b
4 changed files with 46 additions and 20 deletions
|
@ -23,14 +23,18 @@ void GasolineGenerator::initialize(DigitalInputCard* inputCard, DigitalOutputCar
|
||||||
// Initialize all outputs to safe state
|
// Initialize all outputs to safe state
|
||||||
setIgnitionSystem(false);
|
setIgnitionSystem(false);
|
||||||
setStarter(false);
|
setStarter(false);
|
||||||
setCarburetorValve(false); // Close for startup
|
setCarburetorValve(false); // Close for startup // Set debounce time for power output sensor if enabled
|
||||||
|
if (config.usePowerOutputSensor)
|
||||||
// Set debounce time for power output sensor if enabled
|
|
||||||
if (config.usePowerOutputSensor && config.powerOutputSensorPin >= 0)
|
|
||||||
{
|
{
|
||||||
this->inputCard->setDebounceTime(config.powerOutputSensorPin, POWER_SENSOR_DEBOUNCE_MS);
|
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
|
// Register input change callback
|
||||||
this->inputCard->registerCallback([this](uint8_t pin, bool state)
|
this->inputCard->registerCallback([this](uint8_t pin, bool state)
|
||||||
{ this->handleInputChanges(pin, state); });
|
{ this->handleInputChanges(pin, state); });
|
||||||
|
@ -52,9 +56,8 @@ void GasolineGenerator::loop()
|
||||||
handleStartupSequence(currentTime);
|
handleStartupSequence(currentTime);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case GeneratorState::RUNNING:
|
case GeneratorState::RUNNING: // Monitor engine status and handle any issues
|
||||||
// Monitor engine status and handle any issues
|
if (config->usePowerOutputSensor)
|
||||||
if (config->usePowerOutputSensor && config->powerOutputSensorPin >= 0)
|
|
||||||
{
|
{
|
||||||
bool hasPowerOutput = inputCard->digitalRead(config->powerOutputSensorPin);
|
bool hasPowerOutput = inputCard->digitalRead(config->powerOutputSensorPin);
|
||||||
if (!hasPowerOutput)
|
if (!hasPowerOutput)
|
||||||
|
@ -173,10 +176,8 @@ void GasolineGenerator::handleStartupSequence(unsigned long currentTime)
|
||||||
{
|
{
|
||||||
|
|
||||||
setStarter(true);
|
setStarter(true);
|
||||||
}
|
} // Check if we have power output AND starter is off (engine is running independently)
|
||||||
|
if (config->usePowerOutputSensor)
|
||||||
// Check if we have power output AND starter is off (engine is running independently)
|
|
||||||
if (config->usePowerOutputSensor && config->powerOutputSensorPin >= 0)
|
|
||||||
{
|
{
|
||||||
bool hasPowerOutput = inputCard->digitalRead(config->powerOutputSensorPin);
|
bool hasPowerOutput = inputCard->digitalRead(config->powerOutputSensorPin);
|
||||||
if (hasPowerOutput && !starterEngaged && !isEngineRunning)
|
if (hasPowerOutput && !starterEngaged && !isEngineRunning)
|
||||||
|
@ -230,8 +231,9 @@ void GasolineGenerator::onEngineStarted()
|
||||||
/**
|
/**
|
||||||
* @brief Handle input changes from the digital input card.
|
* @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.
|
* 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.
|
* 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 pin The pin number that changed state.
|
||||||
* @param state The new state of the pin (true for HIGH, false for LOW).
|
* @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();
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -14,12 +14,14 @@
|
||||||
|
|
||||||
struct GasolineGeneratorConfig
|
struct GasolineGeneratorConfig
|
||||||
{
|
{
|
||||||
int8_t ignitionSystemPowerRelayPin; // Pin for ignition system power relay
|
uint8_t ignitionSystemPowerRelayPin; // Pin for ignition system power relay
|
||||||
int8_t starterRelayPin; // Pin for starter relay
|
uint8_t starterRelayPin; // Pin for starter relay
|
||||||
int8_t carburetorValvePin; // Pin for carburetor control
|
uint8_t carburetorValvePin; // Pin for carburetor control
|
||||||
bool carburetorValveReverse; // If true, 0 means open, 1 means closed else 0 means closed, 1 means open
|
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)
|
uint8_t powerOutputSensorPin; // Pin for monitoring generator power output
|
||||||
bool usePowerOutputSensor = false; // Enable/disable power output sensor monitoring
|
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
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -6,4 +6,6 @@
|
||||||
#define GENERATOR_CARBURETOR_VALVE_PIN 2
|
#define GENERATOR_CARBURETOR_VALVE_PIN 2
|
||||||
#define GENERATOR_CARBURETOR_VALVE_REVERSE false
|
#define GENERATOR_CARBURETOR_VALVE_REVERSE false
|
||||||
#define GENERATOR_POWER_OUTPUT_SENSOR_PIN -1
|
#define GENERATOR_POWER_OUTPUT_SENSOR_PIN -1
|
||||||
#define GENERATOR_USE_POWER_OUTPUT_SENSOR false
|
#define GENERATOR_USE_POWER_OUTPUT_SENSOR false
|
||||||
|
#define GENERATOR_TOGGLE_PIN -1
|
||||||
|
#define GENERATOR_USE_TOGGLE_PIN false
|
|
@ -56,10 +56,11 @@ void setup()
|
||||||
GasolineGeneratorConfig generatorConfig;
|
GasolineGeneratorConfig generatorConfig;
|
||||||
generatorConfig.ignitionSystemPowerRelayPin = GENERATOR_IGNITION_SYSTEM_POWER_RELAY_PIN;
|
generatorConfig.ignitionSystemPowerRelayPin = GENERATOR_IGNITION_SYSTEM_POWER_RELAY_PIN;
|
||||||
generatorConfig.starterRelayPin = GENERATOR_STARTER_RELAY_PIN;
|
generatorConfig.starterRelayPin = GENERATOR_STARTER_RELAY_PIN;
|
||||||
generatorConfig.carburetorValvePin = GENERATOR_CARBURETOR_VALVE_PIN;
|
generatorConfig.carburetorValvePin = GENERATOR_CARBURETOR_VALVE_PIN; generatorConfig.carburetorValveReverse = GENERATOR_CARBURETOR_VALVE_REVERSE;
|
||||||
generatorConfig.carburetorValveReverse = GENERATOR_CARBURETOR_VALVE_REVERSE;
|
|
||||||
generatorConfig.powerOutputSensorPin = GENERATOR_POWER_OUTPUT_SENSOR_PIN;
|
generatorConfig.powerOutputSensorPin = GENERATOR_POWER_OUTPUT_SENSOR_PIN;
|
||||||
generatorConfig.usePowerOutputSensor = GENERATOR_USE_POWER_OUTPUT_SENSOR;
|
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);
|
generator.initialize(&espmega.inputs, &espmega.outputs, generatorConfig);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue