⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 app_bluescreen_demo.c

📁 just uploading the file to get a new downlad for AD7843
💻 C
字号:
#include "app_config.h"

extern void delay_1ms(unsigned int n);

extern volatile unsigned char time_1ms_cnt;
extern volatile unsigned char time_10ms_cnt;
extern volatile unsigned char time_100ms_cnt;
extern volatile unsigned char time_500ms_cnt;

#define TC_TP_NUM		5

#define CALIB_TEST_TIME	2

#ifdef TS_ORN_PORTRAIT
const int tc_tpx[TC_TP_NUM] = {10,65,120,175,230};
const int tc_tpy[TC_TP_NUM] = {10,85,160,235,310};
#else
const int tc_tpy[TC_TP_NUM] = {10,65,120,175,230};
const int tc_tpx[TC_TP_NUM] = {10,85,160,235,310};
#endif

const long chalfx = TS_SIZE_X/2;
const long chalfy = TS_SIZE_Y/2;	

//x = ((TCGetH() - ccx)*cm1x)/cm2x + chalfx;
long ccx;
long cm1x;
long cm2x;

//y = ((TCGetV() - ccy)*cm1y)/cm2y + chalfy;
long ccy;
long cm1y;
long cm2y;

long cal_posx(unsigned short x);
long cal_posy(unsigned short y);

unsigned char filp_horizontal = 0;
unsigned char last_pen_check = 0;
so_pos_t hpos,vpos;

pstatus_t p_stat;

#define EP_SIZE				8192
//last 128 bytes of EEPROM is used for configuration data 
#define EP_AD_CFG			EP_SIZE - 128
//below are address with EP_AD_CFG offset
#define EP_AD_CFG_STATUS	0

#define CAL_VALID_PORTRAIT	0x01
#define CAL_VALID_LANDSCAPE	0x02

#ifdef TS_ORN_PORTRAIT
#define CAL_VALID			0x01
#define TCGetH()			TCGetX()
#define TCGetV()			TCGetY()
#define EP_AD_CFG_CX		1
#define EP_AD_CFG_M1X		5
#define EP_AD_CFG_M2X		9
#define EP_AD_CFG_CY		13
#define EP_AD_CFG_M1Y		17
#define EP_AD_CFG_M2Y		21
#else
#define CAL_VALID			0x02
#define TCGetH()			TCGetY()
#define TCGetV()			TCGetX()
#define EP_AD_CFG_CX		13
#define EP_AD_CFG_M1X		17
#define EP_AD_CFG_M2X		21
#define EP_AD_CFG_CY		1
#define EP_AD_CFG_M1Y		5
#define EP_AD_CFG_M2Y		9
#endif

long cal_posx(unsigned short x)
{
	long buf;
	buf = x - ccx;
	buf = buf*cm1x;
	buf = buf/cm2x;
	buf = buf + chalfx;
#ifdef TS_ORN_PORTRAIT
	if (filp_horizontal)
		return (TS_SIZE_X - buf);
	else
#endif
		return (buf);
}

long cal_posy(unsigned short y)
{
	long buf;
	buf = y - ccy;
	buf = buf*cm1y;
	buf = buf/cm2y;
	buf = buf + chalfy;
#ifndef TS_ORN_PORTRAIT
	if (filp_horizontal)
		return (TS_SIZE_Y - buf);
	else
#endif
		return (buf);
}

void AppScanPen(void)
{
	unsigned long x,y;
	unsigned short tc_x_buf,tc_y_buf;
	unsigned short tc_x_max,tc_y_max;
	unsigned short tc_x_min,tc_y_min;
	if (TCIsPenOn())
	{
		TCRead();
		tc_x_buf = TCGetH();
		tc_y_buf = TCGetV();

		tc_x_max = TCGetH();
		tc_y_max = TCGetV();
		tc_x_min = TCGetH();
		tc_y_min = TCGetV();

		TCRead();
		tc_x_buf += TCGetH();
		tc_y_buf += TCGetV();

		if (TCGetH() > tc_x_max)
			tc_x_max = TCGetH();
		if (TCGetH() < tc_x_min)
			tc_x_min = TCGetH();
		if (TCGetV() > tc_y_max)
			tc_y_max = TCGetV();
		if (TCGetV() < tc_y_min)
			tc_y_min = TCGetV();

		TCRead();
		tc_x_buf += TCGetH();
		tc_y_buf += TCGetV();

		if (TCGetH() > tc_x_max)
			tc_x_max = TCGetH();
		if (TCGetH() < tc_x_min)
			tc_x_min = TCGetH();
		if (TCGetV() > tc_y_max)
			tc_y_max = TCGetV();
		if (TCGetV() < tc_y_min)
			tc_y_min = TCGetV();

		TCRead();
		tc_x_buf += TCGetH();
		tc_y_buf += TCGetV();

		if (TCGetH() > tc_x_max)
			tc_x_max = TCGetH();
		if (TCGetH() < tc_x_min)
			tc_x_min = TCGetH();
		if (TCGetV() > tc_y_max)
			tc_y_max = TCGetV();
		if (TCGetV() < tc_y_min)
			tc_y_min = TCGetV();

		tc_x_buf -= tc_x_max;
		tc_x_buf -= tc_x_min;
		tc_y_buf -= tc_y_max;
		tc_y_buf -= tc_y_min;

		tc_x_buf = tc_x_buf >> 1;
		tc_y_buf = tc_y_buf >> 1;

		if (TCIsPenOn())
		{
			x = cal_posx(tc_x_buf);
			y = cal_posy(tc_y_buf);

			if (last_pen_check)
			{
				p_stat = PST_HOLD;
			}
			else
			{
				p_stat = PST_DOWN;
			}

			if ((x) && (y) && (x < TS_SIZE_X) && (y < TS_SIZE_Y))
			{
				hpos = x;
				vpos = y;
				ScrObjDo(hpos,vpos,p_stat);
			}

			last_pen_check = 1;
		}
	}
	else
	{
		if (last_pen_check)
		{
			p_stat = PST_UP;
		}
		else
		{
			p_stat = PST_NOTFOUND;		
		}

		ScrObjDo(hpos,vpos,p_stat);

		last_pen_check = 0;
	}
}

void AppTask1ms(void)
{
}

void AppTask10ms(void)
{
	AppScanPen();
}

void AppTask100ms(void)
{
	ScrObjTask100ms();
}

void AppTask500ms(void)
{
	if (time_500ms_cnt & 1)
	{	
		Clrb(LED1_PRTC,LED1_PIN);
	}
	else
	{
		Setb(LED1_PRTS,LED1_PIN);
	}
}

void AppCalibrateScreen(void)
{
	unsigned char i2c_buf[32];
	long dat_buf;
	int buf;

	int x[TC_TP_NUM*TC_TP_NUM];
	int y[TC_TP_NUM*TC_TP_NUM];

    long sum_xx = 0;
    long sum_yy = 0;
    long sum_fx = 0;
    long sum_fy = 0;
    long sum_fxx = 0;
    long sum_fyy = 0;

	float cx;
	float mx;
	float cy;
	float my;

    float mx_new;
    float my_new;

	float buf_mod1;

	long fx[TC_TP_NUM*TC_TP_NUM][CALIB_TEST_TIME];
	long fy[TC_TP_NUM*TC_TP_NUM][CALIB_TEST_TIME];
	long xx[TC_TP_NUM*TC_TP_NUM];
	long yy[TC_TP_NUM*TC_TP_NUM];
	long fxx[TC_TP_NUM*TC_TP_NUM][CALIB_TEST_TIME];
	long fyy[TC_TP_NUM*TC_TP_NUM][CALIB_TEST_TIME];

	unsigned char i,j,k;
	unsigned char good_press;

	EPRead(EP_AD_CFG,25,i2c_buf);

	if ((i2c_buf[EP_FIRST_READ] == CAL_VALID_PORTRAIT) || 
		(i2c_buf[EP_FIRST_READ] == CAL_VALID_LANDSCAPE)) //check for valid touch screen calibration data
	{
#ifdef TS_ORN_PORTRAIT
		TSLCDFillRect(0,TS_SIZE_X-1,0,TS_SIZE_Y-1,TS_COL_BLUE,TS_MODE_NORMAL);
		TSLCDPrintStr( 8,0,"       Press the screen",TS_MODE_FULL);
		TSLCDPrintStr(10,0,"        to recalibrate",TS_MODE_FULL);
		TSLCDPrintStr(12,0,"       within 3 seconds",TS_MODE_FULL);
#else
		TSLCDFillRect(0,TS_SIZE_X-1,0,TS_SIZE_Y-1,TS_COL_BLUE,TS_MODE_NORMAL);
		TSLCDPrintStr( 5,0,"            Press the screen",TS_MODE_FULL);
		TSLCDPrintStr( 7,0,"             to recalibrate",TS_MODE_FULL);
		TSLCDPrintStr( 9,0,"            within 3 seconds",TS_MODE_FULL);
#endif
		k = 0;

		for (i=0;i<3;i++)
		{
#ifdef TS_ORN_PORTRAIT
			TSLCDPrintCh(12, 14, '3' - i, TS_MODE_FULL);
#else
			TSLCDPrintCh(9, 19, '3' - i, TS_MODE_FULL);
#endif
			for (j=0;j<10;j++)
			{
				if (TCIsPenOn())
				{
					k = 1;
					break;
				}
				delay_1ms(100);
			}
			if (k)
				break;
		}

		if (k == 0) //use old data
		{
			if (i2c_buf[EP_FIRST_READ] != CAL_VALID)
				filp_horizontal = 1;

			ccx = 0;
			for (i=0;i<4;i++) //sizeof long = 4
			{
				dat_buf = i2c_buf[EP_FIRST_READ + EP_AD_CFG_CX + i];
				dat_buf	<<= i*8;
				ccx |= dat_buf;
			}
		
			cm1x = 0;
			for (i=0;i<4;i++) //sizeof long = 4
			{
				dat_buf = i2c_buf[EP_FIRST_READ + EP_AD_CFG_M1X + i];
				dat_buf	<<= i*8;
				cm1x |= dat_buf;
			}
		
			cm2x = 0;
			for (i=0;i<4;i++) //sizeof long = 4
			{
				dat_buf = i2c_buf[EP_FIRST_READ + EP_AD_CFG_M2X + i];
				dat_buf	<<= i*8;
				cm2x |= dat_buf;
			}
		
			ccy = 0;
			for (i=0;i<4;i++) //sizeof long = 4
			{
				dat_buf = i2c_buf[EP_FIRST_READ + EP_AD_CFG_CY + i];
				dat_buf	<<= i*8;
				ccy |= dat_buf;
			}
		
			cm1y = 0;
			for (i=0;i<4;i++) //sizeof long = 4
			{
				dat_buf = i2c_buf[EP_FIRST_READ + EP_AD_CFG_M1Y + i];
				dat_buf	<<= i*8;
				cm1y |= dat_buf;
			}
		
			cm2y = 0;
			for (i=0;i<4;i++) //sizeof long = 4
			{
				dat_buf = i2c_buf[EP_FIRST_READ + EP_AD_CFG_M2Y + i];
				dat_buf	<<= i*8;
				cm2y |= dat_buf;
			}

			return;
		}
	}

	TSLCDFillRect(0,TS_SIZE_X-1,0,TS_SIZE_Y-1,TS_COL_BLUE,TS_MODE_NORMAL);

	say();
	delay_1ms(100);
	say();
	delay_1ms(100);

	saystr("\r\n");
	saystr("\r\n");
	delay_1ms(100);

	for (i=0; i<TC_TP_NUM; i++)
		for (j=0; j<TC_TP_NUM; j++)
		{
			sayhex(i);
			saych(',');
			sayhex(j);
			saych('=');
			saych(' ');
			say();
			delay_1ms(100);
			say();
			delay_1ms(100);
			say();
			delay_1ms(100);

			TSLCDFillRect(tc_tpx[i],tc_tpx[i],tc_tpy[j],tc_tpy[j],TS_COL_WHITE,TS_MODE_NORMAL);
			TSLCDSetOffset(tc_tpx[i]%8,tc_tpy[j]%16 - 8);
			if (i < 3)
			{
				TSLCDPrintStr(tc_tpy[j]/16,(tc_tpx[i]/8)+1,"<Press here",TS_MODE_FULL);
			}
			else
			{
			 	TSLCDPrintStr(tc_tpy[j]/16,(tc_tpx[i]/8)-12,"Press here>",TS_MODE_FULL);
			}

			for (k=0; k<CALIB_TEST_TIME; k++)
			{
				good_press = 0;
				while (!good_press)
				{
					while (!TCIsPenOn());
					TCRead();
					delay_1ms(50);
					if (TCIsPenOn())
					{
						TCRead();
						//delay_1ms(500);
						while (TCIsPenOn());
						good_press = 1;
					}
				}
				saych('<');
				sayhex16(TCGetH());
				fx[i*5+j][k] = TCGetH();
				saych(',');
				sayhex16(TCGetV());
				fy[i*5+j][k] = TCGetV();
				saych('>');
				saych(' ');
				say();
				delay_1ms(100);
				say();
				delay_1ms(100);
				say();
				delay_1ms(100);

			}
			if (i < 3)
			{
				TSLCDPrintStr(tc_tpy[j]/16,(tc_tpx[i]/8)+1,"           ",TS_MODE_FULL);
			}
			else
			{
			 	TSLCDPrintStr(tc_tpy[j]/16,(tc_tpx[i]/8)-12,"           ",TS_MODE_FULL);
			}
			TSLCDSetOffset(0,0);
			TSLCDFillRect(tc_tpx[i],tc_tpx[i],tc_tpy[j],tc_tpy[j],TS_COL_BLUE,TS_MODE_NORMAL);

			saystr("\r\n");
			say();
			delay_1ms(100);
			say();
			delay_1ms(100);
		}

	for (i=0;i<TC_TP_NUM*TC_TP_NUM;i++)
	{
        x[i] = tc_tpx[i/5] - chalfx;
        y[i] = tc_tpy[i%5] - chalfy;
        xx[i] = x[i] * x[i];
        yy[i] = y[i] * y[i];
        fxx[i][0] = fx[i][0] * x[i];
        fxx[i][1] = fx[i][1] * x[i];
        fyy[i][0] = fy[i][0] * y[i];
        fyy[i][1] = fy[i][1] * y[i];
	}

    for (i = 0; i < TC_TP_NUM*TC_TP_NUM; i++)
    {
        sum_xx += (xx[i] + xx[i]);
        sum_yy += (yy[i] + yy[i]);
        sum_fx += (fx[i][0] + fx[i][1]);
        sum_fy += (fy[i][0] + fy[i][1]);
        sum_fxx += (fxx[i][0] + fxx[i][1]);
        sum_fyy += (fyy[i][0] + fyy[i][1]);
    }

    cx = ((float)sum_fx) / ((float)(TC_TP_NUM * TC_TP_NUM * CALIB_TEST_TIME));
    mx = ((float)sum_fxx) / ((float)sum_xx);
    cy = ((float)sum_fy) / ((float)(TC_TP_NUM * TC_TP_NUM * CALIB_TEST_TIME));
    my = ((float)sum_fyy) / ((float)sum_yy);

	ccx = (long)cx;
	ccy = (long)cy;

    mx_new = mx;
    my_new = my;

	buf_mod1 = mx_new - (long)mx_new;
	if (buf_mod1 < 0)
		buf_mod1 = -buf_mod1;
	buf = 1;

    while ((buf_mod1 > 0.05) && (buf_mod1 < 0.95))
    {
        buf++;
        mx_new = mx*((float) buf);
		buf_mod1 = mx_new - (long)mx_new;
		if (buf_mod1 < 0)
			buf_mod1 = -buf_mod1;
    }

	cm1x = buf;
    cm2x = (long)(mx * buf);

	buf_mod1 = my_new - (long)my_new;
	if (buf_mod1 < 0)
		buf_mod1 = -buf_mod1;
    buf = 1;

    while ((buf_mod1 > 0.05) && (buf_mod1 < 0.95))
    {
        buf++;
        my_new = my*((float)buf);
		buf_mod1 = my_new - (long)my_new;
		if (buf_mod1 < 0)
			buf_mod1 = -buf_mod1;
    }

    cm1y = buf;
    cm2y = (long)(my * buf);

	i2c_buf[EP_FIRST_WRITE] = CAL_VALID; //EP_AD_CFG_STATUS --> config data valid

	dat_buf = ccx;
	for (i=0;i<4;i++) //sizeof long = 4
	{
		i2c_buf[EP_FIRST_WRITE + EP_AD_CFG_CX + i] = dat_buf;
		dat_buf >>= 8;
	}

	dat_buf = cm1x;
	for (i=0;i<4;i++) //sizeof long = 4
	{
		i2c_buf[EP_FIRST_WRITE + EP_AD_CFG_M1X + i] = dat_buf;
		dat_buf >>= 8;
	}

	dat_buf = cm2x;
	for (i=0;i<4;i++) //sizeof long = 4
	{
		i2c_buf[EP_FIRST_WRITE + EP_AD_CFG_M2X + i] = dat_buf;
		dat_buf >>= 8;
	}

	dat_buf = ccy;
	for (i=0;i<4;i++) //sizeof long = 4
	{
		i2c_buf[EP_FIRST_WRITE + EP_AD_CFG_CY + i] = dat_buf;
		dat_buf >>= 8;
	}

	dat_buf = cm1y;
	for (i=0;i<4;i++) //sizeof long = 4
	{
		i2c_buf[EP_FIRST_WRITE + EP_AD_CFG_M1Y + i] = dat_buf;
		dat_buf >>= 8;
	}

	dat_buf = cm2y;
	for (i=0;i<4;i++) //sizeof long = 4
	{
		i2c_buf[EP_FIRST_WRITE + EP_AD_CFG_M2Y + i] = dat_buf;
		dat_buf >>= 8;
	}

	EPWrite(EP_AD_CFG,25,i2c_buf);
	delay_1ms(100);
}

void AppEPTest(void)
{
	unsigned char i2c_buf[5];
	unsigned char ep_test_val;

	EPRead(EP_SIZE-8,1,i2c_buf);
	ep_test_val = i2c_buf[EP_FIRST_READ];
	ep_test_val++;

	i2c_buf[EP_FIRST_WRITE] = ep_test_val;
	EPWrite(EP_SIZE-8,1,i2c_buf);
	delay_1ms(100);

	i2c_buf[EP_FIRST_READ] = ~ep_test_val;

	EPRead(EP_SIZE-8,1,i2c_buf);
	if (i2c_buf[EP_FIRST_READ] == ep_test_val)
	{
		saystr("\r\nEEPROM test passed\r\n");
	}
	else
	{
		saystr("\r\nEEPROM test failed\r\n");
	}
}

void AppInit(void)
{
	Clrb(BL_CTL_PRTC,BL_CTL_PIN);

  	SerialInit(CONS_SER,8,0x34);				// 57600 
	EPInit(24,24); 								// ~357 kHz

	Orb(VS_CS_DPRT,VS_CS_PIN);
	Setb(VS_CS_PRTS,VS_CS_PIN);
	Orb(VS_DCS_DPRT,VS_DCS_PIN);
	Setb(VS_DCS_PRTS,VS_DCS_PIN);
	Orb(TC_CS_DPRT,TC_CS_PIN);
	Setb(TC_CS_PRTS,TC_CS_PIN);

//	AppSDInit();
	TCInit();

	delay_1ms(100);

//	AppEPTest();
//	VSInit();

	Setb(BL_CTL_PRTS,BL_CTL_PIN);
	TSLCDRst();
	TSLCDInit();
	TSLCDSetFontColor(TS_COL_WHITE);
	TSLCDSetBackColor(TS_COL_BLUE);

	AppCalibrateScreen();
	ScrObjInit(KeyPadScreenInit);

//	saystr("\r\nBlue Screen MP3 player demo\r\n");
}

void AppRun(void)
{
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -