#include <stdint.h>

#include "farbe.h"
#include "stm32f042x6.h"
#include "stm32f0xx_hal.h"
#include "stm32f0xx_hal_tim.h"

#define DELAY 50000

int Counter = 0;

void led_init(TIM_HandleTypeDef* htim) {
    HAL_TIM_PWM_Start(htim, TIM_CHANNEL_1);
    HAL_TIM_PWM_Start(htim, TIM_CHANNEL_2);
    HAL_TIM_PWM_Start(htim, TIM_CHANNEL_3);
  
    int32_t color = SLSERROR;

    int32_t Blue = color & 0x000000FF;
    int32_t Green = (color & 0x0000FF00) >> 8;
    int32_t Red = (color & 0x00FF0000) >> 16;

    TIM2->CCR1 = Green;
    TIM2->CCR2 = Red;
    TIM2->CCR3 = Blue;
    HAL_Delay(1000);
}

void blink(TIM_HandleTypeDef* htim, int32_t color) {
    if(Counter < DELAY) {
        Counter ++;
    } else {
        Counter = 0;
    }
    int32_t Blue = color & 0x000000FF;
    int32_t Green = (color & 0x0000FF00) >> 8;
    int32_t Red = (color & 0x00FF0000) >> 16;

    TIM2->CCR1 = Green;
    TIM2->CCR2 = Red;
    TIM2->CCR3 = Blue;

    if (Counter < DELAY/2) {
        HAL_TIM_PWM_Stop(htim, TIM_CHANNEL_1);
        HAL_TIM_PWM_Stop(htim, TIM_CHANNEL_2);
        HAL_TIM_PWM_Stop(htim, TIM_CHANNEL_3);
    } else {
        HAL_TIM_PWM_Start(htim, TIM_CHANNEL_1);
        HAL_TIM_PWM_Start(htim, TIM_CHANNEL_2);
        HAL_TIM_PWM_Start(htim, TIM_CHANNEL_3);
    }
}