/**
  *****************************************************************************
  * 					ûжϴͳһ
  *****************************************************************************
  *
  * @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 u8 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, u32 Bank, u32 Status)
{
	u32 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++;
		}
	}
}
