Posted 17 March 2015 - 02:13 AM
I have more of a request than a question.
I would like someone to explain to me (and anyone else who doesnt know) how to proplerly use the parallel api in a sitaution involving user input (such as os.pullEventRaw).
I have a simple program that i am developing for (advanced) turtles which needs to accept user input (via mouse) at the same time as running a prgram in the background, which includes displaying to the screen. At times the screen display will be "flashing" different colors due to a specific mode selected. The rate of the flashing in my program is controlled by the sleep() functon, which i am sure is the culprit of my problem.
Here is my current most basic code. When the "button" are clicked the are supposed to blink red and black.
I hope you can help me understand this better. If you are going to say Google it … please don't. I have. It hasn't helped. Or maybe I'm not phrasing my problem in the correct way to get an answer from google.
I would like someone to explain to me (and anyone else who doesnt know) how to proplerly use the parallel api in a sitaution involving user input (such as os.pullEventRaw).
I have a simple program that i am developing for (advanced) turtles which needs to accept user input (via mouse) at the same time as running a prgram in the background, which includes displaying to the screen. At times the screen display will be "flashing" different colors due to a specific mode selected. The rate of the flashing in my program is controlled by the sleep() functon, which i am sure is the culprit of my problem.
Here is my current most basic code. When the "button" are clicked the are supposed to blink red and black.
Spoiler
t = {}
t[1] = {["sX"] = 3,["eX"] = 18,["sY"] = 3,["eY"] = 5,["BsX"] = 6,["BsY"] = 4,["text"] = "NOT SYNCED",["BsX2"] = 8,["BsY2"] = 4,["text2"] = "SYNCED",["G2G"] = true}
t[2] = {["sX"] = 22,["eX"] = 37,["sY"] = 3,["eY"] = 5,["BsX"] = 25,["BsY"] = 4,["text"] = "ELT ACTIVE",["BsX2"] = 24,["BsY2"] = 4,["text2"] = "ELT INACTIVE",["G2G"] = false}
t[3] = {["sX"] = 3,["eX"] = 18,["sY"] = 9,["eY"] = 11,["BsX"] = 8,["BsY"] = 10,["text"] = "PAUSED",["BsX2"] = 6,["BsY2"] = 10,["text2"] = "NOT PAUSED",["G2G"] = true}
tick = 0
function drawDesktop()
term.setBackgroundColor(128)
term.clear()
term.setTextColor(1)
for i = 1,3 do
if t[i]["G2G"] == false then
if tick >= 0 and tick <= 4 then
term.setBackgroundColor(16384)
else
term.setBackgroundColor(32768)
end
else
term.setBackgroundColor(8192)
end
for ii = t[i]["sY"],t[i]["eY"] do
for iii = t[i]["sX"],t[i]["eX"] do
term.setCursorPos(iii,ii)
write(" ")
end
end
if t[i]["G2G"] == false then
term.setCursorPos(t[i]["BsX"],t[i]["BsY"])
write(t[i]["text"])
else
term.setCursorPos(t[i]["BsX2"],t[i]["BsY2"])
write(t[i]["text2"])
end
end
term.setCursorPos(1,1)
return
end
function toggle(toggleInt)
if t[toggleInt]["G2G"] == false then
t[toggleInt]["G2G"] = true
elseif t[toggleInt]["G2G"] == true then
t[toggleInt]["G2G"] = false
end
return
end
function mouseInput()
local event, button, X, Y = os.pullEventRaw()
if event == "mouse_click" then
for i=1,3 do
if X >= t[i]["sX"] and X <= t[i]["eX"] and Y >= t[i]["sY"] and Y <= t[i]["eY"] and button ==1 then
toggle(i)
end
end
end
end
function desktop()
drawDesktop()
sleep(.1)
tick = tick+1
if tick == 10 then
tick = 0
end
end
while true do
parallel.waitForAny(mouseInput, desktop)
end
I hope you can help me understand this better. If you are going to say Google it … please don't. I have. It hasn't helped. Or maybe I'm not phrasing my problem in the correct way to get an answer from google.