/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2023. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */
/******************************************************************************
 * @version: V1.0.0 demo
 ******************************************************************************/

/*!
* @file gpio.c
*
* @brief This file contains the user gpio function..
*
*/

/* ===========================================  Includes  =========================================== */
#include "gpio.h"

/* ============================================  Define  ============================================ */

/* ===========================================  Typedef  ============================================ */

/* ==========================================  Variables  =========================================== */

/* ====================================  Functions declaration  ===================================== */

/* ======================================  Functions define  ======================================== */

/*!
* @brief GPIO callback
*
* @param[in] none
* @return none
*/
void EXTI_K3_Callback(void *device, uint32_t wpara, uint32_t lpara)
{
    if (wpara == KEY3_PIN)
    {
        LED1_TOGGLE;
        LED2_TOGGLE;
    }
}

/*!
* @brief  Init LED pin
*
* @param[in] none
* @return none
*/
void GPIO_LedInit(void)
{
    /* Select GPIO multiplexing function */
    GPIO_SetFunc(LED1_GPIO, LED1_PIN, GPIO_FUN0);
    GPIO_SetFunc(LED2_GPIO, LED2_PIN, GPIO_FUN0);
    GPIO_SetFunc(RGBLED_R_GPIO, RGBLED_R_PIN, GPIO_FUN0);
    GPIO_SetFunc(RGBLED_G_GPIO, RGBLED_G_PIN, GPIO_FUN0);
    GPIO_SetFunc(RGBLED_B_GPIO, RGBLED_B_PIN, GPIO_FUN0);

    /* Set the LED pin to GPIO output */
    GPIO_SetDir(LED1_GPIO, LED1_PIN, GPIO_OUT);
    GPIO_SetDir(LED2_GPIO, LED2_PIN, GPIO_OUT);
    GPIO_SetDir(RGBLED_R_GPIO, RGBLED_R_PIN, GPIO_OUT);
    GPIO_SetDir(RGBLED_G_GPIO, RGBLED_G_PIN, GPIO_OUT);
    GPIO_SetDir(RGBLED_B_GPIO, RGBLED_B_PIN, GPIO_OUT);

    LED1_ON;
    LED2_ON;
}

/*!
* @brief  Init Key pin
*
* @param[in] none
* @return none
*/
void GPIO_KeyInit(void)
{
    /* Select GPIO multiplexing function */
    GPIO_SetFunc(KEY3_GPIO, KEY3_PIN, GPIO_FUN0);
    GPIO_SetFunc(KEY4_GPIO, KEY4_PIN, GPIO_FUN0);

    /* Set the Key pin to GPIO input */
    GPIO_SetDir(KEY3_GPIO, KEY3_PIN, GPIO_IN);
    GPIO_SetDir(KEY4_GPIO, KEY4_PIN, GPIO_IN);

    /* Enable Key pin pull-up */
    GPIO_SetPullup(KEY3_GPIO, KEY3_PIN, ENABLE);
    GPIO_SetPullup(KEY4_GPIO, KEY4_PIN, ENABLE);

    GPIO_EnableExtInterrupt(KEY3_GPIO, KEY3_PIN, EXTI_TRIGGER_RISING); /* Enable GPIO rising edge interrupt */
    GPIO_SetCallback(KEY3_PIN, EXTI_K3_Callback);    /* Set GPIO callback */
}

/*!
* @brief  RGB Toggle
*
* @param[in] none
* @return none
*/
void RGB_Toggle(void)
{
    static uint8_t flag = 0;

    flag++;
    if (flag == 5)
    {
        flag = 1;
    }
    switch (flag)
    {
    case 1:
        RGBLED_R_ON;
        RGBLED_G_OFF;
        RGBLED_B_OFF;
        break;
    case 2:
        RGBLED_R_OFF;
        RGBLED_G_ON;
        RGBLED_B_OFF;
        break;
    case 3:
        RGBLED_R_OFF;
        RGBLED_G_OFF;
        RGBLED_B_ON;
        break;
    case 4:
        RGBLED_R_ON;
        RGBLED_G_ON;
        RGBLED_B_ON;
        break;
    }
    mdelay(200);
}

/* =============================================  EOF  ============================================== */
