PlowmanPlow

ComputerCraft - Induction Matrix Monitor

Feb 25th, 2016
594
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local auxChannelID = 6
  2. local emergencyChannelID = 7
  3. local statusChannelID = 5
  4. local auxPercentage = 95
  5. local emergencyPercentage = 75
  6.  
  7. -- Find Modem
  8. local modem = peripheral.find("modem", function(n, o) return o.isWireless() end)
  9.  
  10. -- Find Induction Matrix
  11. local matrix = peripheral.find("Induction Matrix")
  12. if matrix == nil then
  13.    error("Could not bind to Induction Matrix. Is one attached?")
  14. end
  15.  
  16. function round(num, idp)
  17.   local mult = 10^(idp or 0)
  18.   return math.floor(num * mult + 0.5) / mult
  19. end
  20.  
  21. term.write("Waiting for Matrix to form...")
  22. while matrix.getMaxEnergy() == "Unformed." do
  23.    term.write(".")
  24.    os.sleep(1)
  25. end
  26.  
  27. local maxEnergy = matrix.getMaxEnergy()/2.5
  28. local curEnergy = matrix.getEnergy()/2.5
  29. local curInput = matrix.getInput()/2.5
  30. local curOutput = matrix.getOutput()/2.5
  31.  
  32. local function prettyAmt(num)
  33.    if num >= 1000000000000 then
  34.       return (tostring(round(num/1000000000000, 2)) .. "T")
  35.    elseif num >= 1000000000 then
  36.       return (tostring(round(num/1000000000, 2)) .. "B")
  37.    elseif num >= 1000000 then
  38.       return (tostring(round(num/1000000, 2)) .. "M")
  39.    elseif num >= 1000 then
  40.       return (tostring(round(num/1000, 2)) .. "t")
  41.    else
  42.       return tostring(num)
  43.    end
  44. end
  45.  
  46. term.clear()
  47. term.setCursorPos(1,1)
  48. print("Monitoring Induction Matrix")
  49. print()
  50. print("Maximum stored energy: " .. maxEnergy .. " RF")
  51. print("Current stored energy: " .. string.format("%u", curEnergy) .. " RF")
  52. print()
  53. print("Auxiliary power status: Disabled")
  54. print("Emergency power status: Disabled")
  55. print()
  56. print("Press 'a' to toggle auxiliary power.")
  57. print("Press 'q' to quit.")
  58. print()
  59.  
  60. local aux = false
  61. local emergency = false
  62. local status = {}
  63. status["Power Storage Max"] = prettyAmt(maxEnergy) .. " RF"
  64. status["Power Storage"] = "0%"
  65. status["Power Auxiliary"] = "Disabled"
  66. status["Power Emergency"] = "Disabled"
  67. status["Power Input"] = tostring(round(curInput, 1)) .. " RF/t"
  68. status["Power Output"] = tostring(round(curOutput, 1)) .. " RF/t"
  69. local pollTimer = os.startTimer(0)
  70. while true do
  71.    local event, p1, p2, p3, p4 = os.pullEvent()
  72.    if event == "key" then
  73.       if p1 == keys.q then
  74.          term.setCursorPos(1,11)
  75.          return
  76.       elseif p1 == keys.a then
  77.          if aux then aux = false else aux = true end
  78.       end
  79.    elseif event == "timer" and p1 == pollTimer then
  80.       curEnergy = round(matrix.getEnergy())/2.5
  81.       curInput = round(matrix.getInput())/2.5
  82.       curOutput = matrix.getOutput()/2.5
  83.       local fillP = math.floor((curEnergy / maxEnergy) * 100)
  84.       if fillP <= auxPercentage and aux == false then
  85.          aux = true
  86.       end
  87.       if fillP <= emergencyPercentage and emergency == false then
  88.          emergency = true
  89.       end
  90.       if fillP >= 99 and (aux == true or emergency == true) then
  91.          aux = false
  92.          emergency = false
  93.       end
  94.       term.setCursorPos(1,6)
  95.       status["Power Storage"] = fillP .. "%"
  96.       if aux == true then
  97.          if modem ~= nil then modem.transmit(auxChannelID, 0, "enabled") end
  98.          term.write("Auxiliary power status: Enabled   ")
  99.          status["Power Auxiliary"] = "Enabled   "
  100.       else
  101.          if modem ~= nil then modem.transmit(auxChannelID, 0, "disabled") end
  102.          term.write("Auxiliary power status: Disabled  ")
  103.          status["Power Auxiliary"] = "Disabled  "
  104.       end
  105.       term.setCursorPos(1,7)
  106.       if emergency == true then
  107.          if modem ~= nil then modem.transmit(emergencyChannelID, 0, "enabled") end
  108.          term.write("Emergency power status: Enabled   ")
  109.          status["Power Emergency"] = "Enabled   "
  110.       else
  111.          if modem ~= nil then modem.transmit(emergencyChannelID, 0, "disabled") end
  112.          term.write("Emergency power status: Disabled  ")
  113.          status["Power Emergency"] = "Disabled  "
  114.       end
  115.       status["Power Input"] = tostring(round(curInput, 1)) .. " RF/t"
  116.       status["Power Output"] = tostring(round(curOutput, 1)) .. " RF/t"
  117.       if modem ~= nil then modem.transmit(statusChannelID, 0, textutils.serialize(status)) end
  118.       term.setCursorPos(1,4)
  119.       term.write("Current stored energy: " .. string.format("%u", curEnergy) .. " RF        ")
  120.       pollTimer = os.startTimer(5)
  121.    end
  122. end
RAW Paste Data