Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --EyeDeck's extractor controller program
- os.loadAPI("buttonAPI")
- ex = peripheral.wrap("front")
- cvt = peripheral.wrap("AdvancedGears_2")
- coil = peripheral.wrap("AdvancedGears_3")
- mon = peripheral.wrap("left")
- mon.setTextScale(0.5)
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.white)
- mon.clear()
- coil.setSpeed(4096)
- coil.setTorque(0)
- buttonAPI.drawButton(mon,2,2,16,7,colors.black,colors.lime,"Start","startstop")
- buttonAPI.drawButton(mon,20,2,16,7,colors.black,colors.blue,"Manual","manualauto")
- isOn = false
- autoOn = false
- --Returns right-most item in extractor, else if empty returns false
- function notEmpty()
- for i = 4,1,-1 do
- local item = ex.getStackInSlot(i)
- if item ~= nil then
- return item
- end
- end
- return false
- end
- -- Prints a message to the console and halts execution until given handle exists
- -- too lazy to implement yet
- function sanityCheck(handle)
- end
- function updateDisplay(name)
- local old_disp = term.redirect(mon)
- local power_in_hours = math.floor(coil.getEnergy() / 37507786621 * 100)*.01
- 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)
- cvt.setRatio(1)
- coil.setTorque(4096)
- sleep(4.2)
- cvt.setRatio(32)
- coil.setTorque(256)
- sleep(2.85)
- else
- if jammed == true then
- updateDisplay("Jammed (check output)")
- else
- updateDisplay("Idle")
- end
- if autoOn then
- coil.setTorque(0)
- sleep(8)
- else
- isOn = false
- buttonAPI.drawButton(mon,2,2,16,7,colors.black,colors.lime,"Start")
- end
- end
- end
- coil.setTorque(0)
- print("ceasing extraction")
- else -- isOn == false
- sleep(2)
- end
- end
- end
- function uiLoop()
- while true do
- 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
- 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
- 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 = nil
- local item = nil
- while true do
- if isOn then
- item = ex.getStackInSlot(8)
- if item ~= nil then
- if lastItem == item then
- jamCount = jamCount + 1
- else
- jamCount = 0
- end
- lastItem = item
- if jamCount >= 3 then
- jammed = true
- end
- end
- end
- sleep(4)
- end
- end
- if (notEmpty() ~= false) then
- updateDisplay(notEmpty().display_name)
- else
- updateDisplay("Idle")
- end
- parallel.waitForAll(uiLoop, exLoop, jamLoop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement