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

touchscreen on off

Started by Kev30, 22 May 2013 - 05:49 AM
Kev30 #1
Posted 22 May 2013 - 07:49 AM
I'm creating a monitor for all my farms but i cant get my bundle cable working

What does it need to do : When i touch the first button it needs to turn on the bundled cable color.(then the button on the screen is green) then by pressing it again it is turning red and it disables the bundled cable




Script


local bcable = "back"
local mon = peripheral.wrap("top")
mon.setTextScale(1)
mon.setTextColor(colors.white)
local button={}
mon.setBackgroundColor(colors.black)
	  
function setTable(name, func, bcolor, xmin, xmax, ymin, ymax)
   button[name] = {}
   button[name]["func"] = func
   button[name]["active"] = false
   button[name]["bcolor"] = bcolor
   button[name]["xmin"] = xmin
   button[name]["ymin"] = ymin
   button[name]["xmax"] = xmax
   button[name]["ymax"] = ymax
end
function Chicken()
   print("Chicken")
   redstone.setBundledOutput("back",colors.black)
   sleep(1)
   redstone.setBundledOutput("back",0)
end
function Cow()
   print("Cow")
   redstone.setBundledOutput("back",colors.blue)
   sleep(1)
   redstone.setBundledOutput("back",0)  
end	  
function Pig()
   print("Pig")
end
function Sheep()
   print("Sheep")
end
function Blaze()
   print("Blaze")
   redstone.setBundledOutput("back",colors.white)
   sleep(100)
   redstone.setBundledOutput("back",0)
end
function Creeper()
   print("Creeper")
end
function Skeleton()
   print("Skeleton")
end
function Spider()
   print("Spider")
end
function Zombie()
   print("Zombie")
end
function fillTable()
   setTable("Blaze", Blaze, 5, 20, 4, 6)
   setTable("Chicken", Chicken, 30, 45, 4, 6)
   setTable("Creeper", Creeper, 5, 20, 8, 10)
   setTable("Cow", Cow, 30, 45, 8, 10)
   setTable("Skeleton", Skeleton, 5, 20, 12, 14)
   setTable("Pig", Pig, 30, 45, 12, 14)
   setTable("Spider", Spider, 5, 20, 16, 18)
   setTable("Sheep", Sheep, 30, 45, 16, 18)
   setTable("Zombie", Zombie, 5, 20, 20, 22)
end  
function fill(text, color, bData)
   mon.setBackgroundColor(color)
   local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
   local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
   for j = bData["ymin"], bData["ymax"] do
	  mon.setCursorPos(bData["xmin"], j)
	  if j == yspot then
		 for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
			if k == xspot then
			   mon.write(text)
			else
			   mon.write(" ")
			end
		 end
	  else
		 for i = bData["xmin"], bData["xmax"] do
			mon.write(" ")
		 end
	  end
   end
   mon.setBackgroundColor(colors.black)
end
	  
function screen()
   local currColor
   for name,data in pairs(button) do
		  local on = data["active"]
		  if on == true then currColor = colors.lime else currColor = colors.red end
		  fill(name, currColor, data)
   end
end
	  
function checkxy(x, y)
   for name, data in pairs(button) do
		  if y>=data["ymin"] and  y <= data["ymax"] then
				 if x>=data["xmin"] and x<= data["xmax"] then
					data["func"]()
					data["active"] = not data["active"]
					print(name)
				 end
		  end
   end
end
	  
function heading(text)
   w, h = mon.getSize()
   mon.setCursorPos((w-string.len(text))/2+1, 1)
   mon.write(text)
end
	  
fillTable()
while true do
   mon.clear()
   heading("Mobspawner Control")
   screen()
   local e,side,x,y = os.pullEvent("monitor_touch")
   print(x..":"..y)
   checkxy(x,y)
   sleep(.1)
end


I hope i can get some help because i cant get it working

Also sorry for my bad english :P/>


~Kev
Apfeldstrudel #2
Posted 22 May 2013 - 01:31 PM
In what way does it not work
BlankTitle #3
Posted 22 May 2013 - 05:46 PM
From a quick scan I can't find any errors, may you please inform us what line or lines the error is on?
Kev30 #4
Posted 23 May 2013 - 09:54 AM
what i want : when i click on button the button goes green and the signal is on. When i press it again the signal is off and the button turns red

What it does: green = on for a few seconds then signal is off
W00dyR #5
Posted 23 May 2013 - 10:05 AM
what i want : when i click on button the button goes green and the signal is on. When i press it again the signal is off and the button turns red

What it does: green = on for a few seconds then signal is off

Easiest way for displaying this (in my opinion) is to just have a loop, which waits for a touch and sets a variable to a boolean value. In the loop you have a function that checks the variable and displays it/sets the bundled cable output. Then the loop just starts over and it waits for a touch again. If you want the signal to turn off after a certain time, start a timer in the start of the loop if the boolean value is set to false for example. Then the os.pullEvent() will notice the timer triggering instead of the monitor_touch, if its the timer then it will set it to a different boolean value as if you were pressing the button yourself.