You can ignore all the unfinished motion predicition code. I was going to use that for touchscreen capture point compression, but I did not finish it... (Yes, the deeper derivatives of motion are named after a breakfast cereal. In real life motion control, people rarely go deeper than snap vector analysis.)
This is working code that was not cleaned up and not documented for publication. It writes binary directly to the framebuffer. It reads binary directly from the device input events. C code can do this same stuff. It is here to show you how you can read touchscreen data directly from the touchscreen device. This method is similar to how the myts onscreen terminal program reads keyboard events on a K3.
This old version contains debug code that displays all the touchscreen events, which I removed from later (better) code that I use elsewhere. I thought this old (unfinished "dirty") version with the debug stuff would be more helpful in this case, so here it is...
PHP Code:
#!/bin/sh
# Touchpaint2 (v2.12b)
# copyright (c) 2012 geekmaster
# MIT license.
self=$0; cmd=$1; shift 2
[[ $cmd ]] && $cmd $@ && exit
init() {
E3="echo"
# E3="nop"
E2="echo"
E1="echo"
DN=/dev/null; DZ=/dev/zero; DF=/dev/fb0
DT=$(for i in /dev/input/event*;do evtest info $i|grep zforce>$DN&&echo $i&&break;done)
set $(eips -i|grep line_length); VX=$4
W='\xff';W=$W$W$W$W$W$W;W=$W$W$W$W$W;W=$W$W$W$W$W;W=$W$W$W$W # 600px white
eips -c -f # flash clear
X1=0 Y1=0 X2=0 Y2=0 X3=0 Y3=0 x1s=0 y1s0=0 x2s=0 y2s=0 B=0 S=0 Q=0 R='\x0d'
eips 44 38 '+~~~~~'
eips 44 39 '| Exit'
lm=0 # line mode
# lm=1
# gs=8 # granularity size
# gs=16 # granularity size
# gs=12 # granularity size
# gs=5 # granularity size
gs=6 # granularity size
# gs=2 # granularity size
# gs=1 # granularity size
GG=$((gs*gs))
H=""
}
nop() {
:
}
#=================================
# abortsh - abort subprocess shell
#---------------------------------
abortsh() {
set $(ps a|grep "sh -c hexdump $DT")
kill -HUP $1 # abort shell
Q=1 # signal quit
}
# Bresenham's line algorithm
line() {
local x0=$1 y0=$2 x1=$3 y1=$4 dx dy sx sy
if [[ $x1 -gt $x0 ]]; then dx=$((x1-x0)) sx=1; else dx=$((x0-x1)) sx=-1; fi
if [[ $y1 -gt $y0 ]]; then ny=$((y0-y1)) sy=1; else ny=$((y1-y0)) sy=-1; fi
err=$((dx+ny))
while :; do # to do: use count="segment width"
dd if=$DZ of=$DF bs=1 count=1 seek=$((y0*VX+x0)) 2>$DN # black
# dd if=$DZ of=$DF bs=1 count=8 seek=$((y0*VX+x0-4)) 2>$DN # black
# echo $W|dd of=$DF bs=1 count=$((DX+1)) seek=$((Y0*VX+X)) 2>$DN # white
[[ $x0 -eq $x1 && $y0 -eq $y1 ]] && break
e2=$((2*err))
[[ $e2 -gt $ny ]] && err=$((err+ny)) x0=$((x0+sx))
[[ $e2 -lt $dx ]] && err=$((err+dx)) y0=$((y0+sy))
done
# eips '' # temp for testing
}
lineart() {
for i in $(seq 0 1 50); do
j=$((50-i))
line $((i*12)) 0 599 $((i*16)); eips ''
line 599 $((i*16)) $((j*12)) 799; eips ''
line $((j*12)) 799 0 $((j*16)); eips ''
line 0 $((j*16)) $((i*12)) 0; eips ''
done
}
paint() {
script -c "hexdump $DT" $DN|while [[ $Q -eq 0 ]]; do
read L; [[ ${#L} -ne 48 ]]&& break # rare script exit output
# $E3 -ne "${L:0:$((${#L}-1))}"
set $L; local T=$((0x$6)) C=$((0x$7)) V=$((0x$8))
case $T in
0) $E3 -ne " (Sync)"
$E3 -ne " [S=$S X1=$X1 Y1=$Y1 X2=$X2 Y2=$Y2]"
### $E1 -e "SYN $S $x1s $y1s $X1 $Y1 $R"
if [[ $B -eq 9 ]]; then
$E3 -e " exit button pressed"
abortsh # abort subprocess shell
Q=1 # signal quit
elif [[ $X1 -ge 540 && $Y1 -ge 750 ]]; then # on exit button
# elif [[ $X1 -ge 135 && $Y1 -ge 188 ]]; then # on exit button
if [[ $B -ne 1 ]]; then # was not pressed
eips -v 44 38 '+~~~~~'
eips -v 44 39 '| Exit'
fi
B=1
$E3 " on exit button"
else # off exit button
if [[ $B -ne 2 ]]; then # was not released
eips 44 38 '+~~~~~'
eips 44 39 '| Exit'
fi
B=2
$E3 -ne " off exit button"
fi
DX=$((X1-x1s)); [[ $DX -lt 0 ]]&& DX=$((0-DX))
DY=$((Y1-y1s)); [[ $DY -lt 0 ]]&& DY=$((0-DY))
RR=$((DX*DX+DY*DY))
# if [[ $DX -ge $gs -o $DY -ge $gs ]];then
if [[ $RR -ge $GG ]];then
# $E2 -e "MOV $S $X1 $Y1 $X2 $Y2 $R"
# H="$H:MOV $S $X1 $Y1 $X2 $Y2 $R\x0a"
# echo -e "$H $R"
if [[ $lm -eq 1 -a $x1s -ne 0 -a $y1s -ne 0 ]];then
line $x1s $y1s $X1 $Y1
# line $((x1s*4)) $((y1s*4)) $((X1*4)) $((Y1*4))
else
dd if=$DZ of=$DF bs=1 count=5 seek=$((Y1*VX+X1-4)) 2>$DN # black
# dd if=$DZ of=$DF bs=1 count=8 seek=$((Y1*VX*4+X1*4-4)) 2>$DN # black
fi
# [[ $x1s -ne 0 -a $y1s -ne 0 ]]&& line $((x1s/4)) $((y1s/4)) $((X1/4)) $((Y1/4))
dd if=$DZ of=$DF bs=1 count=1 seek=$((Y1/5*VX+X1/5)) 2>$DN # black
# sliding-window position history:
x1h3=x1h2 y1h3=y1h2;x1h2=x1h1 y1h2=y1h1;x1h1=x1s y1h1=y1s;x1h=X1 y1h=Y1
# 1st derivative: velocity vectors:
x1v2=$((x1h2-x1h3)) y1v2=$((y1h2-y1h4));x1v1=$((x1h1-x1h2)) y1v1=$((y1h1-y1h2))
x1v=$((x1h-x1h1)) y1v=$((y1h-y1h1)) # velocity
# 2nd derivative: acceleration vectors:
x1a1=$((x1v2-x1v1)) y1a1=$((y1v2-y1v1));x1a=$((x1v-x1v1)) y1a=$((y1v-y1v1)) # accel
# 3rd derivative: jerk vectors:
x1t=$((x1a-x1a1)) y1t=$((y1a-y1a1)) # thrust
# jerk position interpolation:
### (more code here)
# 4th derivative: snap
# 5th derivative: crackle
# 6th derivative: pop
x1s=$X1 y1s=$Y1; eips '';
fi
# DX=$((X2-x2s)); [[ $DX -lt 0 ]]&& DX=$((0-DX))
# DY=$((Y2-y2s)); [[ $DY -lt 0 ]]&& DY=$((0-DY))
# if [[ $DX -ge 4 -o $DY -ge 4 ]];then
# if [[ $x2s -ne 0 && $y2s -ne 0 ]];then
# line $x2s $y2s $X2 $Y2
# fi
# dd if=$DZ of=$DF bs=1 count=8 seek=$((Y2*VX+X2-4)) 2>$DN # black
# x2s=$X2 y2s=$Y2; eips '';
# fi
;;
1) $E3 -ne " (Key)"
case $C in
0) $E3 -ne " (Reserved)" ;;
1) $E3 -ne " (Esc)" ;;
325) $E3 -ne " (ToolFinger)" ;;
330) $E3 -ne " (Touch)" ;;
V=$((V*200/4096*4))
$E3 -ne " Y=$V [S=$S X1=$X1 X2=$X2]"
if [[ $S -eq 0 ]];then Y1=$V;
elif [[ $S -eq 1 ]];then Y2=$V
else echo -ne " *** slot $S *** "
fi
;;
57) $E3 -ne " (MT Tracking ID) [0-255]"
$E3 -ne " [X1=$X1 Y1=$Y1 X2=$X2 Y2=$Y2]"
case $V in
0) x1s=0 y1s=0; eips -v ' '; $E3 -ne " touch 1 ($((S+1)))"
;;
1) x2s=0 y2s=0; eips -v ' '; $E3 -ne " touch 2 ($((S+1)))"
;;
65535) eips ' '; $E3 -ne " release $((S+1))"
# $E1 -e "REL $S $X1 $Y1 $X2 $Y2 $R"
# H="$H:REL $S $X1 $Y1 $X2 $Y2 $R\x0a"
# echo -e "$H $R"
####abortsh # abort subprocess shell
[[ $B -eq 1 ]]&& B=9 # signal exit button pressed
;;
*) $E3 -ne " (Value $V)"
;;
esac
# if [[ $V -eq 0 ]]; then x1s=0 y1s=0; eips -v ' '; $E3 -ne " touch 1 ($((S+1)))"
# elif [[ $V -eq 1 ]]; then x2s=0 y2s=0; eips -v ' '; $E3 -ne " touch 2 ($((S+1)))"
# else eips ' '; $E3 -ne " release $((S+1))"
# [[ $B -eq 1 ]]&& B=9 # signal exit button pressed
# fi
;;
*) $E3 -ne " (Code $C)"
;;
esac
;;
*) $E3 -ne " (Type $T)"
;;
esac
$E3 -e $R
done
# $E3 "touchscreen processing complete"
eips 44 38 ' '
eips 44 39 ' '
}
killall -STOP Xorg cvm # pause framework
init
#line 100 45 500 750; eips ''
#line 130 690 580 75; eips ''
#for i in $(seq 1 1 20);do
paint
#done
#paint #|while :;do
# read L
# echo "($L)"
#done
#lineart
killall -CONT cvm Xorg # resume framework
Run it on the K5. If the touchscreen is not working (screensaver mode) tap the power button to wake it up.
I have newer code that supports two finger drawing, pinch and stretch, and limited rotate, but that is missing the debug output messages that you probably could use. I may publish a newer version soon.
I plan to port this stuff to C soon. It is all from the "eink algorithmic art" collection (but not yet published).
You should exit by pressing the "Exit" button in the lower right corner to resume Xorg and cvm. My newer versions of this script intercept ctrl-c and do a proper shutdown that includes resuming the framework. Not this old one.
P.S. Firmware for 5.0.x (and perhaps 5.1.x) has a bug when you are moving two fingers on the screen: when rotating, when both fingers cross the same X coord or Y coord, sometimes the OTHER coordinates get swapped (and stay swapped). This makes it return coordinates for other untouched coordinates of the bounding box. That is not a problem for pinch and stretch though.
ToDo: You can see in the myts program from matan, how to capture an input device so that the touches (in this case) are not passed through to the framework. I like to do touchscreen stuff in diags, where there is a menu item that stops looking at the touchscreen events until the menu button is pressed. (See the old Touchpaint 1.0 thread for details).