/*
 * task_led.c
 *
 *  Created on: May 9, 2025
 *      Author: 911BL
 */

#include "globals.h"
#include "rgb.h"
#include "buttons.h"
#include "tim.h"
#include "pwm.h"

void led_task_execute(void)
{
    if (GOAL == 0) return; // prevent divide-by-zero crash

    uint32_t progress = (STEPCOUNT * 100) / GOAL; // percentage complete
    uint8_t num_leds_on = progress / 25;          // full LEDs to turn ON
    uint8_t partial_brightness = (progress % 25) * 4; // 0-100% brightness for the first 25%

    // First, always set PWM for DS3 (partial brightness LED)
    if (num_leds_on >= 1) {
        pwm_setDutyCycle(&htim2, TIM_CHANNEL_3, 100); // after 25% always 100% brightness
    } else {
        pwm_setDutyCycle(&htim2, TIM_CHANNEL_3, partial_brightness);
    }

    if (progress >= 50) {
        rgb_led_on(RGB_RIGHT); // 50%+
    }
    if (progress >= 75) {
        rgb_led_on(RGB_DOWN); // 75%+
    }
    if (progress >= 100) {
        rgb_led_on(RGB_LEFT); // 100% (if you have a 4th LED for 100%)
    }
}
