#include "platform.h"
#include "hal_headfile.h"
#include "system.h"
#include "debug.h"
#include "config.h"
#include "key.h"
uint8_t check_adc_num(uint16_t adc_value);


const uint8_t KET_TAB[] = {KEY_NULL, KEY_UP, KEY_LEFT, KEY_OK, KEY_DOWN, KEY_RIGHT};
/**
 * This function is for Initializing lradc key and gpio key.
 *
 * @param : none.
 * @return: none.
 */
void key_init(void)
{
#ifdef  ADC_KEY
    hal_lradc_init(ADC_KEY_PORT);
    hal_lradc_set_samplemode(ADC_KEY_PORT, 1); //continue  mode
    hal_lradc_enable(ADC_KEY_PORT, ENABLE);
#endif
#ifdef  GPIO_KEY

    if (user_param.mute_switch_pin != 6)
    {
        gpio_key_init(user_param.mute_switch_pin);
    }

    if (user_param.function_switch_pin != 6)
    {
        gpio_key_init(user_param.function_switch_pin);
    }

#endif
}



void gpio_key_init(gpio_pin_t pin)
{
    hal_gpio_set_direction(pin, GPIO_DIRECTION_INPUT);
    hal_gpio_set_pull(pin, GPIO_PULL_UP);
    hal_gpio_pinmux_set_function(pin, GPIO_MUXSEL_GPIO);
}

int8_t get_gpio_hold_1_status(gpio_pin_t pin)
{
    static uint8_t gpio_key_cnt = 0;
    static uint8_t status_bak = 255;
    uint8_t temp =  hal_gpio_read(pin);

    if (status_bak != temp)
    {
        if (gpio_key_cnt < 250)
        {
            gpio_key_cnt++;
        }

        if (gpio_key_cnt == 4)
        {
            status_bak = temp;
            return temp;
        }
    }
    else
    {
        gpio_key_cnt = 0;
    }

    return -1;
}

int8_t get_gpio_hold_2_status(gpio_pin_t pin)
{
    static uint8_t gpio_key_cnt = 0;
    static uint8_t status_bak = 255;
    uint8_t temp =  hal_gpio_read(pin);

    if (status_bak != temp)
    {
        if (gpio_key_cnt < 250)
        {
            gpio_key_cnt++;
        }

        if (gpio_key_cnt == 4)
        {
            status_bak = temp;
            return temp;
        }
    }
    else
    {
        gpio_key_cnt = 0;
    }

    return -1;
}



uint8_t get_adc_key(void)
{
    static uint8_t key_time_cnt;
    uint16_t temp, key_num = 0;
    temp = hal_lradc_getvalue(ADC_KEY_PORT);

    if (temp > BUTTON_EVENT_DEBONSE) //大于去抖值，初步判定按键被按下
    {
        if (key_time_cnt < 250)
        {
            key_time_cnt ++;
        }

        if (key_time_cnt == 4) //short press
        {
            debug("dac:%d\r\n", temp);
            key_num = check_adc_num(temp);
        }
        else if (key_time_cnt == 100) //long press
        {
            key_time_cnt -= 15;
            key_num = check_adc_num(temp);
        }
    }
    else
    {
        key_time_cnt = 0;
    }

    return KET_TAB[key_num];
}


uint8_t check_adc_num(uint16_t adc_value)
{
    if (adc_value >= (BUTTON_EVENT_BUTTION1 - BUTTON_ERROR_STEP) && adc_value <= (BUTTON_EVENT_BUTTION1 + BUTTON_ERROR_STEP))
    {
        return 1;
    }
    else if (adc_value >= (BUTTON_EVENT_BUTTION2 - BUTTON_ERROR_STEP) && adc_value <= (BUTTON_EVENT_BUTTION2 + BUTTON_ERROR_STEP))
    {
        return 2;
    }
    else if (adc_value >= (BUTTON_EVENT_BUTTION3 - BUTTON_ERROR_STEP) && adc_value <= (BUTTON_EVENT_BUTTION3 + BUTTON_ERROR_STEP))
    {
        return 3;
    }
    else if (adc_value >= (BUTTON_EVENT_BUTTION4 - BUTTON_ERROR_STEP) && adc_value <= (BUTTON_EVENT_BUTTION4 + BUTTON_ERROR_STEP))
    {
        return 4;
    }
    else if (adc_value >= (BUTTON_EVENT_BUTTION5 - BUTTON_ERROR_STEP) && adc_value <= (BUTTON_EVENT_BUTTION5 + BUTTON_ERROR_STEP))
    {
        return 5;
    }
    else
    {
        return 0;
    }
}





uint8_t key_scan(void)
{

    int8_t hold_key_num = 0;
#ifdef   ADC_KEY
    uint8_t adkey_num = 0;
    adkey_num = get_adc_key();//获取adc按键值

    if (adkey_num != KEY_NULL)
    {
        if (adkey_num == KEY_UP)
        {
            debug("key_up\r\n");
        }
        else if (adkey_num == KEY_DOWN)
        {
            debug("key_down\r\n");
        }
        else if (adkey_num == KEY_LEFT)
        {
            debug("key_left\r\n");
        }
        else if (adkey_num == KEY_RIGHT)
        {
            debug("key_right\r\n");
        }
        else if (adkey_num == KEY_OK)
        {
            debug("key_ok\r\n");
        }
    }

#endif
#ifdef  GPIO_KEY

    if (user_param.function_switch_pin != 6)
    {
        hold_key_num = get_gpio_hold_1_status(user_param.function_switch_pin);

        if (HOLD_KEY_NULL != hold_key_num)
        {
            debug("hold_key_num = %d\r\n", hold_key_num);
            return hold_key_num + HOLD_KEY1_DOWN;
        }
    }

    if (user_param.mute_switch_pin != 6)
    {
        hold_key_num = get_gpio_hold_2_status(user_param.mute_switch_pin);

        if (HOLD_KEY_NULL != hold_key_num)
        {
            debug("hold_key_num = %d\r\n", hold_key_num);
            return hold_key_num + HOLD_KEY2_DOWN;
        }
    }

#endif
    return KEY_NULL;
}


