choekstr

ComputerCraft EU Reader & Display

Feb 19th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.54 KB | None | 0 0
  1. mon = peripheral.wrap("right")  --define monitor
  2. mon.setTextScale(0.5)           --text size in 0.5 increments
  3. monX,monY = mon.getSize()
  4. rednet.open("back")             --wireless modem only.  wired just work
  5. power1 = peripheral.wrap("ic2:mfsu_2")
  6. power2 = peripheral.wrap("ic2:mfsu_3")
  7. power3 = peripheral.wrap("ic2:mfsu_4")
  8. power4 = peripheral.wrap("ic2:mfsu_1")
  9. generator1 = peripheral.wrap("ic2:matter_generator_1")
  10. local DEBUG = 1
  11. local reactorOnline = 0
  12. local turnOnAt = 30
  13. local turnOffAt = 90
  14. local maxpower1 = 0
  15. local curpower1 = 0
  16. local perpower1 = 0
  17. local maxpower2 = 0
  18. local curpower2 = 0
  19. local perpower2 = 0
  20. local maxpower3 = 0
  21. local curpower3 = 0
  22. local perpower3 = 0
  23. local maxpower4 = 0
  24. local curpower4 = 0
  25. local perpower4 = 0
  26.  
  27. function readMFSU()
  28.     maxPower1 = power1.getEUCapacity()
  29.     curPower1 = power1.getEUStored()
  30.     perPower1 = math.floor(((curPower1/maxPower1)*100)+0.5)
  31.     maxPower2 = power2.getEUCapacity()
  32.     curPower2 = power2.getEUStored()
  33.     perPower2 = math.floor(((curPower2/maxPower2)*100)+0.5)
  34.     maxPower3 = power3.getEUCapacity()
  35.     curPower3 = power3.getEUStored()
  36.     perPower3 = math.floor(((curPower3/maxPower3)*100)+0.5)
  37.     maxPower4 = power4.getEUCapacity()
  38.     curPower4 = power4.getEUStored()
  39.     perPower4 = math.floor(((curPower4/maxPower4)*100)+0.5)
  40.     avgPower  = math.floor(((curPower1+curPower2+curPower3+curPower4)/(maxPower1*4)*100)+0.5)
  41. end
  42.  
  43. function centerT(text,line,backColor,txtColor)
  44.     mon.setBackgroundColor(backColor)
  45.     mon.setTextColor(txtColor)
  46.     length = (string.len(text))
  47.     dif = math.floor(monX-length)
  48.     x = math.floor(dif/2)
  49.     mon.setCursorPos(x+1,line)
  50.     mon.write(text)
  51. end
  52.  
  53. function autoUU(per)
  54.     if per < 5 then
  55.         redstone.setOutput("left",true)
  56.         print("UUMatter : OFF")
  57.         title = "  UUMatter Status: OFF  "
  58.         centerT(title,4,colors.red,colors.white)
  59.     elseif per >= 5 then
  60.         redstone.setOutput("left",false)
  61.         print("UUMatter : ON")
  62.         title = "  UUMatter Status: ON  "
  63.         centerT(title,4,colors.black,colors.green)
  64.     end
  65. end
  66.  
  67. function drawBar(current,max,per,title,Y)
  68.     if per > 70 then
  69.         color=colors.green
  70.     elseif (per >40) and (per < 70) then
  71.         color=colors.orange
  72.     elseif per < 40 then
  73.         color=colors.red
  74.     end
  75.    
  76.     bar = math.floor(((current/max)*(monX-2))+0.5)
  77.      --Draw Background Bar
  78.      mon.setCursorPos(2,Y)
  79.      mon.setBackgroundColor(colors.black)
  80.      mon.write(title.." MFSU")
  81.      mon.setCursorPos(2,(Y+1))
  82.      mon.setBackgroundColor(colors.blue)
  83.      mon.write(string.rep(" ", monX-2))
  84.      --Draw Percentage Bar
  85.      mon.setCursorPos(2,(Y+1))
  86.      mon.setBackgroundColor(color)
  87.      mon.write(string.rep(" ", bar))
  88.      mon.setCursorPos(2,(Y+1))
  89.      mon.write(current .. "/" .. max .. " (" .. per .. "%)")  
  90. end
  91.  
  92. function updateScreens()
  93.     --update computer screen
  94.     term.setCursorPos(1,1)
  95.     print("Average MFSU power: "..avgPower.."% Full")
  96.     --update monitor
  97.     mon.setCursorPos(1,1)
  98.     mon.setBackgroundColor(colors.black)
  99.     title = "  Average MFSU Power  " .. avgPower .. "% Full  "
  100.     centerT(title,1,colors.green,colors.white)
  101.     if reactorOnline == 1 then
  102.         print("Redstone Status: start")
  103.         title = "  Reactor Status: STARTED  "
  104.         centerT(title,3,colors.black,colors.green)
  105.     elseif reactorOnline == 0 then
  106.         print("Redstone Status: STOP")
  107.         title = "  Reactor Status: STOPPED  "
  108.         centerT(title,3,colors.red,colors.white)
  109.     end
  110.     if DEBUG == 1 then
  111.         print("avgPower: "..avgPower)
  112.         print("turnOnAt: "..turnOnAt)
  113.         print("turnOffAt: "..turnOffAt)
  114.         print("reactorOnline: "..reactorOnline)
  115.         print("overlay bar value: "..bar)
  116.         print("monitor XxY size: "..monX.."x"..monY)
  117.     end
  118. end
  119.  
  120. function autoReactor()
  121.     if avgPower < turnOnAt then
  122.         if reactorOnline == 0 then
  123.             rednet.broadcast("start")
  124.             reactorOnline = 1
  125.         end
  126.     end
  127.     if avgPower >= turnOffAt then
  128.         if reactorOnline == 1 then
  129.             rednet.broadcast("STOP")
  130.             reactorOnline = 0
  131.         end
  132.     end
  133. end
  134.  
  135. mon.setBackgroundColor(colors.black)
  136. mon.clear()
  137. term.clear()
  138. while true do
  139.     readMFSU()
  140.     drawBar(curPower1,maxPower1,perPower1,"1st",5)
  141.     drawBar(curPower2,maxPower2,perPower2,"2nd",8)
  142.     drawBar(curPower3,maxPower3,perPower3,"3rd",11)
  143.     drawBar(curPower4,maxPower4,perPower4,"4th",14)
  144.     autoReactor()
  145.     autoUU(avgPower)
  146.     updateScreens()
  147.     sleep(1)
  148. end
Add Comment
Please, Sign In to add comment