
/* 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.
 */

/*************<start>******************/


/*************<include>****************/
#include "gpio.h"

/*************<macro>******************/
/*Define the button action, with low level pin indicating release status and high level pin indicating press status.*/
#define KEY_PRESS           (GPIO_LEVEL_LOW)
#define KEY_RELEASE         (GPIO_LEVEL_HIGH)

#define GET_K3_STS()      (GPIO_GetPinLevel(K3_PORT, K3_PIN))
#define GET_K4_STS()      (GPIO_GetPinLevel(K4_PORT, K4_PIN))


/*************<enum>*******************/


/*************<union>******************/


/*************<struct>*****************/


/*************<variable>***************/
uint8_t     g_getK3Sts;       /*K3 button status*/
uint8_t     g_getK4Sts;       /*K4 button status*/

uint8_t     g_scanKeyTime;      /*Scan button interval time*/


/*************<prototype>**************/

/**
* @prototype GPIO_LedInit(void)
*
* @param[in] void
* @return    void
*
* @brief     Initialize LED pins.
*/
void GPIO_LedInit(void)
{
    /*Initialize the pin function. If the pin defaults to GPIO after power on, the initialization step can be omitted
    If some pins are powered on and default to non GPIO, their function must be selected as GPIO in order to be used as GPIO.*/
    GPIO_SetFunc(LED1_PORT, LED1_PIN, GPIO_FUN0);/*Function reuse selection.*/
    GPIO_SetFunc(LED2_PORT, LED2_PIN, GPIO_FUN0);

    /*Set the LED pin to GPIO output.*/
    GPIO_SetDir(LED1_PORT, LED1_PIN, GPIO_OUT);
    GPIO_SetDir(LED2_PORT, LED2_PIN, GPIO_OUT);

    /*Default LED on when powered on.*/
    LED1_ON;
    LED2_ON;
}

/**
* @prototype GPIO_KeyInit(void)
*
* @param[in] void
* @return    void
*
* @brief     Initialize key pins.
*/
void GPIO_KeyInit(void)
{
    /*Initialize the pin function. If the pin defaults to GPIO after power on, the initialization step can be omitted
    If some pins are powered on and default to non GPIO, their function must be selected as GPIO in order to be used as GPIO.*/
    GPIO_SetFunc(K3_PORT, K3_PIN, GPIO_FUN0);/*Function reuse selection.*/
    GPIO_SetFunc(K4_PORT, K4_PIN, GPIO_FUN0);

    /*Set the key pin as input to detect key action.*/
    GPIO_SetDir(K3_PORT, K3_PIN, GPIO_IN);
    GPIO_SetDir(K4_PORT, K4_PIN, GPIO_IN);

    /*Enable button pin pull-up.*/
    GPIO_SetPullup(K3_PORT, K3_PIN, ENABLE);
    GPIO_SetPullup(K4_PORT, K4_PIN, ENABLE);

    /*Initialize control variables.*/
    g_scanKeyTime = 0;
    g_getK3Sts  = KEY_RELEASE;
    g_getK4Sts  = KEY_RELEASE;
}

/**
* @prototype GPIO_ScanKeyInput(void)
*
* @param[in] void
* @return    void
*
* @brief     Scan button input.
*/
void GPIO_ScanKeyInput(void)
{
    if (g_scanKeyTime >= SCAN_KEY_INTVL)/*Timing cycle arrives, scan button action.*/
    {
        g_scanKeyTime = 0;

        /*Last time K3 was memorized as released and the current K3 was pressed, perform a second confirmation and re memorize the K3 state.*/
        if ((g_getK3Sts == KEY_RELEASE) && (GET_K3_STS() == KEY_PRESS))
        {
            mdelay(10);
            if (GET_K3_STS() == KEY_PRESS)
            {
                g_getK3Sts = KEY_PRESS;


            }
        }
        /*Last time K3 was pressed and the current K3 was released, update the memory K3 status to ensure the effectiveness of each button action.*/
        else if ((g_getK3Sts == KEY_PRESS) && (GET_K3_STS() == KEY_RELEASE))
        {
            g_getK3Sts = KEY_RELEASE;
        }

        /*Last time the K4 state was released and the current K4 was pressed, perform a second confirmation and re remember the K4 state.*/
        if ((g_getK4Sts == KEY_RELEASE) && (GET_K4_STS() == KEY_PRESS))
        {
            mdelay(10);
            if (GET_K4_STS() == KEY_PRESS)
            {
                g_getK4Sts = KEY_PRESS;


            }
        }
        /*Last time K4 was pressed and the current K4 was released, update the memory K4 status to ensure the effectiveness of each button action.*/
        else if ((g_getK4Sts == KEY_PRESS) && (GET_K4_STS() == KEY_RELEASE))
        {
            g_getK4Sts = KEY_RELEASE;
        }
    }
}


/*************<end>********************/
