/*
 * 01_led_serial_control.c
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 *
 * Copyright (c) 2026 Georgy Moshkin <https://georgymoshkin.com>
 * All rights reserved.
 *
 */

#include "examples.h"

#ifdef EXAMPLE_01_LED_SERIAL_CONTROL

#ifndef SRC_01_LED_SERIAL_CONTROL_C_
#define SRC_01_LED_SERIAL_CONTROL_C_


#define UART_BUFFER_SIZE 64
__IO uint8_t uartBuffer[UART_BUFFER_SIZE];	// DMA buffer for serial port RX
uint8_t cmdByte; 							// Temporary TX/RX byte variable

void APP_Boot()
{
	// implement boot logic here
}

void APP_Init(UART_HandleTypeDef *huartPtr)
{
	// Initialize universal serial port library (uart.h, uart.c)
	COM_Init(0,					// Port number
			huartPtr,			// HAL port handle
			uartBuffer,			// RX buffer array
			UART_BUFFER_SIZE);	// RX buffer size
}


void APP_Loop()
{
	uint8_t cmd = 0xff; // invalid command by default

	COM_Read(&cmd,1);

	// Turn LED on
	if (cmd == 0x01) {
		HAL_GPIO_WritePin(GPIOC,
						  GPIO_PIN_13,
						  GPIO_PIN_RESET);
	}

	// Turn LED ff
	if (cmd == 0x02) {
		HAL_GPIO_WritePin(GPIOC,
						  GPIO_PIN_13,
						  GPIO_PIN_SET);
	}
}


#endif /* SRC_01_LED_SERIAL_CONTROL_C_ */
#endif /* EXAMPLE_01_LED_SERIAL_CONTROL */
