/*
 * 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()
{
	if (COM_ReadFast(&cmdByte, 1))
		switch(cmdByte)
		{
			case 0x01:
			{
				// Turn LED on
				HAL_GPIO_WritePin(GPIOC,
								  GPIO_PIN_13,
								  GPIO_PIN_RESET);
				break;
			}

			case 0x02:
			{
				// Turn LED off
				HAL_GPIO_WritePin(GPIOC,
								  GPIO_PIN_13,
								  GPIO_PIN_SET);
				break;
			}

			case 0x03:
			{
				uint8_t array[5] = {1, 2, 3, 4, 5};
				COM_Write(array, sizeof(array));
				break;
			}

			case 0x04:
			{
				uint8_t a, b;
				COM_Read(&a, 1);
				COM_Read(&b, 1);

				uint8_t c = a + b;
				COM_Write(&c, 1);
				break;
			}
		}

}


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