Advertisement
DuckStrom

Computercraft Extractor Controller

Feb 11th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.08 KB | None | 0 0
  1. --EyeDeck's extractor controller program
  2. os.loadAPI("buttonAPI")
  3.  
  4. ex = peripheral.wrap("front")
  5. cvt = peripheral.wrap("AdvancedGears_2")
  6. coil = peripheral.wrap("AdvancedGears_3")
  7. mon = peripheral.wrap("left")
  8.  
  9. mon.setTextScale(0.5)
  10. mon.setBackgroundColor(colors.black)
  11. mon.setTextColor(colors.white)
  12. mon.clear()
  13.  
  14. coil.setSpeed(4096)
  15. coil.setTorque(0)
  16.  
  17. buttonAPI.drawButton(mon,2,2,16,7,colors.black,colors.lime,"Start","startstop")
  18. buttonAPI.drawButton(mon,20,2,16,7,colors.black,colors.blue,"Manual","manualauto")
  19.  
  20. isOn = false
  21. autoOn = false
  22.  
  23. --Returns right-most item in extractor, else if empty returns false
  24. function notEmpty()
  25.   for i = 4,1,-1 do
  26.     local item = ex.getStackInSlot(i)
  27.     if item ~= nil then
  28.       return item
  29.     end
  30.   end
  31.  
  32.   return false
  33. end
  34.  
  35. -- Prints a message to the console and halts execution until given handle exists
  36. -- too lazy to implement yet
  37. function sanityCheck(handle)
  38. end
  39.  
  40. function updateDisplay(name)
  41.   local old_disp = term.redirect(mon)
  42.   local power_in_hours = math.floor(coil.getEnergy() / 37507786621 * 100)*.01
  43.  
  44.   term.setCursorPos(2,10)
  45.   term.setTextColor(colors.white)
  46.   -- extra spaces are a hack to clear the last item if necessary ("Ammonium Chloride Solution" is long)
  47.   write("Extracting: ".. name .."                     \n                       ")
  48.   term.setCursorPos(2,12)
  49.   write("Power remaining: ".. power_in_hours .."h         ")
  50.   term.redirect(old_disp)
  51. end
  52.  
  53. function exLoop()
  54.   while true do
  55.     if isOn then
  56.       print("beginning extraction")
  57.       jammed = false
  58.      
  59.       while isOn do
  60.         local currentItem = notEmpty()
  61.        
  62.         if currentItem ~= false and jammed == false then
  63.           updateDisplay(currentItem.display_name)
  64.          
  65.           cvt.setRatio(1)
  66.           coil.setTorque(4096)
  67.           sleep(4.2)
  68.          
  69.           cvt.setRatio(32)
  70.           coil.setTorque(256)
  71.           sleep(2.85)
  72.         else
  73.           if jammed == true then
  74.             updateDisplay("Jammed (check output)")
  75.           else
  76.             updateDisplay("Idle")
  77.           end
  78.          
  79.           if autoOn then
  80.              coil.setTorque(0)
  81.             sleep(8)
  82.           else
  83.             isOn = false
  84.             buttonAPI.drawButton(mon,2,2,16,7,colors.black,colors.lime,"Start")
  85.           end
  86.         end
  87.       end
  88.    
  89.     coil.setTorque(0)
  90.     print("ceasing extraction")
  91.  
  92.     else -- isOn == false
  93.       sleep(2)
  94.     end
  95.   end
  96. end
  97.  
  98. function uiLoop()
  99.   while true do
  100.     event, _, x, y = os.pullEvent("monitor_touch")
  101.     local button_name = buttonAPI.getButton(x,y)
  102.     --print(button_name .. " pressed at " .. x .. "," .. y)
  103.    
  104.     if button_name == "startstop" then
  105.       if isOn == false then
  106.         buttonAPI.drawButton(mon,2,2,16,7,colors.black,colors.red,"Stop")
  107.         isOn = true
  108.       else
  109.         buttonAPI.drawButton(mon,2,2,16,7,colors.black,colors.lime,"Start")
  110.         isOn = false
  111.       end
  112.     elseif button_name == "manualauto" then
  113.       if autoOn == false then
  114.         buttonAPI.drawButton(mon,20,2,16,7,colors.black,colors.yellow,"Automatic")
  115.         autoOn = true
  116.       else
  117.         buttonAPI.drawButton(mon,20,2,16,7,colors.black,colors.blue,"Manual")
  118.         autoOn = false
  119.       end
  120.     end
  121.   end  
  122. end
  123.  
  124. --[[
  125.   While main function is active, if the item in the output slot remains
  126.   unchanged for ~12 seconds or longer, assume the machine has jammed
  127.   and set a variable to stop the main loop.
  128. --]]
  129. function jamLoop()
  130.   local jamCount = 0
  131.   local lastItem = nil
  132.   local item = nil
  133.  
  134.   while true do
  135.     if isOn then
  136.       item = ex.getStackInSlot(8)
  137.  
  138.       if item ~= nil then
  139.         if lastItem == item then
  140.           jamCount = jamCount + 1
  141.         else
  142.           jamCount = 0
  143.         end
  144.        
  145.         lastItem = item
  146.        
  147.         if jamCount >= 3 then
  148.           jammed = true
  149.         end
  150.       end
  151.     end
  152.    
  153.     sleep(4)
  154.   end
  155. end
  156.  
  157. if (notEmpty() ~= false) then
  158.   updateDisplay(notEmpty().display_name)
  159. else
  160.   updateDisplay("Idle")
  161. end
  162.  
  163. parallel.waitForAll(uiLoop, exLoop, jamLoop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement