📄 touch_screen.c
字号:
/**************************************************************
Touch Screen control
**************************************************************/
#include "test.h"
#include <string.h>
#include "S3C2410.h"
#include "Touch_Screen.h"
#define ADCPRS 39
#define ITERATION 5
static unsigned int samplebuf[ITERATION][2];
static int CurrSample = 0;
int TouchFlag = 0;
/**************************************************************
The Initial of Touch Screen
**************************************************************/
void Touch_Screen_Init(void)
{
rADCDLY = (30000); // ADC Start or Interval Delay
rADCCON = (1<<14)|(ADCPRS<<6)|(0<<3)|(0<<2)|(0<<1)|(0);
// Enable Prescaler,Prescaler,AIN5/7 fix,Normal,Disable read start,No operation
rADCTSC = (0<<8)|(1<<7)|(1<<6)|(0<<5)|(1<<4)|(0<<3)|(0<<2)|(3);//tark
// Down,YM:GND,YP:AIN5,XM:Hi-z,XP:AIN7,XP pullup En,Normal,Waiting for interrupt mode
}
//*************************************************************
void Touch_Screen_Off(void)
{
}
#define MAX_DIS 10
#define ABS(x) ((x) < 0 ? (-(x)) : (x))
static int lx,ly;
static int Button;
int MouseDown(void)
{
return Button;
}
void GetMouseLastPos(int* x,int* y)
{
*x = lx;
*y = ly;
}
int IsPtInRect(int x,int y,int rx,int ry,int rw,int rh)
{
if( x < rx || x > (rx+rw)
|| y < ry || y > (ry+rh) )
return 0;
return 1;
}
void Touch2Screen(int* x,int *y)
{
*x = TX2SX(*x);
*y = TY2SY(*y);
}
int GetMousePos(int* x,int* y)
{
int i;
int s1,s2,sub1,sub2;
int count = 1;
//if( TouchFlag & 0x01 )
count = ITERATION;
//Auto X-Position and Y-Position Read
rADCTSC=(1<<7)|(1<<6)|(0<<5)|(1<<4)|(1<<3)|(1<<2)|(0);
//[7] YM=GND, [6] YP is connected with AIN[5], [5] XM=Hi-Z, [4] XP is connected with AIN[7]
//[3] XP pull-up disable, [2] Auto(sequential) X/Y position conversion mode, [1:0] No operation mode
for(i=0;i< count;i++)
{
rADCTSC = (1<<7)|(1<<6)|(0<<5)|(1<<4)|(1<<3)|(1<<2)|(0);
rADCCON |= 0x1; //Start Auto conversion
while(rADCCON & 0x1) //Check if Enable_start is low
{
}
while(!(0x8000&rADCCON)) //Check ECFLG
{
}
samplebuf[CurrSample][0] = (0x3ff&rADCDAT0);
samplebuf[CurrSample][1] = (0x3ff&rADCDAT1);
CurrSample++;
if( CurrSample >= ITERATION )
CurrSample = 0;
}
// rADCTSC = (1<<7)|(1<<6)|(0<<5)|(1<<4)|(0<<3)|(0<<2)|(3);
s1 = s2 = 0;
for(i=0;i<ITERATION;i++)
{
s1 += samplebuf[i][0];
s2 += samplebuf[i][1];
}
s1 /= ITERATION;
s2 /= ITERATION;
for(i=0;i<ITERATION;i++)
{
sub1 = s1 - samplebuf[i][0];
sub2 = s2 - samplebuf[i][1];
// printf("%d- %d %d %d %d %d %d\r\n",
// i,s1,buf[i][0],sub1,s2,buf[i][1],sub2);
if( ABS(sub1) > MAX_DIS || ABS(sub2) > MAX_DIS )
{
if( Button > 0 )
Button = MS_RELEASING;
else
Button = MS_RELEASED;
*x = lx;
*y = ly;
return 0;
}
}
sub1 = s1 - lx;
sub2 = s2 - ly;
if( (ABS(sub1) + ABS(sub2)) < 2 )
{// 去抖动
return 0;
}
if( Button <= 0 )
{// 原来释放状态,现在则为按下状态
Button = MS_PRESSING;// 第1次按下
}
else
{
Button = MS_PRESSED; // 第N次按下
}
lx = s1;
ly = s2;
*x = s1;
*y = s2;
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -