This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
TheS0m3b0dy's profile picture

[Lua]Problem with sleep()

Started by TheS0m3b0dy, 19 May 2013 - 12:43 PM
TheS0m3b0dy #1
Posted 19 May 2013 - 02:43 PM
Hey everyone!
I'm trying to use the touchscreen functionality of advanced monitors to toggle Redpower bundled cable outputs.
So first of all here is my code:

mon = peripheral.wrap("left")
mon.clear()
mon.setCursorPos(1,1)
mon.setBackgroundColor(colors.black)
w1 = colors.white
o1 = colors.orange
m1 = colors.magenta
lb1 = colors.lightBlue
w0 = colors.subtract(rs.getBundledOutput("back"), colors.white)
o0 = colors.subtract(rs.getBundledOutput("back"), colors.orange)
m0 = colors.subtract(rs.getBundledOutput("back"), colors.magenta)
lb0 = colors.subtract(rs.getBundledOutput("back"), colors.lightBlue)
buttons = {
		  {text = "White", x = 1, y = 1, txtCol = colors.white, bgCol = colors.red},
		  {text = "Orange", x = 1, y = 2, txtCol = colors.white, bgCol = colors.red},
		  {text = "Magenta", x = 1, y = 3, txtCol = colors.white, bgCol = colors.red},
		  {text = "Light blue", x = 1, y = 4, txtCol = colors.white, bgCol = colors.red}
}
function writeButtons(table)
  for i, v in pairs(table) do
	term.setCursorPos(v.x, v.y)
	term.setTextColor(v.txtCol)
	term.setBackgroundColor(v.bgCol)
	write(v.text)
  end
end
function isValidClick(table, mx, my)
  for i, v in pairs(table) do
	if mx >= v.x and mx <= v.x + #v.text - 1 and my == v.y then
	  return true, v.text
	end
  end
  return false, nil
end
function toggleOutput(c1, c0)
  if colors.test(rs.getBundledOutput("back"), c1) then
	rs.setBundledOutput("back", c0)
  else
	rs.setBundledOutput("back", c1)
  end
end
writeButtons(buttons)
while true do
  print("main")
  evt, p, x, y = os.pullEvent("monitor_touch")
  bClick, option = isValidClick(buttons, x, y)
  if bClick then
	if option == "White" then
	  toggleOutput(w1, w0)
   print("This prints")
   sleep(0.1)
   print("This doesn't print")
	end
  end
end
(This is actually a modified version of remiX's code that I saw somewhere)

For testing purposes I currently have only the code for the white cable.
So the problem is: The first time I right click on the white button it works fine, but the second time I try it, it just does nothing.
The program is still running and I can terminate it, but clicking on the monitor doesn't work.
So what I did was I added some print() commands to try to solve this thing and I found out that it stops when it hits the sleep()
command. I just want to know what am I doing wrong or is this a bug because it's really annoying.
If I remove the sleep, it will just toggle twice and after that I can click on the button again, but this doesn't solve anything since it
turns the cable off instantly.
I would really appreciate a quick answer.

Oh and I'm using FTB Ultimate to run ComputerCraft if it helps.
LBPHacker #2
Posted 19 May 2013 - 03:14 PM
Dunno what the hell is up with sleep, but what did you use to run this program? "monitor left <name>" or just "<name>"?

Anyways, simplified code:
term.setTextColor(1)
term.setBackgroundColor(32768)
term.clear()
term.setCursorPos(1, 1)
print("White")
print("Orange")
print("Magenta")
print("Light blue")
while true do
    local evt, p, x, y = os.pullEvent("monitor_touch")
    if y < 5 then rs.setBundledOutput("back", bit.bxor(rs.getBundledOutput("back"), 2 ^ (y - 1))) end
end

EDIT: Yup, that should be launched using "monitor <side> <name>", but you could just wrap the monitor and .write everything as well.
TheS0m3b0dy #3
Posted 19 May 2013 - 03:35 PM
Yes, I am launching it using "monitor left control" (control is the name of the program, of course).
And will your "simplified code" just do the exact same thing?

Thank you for your quick answer BTW.
LBPHacker #4
Posted 19 May 2013 - 03:41 PM
Actually, it will do the exact same thing except the colors (so it's not red, but term.setBackgroundColor(16384) instead of 32768 and done). And by the way if you increase that "5" in "if y < 5 then", the program will be able to handle even more colors, cuz the (2 ^ (y - 1)) part always works (y-minus-1-th power of 2).

Another BTW: Took half an hour to answer, that's slow on this forum, relatively speaking.
TheS0m3b0dy #5
Posted 19 May 2013 - 03:45 PM
I just tested your code and it didn't seem to work :huh:/>. It doesn't give any errors, but right-clicking does nothing.
LBPHacker #6
Posted 19 May 2013 - 03:50 PM
Tested in-game, works fine. Try it without using "monitor" - that way the text will appear on the terminal, but it should handle the monitor_touch events. Other than that, I have really no idea.
TheS0m3b0dy #7
Posted 19 May 2013 - 03:56 PM
This is getting weird… It worked fine when I tested it without the "monitor" program, but not with it…
LBPHacker #8
Posted 19 May 2013 - 04:02 PM
Wrapping the monitor and using .write for the win. And I'm gonna hack into "monitor". Last time I checked it was only a piece of term.redirect.
LBPHacker #9
Posted 19 May 2013 - 04:03 PM
Holy potato… Coroutines in "monitor"! What the hell?!
LBPHacker #10
Posted 19 May 2013 - 04:06 PM
Yeah, found it. monitor uses coroutines. Does that because it automatically converts monitor_touch events into mouse_click events. [huge facepalm]
TheS0m3b0dy #11
Posted 20 May 2013 - 02:52 AM
So… Does this mean I should use term.redirect instead of monitor?
Sorry for asking dumb questions, I'm still a noob in Computercraft.
LBPHacker #12
Posted 20 May 2013 - 04:21 AM
Nope. This means if you have a program that you use with "monitor", you won't get monitor_touch events, because "monitor" turns them into mouse_click events.
TheS0m3b0dy #13
Posted 20 May 2013 - 08:09 AM
So what do I need to do to fix this?
LBPHacker #14
Posted 20 May 2013 - 08:21 AM
If you want to use my code with "monitor", just replace monitor_touch with mouse_click:
term.setTextColor(1)
term.setBackgroundColor(32768)
term.clear()
term.setCursorPos(1, 1)
print("White")
print("Orange")
print("Magenta")
print("Light blue")
while true do
    local evt, p, x, y = os.pullEvent("mouse_click") -- instead of monitor_touch
    if y < 5 then rs.setBundledOutput("back", bit.bxor(rs.getBundledOutput("back"), 2 ^ (y - 1))) end
end
TheS0m3b0dy #15
Posted 20 May 2013 - 09:18 AM
I tested that and it didn't seem to work. It just tested for clicks on the computer itself. <_</>