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

Monitor sync for CC and Thaumcraft lift

Started by MaddocksL, 16 March 2013 - 02:02 PM
MaddocksL #1
Posted 16 March 2013 - 03:02 PM
I have been trying to figure out how to get monitors for a lift system (using arcane levitators activated with redstone) to sync between floors. As shown by the (bad) code when a button is selected it turns green showing which floor has been selected. When another floor is selected the button turns back red and the selected turns green. I have been trying to figure out a way to get each monitor to show which floor has been selected and to stop outputting any signal it may have already been emitting. I am looking for a way to run similar code on each computer for each floor with only minor changes.

Any help with this or even suggestions on how to tidy up the code would be very much appreciated.

Here is my ugly code stitched together using other peoples code and some of my own.

Spoiler

local side = "right"
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 resetAll()
  rs.setBundledOutput(side, 0)
  button["1"]["active"] = false
  button["2"]["active"] = false
  button["3"]["active"] = false
  button["4"]["active"] = false
  button["5"]["active"] = false
end

function set1()
  resetAll()
end

function set2()
  resetAll()
  rs.setBundledOutput(side, colors.purple)
end

function set3()
  resetAll()
  rs.setBundledOutput(side, colors.lime)
end

function set4()
  resetAll()
  rs.setBundledOutput(side, colors.blue)
end

function set5()
  resetAll()
  rs.setBundledOutput(side, colors.red)
end

	  
function fillTable()
   setTable("1", set1, 2, 6, 3, 5)
   setTable("2", set2, 2, 6, 8, 10)
   setTable("3", set3, 2, 6, 13, 15)
   setTable("4", set4, 2, 6, 18, 20)
   setTable("5", set5, 2, 6, 23, 25)
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("Floor 1")
   screen()
   local e,side,x,y = os.pullEvent("monitor_touch")
   print(x..":"..y)
   checkxy(x,y)
   sleep(.1)
end

P.S.This is the first thing I have pretty much ever coded, feel free to rip me a new one :D/>
Lyqyd #2
Posted 16 March 2013 - 05:10 PM
Split into new topic.
MaddocksL #3
Posted 17 March 2013 - 06:13 PM
Ok well as no one has added to this I suppose I'll add my progress (if you can call it that).

Heres my code.
Spoiler

local side = "right"
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 resetAll()
  rs.setBundledOutput(side, 0)
  button["1"]["active"] = false
  button["2"]["active"] = false
  button["3"]["active"] = false
  button["4"]["active"] = false
  button["5"]["active"] = false
end

function set1()
  resetAll()
  rs.setBundledOutput(side, colors.brown)
end

function set2()
  resetAll()
  rs.setBundledOutput(side, colors.purple)
end

function set3()
  resetAll()
  rs.setBundledOutput(side, colors.lime)
end

function set4()
  resetAll()
  rs.setBundledOutput(side, colors.blue)
end

function set5()
  resetAll()
  rs.setBundledOutput(side, colors.red)
end

	  
function fillTable()
   setTable("1", set1, 2, 6, 3, 5)
   setTable("2", set2, 2, 6, 8, 10)
   setTable("3", set3, 2, 6, 13, 15)
   setTable("4", set4, 2, 6, 18, 20)
   setTable("5", set5, 2, 6, 23, 25)
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

function update()
local event,p1,p2,p3 = os.pullEvent()
   if event == "redstone" then
	  local rsInp = rs.getBundledInput("right")
	  if rsInp == 4096 then
		resetAll()
		button["1"]["active"] = true
	  elseif rsInp == 1024 then
		resetAll()
		button["2"]["active"] = true
	  elseif rsInp == 32 then
		resetAll()
		button["3"]["active"] = true
	  elseif rsInp == 2048 then
		resetAll()
		button["4"]["active"] = true
	  elseif rsInp == 16384 then
		resetAll()
		button["5"]["active"] = true
	  end
	  screen()
   elseif event == "monitor_touch" then
	  local side = p1
	  x = p2
	  y = p3
	  print(x..":"..y)
	  checkxy(x,y)
   end
end
	
fillTable()
while true do
   mon.clear()
   heading("Floor 1")
   screen()
   update()
   sleep(.1)
end
The new code shows the update function in replacement for the os.pullEvent(monitor_touch). The problem I have now is a bit weird.

Example:
I press button 4(or any other) and both monitors shows the update, I press button 3 and both update.

After pressing button 3 on Monitor 1, I press button 5 on Monitor 2. Monitor 2 updates but Monitor 1 stays at only button 3 active. I then press button 3 on Monitor 2 which updates. Then press any other button and both monitors update.

I am completely stumped as to where to go with this. Any idea's would be very appreciated
MaddocksL #4
Posted 31 March 2013 - 10:01 AM
Bump! :(/>