#include "bsp.hpp"
#include "_bsp_private.hpp"

static GPIO_TypeDef* LED_PORT[LED_COUNT] = {LED_OK_PORT, LED_ERROR_PORT};
static const uint16_t LED_PIN[LED_COUNT] = {LED_OK_PIN, LED_ERROR_PIN};

void bsp_private::initializeLEDs()
{
	GPIO_InitTypeDef gpio_init_structure{
		.Pin = 0, // will be set by the loop below
		.Mode = GPIO_MODE_OUTPUT_PP,
		.Pull = GPIO_PULLUP,
		.Speed = GPIO_SPEED_FREQ_HIGH,
		.Alternate = 0 // not used
	};

	for(uint8_t led = 0; led < LED_COUNT; led++)
	{
		gpio_init_structure.Pin = LED_PIN[led];
		HAL_GPIO_Init(LED_PORT[led], &gpio_init_structure);
		HAL_GPIO_WritePin(LED_PORT[led], LED_PIN[led], GPIO_PIN_RESET);
	}
}

void enableLED(LED target)
{
	auto port = LED_PORT[static_cast<uint8_t>(target)];
	auto pin = LED_PIN[static_cast<uint8_t>(target)];
	HAL_GPIO_WritePin(port, pin, GPIO_PIN_SET);
}

void disableLED(LED target)
{
	auto port = LED_PORT[static_cast<uint8_t>(target)];
	auto pin = LED_PIN[static_cast<uint8_t>(target)];
	HAL_GPIO_WritePin(port, pin, GPIO_PIN_RESET);
}

void toggleLED(LED target)
{
	auto port = LED_PORT[static_cast<uint8_t>(target)];
	auto pin = LED_PIN[static_cast<uint8_t>(target)];
	HAL_GPIO_TogglePin(port, pin);
}
