Posted 17 July 2013 - 08:08 PM
Hi, I'm a bit new to computercraft and lua in general, but have gotten by on what I know up until now
basically I am trying to create a menu to control my MFR planters and harvesters, I am basing my code off Direwolf20's button API. I am trying to make it so that when I go into the menu, each button will display the state of the planter it controls, based on an external file. When I try to use rs.setBundledOutput to toggle the planter on or off, I get an attempt to call number error, and I am not quite sure what that means
I will be able to easily expand this later by adding more if statements into the if statement on line 55 to test for the subsequent colors
and here is the button API I am using in case you need it
Any help you could offer would be much appreciated
basically I am trying to create a menu to control my MFR planters and harvesters, I am basing my code off Direwolf20's button API. I am trying to make it so that when I go into the menu, each button will display the state of the planter it controls, based on an external file. When I try to use rs.setBundledOutput to toggle the planter on or off, I get an attempt to call number error, and I am not quite sure what that means
os.loadAPI("button")
m = peripheral.wrap("left")
m.clear()
file = fs.open("jacencaedus/buttonstates","r")--this is where the number that controls the rsBundledOutput is stored
bStates = file.readAll()
bStatesN = tonumber(bStates)--for later use
file.close()
local switches = {}
local loopControl = true
function fillTable()--position the buttons
local w,h = m.getSize()
button.clearTable()
button.heading("Planter Control", 1)
button.setTable("Sugar Cane", sugar,8,18,4,6)
button.setTable("Main Menu",back,(w/2)-5,(w/2)+5,h-4,h-2)
button.screen()
end
function switch(name)
switches[name] = not switches[name]
term.setCursorPos(1,2)
term.write("Switched")
end
function back()
button.flash("Main Menu")
loopControl = false
bStates = tostring(rs.getBundledOutput("back"))
file = fs.open("jacencaedus/buttonstates","w")
file.write(bStates)--write the rs.bundledOutput to the file
file.close()
shell.run("startup")
end
function sugar()--called when the button is pressed
if not switches["Sugar Cane"] then --if the "Sugar Cane" button is off
rs.setBundledOutput("back", rs.getBundledInput("back")+colors.white())--enable the white bundle--the problem is right around here
else
rs.setBundledOutput("back", rs.getBundledInput("back")-colors.white())--disable the white bundle
end
switch("Sugar Cane")--toggle the button
end
function initSwitches()
switches["Main Menu"] = false
switches["Sugar Cane"] = false
end
fillTable()
sleep(0.1)
initSwitches()
rs.setBundledOutput("back",bStatesN)--set the bundled output to what the file had stored
if bStatesN > 1 then--test if the button should be on
switch("Sugar Cane")
end
while loopControl do
local timeout = os.startTimer(1)
local event = {os.pullEvent()}
if event[1] == "monitor_touch" then
button.checkxy(event[3], event[4])
elseif event[1] == "timer" and event[2] == timeout then
end
end
I will be able to easily expand this later by adding more if statements into the if statement on line 55 to test for the subsequent colors
and here is the button API I am using in case you need it
local mon = peripheral.wrap("left")
mon.setTextScale(1)
mon.setTextColor(colors.white)
local button={}
mon.setBackgroundColor(colors.black)
function clearTable()
button = {}
mon.clear()
end
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("ButtonText", funcName, 5, 25, 4, 8)
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 toggleButton(name)
button[name]["active"] = not button[name]["active"]
screen()
end
function flash(name)
toggleButton(name)
screen()
sleep(0.15)
toggleButton(name)
screen()
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"]()
return true
--data["active"] = not data["active"]
--print(name)
end
end
end
return false
end
function heading(text, line)
w, h = mon.getSize()
mon.setCursorPos((w-string.len(text))/2+1, line)
mon.write(text)
end
function label(w, h, text)
mon.setCursorPos(w, h)
mon.write(text)
end
Any help you could offer would be much appreciated