/**
  ******************************************************************************
  * file           : main.c
  * brief          : Main program body
  *                  Calls target system initialization then loop in main.
  ******************************************************************************
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include <stm32_hal.h>
#include <ux_api.h>
#include <ux_device_class_cdc_acm.h>
//#include <mx_usbx_device_cdc_acm.h>

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private functions prototype -----------------------------------------------*/

extern UX_SLAVE_CLASS_CDC_ACM  *cdc_acm;

// Send data via USB CDC
static void sendCDC(const void *ptr, size_t size) {
    UCHAR *p = (UCHAR *)ptr;
    while (size != 0) {
        ULONG n = 0;
        UINT r = ux_device_class_cdc_acm_write_run(cdc_acm, p, size, &n);
        if (r == UX_STATE_NEXT) {
            p += n;
            size -= n;
            continue;
        } else if (r != UX_STATE_WAIT) {
            break;
        }
        ux_system_tasks_run();
    }
}


/**
  * brief:  The application entry point.
  * retval: none but we specify int to comply with C99 standard
  */
int main(void)
{
  /** System Init: this code placed in targets folder initializes your system.
    * It calls the initialization (and sets the initial configuration) of the peripherals.
    * You can use STM32CubeMX to generate and call this code or not in this project.
    * It also contains the HAL initialization and the initial clock configuration.
    */
  if (mx_system_init() != SYSTEM_OK)
  {
    return (-1);
  }
  else
  {
    /*
      * You can start your application code here
      */
    while (1) {
        // LED On
        HAL_GPIO_WritePin(LED_PORT,LED_PIN,LED_ACTIVE_STATE);
        HAL_Delay(500);

        // LED Off
        HAL_GPIO_WritePin(LED_PORT,LED_PIN,LED_INACTIVE_STATE);
        HAL_Delay(500);

        // Send data
        char msg[]="Hallo!\n";
        sendCDC(msg, strlen(msg));
    }
  }
} /* end main */

