enabled dimming over ADC input
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#include "BSP_EE24.h"
|
||||
#include "LightTask.h"
|
||||
#include "CanDataHandler.h"
|
||||
#include "BSP_ADC.h"
|
||||
#define DIMM_DEADZONE_VOLTAGE 0.7
|
||||
|
||||
// Memory for the task
|
||||
StaticTask_t LightTask_cb;
|
||||
@@ -30,10 +32,13 @@ static cls_light_ThemeSettings msg_cls_light_ThemeSettings;
|
||||
static cls_light_SaveThemeSettings msg_cls_light_SaveThemeSettings;
|
||||
static cls_light_RequestThemeSetting msg_cls_light_RequestThemeSetting;
|
||||
|
||||
static volatile struct LightSettings_s {
|
||||
typedef struct LightSettings_s {
|
||||
volatile uint8_t brightness;
|
||||
volatile uint8_t theme;
|
||||
} lightSettings;
|
||||
} LightSettings_t;
|
||||
|
||||
static volatile LightSettings_t lightSettings = {0};
|
||||
static volatile LightSettings_t lightSettings_dimmed = {0};
|
||||
|
||||
void LightTask_setBrightness(uint8_t brightness) {
|
||||
lightSettings.brightness = brightness;
|
||||
@@ -57,8 +62,6 @@ void LightTask_start() {
|
||||
|
||||
|
||||
void LightTask_func(void *argument) {
|
||||
osDelay(10);
|
||||
|
||||
BSP_EE24_PartRead(BSP_EE24_PART_GLOBAL_LIGHT, (uint8_t*) &lightSettings, sizeof(lightSettings));
|
||||
uint16_t msg_global_light = GENERATE_CLS_ADDRESS(CLS_CODE_MESSAGE, GLOBAL_CAST_CLS_ADDRESS, CLS_CH_MSG_LIGHT);
|
||||
CLS_BSP_TxHeaderType can_header = CREATE_BSP_CAN_HEADER(msg_global_light, CLS_BSP_DLC_BYTES_2);
|
||||
@@ -68,7 +71,32 @@ void LightTask_func(void *argument) {
|
||||
{
|
||||
tick += 50;
|
||||
osDelayUntil(tick);
|
||||
CLS_BSP_CAN_AddMessageToSend(&can_header,(uint8_t*)&lightSettings);
|
||||
|
||||
|
||||
// calculate the brightness to send
|
||||
uint8_t brightness = lightSettings.brightness;
|
||||
// Read ADC battery voltage and dimmer voltage
|
||||
float v_bus = BSP_ADC_ReadBusValue() - DIMM_DEADZONE_VOLTAGE;
|
||||
float v_dimm = BSP_ADC_ReadDimmerValue();
|
||||
|
||||
// calculate the dimmfactor based on the battery voltage and the dimmer voltage
|
||||
// the dimmfactor shoudl be 0 if the dimmer voltage is 4V.
|
||||
// the dimmfactor should be 1 if the dimmer voltage is the same as the battery voltage
|
||||
float dimmfactor;
|
||||
if (v_dimm >= v_bus) {
|
||||
dimmfactor = 1.0;
|
||||
} else if (v_dimm <= 4.0) {
|
||||
dimmfactor = 0.0;
|
||||
} else {
|
||||
dimmfactor = (v_dimm - 4.0) / (v_bus - 4.0);
|
||||
}
|
||||
|
||||
uint8_t adjustedBrightness = (uint8_t)(brightness * dimmfactor);
|
||||
lightSettings_dimmed.brightness = adjustedBrightness;
|
||||
lightSettings_dimmed.theme = lightSettings.theme;
|
||||
|
||||
|
||||
CLS_BSP_CAN_AddMessageToSend(&can_header,(uint8_t*)&lightSettings_dimmed);
|
||||
|
||||
if( settingChangeTime !=0 && tick > settingChangeTime + settingSaveTimeout) {
|
||||
BSP_EE24_PartWrite(BSP_EE24_PART_GLOBAL_LIGHT, (uint8_t*) &lightSettings, sizeof(lightSettings));
|
||||
|
||||
Reference in New Issue
Block a user