/* USER CODE BEGIN Header */
/* you can remove the copyright */
/*
 * Copyright 2020-2026 Yuntu Microelectronics Co., Ltd.
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 * 
 * @file main.c
 * @brief 
 * 
 */

/* USER CODE END Header */
#include "Mcal.h"
/* Includes ------------------------------------------------------------------*/
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <string.h>
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
static volatile uint8 AdcWakeupFlag = FALSE;
/* Adc result buffer - must be large enough for configured channels */
static Adc_ValueGroupType AdcResultBuffer[1];
/* 延时计数 - 约5秒延时（基于系统时钟） */
#define DELAY_COUNT (50000000U)

#define DELAY_COUNT1 (10000000U)

typedef enum
{
    DEMO_UNINIT    = 0,
    DEMO_RUN       = 1,
    DEMO_SLEEP     = 2,
    DEMO_DEEPSLEEP = 3,
    DEMO_STANDBY   = 4,
    DEMO_POWERDOWN = 5
} Demo_StateType;

volatile Demo_StateType State = DEMO_UNINIT;
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */

/* Private function declare --------------------------------------------------*/
/* USER CODE BEGIN PFDC */
void Uart1_PrintString(const char* str);
void Delay(uint32 count);
/* USER CODE END PFDC */
static void Board_Init(void);

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void Adc_ConvCompleteCallback(void)
{
    AdcWakeupFlag = TRUE;
    State = DEMO_RUN;
    /* Don't call UART in ISR - let main loop handle it */
}

void Uart1_PrintString(const char* str)
{
    if (str == NULL_PTR)
    {
        return;
    }

    (void)CddUart_SyncSend(0U, (uint8*)str, (uint32)strlen(str), 1000000U);
}

void Delay(uint32 count)
{
    volatile uint32 i = count;
    while(i > 0U)
    {
        i--;
    }
}

/* USER CODE END 0 */


/**
 * @brief  The application entry point.
 * @retval int
 */
int main(void)
{
    /* USER CODE BEGIN 1 */
    Mcu_Init(&Mcu_Config);
    Mcu_InitClock(0);
    /* USER CODE END 1 */ 
    Board_Init();
    /* USER CODE BEGIN 2 */
    Uart1_PrintString("MCU Model: MC03\r\n");
    Uart1_PrintString("DeepSleep with ADC Limit Check Demo\r\n");
    Uart1_PrintString("ADC Limits: 2048 - 3276\r\n");
    
    /* Setup Adc result buffer before starting conversion */
    Adc_SetupResultBuffer(AdcConf_AdcConfigSet_AdcGroup_0, AdcResultBuffer);
    
    /* Enable group notification to get callback */
    Adc_EnableGroupNotification(AdcConf_AdcConfigSet_AdcGroup_0);
    
    /* 大延时，方便重新烧录程序 */
    Uart1_PrintString("Waiting 5 seconds before entering DeepSleep...\r\n");
    Delay(DELAY_COUNT);
    
    /* USER CODE END 2 */

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {
        /* USER CODE END WHILE */
        /* USER CODE BEGIN 3 */
        /* 启动ADC转换，清除标志 */
        Uart1_PrintString("Starting ADC conversion...\r\n");
        AdcWakeupFlag = FALSE;
        Adc_StartGroupConversion(AdcConf_AdcConfigSet_AdcGroup_0);
        
        /* 进入深度睡眠模式 */
        State = DEMO_DEEPSLEEP;
        Uart1_PrintString("Calling Mcu_SetMode for DeepSleep...\r\n");
        Mcu_SetMode(McuConf_McuModeSettingConf_McuModeSettingConf_1);
        while (State == DEMO_DEEPSLEEP)
        {
            /* Nothing to do just wait for demo `State` change to `DEMO_DEEPSLEEP` */
            Delay(DELAY_COUNT1);
            if(AdcWakeupFlag == FALSE)
            Uart1_PrintString("Enter DeepSleep err!\r\n");
        }
        /* 从深度睡眠唤醒后执行 */
        Uart1_PrintString("Woke up from DeepSleep!\r\n");
        
        if (AdcWakeupFlag)
        {
            Uart1_PrintString("Wake source: ADC Limit Event!\r\n");
            AdcWakeupFlag = FALSE;
            
            /* 打印ADC结果 */
            Uart1_PrintString("ADC value: ");
            char numStr[10];
            uint16 value = AdcResultBuffer[0];
            uint8 i = 0;
            if (value == 0U)
            {
                numStr[i++] = '0';
            }
            else
            {
                char temp[10];
                uint8 j = 0;
                while (value > 0U)
                {
                    temp[j++] = '0' + (value % 10U);
                    value = value / 10U;
                }
                while (j > 0U)
                {
                    numStr[i++] = temp[--j];
                }
            }
            numStr[i++] = '\r';
            numStr[i++] = '\n';
            numStr[i] = '\0';
            Uart1_PrintString(numStr);
            
            /* 判断是否超限 */
            if (AdcResultBuffer[0] > 3276U)
            {
                Uart1_PrintString("Exceeded HIGH limit (3276)\r\n");
            }
            else if (AdcResultBuffer[0] < 2048U)
            {
                Uart1_PrintString("Below LOW limit (2048)\r\n");
            }
        }
        else
        {
            Uart1_PrintString("Wake source: Unknown (maybe not ADC limit event)\r\n");
        }
        
        /* 再次延时，方便重新烧录 */
        Uart1_PrintString("Waiting 5 seconds before re-entering DeepSleep...\r\n");
        Delay(DELAY_COUNT);
    }
    /* USER CODE END 3 */
}

static void Board_Init(void)
{
    Port_Init(&Port_Config);
    Platform_Init(NULL_PTR);
    CddUart_Init(&CddUart_Config);
    Adc_Init(&Adc_Config);
}

/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
