Savage_Me55iah

CC_PRESSURE_CONTROL

Mar 25th, 2015
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.49 KB | None | 0 0
  1. --Script by Savage_Me55iah of CraftAU.com.au and Nuqube.co--
  2. local version, pastebinCode = "v1.0", "0YGWjwyc"
  3.  
  4. --Other Savage_Me55iah scripts @ http://pastebin.com/u/Savage_Me55iah
  5.  
  6. --INSTRUCTIONS ON USE--
  7. --Instruction video on http://youtube.com/savagemessiah5
  8.  
  9. --Contact Savage_Me55iah @ CraftAU.com.au or Nuqube.co or http://www.computercraft.info/forums2/ with suggestions/fixes/bugs
  10.  
  11. -- SETUP --
  12.  
  13. local tArgs = {...}
  14. local update = tostring(tArgs[1]) or nil
  15.  
  16. id = os.getComputerID()
  17. label = os.getComputerLabel() or false
  18. if label == false then
  19.     print("Please Type label here: ")
  20.     os.setComputerLabel(tostring(read()))
  21. end
  22.  
  23. function boolean_check(params)
  24.     local answer = nil
  25.     if tostring(params) == "true" then
  26.         answer = true
  27.     else
  28.         answer = false
  29.     end
  30.     return answer
  31. end
  32.  
  33. local settings_file = "config"
  34.  
  35. local settings_template = [[-- adjust these configuration options as necessary.
  36. -- IMPORTANT!, API won't load if there are spaces in the values
  37. COMPUTER_LABEL = "${label}"
  38. COMPUTER_ID = ${id}
  39.  
  40. UPPER_THRESHOLD = ${upper_threshold}
  41. LOWER_THRESHOLD = ${lower_threshold}
  42. UPDATE_TIME = ${update_time}
  43. OUTPUT_SIDE = "${output_side}"
  44. INVERT_REDSTONE = "${invert_redstone}"
  45. ]]
  46.  
  47. local function interpolate(s, params)
  48.     return s:gsub('($%b{})', function(w) return params[w:sub(3, -2)] or w end)
  49. end
  50.  
  51. do  
  52.     save_settings = function()
  53.         local filehandler = fs.open(tostring(settings_file), "w")
  54.    
  55.         settings = {
  56.             label = COMPUTER_LABEL or os.getComputerLabel(),
  57.             id = os.getComputerID(),
  58.             upper_threshold = UPPER_THRESHOLD or 3.5,
  59.             lower_threshold = LOWER_THRESHOLD or 3,
  60.             update_time = UPDATE_TIME or 1,
  61.             output_side = OUTPUT_SIDE or "bottom",
  62.             invert_redstone = INVERT_REDSTONE or "false"
  63.             }
  64.  
  65.         filehandler.write(interpolate(settings_template, settings))
  66.         filehandler.close()
  67.     end
  68. end
  69.  
  70. if fs.exists(settings_file) == false then
  71.     save_settings()
  72.     return
  73. else
  74.     os.unloadAPI(settings_file)
  75.  
  76.     if os.loadAPI(settings_file) == false then print("ERROR: Could not load the settings file!") end
  77.  
  78.     CONFIG = nil
  79.  
  80.     for k, v in pairs(_G) do
  81.         if k == settings_file then CONFIG = v break end
  82.     end
  83.  
  84.     if CONFIG == nil then log_add("CONFIG came up nil", true) end
  85. end
  86.  
  87. function find_peripheral(t, e)
  88.   local e = e or nil
  89.   local sides = {[1] = "front", [2] = "back", [3] = "left", [4] = "right", [5] = "top", [6] = "bottom"}
  90.   local peripheral_found = false
  91.   for num=1, 6 do
  92.     local type = peripheral.getType(sides[num])
  93.     if type == t then
  94.       if e == "wireless" then
  95.         if peripheral.call(sides[num], "isWireless") then
  96.           return sides[num]
  97.         end
  98.       elseif e == "remote" then
  99.         if not peripheral.call(sides[num], "isWireless") then
  100.           return sides[num]
  101.         end
  102.       elseif e == nil then
  103.         return sides[num]
  104.       end
  105.     end
  106.   end
  107.   if peripheral_found ~= true then
  108.     error("No " .. t .. " attached to the computer", 0)
  109.   end
  110. end
  111.  
  112. pcm = peripheral.wrap(find_peripheral("pneumaticMachine"))
  113. local active = nil
  114.  
  115. function run()
  116.     if tonumber(pcm.getPressure()) > tonumber(CONFIG.UPPER_THRESHOLD) then active = false end
  117.     if pcm.getPressure() < tonumber(CONFIG.LOWER_THRESHOLD) then active = true end
  118.     if boolean_check(CONFIG.INVERT_REDSTONE) then active = not active end
  119.     redstone.setOutput(tostring(CONFIG.OUTPUT_SIDE), boolean_check(active))
  120. end
  121.        
  122.  
  123. if update ~= "update" then
  124.     while true do
  125.         os.startTimer(tonumber(CONFIG.UPDATE_TIME))
  126.         term.clear()
  127.         term.setCursorPos(1,1)
  128.         print("NAME: "..os.getComputerLabel())
  129.         print("ID: "..os.getComputerID())
  130.         print("ACTIVE: "..tostring(active))
  131.                 print("PRESSURE: "..pcm.getPressure())
  132.         print("THRESHOLD UPPER: "..tostring(CONFIG.UPPER_THRESHOLD))
  133.         print("THRESHOLD LOWER: "..tostring(CONFIG.LOWER_THRESHOLD))
  134.         print("UPDATE TIMER: "..tostring(CONFIG.UPDATE_TIME))
  135.         print("INVERT REDSTONE: "..tostring(CONFIG.INVERT_REDSTONE))
  136.         os.pullEvent()
  137.         run()
  138.     end
  139. else
  140.     print("UPDATING")
  141.     shell.run("pastebin get "..pastebinCode.." update")
  142.     if fs.exists("update") then
  143.         fs.delete(shell.getRunningProgram())
  144.         fs.copy("update", shell.getRunningProgram())
  145.         fs.delete("update")
  146.         print("UPDATE SUCCESS")
  147.     else
  148.         print("UPDATE FAILED")
  149.     end
  150.     sleep(2)
  151.     os.reboot()
  152.  end
Advertisement
Add Comment
Please, Sign In to add comment