Advertisement
mycosis

Computercraft - Energy cell percentage calculation

May 30th, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.34 KB | None | 0 0
  1. -- Settings
  2. monitorSide = "back"
  3. consoleMonitor = "top"
  4. modemSide = "bottom"
  5. redstoneSide = "left"
  6. energylow = 10 -- When the motors should be turned on (percent)
  7. energyhigh = 95 -- When the motors should be turned off (percent)
  8. energyCellLow = 0 -- Lowest energy cell id
  9. energyCellHigh = 63 -- Highest energy cell id
  10. sleepCycle = 60 -- Seconds to sleep between each loop
  11.  
  12. -- Variables
  13. mon = peripheral.wrap(monitorSide)
  14. monX, monY = mon.getSize()
  15. cmon = peripheral.wrap(consoleMonitor)
  16.  
  17. -- initialization
  18. redstone.setOutput(redstoneSide,false) -- Disabled by default
  19. if(rednet.isOpen(modemSide) == "False") then -- Rednet
  20.     rednet.open(modemSide)
  21. end
  22.  
  23. -- Functions
  24. function clear() -- Clear screen
  25.     mon.setBackgroundColor(colors.black)
  26.     mon.clear()
  27.     mon.setCursorPos(1,1)
  28. end
  29.  
  30. function drawText(x, y, text, color_txt, color_bg) -- Write text on monitor
  31.     mon.setBackgroundColor(color_bg)
  32.     mon.setTextColor(color_txt)
  33.     mon.setCursorPos(x,y)
  34.     mon.write(text)
  35. end
  36.  
  37. function drawLine(x, y, length, size, color_bar) -- Write progress bar on monitor
  38.     for yPos = y, y+size-1 do
  39.         mon.setBackgroundColor(color_bar)
  40.         mon.setCursorPos(x, yPos)
  41.         mon.write(string.rep(" ", length))
  42.     end
  43. end
  44.  
  45. function drawProg(x, y, name, length, size, minVal, maxVal, color_bar, color_bg) -- Draw actual progress bar
  46.     drawLine(x, y, length, size, color_bg)
  47.     local barSize = math.floor((minVal/maxVal)*length)
  48.     drawLine(x, y, barSize, size, color_bar)
  49.     local text = name.." "..math.floor((minVal/maxVal)*100).."%"
  50.     drawText(monX/2-#text/2+1, y+size/2, text, colors.black, color_bar)
  51. end
  52.  
  53. function drawConsoleMon(cells, energyPercent, energyColor, invalidCells, redstoneSignal)
  54.  cmon.clear()
  55.  cmon.setBackgroundColor(colors.black)
  56.  cmon.setCursorPos(1,1)
  57.  cmon.setTextColor(colors.white)
  58.  cmon.write("C: "..cells)
  59.  cmon.setCursorPos(1,2)
  60.  cmon.write("E: ")
  61.  cmon.setTextColor(energyColor)
  62.  cmon.write(energyPercent.."%")
  63.  cmon.setTextColor(colors.white)
  64.  cmon.setCursorPos(1,3)
  65.  cmon.write("I: "..invalidCells)
  66.  cmon.setCursorPos(1,4)
  67.  cmon.write("R: ")
  68.  cmon.setTextColor(colors.black)
  69.  if redstoneSignal == "On" then
  70.    cmon.setTextColor(colors.green)
  71.   elseif redstoneSignal == "Off" then
  72.    cmon.setTextColor(colors.red)
  73.  end
  74.  cmon.write(redstoneSignal)
  75. end
  76.  
  77. -- Doing stuff!
  78. while true do
  79.     -- Check stored energy on all nodes
  80.     storedEnergy = 0
  81.     maxEnergy = 0
  82.     i = energyCellLow
  83.     invalidCells = 0
  84.     while i <= energyCellHigh do
  85.         local cellid = "tile_thermalexpansion_cell_resonant_name_"..math.floor(i)
  86.         if peripheral.isPresent(cellid) == true then
  87.             local lES = peripheral.call(cellid,"getEnergyStored","south")
  88.             local lMES = peripheral.call(cellid,"getMaxEnergyStored","south")
  89.             storedEnergy = storedEnergy + lES
  90.             maxEnergy = maxEnergy + lMES
  91.         else
  92.             invalidCells = invalidCells + 1
  93.         end
  94.         i = i + 1
  95.     end
  96.  cellCount = i
  97.  
  98.     -- Energy controller
  99.     energyPercent = math.floor((storedEnergy/maxEnergy)*100)
  100.     if energyPercent < energylow then
  101.         redstone.setOutput(redstoneSide,true)
  102.     elseif energyPercent >= energyhigh then
  103.         redstone.setOutput(redstoneSide,false)
  104.     end
  105.    
  106.     -- Progress bar colour
  107.     if energyPercent <= 10 then
  108.         progColor = colors.red
  109.     elseif energyPercent <= 30 then
  110.         progColor = colors.orange
  111.     else
  112.         progColor = colors.green
  113.     end
  114.    
  115.     -- vars
  116.     if redstone.getOutput(redstoneSide) == true then
  117.         redstoneSignal = "On"
  118.     else
  119.         redstoneSignal = "Off"
  120.     end
  121.    
  122.     clear() -- Draw progress time!
  123.  drawConsoleMon(cellCount, energyPercent, progColor, invalidCells, redstoneSignal)
  124.     drawProg(2, 2, "Battery charge", monX-2, 3, storedEnergy, maxEnergy, progColor, colors.gray)
  125.     datestamp = "Day: "..os.day().." - Time: "..os.time()
  126.     term.clear()
  127.     term.setCursorPos(1,1)
  128.     term.write(datestamp)
  129.     term.setCursorPos(1,2)
  130.     term.write("Energy: "..energyPercent.."% - Invalid cells: "..math.floor(invalidCells))
  131.     term.setCursorPos(1,3)
  132.     term.write("Redstone signal ("..redstoneSide.."): "..redstoneSignal)
  133.     term.setCursorPos(1,4)
  134.     term.write("Press and hold Ctrl+T to terminate script.")
  135.     term.setCursorPos(1,6)
  136.     term.write("Next update in")
  137.    
  138.     i = 0
  139.     while i <= (sleepCycle - 1) do
  140.         term.setCursorPos(15,6)
  141.         term.write("            ")
  142.         term.setCursorPos(15,6)
  143.         term.write(" "..math.floor(sleepCycle-i).." Second(s)")
  144.         i = i + 1
  145.         sleep(1)
  146.     end
  147.  
  148.  term.setCursorPos(1,7)
  149.  term.write("Updating status...")
  150. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement