/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.1
 *
 * @date        2025-04-31
 *
 * @attention
 *
 *  Copyright (C) 2026 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (Geehy Semiconductor Software License Agreement).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the Geehy Semiconductor Software License Agreement for the governing permissions
 *  and limitations under the License.
 */
/* Includes */
#include "stdio.h"

#include "Port.h"
#include "CDD_Usart.h"
#include "Mcu.h"
#include "Dio.h"

#define LED1_PIN             DioConfig_DioChannel_DioChannel_C10
#define LED2_PIN             DioConfig_DioChannel_DioChannel_C11
#define LED3_PIN             DioConfig_DioChannel_DioChannel_C12
#define LED4_PIN             DioConfig_DioChannel_DioChannel_D2

#define BT1_PIN              DioConfig_DioChannel_DioChannel_C0
#define BT2_PIN              DioConfig_DioChannel_DioChannel_C1
#define BT3_PIN              DioConfig_DioChannel_DioChannel_C2
#define BT4_PIN              DioConfig_DioChannel_DioChannel_C3

#define LED1_ON              Dio_WriteChannel(LED1_PIN, DIO_STD_LOW)
#define LED1_OFF             Dio_WriteChannel(LED1_PIN, DIO_STD_HIGH)

#define LED2_ON              Dio_WriteChannel(LED2_PIN, DIO_STD_LOW)
#define LED2_OFF             Dio_WriteChannel(LED2_PIN, DIO_STD_HIGH)

#define LED3_ON              Dio_WriteChannel(LED3_PIN, DIO_STD_LOW)
#define LED3_OFF             Dio_WriteChannel(LED3_PIN, DIO_STD_HIGH)

#define LED4_ON              Dio_WriteChannel(LED4_PIN, DIO_STD_LOW)
#define LED4_OFF             Dio_WriteChannel(LED4_PIN, DIO_STD_HIGH)

void delay(uint32 value)
{
    while(value--);
}

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
int main(void)
{
    Mcu_Init(&g_mcuConfig);
    Mcu_InitClock(McuClockSettingConfig_0);
    Port_Init(&g_portConfig);
    Usart_Init(g_usartConfg);

    /* configure LED pins as GPIO */
    Port_SetPinMode(PortConfigSet_PortContainer_0_LED1,GPIO_MODE_OUT);
    Port_SetPinMode(PortConfigSet_PortContainer_0_LED2,GPIO_MODE_OUT);
    Port_SetPinMode(PortConfigSet_PortContainer_0_LED3,GPIO_MODE_OUT);
    Port_SetPinMode(PortConfigSet_PortContainer_0_LED4,GPIO_MODE_OUT);
    /* configure KEY pins as GPIO */
    Port_SetPinMode(PortConfigSet_PortContainer_0_KEY1,GPIO_MODE_IN);
    Port_SetPinMode(PortConfigSet_PortContainer_0_KEY2,GPIO_MODE_IN);
    Port_SetPinMode(PortConfigSet_PortContainer_0_KEY3,GPIO_MODE_IN);
    Port_SetPinMode(PortConfigSet_PortContainer_0_KEY4,GPIO_MODE_IN);
    /* configure the LED pins as an output */
    Port_SetPinDirection(PortConfigSet_PortContainer_0_LED1,PORT_PIN_OUT);
    Port_SetPinDirection(PortConfigSet_PortContainer_0_LED2,PORT_PIN_OUT);
    Port_SetPinDirection(PortConfigSet_PortContainer_0_LED3,PORT_PIN_OUT);
    Port_SetPinDirection(PortConfigSet_PortContainer_0_LED4,PORT_PIN_OUT);
    /* configure the bt pins as an input */
    Port_SetPinDirection(PortConfigSet_PortContainer_0_KEY1,PORT_PIN_IN);
    Port_SetPinDirection(PortConfigSet_PortContainer_0_KEY2,PORT_PIN_IN);
    Port_SetPinDirection(PortConfigSet_PortContainer_0_KEY3,PORT_PIN_IN);
    Port_SetPinDirection(PortConfigSet_PortContainer_0_KEY4,PORT_PIN_IN);
    
    printf("Port initialized successfully, USART output is normal.\r\n");
    
    while (1)
    {
        if (Dio_ReadChannel(BT1_PIN) == 0)
        {
            LED1_ON;
            LED2_OFF;
            LED3_OFF;
            LED4_OFF;
            printf("LED1 on!\r\n\r\n");
            
            while(Dio_ReadChannel(BT1_PIN) == 0);
        }
        else if (Dio_ReadChannel(BT2_PIN) == 0)
        {
            LED1_OFF;
            LED2_ON;
            LED3_OFF;
            LED4_OFF;
            printf("LED2 on!\r\n\r\n");
            
            while(Dio_ReadChannel(BT2_PIN) == 0);
        }
        else if (Dio_ReadChannel(BT3_PIN) == 0)
        {
            LED1_OFF;
            LED2_OFF;
            LED3_ON;
            LED4_OFF;
            printf("LED3 on!\r\n\r\n");
            
            while(Dio_ReadChannel(BT3_PIN) == 0);
        }
        else if (Dio_ReadChannel(BT4_PIN) == 0)
        {
            LED1_OFF;
            LED2_OFF;
            LED3_OFF;
            LED4_ON;
            printf("LED4 on!\r\n\r\n");
            
            while(Dio_ReadChannel(BT4_PIN) == 0);
        }
    }
}

#if defined (__CC_ARM) || defined (__ICCARM__) || (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       ch:  The characters that need to be send.
*
* @param       *f:  pointer to a FILE that can recording all information
*              needed to control a stream
*
* @retval      The characters that need to be send.
*
* @note
*/
int fputc(int ch, FILE* f)
{
    int reVal = 0;
    reVal = Usart_PutRedefinition(USART1_ID,&ch);
    
    return reVal;
}

/* If configuring the full library using versions after IAR 9.30,
   it is necessary to implement the _write function for printf redirection.*/
size_t __write(int handle,const unsigned char *buf,size_t bufSize)
{
    int32 tempCh = 0;
    for(int i=0; i<bufSize; i++)
    {
        Usart_PutRedefinition(USART1_ID,&tempCh);
    }
    return bufSize;
}

#elif defined (__GNUC__)

/* For Arm Cross GCC: Eclipse */
int _write(int fd,const unsigned char *buf,int len)
{
    int32 tempCh = 0;
    for(int i=0; i<len; i++)
    {
        int32 tempCh = (int32)buf[i];
        Usart_PutRedefinition(USART1_ID,&tempCh);
    }
    return len;
}

#endif
/**@} end of group Template_Functions */
/**@} end of group Template */
/**@} end of group Examples */
