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

Can I please get some help on a "button" program

Started by TCMedia_chuck, 31 May 2013 - 02:27 PM
TCMedia_chuck #1
Posted 31 May 2013 - 04:27 PM
Title: Can I please get some help on a "button" program

I need help working on a code to enable/disable a redstone signal going through bundled cable and turning on/off a nuclear reactor, then automaticly shut a certain reactor off if it gets a redstone signal when a reactor overheats, the code I am using is direwolf20's button api.

the unfortunate part is I have no idea what some of the code means.

Spoiler

local mon = peripheral.wrap("right")
mon.setTextScale(0.9)
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("Reactor 1", funcName, 3, 13, 4, 6)
   setTable("Reactor 2", funcName, 3, 13, 8, 10)
   setTable("Reactor 3", funcName, 3, 13, 12, 14)
   setTable("Reactor 4", funcName, 3, 13, 16, 18)
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("Reactor Control Computer")
   screen()
   local e,side,x,y = os.pullEvent("monitor_touch")
   print(x..":"..y)
   checkxy(x,y)
   sleep(.1)
end

I have made this with looking at code that mhykol (from mindcrack) has made, what I am trying to get is merge this code with the other and get it working, while also shutting down a reactor if it overheats.
Spoiler

local colorname = {"white","orange","magenta","lightBlue","yellow","lime","pink","gray"}
local colornum = {1,2,4,8,16,32,64,128}
local switchname = {"reactor4","reactor3","reactor2","reactor1","over4","over3","over2","over1"}

for i = 1, #colornum do
  status = rs.testBundledInput{bottom, colornum[

Lyqyd #2
Posted 31 May 2013 - 11:21 PM
Split into new topic.