Advertisement
Guest User

inductionDisplay.lua

a guest
May 13th, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.71 KB | None | 0 0
  1. --               [EnergyMonitor]
  2. -- for Mekanism's Inductionmatrix (by BrainGamer)
  3. --
  4. --                 [IMPORTANT]
  5. -- This requires the graphing lib by BrainGamer (use the Installer and type "graphing")
  6. -- This is coded to fit a 5 wide and 4 high display (Tier 3)
  7. --
  8. --                 [Options]
  9. --
  10. -- Energy unit (RF, EU, MJ, J)
  11. energyUnit = "RF"   -- Default: RF
  12. -- Refreshtime (Time between refreshes)
  13. refreshTime = 0.5   -- Default: 0.5
  14. --
  15. --         [DONT CHANGE THE CODE BELOW]
  16. --------------------------------------------------
  17. --------------------------------------------------
  18. --
  19. --  [Functions]
  20.  
  21.  
  22. function round(x, dez)  -- round floats
  23.   local m = 10^(dez or 0)
  24.   return ((x * m + 0.5) - (x * m + 0.5) % 1) / m
  25. end
  26.  
  27. function adjustUnit(value, unit)  -- prevent big numbers
  28.   if value >= 1000000000000 then
  29.     return tostring(round(value/1000000000000, 2)) .. "T" .. unit
  30.   elseif value >= 1000000000 then
  31.     return tostring(round(value/1000000000, 2)) .. "G" .. unit
  32.   elseif value >= 1000000 then
  33.     return tostring(round(value/1000000, 2)) .. "M" .. unit
  34.   elseif value >= 1000 then
  35.     return tostring(round(value/1000, 2)) .. "k" .. unit
  36.   else
  37.     return tostring(round(value)) .. unit
  38.   end
  39. end
  40.  
  41. function adjustTime(time)  -- prevent big numbers
  42.   if time > 8553600 then
  43.     return "99+ days"
  44.   elseif time > 172800 then
  45.     return tostring(round(time/86400,1)) .. " days"
  46.   elseif time > 7200 then
  47.     return tostring(round(time/3600,1)) .. " hours"
  48.   elseif time > 120 then
  49.     return tostring(round(time/60,1)) .. " min."
  50.   else
  51.     return tostring(time,1) .. " sec."
  52.   end
  53. end
  54.  
  55. function removeOverflow(input, maxNum)  -- delete old data
  56.   while #input > maxNum do
  57.     table.remove(input,maxNum+1)
  58.   end
  59.   return input
  60. end
  61.  
  62. function display()  -- display
  63.  
  64.   --  get all data
  65.   local maxTransfer = tonumber(getTransferCap) * energyMultiplier
  66.   local maxStored = tonumber(getMaxEnergy) * energyMultiplier
  67.   local curOutput = tonumber(getOutput) * energyMultiplier
  68.   local curInput = tonumber(getInput) * energyMultiplier
  69.   local stored = tonumber(getEnergy) * energyMultiplier
  70.   table.insert(output,1,curOutput)
  71.   table.insert(input,1,curInput)
  72.   output = removeOverflow(output,40)
  73.   input = removeOverflow(input,40)
  74.  
  75.   --  generate texts
  76.   local text_i = "INPUT: " .. adjustUnit(input[1], energyUnit) .. "/t"
  77.   local len_i = text_i:len()
  78.  
  79.   local text_o = "OUTPUT: " .. adjustUnit(output[1], energyUnit) .. "/t"
  80.   local len_o = text_o:len()
  81.  
  82.   local text_t = "---"
  83.   if curOutput < curInput then
  84.     local ticks = (maxStored - stored) / (curInput - curOutput) / 20
  85.     text_t = "CHARGED IN: " .. adjustTime(ticks)
  86.   elseif curOutput > curInput then
  87.     local ticks = stored / (curOutput - curInput) / 20
  88.     text_t = "EMPTY IN: " .. adjustTime(ticks)
  89.   end
  90.   local len_t = text_t:len()
  91.  
  92.   local text_s = "ENERGY STORED: " .. adjustUnit(stored, energyUnit)
  93.   local len_s = text_s:len()
  94.  
  95.   --  clear textfields
  96.   gpu.setBackground(0x343434)
  97.   gpu.setForeground(0xEFEFEF)
  98.   gpu.fill(1,13,x,2," ")
  99.   gpu.fill(x/2,2,2,11," ")
  100.   gpu.fill(1,25,x,1," ")
  101.  
  102.   --  print texts
  103.   gpu.set((x/2)-(len_s/2),25,text_s)
  104.   gpu.setForeground(0x0ADF0A)
  105.   gpu.set((x/4)-(len_i/2),13,text_i)
  106.   gpu.setForeground(0xFF0A0A)
  107.   gpu.set((x-(x/2))+(len_o/2),13,text_o)
  108.   if curOutput < curInput then
  109.     gpu.setForeground(0x0ADF0A)
  110.     gpu.set((x/2)-(len_t/2),14,text_t)
  111.   elseif curOutput > curInput then
  112.     gpu.setForeground(0xFF0A0A)
  113.     gpu.set((x/2)-(len_t/2),14,text_t)
  114.   else
  115.     gpu.setForeground(0xEFEFEF)
  116.     gpu.set((x/2)-(len_t/2)+1,14,text_t)
  117.   end
  118.  
  119.   --  draw graphs/diagrams (graphing lib required!)
  120.   g.verticalDiagram(2,2,x/2,11,0,30,input,maxTransfer,0x0ADF0A,0x0A0A0A)
  121.   g.horizontalBar(2,15,x-2,10,stored,maxStored,0xFE9A00,0x0A0A0A,0xEFEFEF,"("..tostring(round(stored/maxStored*100,2)).."%)")
  122.   g.verticalDiagram(x/2+2,2,x,11,0,30,output,maxTransfer,0xFF0A0A,0x0A0A0A)
  123. end
  124.  
  125. --  [Main Code]
  126.  
  127. --    [INIT]
  128. computer = require("computer")
  129. comp = require("component")
  130. g = require("graphing")
  131. ev = require("event")
  132. --s = comp.proxy(comp.list("induction_matrix")())
  133. screen = comp.screen
  134. gpu = comp.gpu
  135.  
  136. local getTransferCap
  137. local getMaxEnergy
  138. local getOutput
  139. local getInput
  140. local getEnergy
  141.  
  142. local function assignVals(_,_,_,_,_,getCap, getMax, getOut, getIn, getEner)
  143.     getTransferCap = getCap
  144.     getMaxEnergy = getMax
  145.     getOutput = getOut
  146.     getInput = getIn
  147.     getEnergy = getEner
  148. end
  149.  
  150.  
  151. m = comp.modem
  152. ev.listen("modem", assignVals)
  153. m.open(123)
  154.  
  155.  
  156.  
  157.  
  158. energyMultiplier = 0
  159. x = 129/2
  160. y = 50/2
  161.  
  162. --  Screen setup
  163. gpu.setResolution(x,y)
  164. screen.setTouchModeInverted(true)
  165.  
  166. --  use the right multiplier
  167. if energyUnit == "RF" then
  168.   energyMultiplier= 0.4
  169. elseif energyUnit == "EU" then
  170.   energyMultiplier= 0.1
  171. elseif energyUnit == "MJ" then
  172.   energyMultiplier= 0.04
  173. else
  174.   energyMultiplier= 1
  175. end
  176.  
  177. -- create tables for diagrams
  178. output = {}
  179. input = {}
  180. for i=1,40 do
  181.   table.insert(output,1,tonumber(getOutput) * energyMultiplier)
  182.   table.insert(input,1,tonumber(getInput) * energyMultiplier)
  183. end
  184.  
  185. -- clear screen
  186. gpu.setBackground(0x343434)
  187. gpu.fill(1,1,x,y," ")
  188.  
  189. repeat  --  repeat until a button is pressed
  190.  
  191.   --  test if everything is working
  192.   if not pcall(display) then  --  an error occured
  193.     gpu.setBackground(0x343434)
  194.     gpu.setForeground(0xFF00CC)
  195.     gpu.fill(1,1,x,y," ")
  196.     local text = "CONNECTION LOST"
  197.     local len = text:len()
  198.     gpu.set((x/2)-(len/2),y/2,text)
  199.     computer.beep(800,0.3)
  200.     e = ev.pull(1, "key_down")
  201.   else                          -- everything fine
  202.     e = ev.pull(refreshTime, "key_down")
  203.   end
  204. until e
  205.  
  206. --  reset screen and reboot
  207. screen.setTouchModeInverted(false)
  208. gpu.setResolution(x*2,y*2)
  209. computer.shutdown(true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement