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.