enabled dimming over ADC input
This commit is contained in:
@@ -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