added LightState Switch
This commit is contained in:
@@ -76,3 +76,11 @@ void BSP_GPIO_EE24WriteProtectOn() {
|
|||||||
void BSP_GPIO_EE24WriteProtectOff() {
|
void BSP_GPIO_EE24WriteProtectOff() {
|
||||||
HAL_GPIO_WritePin(EEPROM_WC_GPIO_Port, EEPROM_WC_Pin, GPIO_PIN_RESET);
|
HAL_GPIO_WritePin(EEPROM_WC_GPIO_Port, EEPROM_WC_Pin, GPIO_PIN_RESET);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Checks if the headlight is on.
|
||||||
|
* @return true if the headlight is on, false otherwise.
|
||||||
|
*/
|
||||||
|
bool BSP_GPIO_HeadLightIsSet() {
|
||||||
|
return HAL_GPIO_ReadPin(Headlight_Detect_GPIO_Port, Headlight_Detect_Pin) == GPIO_PIN_SET;
|
||||||
|
}
|
||||||
@@ -25,3 +25,8 @@ void BSP_GPIO_RadioOff();
|
|||||||
|
|
||||||
void BSP_GPIO_EE24WriteProtectOn();
|
void BSP_GPIO_EE24WriteProtectOn();
|
||||||
void BSP_GPIO_EE24WriteProtectOff();
|
void BSP_GPIO_EE24WriteProtectOff();
|
||||||
|
|
||||||
|
|
||||||
|
// Check if the headlight is on
|
||||||
|
bool BSP_GPIO_HeadLightIsSet();
|
||||||
|
|
||||||
|
|||||||
@@ -22,4 +22,4 @@ target_sources(${PROJECT_NAME}
|
|||||||
|
|
||||||
target_include_directories(${PROJECT_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR})
|
target_include_directories(${PROJECT_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR})
|
||||||
target_link_libraries(${PROJECT_NAME} PUBLIC PROTOS CLS)
|
target_link_libraries(${PROJECT_NAME} PUBLIC PROTOS CLS)
|
||||||
target_link_libraries(${PROJECT_NAME} PRIVATE Revision CLS_BSP BSP)
|
target_link_libraries(${PROJECT_NAME} PRIVATE Revision CLS_BSP BSP ulog)
|
||||||
@@ -2,7 +2,8 @@
|
|||||||
#include "LightState.h"
|
#include "LightState.h"
|
||||||
#include "BSP_GPIO.h"
|
#include "BSP_GPIO.h"
|
||||||
#include "BSP_INA.h"
|
#include "BSP_INA.h"
|
||||||
|
#include "LightTask.h"
|
||||||
|
#include "ulog.h"
|
||||||
|
|
||||||
// Create the task with a specific priority and stack size
|
// Create the task with a specific priority and stack size
|
||||||
osThreadAttr_t task_attr = {
|
osThreadAttr_t task_attr = {
|
||||||
@@ -20,19 +21,67 @@ void LightStateTask_start(void)
|
|||||||
osThreadNew(LightStateTask, NULL, &task_attr);
|
osThreadNew(LightStateTask, NULL, &task_attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// 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 13500 // 13.5V = 13500mV
|
||||||
|
|
||||||
|
// Check if the engine is running by checking the voltage of the battery, if above the threshold, the engine is running
|
||||||
|
static bool isEngineRunning()
|
||||||
|
{
|
||||||
|
return BSP_INA_Voltage() > ENGINE_RUNNING_THRESHOLD;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// 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()) {
|
||||||
|
return 2;
|
||||||
|
} else if (isEngineRunning()) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Task function
|
// Task function
|
||||||
void LightStateTask(void *argument)
|
void LightStateTask(void *argument)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
// default state
|
||||||
|
int current_state = 0;
|
||||||
// Infinite loop to keep the task running
|
// Infinite loop to keep the task running
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
// default state
|
// only change the state if the current state is different from the next state
|
||||||
int next_state = 0;
|
int next_state = determineNextState();
|
||||||
|
if (current_state != next_state) {
|
||||||
// check if k15 is on
|
LightTask_setTheme(next_state);
|
||||||
|
ULOG_INFO("Light state changed to %d", next_state);
|
||||||
|
current_state = next_state;
|
||||||
|
}
|
||||||
|
|
||||||
// Delay the task for a certain amount of time (in milliseconds)
|
// Delay the task for a certain amount of time (in milliseconds)
|
||||||
osDelay(500);
|
osDelay(500);
|
||||||
|
|||||||
@@ -35,6 +35,14 @@ static volatile struct LightSettings_s {
|
|||||||
volatile uint8_t theme;
|
volatile uint8_t theme;
|
||||||
} lightSettings;
|
} lightSettings;
|
||||||
|
|
||||||
|
void LightTask_setBrightness(uint8_t brightness) {
|
||||||
|
lightSettings.brightness = brightness;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LightTask_setTheme(uint8_t theme) {
|
||||||
|
lightSettings.theme = theme;
|
||||||
|
}
|
||||||
|
|
||||||
// these are used to save these settings some tome after they change
|
// these are used to save these settings some tome after they change
|
||||||
static const uint32_t settingSaveTimeout = 10*1000;
|
static const uint32_t settingSaveTimeout = 10*1000;
|
||||||
static volatile uint32_t settingChangeTime = 0;
|
static volatile uint32_t settingChangeTime = 0;
|
||||||
|
|||||||
@@ -43,3 +43,6 @@ typedef struct RGB_Theme
|
|||||||
|
|
||||||
|
|
||||||
void LightTask_start();
|
void LightTask_start();
|
||||||
|
|
||||||
|
|
||||||
|
void LightTask_setTheme(uint8_t theme);
|
||||||
@@ -36,6 +36,7 @@
|
|||||||
#include "BSP_INA.h"
|
#include "BSP_INA.h"
|
||||||
#include "BSP_POWER.h"
|
#include "BSP_POWER.h"
|
||||||
#include "LightTask.h"
|
#include "LightTask.h"
|
||||||
|
#include "LightState.h"
|
||||||
#include "BSP_GPIO.h"
|
#include "BSP_GPIO.h"
|
||||||
/* USER CODE END Includes */
|
/* USER CODE END Includes */
|
||||||
|
|
||||||
@@ -69,7 +70,7 @@ const osThreadAttr_t defaultTask_attributes = {
|
|||||||
osThreadId_t waitForStartConfirmHandle;
|
osThreadId_t waitForStartConfirmHandle;
|
||||||
const osThreadAttr_t waitForStartConfirm_attributes = {
|
const osThreadAttr_t waitForStartConfirm_attributes = {
|
||||||
.name = "waitForStartConfirm",
|
.name = "waitForStartConfirm",
|
||||||
.stack_size = 128 * 4,
|
.stack_size = 256 * 4,
|
||||||
.priority = (osPriority_t) osPriorityNormal,
|
.priority = (osPriority_t) osPriorityNormal,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -142,6 +143,7 @@ void MX_FREERTOS_Init(void) {
|
|||||||
CLS_Init();
|
CLS_Init();
|
||||||
ULOG_INFO("Setup LightTask");
|
ULOG_INFO("Setup LightTask");
|
||||||
LightTask_start();
|
LightTask_start();
|
||||||
|
LightStateTask_start();
|
||||||
/* USER CODE END RTOS_THREADS */
|
/* USER CODE END RTOS_THREADS */
|
||||||
|
|
||||||
/* USER CODE BEGIN RTOS_EVENTS */
|
/* USER CODE BEGIN RTOS_EVENTS */
|
||||||
@@ -228,14 +230,23 @@ void WaitForStartConfirm_Task(void *argument) {
|
|||||||
BSP_GPIO_RadioOn();
|
BSP_GPIO_RadioOn();
|
||||||
ULOG_INFO("Power systems started");
|
ULOG_INFO("Power systems started");
|
||||||
osThreadExit();
|
osThreadExit();
|
||||||
return;
|
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
osDelay(1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(tick > maxDelayTime) {
|
if(tick > maxDelayTime) {
|
||||||
BSP_POWER_EnterStandby();
|
BSP_POWER_EnterStandby();
|
||||||
ULOG_INFO("System in standby mode");
|
ULOG_INFO("System in standby mode");
|
||||||
osThreadExit();
|
osThreadExit();
|
||||||
return;
|
while (1)
|
||||||
|
{
|
||||||
|
osDelay(1000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -348,8 +348,8 @@ void Error_Handler(void)
|
|||||||
ULOG_ERROR("Error Handler");
|
ULOG_ERROR("Error Handler");
|
||||||
osThreadId_t error_thread = osThreadGetId();
|
osThreadId_t error_thread = osThreadGetId();
|
||||||
if(error_thread != NULL) {
|
if(error_thread != NULL) {
|
||||||
const char * osThreadGetName(error_thread);
|
const char * thread_name = osThreadGetName(error_thread);
|
||||||
ULOG_ERROR("Error in thread: %s", osThreadGetName(error_thread));
|
ULOG_ERROR("Error in thread: %s", thread_name);
|
||||||
}
|
}
|
||||||
__disable_irq();
|
__disable_irq();
|
||||||
while (1)
|
while (1)
|
||||||
|
|||||||
Reference in New Issue
Block a user