/*
 * led_task.c
 *
 *  Created on: Oct 6, 2025
 *      Author: jomaa
 */

#include "led_task.h"

TaskHandle_t hButtonTask;
TaskHandle_t tH[3];

void CreateLedTask()
{
	xTaskCreate(
		ButtonTask,
		"ButtonTask",
		128,
		NULL,
		1,
		&hButtonTask
	);

	xTaskCreate(
		LedAnimationTask1,
		"LedAnimationTask1",
		128,
		NULL,
		1,
		&tH[0]
	);

	xTaskCreate(
		LedAnimationTask2,
		"LedAnimationTask2",
		128,
		NULL,
		1,
		&tH[1]
	);

	xTaskCreate(
		LedAnimationTask3,
		"LedAnimationTask3",
		128,
		NULL,
		1,
		&tH[2]
	);
}

void ButtonTask(void* pArgs)
{
	for(;;)
	{
		uint8_t key = ReadJoy();
		if(key)
			xTaskNotifyGive(tH[key - 1]);
		vTaskDelay(100);
	}
}

void LedAnimationTask1(void* pArgs)
{
	for(;;)
	{
		ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
		int i;
		for(i = 0; i < 3; i++)
		{
			LED_On(i);
			vTaskDelay(150);
			LED_Off(i);
		}
		for(i = 1; i > 0; i--)
		{
			LED_On(i);
			vTaskDelay(150);
			LED_Off(i);
		}
	}
}

void LedAnimationTask2(void* pArgs)
{
	for(;;)
	{
		ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
		int i, j;
		for(j = 0; j < 5; j++)
		{
			for(i = 0; i < 3; i++)
				LED_On(i);

			vTaskDelay(100);

			for(i = 0; i < 3; i++)
				LED_Off(i);

			vTaskDelay(100);
		}
	}
}

void LedAnimationTask3(void* pArgs)
{
	for(;;)
	{
		ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
		int i;
		for(i = 0; i < 3; i++)
		{
			LED_On(i);
			vTaskDelay(150);
		}
		for(i = 2; i >= 0; i--)
		{
			LED_Off(i);
			vTaskDelay(150);
		}
	}
}

