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

Help with DW20 Elevator Code

Started by jimyh, 11 May 2013 - 07:11 AM
jimyh #1
Posted 11 May 2013 - 09:11 AM
Hello, i am new to the forum, and am fairly new to CC. I have been using CC for a little while writing some simple bundled cable outputs for frame motors etc, however i am now trying to program a touchscreen elevator system. I have alot of code written for the movememnt of the elevator which is situated on my main computer, the trouble im having is creating a button which i will use outside the lift to call it to my floor and again a series of buttons inside the lift to take me to what floor i want to go to.
I have some code which i got from Direwolf20 and have tried to modify it to do what i want however i do not understand what each part does, if someone would be willing to break down the program for me i may understand what i need to add and take away in order to make it work for me, or if someone wants to write it thats cool to.
Thanks in advance for your help, heres the code unedited:


local mon = peripheral.wrap("right")
mon.setTextScale(0.5)
mon.setTextColor(colors.white)
local button={}
mon.setBackgroundColor(colors.black)
	
function setTable(name, xmin, xmax, ymin, ymax, col)
   button[name] = {}
   button[name]["active"] = false
   button[name]["xmin"] = xmin
   button[name]["ymin"] = ymin
   button[name]["xmax"] = xmax
   button[name]["ymax"] = ymax
   button[name]["colour"] = col
end
	  
function fillTable()
   setTable("Basement", 2, 16, 3, 7, colours.white)
   setTable("Ground", 21, 35, 3, 7, colours.orange)
   setTable("Two", 2, 16, 10, 14, colours.magenta)
   setTable("Three", 21, 35, 10, 14, colours.lightBlue)
   setTable("Four", 2, 16, 17, 21, colours.yellow)
   setTable("Roof", 21, 35, 17, 21, colours.lime)
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.lightGray end
	  fill(name, currColor, data)
   end
end
	
function checkxy(x, y)
   local new = false
   local newname
   for name, data in pairs(button) do
	  data["active"] = false
	  if y>=data["ymin"] and  y <= data["ymax"] then
		 if x>=data["xmin"] and x<= data["xmax"] then
			rs.setBundledOutput("bottom", data["colour"])
			new = true
			newname = name
		 end
	  end
   end
   if new then
	  button[newname]["active"] = true
   end
end
	
function heading(text)
   w, h = mon.getSize()
   mon.setCursorPos((w-string.len(text))/2+1, 1)
   mon.write(text)
end
function checkRedstone()
   local c = rs.getBundledInput("back")
   if rs.getBundledOutput("bottom") ~= c then
	  rs.setBundledOutput("bottom", 0)
	  for name, data in pairs(button) do
		 if data["colour"] == c then
			data["active"] = true
		 else
			data["active"] = false
		 end
	  end
   end
end
	
fillTable()
while true do
   mon.clear()
   heading("Please Select Floor")
   screen()
   local e,side,x,y = os.pullEvent()
   if e == "monitor_touch" then
	  checkxy(x,y)
   else
	  if e == "redstone" then
		 checkRedstone()
	  else
	  end
   end
   sleep(.1)
end
Edited by
Lyqyd #2
Posted 11 May 2013 - 04:33 PM
Split into new topic.
jimyh #3
Posted 14 May 2013 - 07:14 AM
bump
jimyh #4
Posted 15 May 2013 - 02:51 PM
come on people, its not as if i want u to write the code. I aint got no where else to ask.
W00dyR #5
Posted 15 May 2013 - 03:19 PM
come on people, its not as if i want u to write the code. I aint got no where else to ask.
bump

Bumping for replies isn't gonna help getting a reply, if people can help you they will, because that's what they are here for.
LordIkol #6
Posted 17 May 2013 - 02:39 AM
Do you still need help with this?
If so i will look into it when im Home tonight.
Frederikam #7
Posted 17 May 2013 - 05:58 AM
Well, I'm not going to explain something you already know how works. If you could be more specific what you don't understand I may be able to help you.
jimyh #8
Posted 17 May 2013 - 03:37 PM
Yes i still need help with this Lord.
Fredrikam, i dont understand the code properly which is why i need each function broken down and explained what each part does. for example, what is bData, where has it come from etc??

I enjoy trying to program and want to learn more which is why i just want a break down of the code so then i can try and rearrange it to work for my own use.

Thanks for replying
LordIkol #9
Posted 18 May 2013 - 05:58 AM
Hi Jim,

i recommend that you make a list of what you not understand so we can explain it to you.
Or start writing your own code with all the parts you know or understand and tell us what you can not do.

So now for your bData Question.


function fill(text, color, bData)

in this case text color and bdata are the values you can pass to the function when you call it.
You can choose their names like you want.
in this case bData stands for the table with the Buttondata, which is button["name"]
so when you call this function for the Basement button you would go for


function fill("moep", colors.red, button["Basement"])

to run it for every Button you have to use a pairs loop. like he did here


for name,data in pairs(button) do  -- name will give you the index in this case the buttonname, data will give you a table with xmin,ymin etc...
		  local on = data["active"]  -- now you can use data to get the stuff from the subtable
		  if on == true then currColor = colors.lime else currColor = colors.lightGray end -- simple if to check if button is active and set the color 
		  fill(name, currColor, data) -- and use the fill command like mentiones before 
   end

hope that helps so far
Loki
jimyh #10
Posted 18 May 2013 - 04:21 PM
thats great thanks, i shall give it another go.