/**
  *****************************************************************************
  * 					存放用户中断处理函数，方便统一处理
  *****************************************************************************
  *
  * @File   : ISR.c
  * @By     : Sun
  * @Version: V0.5
  * @Date   : 2022 / 06 / 01
  * @Shop	: https://xiaomeige.taobao.com/
  *
  *****************************************************************************
**/

#include "ISR.h"
#include "BMP_WR.h"
#include "LCD.h"

volatile uint8_t Cnt = 0;
char Pic_Name[30];

//声明各个进程的标志位
extern uint8_t Flag_DrawWave;		//绘制波形，10ms一次
extern uint8_t Flag_DrawGrid;		//绘制网格，20ms一次
extern uint8_t Flag_TouchScan;		//触摸扫描，30ms一次
extern uint8_t Flag_Refresh_Val;	//刷新电压和频率数值，500ms一次
extern uint8_t Flag_Refresh_Time;	//刷新时间，500ms一次

//各个时间标志位
static uint8_t Flag_20ms = 0;
static uint8_t Flag_30ms = 0;
static uint8_t Flag_50ms = 0;
static uint8_t Flag_100ms = 0;
static uint8_t Flag_200ms = 0;
static uint8_t Flag_500ms = 0;

/**
  *****************************************************
  * @brief	私有定时器中断处理程序
  * @tag	本函数用来处理私有定时器中断，在内部加入用户程序即可
  *****************************************************
**/
void ScuTimer_IRQ_Handler(void *CallBackRef)
{
	/* ↓↓↓用户处理↓↓↓ */
    //以下通过计时器控制每个进程的触发间隔，启动对应的标志位
    Flag_DrawWave = 1;

    if(Flag_20ms >= 1) {
    	Flag_20ms = 0;
    	Flag_DrawGrid = 1;
    }
    else
    	Flag_20ms++;

    if(Flag_30ms >= 2) {
    	Flag_30ms = 0;
    	Flag_TouchScan = 1;
    }
    else
    	Flag_30ms++;

    if(Flag_50ms >= 4) {
    	Flag_50ms = 0;

    }
    else
    	Flag_50ms++;

    if(Flag_100ms >= 9) {
    	Flag_100ms = 0;

    }
    else
    	Flag_100ms++;

    if(Flag_200ms >= 19) {
    	Flag_200ms = 0;

    }
    else
    	Flag_200ms++;

    if(Flag_500ms >= 49) {
    	Flag_500ms = 0;
    	Flag_Refresh_Val = 1;
    	Flag_Refresh_Time = 1;
    }
    else
    	Flag_500ms++;

	/* ↑↑↑结束处理↑↑↑ */
    XScuTimer_ClearInterruptStatus(&ScuTimer);
}

void PS_GPIO_IRQ_Handler(void *CallBackRef, uint32_t Bank, uint32_t Status)
{
	uint32_t Key_State;

//	//添加判断来消除其它Bank的干扰
//	if(Bank == 1) {
//		usleep(10000);	//按键消抖10ms
//		Key_State = XGpioPs_ReadPin(&GpioPs, KEY_S1);
//		if(Key_State == 0) {
//			//截图，存入SD卡
//			printf("Start Writing BMP...\n");
//			sprintf(Pic_Name,"Screenshot(%02d).bmp",Cnt);
//		    bmp_write(Pic_Name, (char *)&BMODE_800x480, (char *)FrameBuffer_Addr);
//		    printf("Write BMP To \"Screenshot(%02d).bmp\"!\n",Cnt);
//		    Cnt++;
//		}
//	}
}
