Kodos

[OC] Dynamic CapBank Monitor with Computronics integration

Jan 14th, 2015
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.72 KB | None | 0 0
  1. -- Version 0.51
  2.  
  3. -- Changelog:
  4. -- Added TODO List
  5. -- Mostly fixed indentation because OCD
  6. -- Updated Pastebin with current code
  7. -- Actually fixed indentation
  8. -- Made lamp colours gradients
  9.  
  10.  
  11. -- ==================================
  12.  
  13. -- TODO
  14. -- Add a way to have a second lamp track net gain/loss of power
  15. -- Make Lamp(s) optional *still*
  16. -- Refactor (Is this even the right word) term.write to use
  17. -- gpu.set instead, as well as setting it up so that only the
  18. -- values that change every update will be redrawn, instead of
  19. -- the entire GUI
  20. -- Make things prettier on the front end
  21.  
  22. -- Code Start
  23.  
  24.  
  25. local component = require("component")
  26. local term = require("term")
  27.  
  28. local gpu = component.gpu            -- Note that this program requires a T3 Screen.
  29.  
  30. local lamp = component.colorful_lamp -- As of right now, a Computronics Lamp is required.
  31.                                      -- I may take another stab at making it optional later on,
  32.                                      -- but for now, it's required or the program will not run.
  33.                                      
  34. local blue = 0x1F
  35.  
  36. local bit32 = bit32 or load([[return {
  37.     band = function(a, b) return a & b end,
  38.     bor = function(a, b) return a | b end,
  39.     bxor = function(a, b) return a ~ b end,
  40.     bnot = function(a) return ~a end,
  41.     rshift = function(a, n) return a >> n end,
  42.     lshift = function(a, n) return a << n end,
  43. }]])()  -- Thanks, Magik6k for the workaround code that I shamelessly sto- I mean borrowed!
  44.  
  45. gpu.setResolution(40,1)
  46.  
  47. local function checkBatt()
  48.   local curr = 0
  49.   for addr in component.list("capacitor_bank") do
  50.     battcheck = component.proxy(addr).getEnergyStored()
  51.     curr = curr + battcheck
  52.   end
  53.   return curr
  54. end
  55.  
  56. local function getMaxBatt()
  57.   local maxStorage = 0
  58.   for addr in component.list("capacitor_bank") do
  59.     maxcheck = component.proxy(addr).getMaxEnergyStored()
  60.     maxStorage = maxStorage + maxcheck
  61.   end
  62.   return maxStorage
  63. end
  64.  
  65. local function updateMon(curr, maxStorage)
  66.   term.clear()
  67.   term.setCursor(1,1)
  68.   io.stdout:write(curr .. "/" .. maxStorage .. " RF Stored.")
  69.   return
  70. end
  71.  
  72. local function updateLamp(curr, maxStorage)
  73.   if curr == 0 and maxStorage == 0 then
  74.     lamp.setLampColor(blue)
  75.     return
  76.   else
  77.     local perc = curr / maxStorage
  78.     local red = math.max(math.min(math.ceil(0x1C * ((1 - perc)*2)), 0x1C), 0)
  79.     local green = math.max(math.min(math.ceil(0x1F * (perc * 2)), 0x1F), 0)
  80.     lamp.setLampColor(bit32.bor(bit32.lshift(red, 10), bit32.lshift(green, 5)))
  81.   end
  82. end
  83.  
  84. function updateReactor()  -- Code WIP
  85. end
  86.  
  87. while true do
  88.   local maxStorage = getMaxBatt()
  89.   local curr = checkBatt()
  90.   updateMon(curr, maxStorage)
  91.   updateLamp(curr, maxStorage)
  92.   os.sleep(1)
  93. end
  94.  
  95. -- Code End
Add Comment
Please, Sign In to add comment