/* 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) 2024. 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: V3.1.0  AC7840x  Demo
 ******************************************************************************/

/*!
 * @file gpio.c
 *
 * @brief This file provides gpio functions.
 */

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

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

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

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

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

/* =========================  Functions definition  ========================= */

/*!
 * @brief  初始化LED引脚
 *
 * @param[in] none
 * @return none
 */
void GPIO_LedInit(void)
{
    /*初始化引脚功能，有部分引脚上电默认为非GPIO，必须选择其功能为GPIO才能作为GPIO使用*/
    GPIO_DRV_SetMuxModeSel(LED3_PORT, LED3_PIN, PORT_MUX_AS_GPIO); /*功能复用选择*/
    GPIO_DRV_SetMuxModeSel(LED4_PORT, LED4_PIN, PORT_MUX_AS_GPIO);
    GPIO_DRV_SetMuxModeSel(RGBLED_R_PORT, RGBLED_R_PIN, PORT_MUX_AS_GPIO);
    GPIO_DRV_SetMuxModeSel(RGBLED_G_PORT, RGBLED_G_PIN, PORT_MUX_AS_GPIO);
    GPIO_DRV_SetMuxModeSel(RGBLED_B_PORT, RGBLED_B_PIN, PORT_MUX_AS_GPIO);

    GPIO_DRV_SetPinDirection(LED3_GPIO, LED3_PIN, GPIO_OUTPUT_DIRECTION); /*设置GPIO为输出*/
    GPIO_DRV_SetPinDirection(LED4_GPIO, LED4_PIN, GPIO_OUTPUT_DIRECTION);
    GPIO_DRV_SetPinDirection(RGBLED_R_GPIO, RGBLED_R_PIN, GPIO_OUTPUT_DIRECTION);
    GPIO_DRV_SetPinDirection(RGBLED_G_GPIO, RGBLED_G_PIN, GPIO_OUTPUT_DIRECTION);
    GPIO_DRV_SetPinDirection(RGBLED_B_GPIO, RGBLED_B_PIN, GPIO_OUTPUT_DIRECTION);

    /*上电默认LED关闭*/
    LED3_OFF;
    LED4_OFF;
}

/*!
 * @brief  初始化按键引脚
 *
 * @param[in] none
 * @return none
 */
void GPIO_KeyInit(void)
{
    /*初始化引脚功能，有部分引脚上电默认为非GPIO，必须选择其功能为GPIO才能作为GPIO使用*/
    GPIO_DRV_SetMuxModeSel(KEY3_PORT, KEY3_PIN, PORT_MUX_AS_GPIO); /*功能复用选择*/
    GPIO_DRV_SetMuxModeSel(KEY4_PORT, KEY4_PIN, PORT_MUX_AS_GPIO);

    GPIO_DRV_SetPinDirection(KEY3_GPIO, KEY3_PIN, GPIO_INPUT_DIRECTION); /*设置按键引脚为输入*/
    GPIO_DRV_SetPinDirection(KEY4_GPIO, KEY4_PIN, GPIO_INPUT_DIRECTION);

    GPIO_DRV_SetPullSel(KEY3_PORT, KEY3_PIN, PORT_INTERNAL_PULL_UP_ENABLED); /*使能按键引脚的上拉*/
    GPIO_DRV_SetPullSel(KEY4_PORT, KEY4_PIN, PORT_INTERNAL_PULL_UP_ENABLED);
}

/*!
 * @brief  按键函数
 *
 * @param[in] none
 * @return  keyvalue
 */
uint8_t Get_Key4Value(void)
{
    uint8_t keyvalue = 0;
    if (GET_K4_STS() == 0)
    {
        OSIF_TimeDelay(10);
        if (GET_K4_STS() == 0)
        {
            while (GET_K4_STS() == 0);
            keyvalue = 1;
        }
    }
    return keyvalue;
}

/*!
 * @brief  RGB彩灯翻转
 *
 * @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;

    default:
        break;
    }
}

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