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:
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