'-----------------------------------------------------------------
'name : TFT_Touch.bas
'compiler: Quick Basic
'author: slyt
'website: http://avr.cnta.net
'date: 2009-09-10
'-----------------------------------------------------------------
DECLARE SUB delay (dt AS SINGLE)
SCREEN 12
CLS
CLEAR
OPEN "COM1:9600,N,8,1,BIN" FOR
INPUT AS #1
ON COM(1) GOSUB COM1HANDDLE
COM(1) OFF
COM(1) ON
DIM x AS INTEGER
DIM y AS INTEGER
DIM a(4) AS INTEGER
DIM b(4) AS INTEGER
DIM aa(4) AS INTEGER
DIM bb(4) AS INTEGER
DIM intrun AS INTEGER
'调入屏幕坐标与触摸屏值换算参数
OPEN "para.ini" FOR INPUT AS #2
LINE INPUT #2, para$
kx = VAL(para$)
LINE INPUT #2, para$
ky = VAL(para$)
LINE INPUT #2, para$
dx = VAL(para$)
LINE INPUT #2, para$
dy = VAL(para$)
CLOSE #2
DO
ak$ = INKEY$
ak$ = UCASE$(ak$)
SELECT CASE ak$
CASE " "
CLS
CASE CHR$(27)
SCREEN 0
END
CASE "C" '校正坐标转换参数,在屏幕角依次显示十字
a(1) = 10: b(1) = 10
a(2) = 10: b(2) = 460
a(3) = 630: b(3) = 10
a(4) = 630: b(4) =
460
LOCATE 5, 1
FOR i = 1 TO 4
LINE (a(i) - 10,
b(i))-(a(i) + 10, b(i)), 15
LINE (a(i), b(i)
- 10)-(a(i), b(i) + 10), 15
intrun = 0
DO
LOOP UNTIL intrun
= 1
aa(i) = x: bb(i)
= y
SOUND 1200, 1
PRINT i, x, y
delay .5
inrun = 0
COM(1) OFF
delay .5
LINE (a(i) - 10,
b(i))-(a(i) + 10, b(i)), 0
LINE (a(i), b(i)
- 10)-(a(i), b(i) + 10), 0
COM(1) ON
NEXT
CLS
'换算参数
daa = (aa(3) + aa(4)
- aa(1) - aa(2)) / 2
dbb = (bb(2) + bb(4)
- bb(1) - bb(3)) / 2
da = a(3) - a(1)
db = b(2) - b(1)
kx = da / daa
ky = db / dbb
dx = a(1) - kx *
(aa(1) + aa(2)) / 2
dx = dx + a(3) - kx *
(aa(3) + aa(4)) / 2
dx = dx / 2
dy = b(1) - ky *
(bb(1) + bb(3)) / 2
dy = dy + b(2) - ky *
(bb(2) + bb(4)) / 2
dy = dy / 2
'存储参数
OPEN "para.ini" FOR
OUTPUT AS #2
PRINT #2, STR$(kx)
PRINT #2, STR$(ky)
PRINT #2, STR$(dx)
PRINT #2, STR$(dy)
CLOSE #2
intrun = 0
CASE ELSE '在触摸位置画圆点
IF intrun = 1 THEN
intrun = 0
px = kx * x + dx
py = ky * y + dy
'colr% = CINT(RND
* 15)
colr% = 15
CIRCLE (px, py),
3, colr%
PAINT (px, py),
colr%
END IF
END SELECT
LOOP UNTIL ak$ = CHR$(27)
END
'串口接收中断服务程序
COM1HANDDLE:
intrun = 1
COM(1) OFF
LINE INPUT #1, dz$
dz$ = RIGHT$(dz$, 8)
xy$ = LEFT$(dz$, 4)
x = VAL(xy$)
xy$ = RIGHT$(dz$, 4)
y = VAL(xy$)
COM(1) ON
RETURN
END
SUB delay (dt AS SINGLE)
t0 = TIMER
DO
t1 = TIMER - t0
LOOP UNTIL t1 < 0 OR t1 > dt
END SUB |