refactor Functions to group everything connected to the vehicle to a central location

This commit is contained in:
2024-05-28 03:52:03 +02:00
parent 9c489c1a25
commit 766d5e9ca8
14 changed files with 274 additions and 186 deletions

View File

@@ -5,6 +5,7 @@
#include "LightTask.h"
#include "ulog.h"
#include "stdbool.h"
#include "Vehicle.h"
// Create the task with a specific priority and stack size
osThreadAttr_t task_attr = {
@@ -24,89 +25,15 @@ void LightStateTask_start(void)
// Check if K15 is on for switching the light state
static bool isK15On()
{
return BSP_GPIO_K15isSet();
}
// Define the threshold voltage for engine running
#define ENGINE_RUNNING_THRESHOLD 13.3 // 13.5V
// Define thresholds with hysteresis
#define ENGINE_RUNNING_THRESHOLD_HIGH (ENGINE_RUNNING_THRESHOLD + 0.125)
#define ENGINE_RUNNING_THRESHOLD_LOW (ENGINE_RUNNING_THRESHOLD - 0.125)
// Global variable to store the current engine state
static bool engineRunning = false;
// Function to initialize the engine state based on the initial voltage
static void initializeEngineState()
{
float initialVoltage = BSP_ADC_ReadBusValue();
if (initialVoltage > ENGINE_RUNNING_THRESHOLD)
{
engineRunning = true;
}
else
{
engineRunning = false;
}
}
// Ensure the engine state is initialized once
static bool isEngineInitialized = false;
static bool isEngineRunning()
{
if (!isEngineInitialized)
{
initializeEngineState();
isEngineInitialized = true;
}
float voltage = BSP_ADC_ReadBusValue();
if (engineRunning)
{
// If engine is currently running, use the lower threshold to turn it off
if (voltage < ENGINE_RUNNING_THRESHOLD_LOW)
{
engineRunning = false;
}
}
else
{
// If engine is currently off, use the higher threshold to turn it on
if (voltage > ENGINE_RUNNING_THRESHOLD_HIGH)
{
engineRunning = true;
}
}
return engineRunning;
}
// check if the headlight is on and the engine is running to switch the light state
// we can check the headlight using the GPIO functions
static bool isHeadlightOn()
{
return BSP_GPIO_HeadLightIsSet();
}
// Function to determine the next state of the light
// return 1 as default state
// return 2 if the engine is running
// return 3 if the headlight and engine is running
static int determineNextState()
{
if (isHeadlightOn() && isEngineRunning()) {
if (Vehicle_isHeadlightOn() && Vehicle_isEngineRunning()) {
return 2;
} else if (isEngineRunning()) {
} else if (Vehicle_isEngineRunning()) {
return 1;
} else {
return 0;