/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-05-06     lijian       the first version
 */
#include <inout.h>


IO_t IO;

//输入端口列表
rt_base_t input_pins[] = {
    GET_PIN(C, 2),
    GET_PIN(C, 3),
    GET_PIN(C, 4),
    GET_PIN(A, 5),
    GET_PIN(A, 6),
    GET_PIN(A, 7),
    GET_PIN(A, 8),
    GET_PIN(C, 9),

};

//输出端口列表
rt_base_t output_pins[] = {

    GET_PIN(B, 12),
    GET_PIN(B, 13),
    GET_PIN(B, 14),
    GET_PIN(B, 15),
    GET_PIN(C, 6),
    GET_PIN(C, 7),
    GET_PIN(C, 8),
    GET_PIN(A, 11),

};


/**
 * 端口中断回调函数
 * @param args
 */
void gpio_irq_callback(void *args)
{
    rt_base_t pin = (rt_base_t)(uintptr_t)args; // 获取触发中断的引脚号
    rt_kprintf("pin: %#x", pin);
    switch (pin)
    {
    case IN1:
        IO.IO_Data[0].Temp_Count++;
        //rt_kprintf("pin1:%d\n", IO.IO_Data[0].Temp_Count);
        break;
    case IN2:
        IO.IO_Data[1].Temp_Count++;
        //rt_kprintf("pin2:%d\n", IO.IO_Data[1].Temp_Count);
        break;
    case IN3:
        IO.IO_Data[2].Temp_Count++;
        //rt_kprintf("pin3:%d\n", IO.IO_Data[2].Temp_Count);
        break;
    case IN4:
        IO.IO_Data[3].Temp_Count++;
        //rt_kprintf("pin4:%d\n", IO.IO_Data[3].Temp_Count);
        break;
    case IN5:
        IO.IO_Data[4].Temp_Count++;
        //rt_kprintf("pin5:%d\n", IO.IO_Data[4].Temp_Count);
        break;
    case IN6:
        IO.IO_Data[5].Temp_Count++;
        //rt_kprintf("pin6:%d\n", IO.IO_Data[5].Temp_Count);
        break;
    case IN7:
        IO.IO_Data[6].Temp_Count++;
        //rt_kprintf("pin7:%d\n", IO.IO_Data[6].Temp_Count);
        break;
    case IN8:
        IO.IO_Data[7].Temp_Count++;
        //rt_kprintf("pin8:%d\n", IO.IO_Data[7].Temp_Count);
        break;
    default:
        break;
    }
}

//配置输入引脚
void configure_inpin(rt_base_t pin)
{
    // 设置引脚模式为上拉输入
    rt_pin_mode(pin, PIN_MODE_INPUT_PULLUP);

    rt_kprintf(" pin: %d  %#x\r\n", pin, pin);
    /* 绑定中断，下降沿模式，回调函数名为 */
    rt_pin_attach_irq(pin, PIN_IRQ_MODE_FALLING, gpio_irq_callback, (void *)(uintptr_t)pin);
    /* 使能中断 */
    rt_pin_irq_enable(pin, PIN_IRQ_ENABLE);
}

/**
 * 输入引脚初始化
 */
void gpio_input_init(void)
{
    rt_kprintf(" pin list:\r\n");

    for (rt_uint8_t var = 0; var < INPUT_NUM; var++)
    {
        configure_inpin(input_pins[var]);
    }
}

/**
 * 输出引脚默认为高电平
 * @param pin
 */
void configure_outpin(rt_base_t pin) {
    // 设置引脚模式为输出模式
    rt_pin_mode(pin, PIN_MODE_OUTPUT);
    // 默认为高电平
    rt_pin_write(pin, PIN_HIGH);
}

//输出引脚初始化
void gpio_output_init(void){
    for (rt_uint8_t var = 0; var < OUTPUT_NUM; var++) {
        configure_outpin(output_pins[var]);
    }
}

//获取端口电平触发次数
rt_uint32_t get_IO_count(rt_uint8_t io){
    return IO.IO_Data[io].Temp_Count;
}


/**
 * @brief 计算转速，500毫秒的中断次数乘2为1秒次数，1秒次数乘60为rpm单位转速
 * @param none
 * @return: None
 */
void updateSpeed(void)
{
    rt_uint16_t tmp_start = 0;
    rt_uint16_t tmp_end = 0;
    //rt_tick_t   tick_start      =  0;
    //rt_tick_t   tick_end        =  0;
    rt_uint16_t temp = 0;

    temp = IO.IO_Data[IO.IO_Flag.Speed_io].Speed;
    tmp_start = IO.IO_Data[IO.IO_Flag.Speed_io].Temp_Count;

    //tick_start = rt_tick_get_millisecond();
    rt_thread_mdelay(500);
    //tick_end = rt_tick_get_millisecond();

    tmp_end = IO.IO_Data[IO.IO_Flag.Speed_io].Temp_Count;

    if (tmp_end < tmp_start)
    {
        IO.IO_Data[IO.IO_Flag.Speed_io].Speed = (tmp_end + 65535 - tmp_start) * 120;
    }
    else
    {
        IO.IO_Data[IO.IO_Flag.Speed_io].Speed = (tmp_end - tmp_start) * 120;
    }

    if (IO.IO_Flag.log) {
        rt_kprintf("Speed: %d %d\n", IO.IO_Flag.Speed_io+1, IO.IO_Data[IO.IO_Flag.Speed_io].Speed);
    }

                    //            if(tmp_end < tmp_start){
                    //                IO.IO_Data[i].Temp_Speed = (tmp_end + 65535 - tmp_start) / ((tick_end - tick_start) / 1000);
                    //            }
                    //            else {
                    //                IO.IO_Data[i].Temp_Speed = (tmp_end - tmp_start) / ((tick_end - tick_start) / 1000);
                    //            }

    if (temp != IO.IO_Data[IO.IO_Flag.Speed_io].Speed)
    {
        IO.IO_Flag.SpeedUpdateFlag = 1;
    }
    else
    {
        IO.IO_Flag.SpeedUpdateFlag = 0;
    }

}

//控制台更改电平
void IO_key(int argc, char**argv){

    rt_uint8_t var = atoi(argv[1]);
    rt_uint8_t var2 = atoi(argv[2]);
    if (var2) {
        rt_pin_write(output_pins[var], PIN_HIGH);
    } else {
        rt_pin_write(output_pins[var], PIN_LOW);
    }
}

MSH_CMD_EXPORT(IO_key, chage IO);
