#include "sr_log.h"
#include "sr_shell.h"
#include "sr_gpio.h"
#include "sr_osal.h"



//#define TEST_LED_RED_ID   SR_GPIO15   // PA15
//#define TEST_LED_GREEN_ID SR_GPIO23   // PB3
#define TEST_KEY_ID       SR_GPIO4     // PA4
//#define TEST_PULSE_ID     SR_GPIO11   // PA11


static uint8_t keyFlag = 0, keyArg = 0;
static uint8_t pulseFlag = 0, pulseArg = 0;
// KEY中断回调
static void UserKeyGpioCb(void *args)
{
    // 中断里不要调用打印函数SR_LOG

    keyFlag = 1;
    keyArg = *((uint8_t *)args);
}
static SR_GPIO_IRQ_CB_T keyGpioCb;


#if   0
// PULSE中断回调
static void UserPauseGpioCb(void *args)
{
    // 中断里不要调用打印函数SR_LOG

    pulseFlag = 1;
    pulseArg = *((uint8_t *)args);
}
static SR_GPIO_IRQ_CB_T pauseGpioCb;
#endif

static int N32GpioExmp(int argc, char *argv[])
{
    int ret = -1, count = 0;
    uint32_t level1 = 0, level2 = 0;
    SR_GPIO_CFG_T config = {0};
    uint8_t intArg = 100;
    char charArg = 'A';

    gShellThreadRun = 1;

#if 0
    config.gpioMode = SR_GPIO_OUTPUT;               // 输出
    config.pull = SR_GPIO_NP;                       // 无上下拉
    config.initLvl = SR_GPIO_HIGH;                  // 初始高电平
    ret = SR_HalGpioInit(TEST_LED_RED_ID, &config); // 初始化
    SR_LOG(INFO, "1: led red gpio init, ret=%d\r\n", ret);

    ret = SR_HalGpioInit(TEST_LED_GREEN_ID, &config); // 初始化
    SR_LOG(INFO, "2: led green gpio init, ret=%d\r\n", ret);
#endif
    
    
#if 0
	config.gpioMode = SR_GPIO_INPUT;		  // 输入
	config.pull = SR_GPIO_NP;				  // 
	ret = SR_HalGpioInit(TEST_KEY_ID, &config);  // 初始化
	SR_LOG(INFO, "2: key init, ret=%d\r\n", ret);
#endif

    config.pull = SR_GPIO_NP;            // 上拉
    config.gpioMode = SR_GPIO_IRQ;       // 中断模式
    config.irqMode = SR_GPIO_IRQ_DOUBLE; // 双沿触发
    keyGpioCb.func = UserKeyGpioCb;      // 中断回调
    keyGpioCb.args = &intArg;            // 中断回调参数
    config.irqCb = &keyGpioCb;
    ret = SR_HalGpioInit(TEST_KEY_ID, &config); // 初始化
    SR_LOG(INFO, "3:key exti init, ret=%d\r\n", ret);

#if   0
	config.pull = SR_GPIO_PD;			       // 上拉
	config.gpioMode = SR_GPIO_IRQ;		       // 中断模式
	config.irqMode = SR_GPIO_IRQ_DOUBLE;      //  双沿触发
	pauseGpioCb.func = UserPauseGpioCb;	           // 中断回调
	pauseGpioCb.args = &charArg;			       // 中断回调参数
	config.irqCb = &pauseGpioCb;
	ret = SR_HalGpioInit(TEST_PULSE_ID, &config);   // 初始化
	SR_LOG(INFO, "4:pause exit init, ret=%d\r\n", ret);
        
#endif

    while (gShellThreadRun)
    {

        if (keyFlag) // 中断，打印提示
        {
            keyFlag = 0;
            SR_LOG(INFO, "[GPIO] key exti enter!,args=%d", keyArg);
        }

		if (pulseFlag) // 中断，打印提示
		{
			pulseFlag = 0;
			SR_LOG(INFO, "[GPIO] pulse exti enter!,args=%c", pulseArg);
		}

        /***间隔1s读写电平*****/
        if (count == 100)
        {
            // 设置电平
            //SR_HalGpioWrite(TEST_LED_RED_ID, SR_GPIO_HIGH);
            //(TEST_LED_GREEN_ID, SR_GPIO_LOW);
            // 读取电平
            //level1 = SR_HalGpioRead(TEST_LED_RED_ID);
            level2 = SR_HalGpioRead(TEST_KEY_ID);

            SR_LOG(INFO, "level1=%d   level2=%d!\r\n", level1, level2);
        }
        else if (count == 200)
        {
            count = 0;

            // 设置电平:翻转
            //SR_HalGpioWrite(TEST_LED_RED_ID, SR_GPIO_TOGGLE);
            //SR_HalGpioWrite(TEST_LED_GREEN_ID, SR_GPIO_TOGGLE);
            // 读取电平
            //level1 = SR_HalGpioRead(TEST_LED_RED_ID);
            level2 = SR_HalGpioRead(TEST_KEY_ID);
            SR_LOG(INFO, "level1=%d   level2=%d!\r\n", level1, level2);
        }

        count++;
        SR_Delay(10);
    }

    return 0;
}
SR_SHELL_CMD_EXPORT(n32GpioExmp, N32GpioExmp);



#if  0
void SR_AppHciInit(void)
{

  App_Key_Init();
  Svc_Lcd_Init(); 

}

void SR_AppHciPollTask(void)
{
    App_Key_Task();
    App_Lcd_Task();
}

#endif