diff --git a/src/main.cpp b/src/main.cpp index 25686a8..ed6d44e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -73,12 +73,14 @@ void initCAN() vTaskDelay(100); } ESP_LOGI("CAN Interface", "Initialization succeeded!"); + can_enabled = true; } void stopCAN() { CAN.end(); ESP_LOGI("CAN Interface", "Shutdown succeeded!"); + can_enabled = false; } // Function to send a CAN message @@ -122,13 +124,8 @@ void handleGPIO() if (!newIgnitionState) { ESP_LOGI("GPIO State machine", "Ignition turned off"); - lastIgnitionTime = currentMillis; - ignitionState = false; - // Shut down CAN interface if ignition is off - if (!ignitionState) - { - stopCAN(); - } + standbyState = true; + ESP_LOGI("State machine", "Ignition set to standby mode"); } else { @@ -234,9 +231,10 @@ void vCANSendTask(void *pvParameters) } // Check if ignition should go into standby mode - if (!gpioIgnitionState && ignitionState && !standbyStartTime) + if (!gpioIgnitionState && ignitionState && !standbyStartTime && standbyState) { standbyStartTime = millis(); // Start the standby timer + ESP_LOGI("State machine", "Start standby timer"); } // Check if standby duration has elapsed and gpio ignition is still false @@ -249,10 +247,11 @@ void vCANSendTask(void *pvParameters) } // If GPIO ignition is true, reset standby timer - if (gpioIgnitionState) + if (gpioIgnitionState && standbyState) { standbyStartTime = 0; // Reset standby timer standbyState = false; + ESP_LOGI("State machine", "Ignition standby timer disabled"); } vTaskDelay(10); } @@ -286,6 +285,11 @@ void vSerialConsoleTask(void *pvParameters) ESP_LOGD("State machine", "Try to enable CAN Interface"); initCAN(); } + if (standbyStartTime != 0) { + standbyStartTime = 0; // Reset standby timer + ESP_LOGI("State machine"," Ignition standby timer disabled"); + } + } else if (input.endsWith("off")) { @@ -309,7 +313,15 @@ void vSerialConsoleTask(void *pvParameters) } else { - ESP_LOGI("State machine", "Ignition is not in standby mode"); + standbyStartTime = millis(); + ESP_LOGI("State machine", "Ignition set to standby mode, retype command to see duration left"); + if (!can_enabled) + { + ESP_LOGD("State machine", "Try to enable CAN Interface"); + initCAN(); + } + standbyState = true; + ignitionState = true; } } }