i am currently experimenting touch events rate. it seems i cant get more than about 60 touch per second with 1 finger. Raise up to 120 with 2 fingers. Is this 60 Hz limit to be expected? Why so? There is no link between screen refresh rate and touch events rate?
Thanks for your help.
here is my test code. The number of touches per second appears on top right.
Draw with 1 finger. It is optimized for speed.
-- quick draw
backingMode(RETAINED)
-- Use this function to perform your initial setup
local list = {}
local cleanTime = 0
local cleanNeeded = false
function setup()
tween.delay(1,function() firstPass = true end)
end
-- This function gets called once every fram
local p0
function draw()
if firstPass then background(50, 40, 39, 255) firstPass=false end
local c = color(198, 198, 198, 255)
fill( c )
rectMode(CENTER)
stroke( c )
w = 4
strokeWidth(w)
for i,p in ipairs(list) do
if p.state == MOVING then line(p0.x,p0.y,p.x,p.y) p0 = p
elseif p.state == ENDED then line(p0.x,p0.y,p.x,p.y) p0 = p
elseif p.state == BEGAN then rect(p.x,p.y,w,w) p0 = p
end
end
list = {}
if cleanNeeded and ElapsedTime > cleanTime then clean() end
end
local timeout
function clean()
collectgarbage()
cleanNeeded = false
local x,y = WIDTH-50,HEIGHT-50
fill(255, 0, 0, 255)
rect(x,y,50,50)
fill(0, 0, 0, 255)
textMode(CENTER)
font("AmericanTypewriter-Bold")
fontSize(25)
text(tostring(speed),x,y)
if timeout then
tween.stop(timeout)
end
timeout = tween.delay(1,function()
fill(0, 0, 0, 255)
rect(x,y,50,50)
fill(255, 0, 0, 255)
text(tostring(speed),x,y)
timeout = nil
end)
end
local start,count
local floor = math.floor
function touched(t)
list[#list+1] = {x=t.x, y=t.y, state=t.state}
cleanTime = ElapsedTime + 0.1
cleanNeeded = true
if t.state == BEGAN then start = ElapsedTime; count = 1
elseif t.state == MOVING then count = count + 1
elseif t.state == ENDED then
count = count + 1
speed = floor( count/(ElapsedTime-start) )
end
end
@Jmv38 Here’s a version I have. I get about 60 or less per 60 draw cycles (1 second). I save a touch count in a table and then show the table when your finger is lifted. The majority of draw cycles have 1 touch event, while some have 0 or some have 2.
function setup()
output.clear()
tab={}
start=true
c,t1=0,0
end
function draw()
background(40, 40, 50)
fill(255)
text("Drag you finger around the screen for a few seconds",WIDTH/2,HEIGHT/2)
if #tab<60 and start then
table.insert(tab,c)
c=0
end
if #tab>=60 then
start=false
end
end
function touched(t)
if t.state==BEGAN then
setup()
end
if t.state==MOVING then
c=c+1
if start then
t1=t1+1
end
end
if t.state==ENDED then
for a,b in pairs(tab) do
print("draw cycle "..a,"touch count "..b)
end
print("total "..t1)
end
end
@Jmv38 I took my code above and added a 1,000,000 math.sqrt loop in the draw cycle. I was getting about 8 to 10 touch cycles per draw cycle. So it looks like the draw and touch routines both run about 60 cycles per second and are independent of each other.
thanks @Dave1707 these results are similar to mine. Does anyone know why the touch cant go faster than 60hz?
I must admit I’m wondering why you would want the touch event to fire faster than 60 times per second. What’s the application?
On the other hand, the discussion has made me think again about how I think about the draw/touch cycle so has been very, very useful! Thanks.
Just for comparision, here’s my test code.
-- TouchSpeed
-- Use this function to perform your initial setup
function setup()
print("Hello World!")
parameter.watch("trate")
parameter.watch("tlcount")
tcount = 0
touches = {}
backingMode(RETAINED)
end
-- This function gets called once every frame
function draw()
trate = math.floor(tcount/DeltaTime)
tlcount = tcount
-- This sets a dark background color
-- background(40, 40, 50,50)
fill(96, 62, 62, 76)
rect(0,0,WIDTH,HEIGHT)
-- This sets the line thickness
-- strokeWidth(5)
fill(203, 200, 104, 255)
for k,v in ipairs(touches) do
fill(k*4, 200, 104, 255)
ellipse(v.x,v.y,5)
end
local s = os.time()
while (os.time() - s < 1) do
math.sin(1000)
end
-- Do your drawing here
touches = {}
tcount = 0
end
function touched(t)
tcount = tcount + 1
table.insert(touches,t)
end
@loopspace hand writing. All the current apps are forcing me to write slowly otherwise they miss touch positions. So i want the fastest possible. 60hz is pretty good, but some sections may still look a bit blocky some times. I just want to know where the limit is.