#include "cmsis_os2.h" #include "LightState.h" #include "BSP_GPIO.h" #include "BSP_ADC.h" #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 = { .name = "LightStateTask", .priority = osPriorityNormal, .stack_size = 2048 }; // Function prototype for the task function void LightStateTask(void *argument); // Function to start the LightStateTask void LightStateTask_start(void) { osThreadNew(LightStateTask, NULL, &task_attr); } // 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 (Vehicle_isHeadlightOn() && Vehicle_isEngineRunning()) { return 2; } else if (Vehicle_isEngineRunning()) { return 1; } else { return 0; } } // Task function void LightStateTask(void *argument) { // default state int current_state = 0; // Infinite loop to keep the task running while (1) { // only change the state if the current state is different from the next state int next_state = determineNextState(); if (current_state != next_state) { 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) osDelay(50); } }