132 lines
2.9 KiB
C
132 lines
2.9 KiB
C
/*
|
|
* status_LED.h
|
|
*
|
|
* Created on: 07.07.2024
|
|
* Author: Hamza
|
|
*/
|
|
|
|
#include "ADBMS_LL_Driver.h"
|
|
#include "state_machine.h"
|
|
#include "stm32f3xx_hal.h"
|
|
#include <status_LED.h>
|
|
#include <stdint.h>
|
|
|
|
// TODO test out pulldown and pushpull settings
|
|
|
|
/* The PWM period (1/FPWM) is defined by the following parameters:
|
|
ARR value, the Prescaler value, and the internal clock itself which drives the timer module FCLK.
|
|
F_PWM = (F_CLK)/((ARR + 1) * (PSC + 1))
|
|
|
|
Prescaler:
|
|
(ARR + 1) * (PSC + 1) = (F_CLK)/(F_PWM)
|
|
(PSC + 1) = (F_CLK)/(F_PWM * (ARR + 1))
|
|
625 = (16MHz)/(100Hz * (255 + 1))
|
|
|
|
F_CLK = 16 MHz
|
|
*/
|
|
|
|
#define STATUS_LED_ARR 255
|
|
#define TIME_BLINK_SLOW 2000
|
|
#define TIME_BLINK_FAST 500
|
|
|
|
TIM_HandleTypeDef* red;
|
|
TIM_HandleTypeDef* green;
|
|
TIM_HandleTypeDef* blue;
|
|
|
|
uint32_t blink_timer;
|
|
bool blink_state;
|
|
|
|
void status_led_init(TIM_HandleTypeDef* r, TIM_HandleTypeDef* g, TIM_HandleTypeDef* b){
|
|
red = r;
|
|
green = g;
|
|
blue = b;
|
|
|
|
blink_timer = 0;
|
|
blink_state = 0;
|
|
|
|
HAL_TIM_PWM_Start(red, TIM_CHANNEL_1); //TIM4CH1
|
|
HAL_TIM_PWM_Start(green, TIM_CHANNEL_2); //TIM4CH2
|
|
HAL_TIM_PWM_Start(blue, TIM_CHANNEL_3); //TIM4CH3
|
|
|
|
status_led_set(255, 255, 255);
|
|
}
|
|
|
|
void status_led_update(){
|
|
if (state.current_state == STATE_ERROR){
|
|
status_led_set_color(RED);
|
|
return;
|
|
}
|
|
if(blink_timer > HAL_GetTick()){
|
|
return;
|
|
}
|
|
|
|
if (blink_state == 1){
|
|
if (state.current_state == STATE_INACTIVE)
|
|
blink_timer = HAL_GetTick() + TIME_BLINK_SLOW/10;
|
|
else
|
|
blink_timer = HAL_GetTick() + TIME_BLINK_FAST/10;
|
|
blink_state = 0;
|
|
status_led_set_color(OFF);
|
|
return;
|
|
} else {
|
|
if (state.current_state == STATE_INACTIVE)
|
|
blink_timer = HAL_GetTick() + TIME_BLINK_SLOW;
|
|
else
|
|
blink_timer = HAL_GetTick() + TIME_BLINK_FAST;
|
|
blink_state = 1;
|
|
}
|
|
|
|
switch (state.current_state) {
|
|
case STATE_INACTIVE:
|
|
status_led_set_color(GREEN);
|
|
break;
|
|
case STATE_CHARGING_PRECHARGE:
|
|
case STATE_PRECHARGE:
|
|
case STATE_DISCHARGE:
|
|
status_led_set_color( YELLOW);
|
|
break;
|
|
case STATE_CHARGING:
|
|
case STATE_READY:
|
|
case STATE_ACTIVE:
|
|
status_led_set_color(PINK);
|
|
break;
|
|
case STATE_ERROR:
|
|
status_led_set_color(RED);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void status_led_set_color(color color){
|
|
switch (color) {
|
|
case RED:
|
|
status_led_set(0, 255, 255);
|
|
break;
|
|
case GREEN:
|
|
status_led_set(255, 0, 255);
|
|
break;
|
|
case BLUE:
|
|
status_led_set(255, 255, 0);
|
|
break;
|
|
case YELLOW:
|
|
status_led_set(0, 0, 255);
|
|
break;
|
|
case PINK:
|
|
status_led_set(0, 255, 0);
|
|
break;
|
|
case CYAN:
|
|
status_led_set(255, 0, 0);
|
|
break;
|
|
case WHITE:
|
|
status_led_set(0, 0, 0);
|
|
break;
|
|
case OFF:
|
|
status_led_set(255,255,255);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void status_led_set(uint8_t r, uint8_t g, uint8_t b){
|
|
__HAL_TIM_SET_COMPARE(red, TIM_CHANNEL_1, r);
|
|
__HAL_TIM_SET_COMPARE(green, TIM_CHANNEL_2, g);
|
|
__HAL_TIM_SET_COMPARE(blue, TIM_CHANNEL_3, b);
|
|
} |