enabled dimming over ADC input

This commit is contained in:
2024-05-21 03:39:07 +02:00
parent 92c8624fe4
commit a9d11ea891
10 changed files with 159 additions and 41 deletions

View File

@@ -1,7 +1,7 @@
#include "cmsis_os2.h"
#include "LightState.h"
#include "BSP_GPIO.h"
#include "BSP_INA.h"
#include "BSP_ADC.h"
#include "LightTask.h"
#include "ulog.h"
@@ -31,16 +31,63 @@ static bool isK15On()
// Define the threshold voltage for engine running
#define ENGINE_RUNNING_THRESHOLD 13500 // 13.5V = 13500mV
#define ENGINE_RUNNING_THRESHOLD 13.3 // 13.5V
// Check if the engine is running by checking the voltage of the battery, if above the threshold, the engine is running
static bool isEngineRunning()
// 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()
{
return BSP_INA_Voltage() > ENGINE_RUNNING_THRESHOLD;
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()
@@ -84,6 +131,6 @@ void LightStateTask(void *argument)
}
// Delay the task for a certain amount of time (in milliseconds)
osDelay(500);
osDelay(50);
}
}