#include "app_init.h"
#include "oled.h"
#include "tim.h"

static void motor_output_brake_init(void)
{
    const uint32_t max_pwm = __HAL_TIM_GET_AUTORELOAD(&htim1);

    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, max_pwm);
    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, max_pwm);
    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, max_pwm);
    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_4, max_pwm);
}

void App_Init(void)
{
    HAL_Delay(20);
    OLED_Init();
    OLED_Clear();
}

void App_StartPeripherals(void)
{
    (void)HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
    (void)HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
    (void)HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
    (void)HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);

    (void)HAL_TIM_PWM_Start(&htim8, TIM_CHANNEL_1);
    (void)HAL_TIM_PWM_Start(&htim8, TIM_CHANNEL_2);
    (void)HAL_TIM_PWM_Start(&htim8, TIM_CHANNEL_3);
    (void)HAL_TIM_PWM_Start(&htim8, TIM_CHANNEL_4);

    motor_output_brake_init();

    __HAL_TIM_SET_COUNTER(&htim2, 0U);
    __HAL_TIM_SET_COUNTER(&htim4, 0U);
    (void)HAL_TIM_Encoder_Start(&htim2, TIM_CHANNEL_ALL);
    (void)HAL_TIM_Encoder_Start(&htim4, TIM_CHANNEL_ALL);
}
