Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --EyeDeck's extractor controller program
- f = fs.open("buttonAPI","r")
- if f == nil then
- print("Attempting to fetch ButtonAPI...")
- bapi = http.get("http://idek.chir.uno/mc/buttonAPI.lua")
- if (bapi) then
- f = fs.open("buttonAPI","w")
- f.write(bapi.readAll())
- f.close()
- print("...complete, ButtonAPI has been downloaded.")
- else
- print("...failed, ButtonAPI could not be downloaded. Re-run this program to retry, or supply ButtonAPI manually.")
- error("ButtonAPI download failed")
- end
- else
- f.close()
- end
- os.loadAPI("buttonAPI")
- ex, cvt, coil, mon = nil, nil, nil, nil
- warning = {}
- print("Checking peripherals...")
- while ex == nil or cvt == nil or coil == nil or mon == nil do
- ex = peripheral.find("Extractor")
- adv1,adv2 = peripheral.find("AdvancedGear")
- mon = peripheral.find("monitor")
- if adv1.getName() == "CVT Unit" then
- cvt = adv1
- coil = adv2
- else
- cvt = adv2
- coil = adv1
- end
- if ex == nil then
- if warning.ex == nil then
- print("Extractor missing!")
- warning.ex = true
- end
- sleep(2)
- elseif cvt == nil then
- if warning.cvt == nil then
- print("CVT missing!")
- warning.cvt = true
- end
- sleep(2)
- elseif coil == nil then
- if warning.coil == nil then
- print("Coil missing!")
- warning.coil = true
- end
- sleep(2)
- elseif mon == nil then
- if warning.mon == nil then
- print("Monitor missing!")
- warning.mon = true
- end
- sleep(2)
- else
- break
- end
- end
- print("All peripherals present.")
- peripherals = 0
- --Returns right-most item in extractor, else if empty returns false
- function notEmpty()
- item = {}
- for i = 4,1,-1 do
- item = getStackInSlot(ex,i)
- if item ~= nil then
- return item
- end
- end
- return false
- end
- function getStackInSlot(p,slot)
- sanityCheck()
- item = {}
- item.raw_name, item.dmg, item.qty, item.display_name = p.getSlot(slot-1)
- -- make sure that nil is returned instead of a useless object if there is no item or the script will break in 12 different places
- if item.qty ~= nil then
- return item
- else
- return nil
- end
- end
- -- A huge hack to prevent a crash when a peripheral is disconnected
- function sanityCheck()
- if peripherals < 0 then
- print("A peripheral has been detached!\nWaiting for reattachment...")
- while peripherals < 0 do
- sleep(2)
- end
- print("Resuming.")
- end
- end
- function writeState(state,val)
- if val == true then val = "1" else val = "0" end
- local file = fs.open(state,"w")
- file.write(val)
- file.close()
- end
- function readState(state)
- local file = fs.open(state,"r")
- local val
- if file == nil then
- val = false
- else
- val = file.readAll()
- if val == "1" then val = true else val = false end
- file.close()
- end
- return val
- end
- function updateDisplay(name)
- sanityCheck()
- local old_disp = term.redirect(mon)
- local power_in_hours = math.floor(coil.getEnergy() / 37507786621 * 100)*.01
- if name == nil then -- prevent a crash just in case
- name = "nil"
- end
- term.setCursorPos(2,10)
- term.setTextColor(colors.white)
- -- extra spaces are a hack to clear the last item if necessary ("Ammonium Chloride Solution" is long)
- write("Extracting: ".. name .." \n ")
- term.setCursorPos(2,12)
- write("Power remaining: ".. power_in_hours .."h ")
- term.redirect(old_disp)
- end
- function exLoop()
- while true do
- if isOn then
- print("Beginning extraction...")
- jammed = false
- while isOn do
- local currentItem = notEmpty()
- if currentItem ~= false and jammed == false then
- updateDisplay(currentItem.display_name)
- coil.setSpeed(4096)
- sanityCheck()
- cvt.setRatio(1)
- coil.setTorque(4096)
- sleep(0.75) --sleep(4.2)
- sanityCheck()
- cvt.setRatio(32)
- coil.setTorque(256)
- sleep(0.5) --sleep(2.85)
- else
- if jammed == true then
- updateDisplay("Jammed (check output)")
- else
- updateDisplay("Idle")
- end
- if autoOn then
- sanityCheck()
- coil.setTorque(0)
- sleep(8)
- else
- isOn = false
- buttonAPI.drawButton(mon,2,2,16,7,colors.black,colors.lime,"Start")
- end
- end
- end
- sanityCheck()
- coil.setTorque(0)
- print("Ceasing extraction.")
- else -- isOn == false
- sleep(2)
- end
- end
- end
- function uiLoop()
- while true do
- sanityCheck()
- event, _, x, y = os.pullEvent("monitor_touch")
- local button_name = buttonAPI.getButton(x,y)
- --print(button_name .. " pressed at " .. x .. "," .. y)
- if button_name == "startstop" then
- if isOn == false then
- buttonAPI.drawButton(mon,2,2,16,7,colors.black,colors.red,"Stop")
- isOn = true
- else
- buttonAPI.drawButton(mon,2,2,16,7,colors.black,colors.lime,"Start")
- isOn = false
- end
- writeState("isOn",isOn)
- elseif button_name == "manualauto" then
- if autoOn == false then
- buttonAPI.drawButton(mon,20,2,16,7,colors.black,colors.yellow,"Automatic")
- autoOn = true
- else
- buttonAPI.drawButton(mon,20,2,16,7,colors.black,colors.blue,"Manual")
- autoOn = false
- end
- writeState("autoOn",autoOn)
- end
- end
- end
- --[[
- While main function is active, if the item in the output slot remains
- unchanged for ~12 seconds or longer, assume the machine has jammed
- and set a variable to stop the main loop.
- --]]
- function jamLoop()
- local jamCount = 0
- local lastItem = {}
- local item = nil
- while true do
- if isOn then
- item = getStackInSlot(ex,9)
- if item == nil then
- item = getStackInSlot(ex,8)
- end
- if item == nil then
- lastItem = {}
- jammed = false
- jamCount = 0
- else
- -- comparing the item object directly doesn't work anymore, test count and name instead
- if lastItem.display_name == item.display_name and lastItem.qty == item.qty then
- jamCount = jamCount + 1
- else
- jamCount = 0
- end
- lastItem = item
- if jamCount >= 3 then
- if jammed == false then
- jammed = true
- print("Extractor jammed!")
- end
- else
- jammed = false
- end
- end
- end
- sleep(4)
- end
- end
- function peripheralLoop()
- while true do
- local event = os.pullEvent()
- if event == "peripheral" then
- peripherals = peripherals + 1
- print("A peripheral has been detached!")
- end
- if event == "peripheral_detach" then
- peripherals = peripherals - 1
- print("A peripheral has been reattached!")
- end
- end
- end
- mon.setTextScale(0.5)
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.white)
- mon.clear()
- coil.setSpeed(4096)
- coil.setTorque(0)
- isOn = readState("isOn")
- autoOn = readState("autoOn")
- buttonAPI.drawButton(mon,2,2,16,7,colors.black,isOn and colors.red or colors.lime,isOn and "Stop" or "Start","startstop")
- buttonAPI.drawButton(mon,20,2,16,7,colors.black,autoOn and colors.yellow or colors.blue,autoOn and "Automatic" or "Manual","manualauto")
- if (notEmpty() ~= false) then
- updateDisplay(notEmpty().display_name)
- else
- updateDisplay("Idle")
- end
- parallel.waitForAll(uiLoop, exLoop, jamLoop, peripheralLoop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement