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

//修改引脚编号
#define  GPIO_ID1  SR_GPIO83
#define  GPIO_ID2  SR_GPIO109
#define  GPIO_ID3  SR_GPIO108
#define  GPIO_ID4  SR_GPIO107


static uint8_t gpioFlag3 = 0, gpioArg3 = 0;
static uint8_t gpioFlag4 = 0, gpioArg4 = 0;

// 中断回调
static void UserGpioCbFun3(void *args)
{
	// 中断里不要调用朝阳写的打印函数SR_LOG
	
	gpioFlag3 = 1;
	gpioArg3 = *((uint8_t *)args);
}
static SR_GPIO_IRQ_CB_T gpioCb_3;

static void UserGpioCbFun4(void *args)
{
	// 中断里不要调用朝阳写的打印函数SR_LOG

	gpioFlag4 = 1;
	gpioArg4 = *((uint8_t *)args);
}
static SR_GPIO_IRQ_CB_T gpioCb_4;


static int GpioExmp(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;

	config.gpioMode = SR_GPIO_OUTPUT;		  // 输出
	config.pull = SR_GPIO_NP;				      // 无上下拉
	config.initLvl = SR_GPIO_LOW;			    // 初始低电平
	ret = SR_HalGpioInit(GPIO_ID1, &config); // 初始化
	SR_LOG(INFO, "1: gpio init, ret=%d\r\n", ret);

	config.gpioMode = SR_GPIO_INPUT;		  // 输入
	config.pull = SR_GPIO_PU;				      // 上拉
	ret = SR_HalGpioInit(GPIO_ID2, &config); // 初始化
	SR_LOG(INFO, "2: gpio init, ret=%d\r\n", ret);

	config.pull = SR_GPIO_PU;			        // 上拉
	config.gpioMode = SR_GPIO_IRQ;		    // 中断模式
	config.irqMode = SR_GPIO_IRQ_FALLING; // 下降沿触发
	gpioCb_3.func = UserGpioCbFun3;	      // 中断回调
	gpioCb_3.args = &intArg;			        // 中断回调参数
	config.irqCb = &gpioCb_3;
	ret = SR_HalGpioInit(GPIO_ID3, &config); // 初始化
	SR_LOG(INFO, "3:gpio exit init, ret=%d\r\n", ret);

	config.pull = SR_GPIO_PU;			        // 上拉
	config.gpioMode = SR_GPIO_IRQ;		    // 中断模式
	config.irqMode = SR_GPIO_IRQ_FALLING; // 下降沿触发
	gpioCb_4.func = UserGpioCbFun4;	      // 中断回调
	gpioCb_4.args = &charArg;			        // 中断回调参数
	config.irqCb = &gpioCb_4;
	ret = SR_HalGpioInit(GPIO_ID4, &config); // 初始化
	SR_LOG(INFO, "4:gpio exit init, ret=%d\r\n", ret);

	while (gShellThreadRun)
	{
		if (gpioFlag3) // 中断，打印提示
		{
			gpioFlag3 = 0;
			SR_LOG(INFO, "[GPIO] gpio_cb3 enter!,args=%d", gpioArg3);
		}

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

		/***间隔1s读写电平*****/
		if (count == 100)
		{
			// 设置电平
			SR_HalGpioWrite(GPIO_ID1, SR_GPIO_HIGH);
			// 读取电平
			level1 = SR_HalGpioRead(GPIO_ID1);
			level2 = SR_HalGpioRead(GPIO_ID2);
			SR_LOG(INFO, "level1=%d   level2=%d!\r\n", level1, level2);
		}
		else if (count == 200)
		{
			count = 0;

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

		count++;
		SR_OsDelay(10);
	}
	
	return 0;

}
SR_SHELL_CMD_EXPORT(gpioExmp, GpioExmp);

