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

[Lua] Coding advanced monitors and bundled cable

Started by Marshen, 23 March 2013 - 10:27 PM
Marshen #1
Posted 23 March 2013 - 11:27 PM
Hi all so I have got my touch screen to the point where it can turn the background text from red to green when i click on it however what I need to do next is toggle an bundled cable off and on with 6 seprate outputs. The bundled cable need to start sending a redstone single to all of 6 output and then be able to turn off each indivually so when for example the creeper button is red i will send rs.setBundledCable ("left'',color.blue) but then when I click the creeper button and it turns green it will turn it off.

	local mon = peripheral.wrap("left")
	mon.setTextScale(1)
	mon.setTextColor(colors.white)
	local button={}
	mon.setBackgroundColor(colors.black)
		
	function setTable(name, func, xmin, xmax, ymin, ymax)
	   button[name] = {}
	   button[name]["func"] = func
	   button[name]["active"] = false
	   button[name]["xmin"] = xmin
	   button[name]["ymin"] = ymin
	   button[name]["xmax"] = xmax
	   button[name]["ymax"] = ymax
	end
	
	function funcName()
	   print("You clicked buttonText")
	end
		  
	function fillTable()
	   setTable("Wither" , funcName, 2, 9, 4, 6)
	   setTable("Creeper", funcName, 11, 19, 4, 6)
	   setTable("Zombie", funcName, 21, 28, 4, 6)
	   setTable("Spider", funcName, 2, 9, 8, 10)
	   setTable("1", funcName, 11, 19, 8, 10)
	   setTable("2", funcName, 21, 28, 8, 10)
	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("MOB SPAWNER")
	   screen()
	   local e,side,x,y = os.pullEvent("monitor_touch")
	   print(x..":"..y)
	   checkxy(x,y)
	   sleep(.1)
	end

pastebin code c41bsCPd
KaoS #2
Posted 24 March 2013 - 12:17 AM
new code here

basically I added a color tag to your existing buttons and modified your checkxy function. I made the side of the cables the back, you can change that if you like

If there is anything you do not understand PLEASE ask
Marshen #3
Posted 24 March 2013 - 12:50 AM
thanks for you help however i am getting bios:338: [string "startup"] :69; '<name> expected
KaoS #4
Posted 24 March 2013 - 01:30 AM
haha sorry about that. 2 minor typos. try it again here
Marshen #5
Posted 24 March 2013 - 05:58 AM
after looking over the code I see that it is asking for a color value however it is getting a true or false value and then giving an error. Basicly I need to call the function while true do and then an if else statement but im not quite sure how to do that.
Marshen #6
Posted 24 March 2013 - 09:49 AM
can anyone else help me with this I want to learn how this should be
Engineer #7
Posted 25 March 2013 - 01:37 AM
I started working this out, but it doesnt quite work yet. This code should get things staeted, but its pretty complicated though.
http://pastebin.com/Bmauzacq

Its untested and just out of my mind.
You can finish it if you want, but it is pretty complicated as Ive said.

If I find more time then I will finish it, but I have a lot of test the next 2 weeks, dont expect anytime soon from me.
Edited on 25 March 2013 - 02:22 AM