Advertisement
granddave

Control System v1

Apr 12th, 2015
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.93 KB | None | 0 0
  1. mon.clear()
  2. mon.setBackgroundColor(colors.black)
  3.  
  4. function program()
  5.     while true do
  6.  
  7.        --Variables--
  8.     refresh         = 1
  9.     mon             = peripheral.wrap("monitor_0")
  10.     monX, monY      = mon.getSize()
  11.     reactor         = peripheral.wrap("BigReactors-Reactor_0")
  12.     turbine         = peripheral.wrap("BigReactors-Turbine_0")
  13.     turbEnergy      = round(math.floor(turbine.getEnergyStored())/100000,1)
  14.     turbEnergyTick  = round(math.floor(turbine.getEnergyProducedLastTick()),1)
  15.     reacCaseTemp    = round(math.floor(reactor.getCasingTemperature()),1)
  16.     reacFuelTemp    = round(math.floor(reactor.getFuelTemperature()),1)
  17.     mon             = peripheral.wrap("monitor_0")
  18.     tank            = peripheral.wrap("rcirontankvalvetile_0")
  19.     lever           = rs.getInput("right")
  20.     --Energi variabler
  21.     totEnergyLevel  = 0
  22.     totStorage      = 0
  23.     cells           = {
  24.         peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_2"),
  25.         peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_3"),
  26.         peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_4"),
  27.         peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_5"),
  28.         peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_6"),
  29.         peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_7"),
  30.     }
  31.  
  32.     ----Functions----
  33.  
  34.     --GUI--
  35.     function drawText(x, y, text, color_txt, color_bg)
  36.         mon.setBackgroundColor(color_bg)
  37.         mon.setTextColor(color_txt)
  38.         mon.setCursorPos(x, y)
  39.         mon.write(text)
  40.     end
  41.     function drawLine(x, y, length, size, color_bar)
  42.         for yPos = y, y+size-1 do
  43.             mon.setBackgroundColor(color_bar)
  44.             mon.setCursorPos(x, yPos)
  45.             mon.write(string.rep(" ", length))
  46.         end
  47.     end
  48.     function drawProg(x, y, name, length, size, minVal, maxVal, color_bar, color_bg)
  49.         drawLine(x, y, length, size, color_bg)
  50.         local barSize = math.floor((minVal/maxVal)*length)
  51.         drawLine(x, y, barSize, size, color_bar)
  52.         local text = name.." "..math.floor((minVal/maxVal)*100).."%"
  53.         if barSize > monX/2+#text/2 then
  54.             drawText(monX/2-#text/2+1, y+size/2, text, colors.white, color_bar)
  55.         elseif  barSize > #text then
  56.             drawText((x+barSize)-#text, y+size/2, text, colors.white, color_bar)
  57.         else
  58.             drawText(monX/2-#text/2+1, y+size/2, text, colors.white, color_bg)
  59.         end  
  60.     end
  61.     function refreshMon()
  62.         mon.setBackgroundColor(colors.black)
  63.         mon.clear()
  64.         mon.setCursorPos(1,1)
  65.     end
  66.     function refreshTerm()
  67.         term.setBackgroundColor(colors.black)
  68.         term.clear()
  69.         term.setCursorPos(1,1)
  70.     end
  71.     --Layout functions--
  72.     function round(val, decimal)
  73.         if (decimal) then
  74.             return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
  75.         else
  76.             return math.floor(val+0.5)
  77.         end
  78.     end
  79.     function writeStats(x, y, text, var)
  80.         mon.setCursorPos(x, y)
  81.         mon.setTextColor(colors.white)
  82.         mon.write(text)
  83.         mon.setTextColor(colors.lime)
  84.         mon.write(var)
  85.     end
  86.     --Energi--
  87.     function getEnergyStatus(t)
  88.         for i,v in ipairs(t) do
  89.             totStorage = totStorage + v.getMaxEnergyStored()
  90.         end
  91.         for i,v in ipairs(t) do
  92.             totEnergyLevel = totEnergyLevel + v.getEnergyStored()
  93.         end
  94.         totPercent = (100*totEnergyLevel/totStorage)
  95.         return totPercent, totEnergyLevel, totStorage
  96.     end
  97.     --Reactor+Turbine--
  98.     function setActive(state)
  99.         turbine.setActive(state)
  100.         reactor.setActive(state)
  101.     end
  102.     --Vattentank--
  103.     function updateTank(tankPeriph)
  104.         local tableInfo = tankPeriph.getTankInfo("unknown") -- Local to the getTank function.
  105.        
  106.         fluidRaw = nil      --for clarity
  107.         fluidName = nil     --for clarity
  108.         fluidAmount = nil   --for clarity
  109.         fluidCapacity = tableInfo[1].capacity
  110.        
  111.         local contents = tableInfo[1].contents
  112.        
  113.         if contents then
  114.             fluidRaw = contents.rawName
  115.             fluidAmount = contents.amount
  116.             fluidName = contents.name
  117.         end
  118.         cap = fluidCapacity / 1000
  119.         amount = fluidAmount
  120.         tankPercentFull = 0
  121.         if amount == nil then
  122.             amount = 0
  123.             tankPercentFull = 0
  124.           else
  125.             -- Use math.floor to convert to integers
  126.             amount = math.floor(amount / 1000)
  127.             tankPercentFull = math.floor(100 * amount / cap)
  128.         end
  129.         return tankPercentFull
  130.     end
  131.  
  132.     ----------Main Program----------
  133.  
  134.     updateTank(tank)
  135.     local energyLevel = getEnergyStatus(cells)
  136.  
  137.  
  138.     refreshMon()
  139.     refreshTerm()
  140.     print("Program is running")
  141.     print("Press backspace to exit")
  142.  
  143.  
  144.  
  145.     -----Water Refill-----
  146.  
  147.     if lever == true then
  148.         setActive(true)
  149.         else
  150.         setActive(false)
  151.     end
  152.     if tankPercentFull == 5 then
  153.     --if lever == true then
  154.         setActive(false)
  155.         repeat
  156.             drawText(10, 1, "Refilling water tank...", colors.green, colors.black)
  157.             drawText(140, 2, "Reactor is ", colors.green, colors.black)
  158.             mon.setTextColor(colors.red)
  159.             mon.write("off")
  160.             mon.setTextColor(colors.green)
  161.            
  162.             updateTank(tank)
  163.             drawProg(2, 6, "Water level", monX-2, 3, tankPercentFull, 100, colors.blue, colors.gray)
  164.             sleep(1)
  165.         until (tankPercentFull > 2)
  166.         refreshMon()
  167.     end
  168.  
  169.     --Auto active reactor--
  170.  
  171.     if energyLevel < 10 then
  172.         setActive(true)
  173.     elseif energyLevel > 92 then
  174.         setActive(false)
  175.     end
  176.  
  177.     -----UI-----
  178.  
  179.     writeStats(1, 1, "Reac Active: ", reactor.getActive())
  180.     writeStats(21,1, "Turb Active: ", turbine.getActive())
  181.     writeStats(1, 2, "RF/T:        ", turbEnergyTick)
  182.     writeStats(21,2, "Turb Energy: ", turbEnergy)
  183.     writeStats(1, 3, "Casing Heat: ", reacCaseTemp)
  184.     writeStats(21, 3,"Fuel Heat:   ", reacFuelTemp)
  185.  
  186.     --debug--
  187.     --for i = 1, 100 do
  188.     --    drawProg(2, 4, "Test level", monX-2, 3, i, 100, colors.red, colors.gray)
  189.     --    sleep(0.1)
  190.     --end
  191.     ---------
  192.  
  193.  
  194.     --Energy bar--
  195.     drawProg(2, 5, "Energy level", monX-2, 3, energyLevel, 100, colors.red, colors.gray)
  196.     --Watertank bar--
  197.     drawProg(2, 9, "Water level", monX-2, 3, tankPercentFull, 100, colors.blue, colors.gray)
  198.  
  199.  
  200.     ------------Endstuff---------------
  201.     sleep(refresh)
  202.     end
  203. end
  204.  
  205. function exitProgram()
  206.     repeat
  207.         local ev, key = os.pullEvent('key')
  208.     --shutdown with backspace--
  209.     until key == keys.backspace
  210. end
  211. parallel.waitForAny(program, exitProgram)
  212.  
  213. --The last words...--
  214. mon.setBackgroundColor(colors.black)
  215. mon.clear()
  216. mon.setTextColor(colors.gray)
  217. mon.setCursorPos((monX/2)-9, monY/2)
  218. mon.write("Program is offline...")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement