/**
 * @file app_key.c
 * @brief tuyaos quick start demo
 * @version 1.0
 * @date 2022-02-18
 *
 * @copyright Copyright 2021-2031 Tuya Inc. All Rights Reserved.
 *
 */

/***********************************************************************
 ** INCLUDE                                                           **
 **********************************************************************/
#include "tal_log.h"
#include "tkl_gpio.h"
#include "tal_system.h"
#include "tal_thread.h"

#include "tuya_app_key.h"
#include "tuya_app_led.h"
#include "tuya_tal_2_tkl.h"
#include "tuya_iot_wifi_api.h"
#include "tuya_dp_process.h"

/***********************************************************************
 ** CONSTANT ( MACRO AND ENUM )                                       **
 **********************************************************************/
#define KEY_PIN 21
#define LONE_PRESS_TIME 3000 //long press time, uint: ms

/***********************************************************************
 ** STRUCT                                                            **
 **********************************************************************/

/***********************************************************************
 ** VARIABLE                                                          **
 **********************************************************************/

/***********************************************************************
 ** FUNCTON                                                           **
 **********************************************************************/

/**
* @brief key thread task, judge whether the key is pressed, long or short press
*
* @param[in] args: 
* @return none
*/
static void app_key_task(void* args)
{
    OPERATE_RET op_ret = OPRT_OK;
    TUYA_GPIO_LEVEL_E read_level = TUYA_GPIO_LEVEL_HIGH;
    UINT32_T time_start = 0, timer_end = 0;

    for (;;) {
        tal_gpio_read(KEY_PIN, &read_level);
        if (TUYA_GPIO_LEVEL_LOW == read_level) {
            tal_system_sleep(3);
            tal_gpio_read(KEY_PIN, &read_level);

            time_start = tal_system_get_millisecond();
            while (TUYA_GPIO_LEVEL_LOW == read_level) {
                tal_gpio_read(KEY_PIN, &read_level);
                tal_system_sleep(50);
            }
            timer_end = tal_system_get_millisecond();

            if (timer_end-time_start >= LONE_PRESS_TIME) {
                /* long press, remove device */
                op_ret = tuya_iot_wf_gw_unactive();
                if (op_ret != OPRT_OK) {
                    TAL_PR_ERR("long press tuya_iot_wf_gw_unactive error, %d", op_ret);
                    return;
                }
            } else if (timer_end-time_start>50) {
                /* normal press */
                if (get_led_status()) {
                    set_led_status(LED_OFF);
                } else {
                    set_led_status(LED_ON);
                }
                update_all_dp();
            }
        }
        tal_system_sleep(100);
    }
}

/**
* @brief key gpio init, creat key thread
*
* @param[in] none
* @return none
*/
void app_key_init(void)
{
    /* init key gpio */
    TUYA_GPIO_BASE_CFG_T key_cfg = {
        .mode = TUYA_GPIO_PULLUP,
        .direct = TUYA_GPIO_INPUT,
        .level = TUYA_GPIO_LEVEL_HIGH
    };

    tal_gpio_init(KEY_PIN, &key_cfg);

    /* key thread */
    THREAD_HANDLE key_task_handle;
    THREAD_CFG_T  thread_cfg = {
        .thrdname = "key_task",
        .priority = THREAD_PRIO_6,
        .stackDepth = 2048+1024
    };
    tal_thread_create_and_start(&key_task_handle, NULL, NULL, app_key_task, NULL, &thread_cfg);
}
