Advertisement
MoJoCreatior

Power Monitor OC:Edition v1.0

Jan 11th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.82 KB | None | 0 0
  1. --[[  Power Monitoring Program Written By: Player_Athena / MoJoCreatior
  2. This code is considered free for use both commercial and private and can be redistributed so long as it meets following criteria:
  3. 1: Player_Athena / MoJoCreatior are credited as the original authors
  4. 2: You do not try to take full ownership of the code/written program
  5. 3: If you modify the code for re-upload you must provide it under the same conditions. as in- Open Source, and credited to Player_Athena / MoJoCreatior as original author]]
  6.  
  7. --This program, in it's current state, is an Alpha version and may contain bugs
  8.  
  9. local program = "Power Monitor OC:Edition v1.0"
  10. local title = program --Change this to whatever string you would like to use, by default it just uses the name of the program itself
  11.  
  12.  
  13. --Important!!! DO NOT ALTER IN ANY WAY!! IT WILL BREAK THINGS!
  14. local component = require("component")
  15. local thread = require("thread")
  16. local event = require("event")
  17. local gpu = component.gpu
  18. gpu.setResolution(64,20)
  19. local x,y = gpu.getResolution()
  20. local deviceRF = {}
  21. local RF_devices = {}
  22. local deviceMek = {}
  23. local Mek_devices = {}
  24. local deviceIC2 = {}
  25. local IC2_devices = {}
  26. local RF = {[1]={},[2]={},[3]={}}
  27. local EU = {[1]={},[2]={},[3]={}}
  28. local kJ = {[1]={},[2]={},[3]={}}
  29. local fold = 1
  30. local totFold = 1
  31. local page = "RF"
  32. local RFc,RFp,EUc,EUp,kJc,kJp,filled = 0,0,0,0,0,0,0
  33.  
  34. --Functions
  35. local function intro()
  36.   gpu.setForeground(0x00FF00)
  37.   gpu.fill(1,1,x,y, " ")
  38.   gpu.set(3,2, program.." -Written By: Player_Athena")
  39.   os.sleep(1.5)
  40. end
  41.  
  42. local function clear()
  43.   gpu.setForeground(0xFFFFFF)
  44.   gpu.fill(1,1,x,y, "█")
  45. end
  46.  
  47. local function banner(text,y)
  48.   gpu.setForeground(0xFFFFFF)
  49.   gpu.setBackground(0x000000)
  50.   gpu.set((x-#text-3)/2,y, "  "..text.."  ")
  51.   gpu.set((x-#text-3)/2,y+1, string.rep(" ",#text+4))
  52. end
  53.  
  54. --gather devices into a table
  55. local function deviceDiscover()
  56.   for address in component.list("v_capacitor") do
  57.     table.insert(deviceRF, address)
  58.   end
  59.   for address in component.list("energy_device") do
  60.     table.insert(deviceRF, address)
  61.   end
  62.   for address in component.list("induction_matrix") do
  63.     table.insert(deviceMek, address)
  64.   end
  65.   for address in component.list("basic_energy_cube") do
  66.     table.insert(deviceMek, address)
  67.   end
  68.   for address in component.list("ic2") do
  69.     table.insert(deviceIC2, address)
  70.   end
  71.   if deviceRF[1] ~= nil then
  72.     for _,address in ipairs(deviceRF) do
  73.       table.insert(RF_devices, component.proxy(address))
  74.     end
  75.   end
  76.   if deviceMek[1] ~= nil then
  77.     for _,address in ipairs(deviceMek) do
  78.       table.insert(Mek_devices, component.proxy(address))
  79.     end
  80.   end
  81.   if deviceIC2[1] ~= nil then
  82.     for _,address in ipairs(deviceIC2) do
  83.       table.insert(IC2_devices, component.proxy(address))
  84.     end
  85.   end
  86. end
  87.  
  88. local function totalDevices()
  89.   if page == "Mek" then return #Mek_devices
  90.   elseif page == "IC2" then return #IC2_devices
  91.   elseif page == "RF" then return #RF_devices
  92.   else return "Error"
  93.   end
  94. end
  95.  
  96. local function percent(m,c)
  97.   local f = math.floor((100/m*c)+0.5)
  98.   return f
  99. end
  100.  
  101. --Logic
  102. local function totalsBarLogic()
  103.   local RFt,RFc = 0,0
  104.   local EUt,EUc = 0,0
  105.   local kJt,kJc = 0,0
  106.   local filled = 0
  107.   for i=1, #RF_devices do
  108.     RFc = RFc + RF_devices[i].getEnergyStored()
  109.   end
  110.   for i=1, #RF_devices do
  111.     RFt = RFt + RF_devices[i].getMaxEnergyStored()
  112.   end
  113.   for i=1, #IC2_devices do
  114.     EUc = EUc + IC2_devices[i].getEnergy()
  115.   end
  116.   for i=1, #IC2_devices do
  117.     EUt = EUt + IC2_devices[i].getCapacity()
  118.   end
  119.   for i=1, #Mek_devices do
  120.     kJc = kJc + Mek_devices[i].getEnergy()
  121.   end
  122.   for i=1, #Mek_devices do
  123.     kJt = kJt + Mek_devices[i].getMaxEnergy()
  124.   end
  125.   EUc = math.floor(EUc+0.5)
  126.   local RFp = percent(RFt,RFc)
  127.   local EUp = percent(EUt,EUc)
  128.   local kJp = percent(kJt,kJc)
  129.   if page == "RF" then filled = math.floor((0.6*RFp)+0.5) end
  130.   if page == "IC2" then filled = math.floor((0.6*EUp)+0.5) end
  131.   if page == "Mek" then filled = math.floor((0.6*kJp)+0.5) end
  132.   return RFc,RFp,EUc,EUp,kJc,kJp,filled
  133. end
  134.  
  135. local function nextPage()
  136.   if page == "IC2" then
  137.     page = "Mek"
  138.   elseif page == "Mek" then
  139.     page = "RF"
  140.   elseif page == "RF" then
  141.     page = "IC2"
  142.   end
  143.   fold = 1
  144.   gpu.setForeground(0x000000)
  145.   gpu.fill(4,6,16,5, "█")
  146.   gpu.fill(21,6,9,5, "█")
  147. end
  148.  
  149.  
  150.  
  151. --Folders
  152. local function folderRF()
  153.   for i=1, #RF_devices do
  154.     if i <= 5 then
  155.       RF[1][i] = percent(RF_devices[i].getMaxEnergyStored(), RF_devices[i].getEnergyStored())
  156.     end
  157.     if i >= 6 then if i <= 10 then
  158.       RF[2][i-5] = percent(RF_devices[i].getMaxEnergyStored(), RF_devices[i].getEnergyStored())
  159.     end end
  160.     if i >= 11 then if i <= 16 then
  161.       RF[3][i-10] = percent(RF_devices[i].getMaxEnergyStored(), RF_devices[i].getEnergyStored())
  162.     end end
  163.     if i >=17 then
  164.       print("error! too many devices")
  165.     end
  166.   end
  167. end
  168.  
  169. local function folderIC2()
  170.   for i=1, #IC2_devices do
  171.     if i <= 5 then
  172.       EU[1][i] = percent(IC2_devices[i].getCapacity(), IC2_devices[i].getEnergy())
  173.     end
  174.     if i >= 6 then if i <= 10 then
  175.       EU[2][i-5] = percent(IC2_devices[i].getCapacity(), IC2_devices[i].getEnergy())
  176.     end end
  177.     if i >= 11 then if i <= 16 then
  178.       EU[3][i-10] = percent(IC2_devices[i].getCapacity(), IC2_devices[i].getEnergy())
  179.     end end
  180.     if i >=17 then
  181.       print("error! too many devices")
  182.     end
  183.   end
  184. end
  185.  
  186. local function folderMek()
  187.   for i=1, #Mek_devices do
  188.     if i <= 5 then
  189.       kJ[1][i] = percent(Mek_devices[i].getMaxEnergy(), Mek_devices[i].getEnergy())
  190.     end
  191.     if i >= 6 then if i <= 10 then
  192.       kJ[2][i-5] = percent(Mek_devices[i].getMaxEnergy(), Mek_devices[i].getEnergy())
  193.     end end
  194.     if i >= 11 then if i <= 16 then
  195.       kJ[3][i-10] = percent(Mek_devices[i].getMaxEnergy(), Mek_devices[i].getEnergy())
  196.     end end
  197.     if i >=17 then
  198.       print("error! too many devices")
  199.     end
  200.   end
  201. end
  202.  
  203. local function folds()
  204.   if page == "RF" then if RF[3][1] ~= nil then totFold = 3
  205.   elseif RF[2][1] ~= nil then totFold = 2
  206.   else totFold = 1 end end
  207.   if page == "IC2" then if EU[3][1] ~= nil then totFold = 3
  208.   elseif EU[2][1] ~= nil then totFold = 2
  209.   else totFold = 1 end end
  210.   if page == "Mek" then if kJ[3][1] ~= nil then totFold = 3
  211.   elseif kJ[2][1] ~= nil then totFold = 2
  212.   else totFold = 1 end end
  213.   return totFold
  214. end
  215.  
  216. local function foldMe(i)
  217.   if i == "l" then fold = fold-1 end
  218.   if i == "r" then fold = fold+1 end
  219.   if fold == 0 then fold = 3 end
  220.   if fold == 4 then fold = 1 end
  221.   gpu.setForeground(0x000000)
  222.   gpu.fill(4,6,16,5, "█")
  223.   gpu.fill(21,6,9,5, "█")
  224. end
  225.  
  226. --Rendering
  227. local function selector()
  228.   gpu.setForeground(0x404099)
  229.   if page == "RF" then
  230.     gpu.set(35,8,"█")
  231.     gpu.setForeground(0xFFFFFF)
  232.     gpu.set(36,8,">")
  233.     gpu.setForeground(0x000000)
  234.     gpu.set(35,9,"█>")
  235.     gpu.set(35,10,"█>")
  236.   elseif page == "IC2" then
  237.     gpu.set(35,9,"█")
  238.     gpu.setForeground(0xFFFFFF)
  239.     gpu.set(36,9,">")
  240.     gpu.setForeground(0x000000)
  241.     gpu.set(35,8,"█>")
  242.     gpu.set(35,10,"█>")
  243.   elseif page == "Mek" then
  244.     gpu.set(35,10,"█")
  245.     gpu.setForeground(0xFFFFFF)
  246.     gpu.set(36,10,">")
  247.     gpu.setForeground(0x000000)
  248.     gpu.set(35,8,"█>")
  249.     gpu.set(35,9,"█>")
  250.   end
  251. end
  252.  
  253. local function renderPageBar()
  254.   gpu.setForeground(0x000000)
  255.   gpu.set(41,8,string.rep("█",16))
  256.   gpu.set(41,9,string.rep("█",16))
  257.   gpu.set(41,10,string.rep("█",16))
  258.   gpu.setForeground(0xFF4545)
  259.   gpu.set(41,8,RFp.."% | "..RFc)
  260.   gpu.setForeground(0x4545FF)
  261.   gpu.set(41,9,EUp.."% | "..EUc)
  262.   gpu.setForeground(0x45FF45)
  263.   gpu.set(41,10,kJp.."% | "..kJc)
  264.   if page == "RF" then gpu.setForeground(0xFF4545) end
  265.   if page == "IC2" then gpu.setForeground(0x4545FF) end
  266.   if page == "Mek" then gpu.setForeground(0x45FF45) end
  267.   gpu.fill(3,16,filled,3,"█")
  268. end
  269.  
  270. local function renderFolder()
  271.   if page == "RF" then
  272.     folderRF()
  273.     for i=1, #RF[fold] do
  274.       gpu.setForeground(0x858585)
  275.       gpu.set(6,(5+i),"RF Device:"..i)
  276.       gpu.set(24,(5+i), RF[fold][i].." %")
  277.     end
  278.   end
  279.   if page == "IC2" then
  280.     folderIC2()
  281.     for i=1, #EU[fold] do
  282.       gpu.setForeground(0x858585)
  283.       gpu.set(6,(5+i),"IC2 Device:"..i)
  284.       gpu.set(24,(5+i), EU[fold][i].." %")
  285.     end
  286.   end
  287.   if page == "Mek" then
  288.     folderMek()
  289.     for i=1, #kJ[fold] do
  290.       gpu.setForeground(0x858585)
  291.       gpu.set(6,(5+i),"Mek Device:"..i)
  292.       gpu.set(24,(5+i), kJ[fold][i].." %")
  293.     end
  294.   end
  295. end
  296.  
  297. local function activeScreen()
  298.  
  299.   --[[gpu.setForeground(0x000000) --Left Menu
  300.   gpu.fill(4,6,16,5, "█")
  301.   gpu.fill(21,6,9,5, "█")]]
  302.   gpu.setForeground(0xFFFFFF)
  303.   gpu.set(25,5, tostring(totalDevices()).." ")
  304.   renderFolder()
  305.   gpu.setForeground(0x909010)
  306.   gpu.set(16,13,fold.."/3")
  307.   selector() --Right Menu
  308.   gpu.setForeground(0x858585) --bar
  309.   gpu.fill(3,16,60,3, "█")
  310.   renderPageBar()
  311. end
  312.  
  313. local function inertScreen()
  314.   clear()
  315.   banner(title,1)
  316.   gpu.setForeground(0x000000) --bar
  317.   gpu.fill(2,15,62,5, "█")
  318.   gpu.setForeground(0x500050) --Left Menu
  319.   gpu.fill(3,4,28,10, "█")
  320.   gpu.setForeground(0x000000)
  321.   gpu.fill(4,5,16,6, "█")
  322.   gpu.fill(21,5,9,6, "█")
  323.   gpu.fill(11,12,12,2, "█")
  324.   gpu.setForeground(0x904510)
  325.   gpu.fill(3,12,8,1,"/")
  326.   gpu.fill(3,13,8,1,"\\")
  327.   gpu.setForeground(0x459010)
  328.   gpu.fill(23,12,8,1,"\\")
  329.   gpu.fill(23,13,8,1,"/")
  330.   gpu.setForeground(0x909010)
  331.   gpu.set(15,12,"PAGE:")
  332.   gpu.setForeground(0xFFFFFF)
  333.   gpu.set(5,5,"Total Devices:")
  334.   gpu.setForeground(0x404099) --Right Menu
  335.   gpu.fill(34,6,28,7, "█")
  336.   gpu.setForeground(0x000000)
  337.   gpu.fill(35,7,26,5, "█")
  338.   gpu.setForeground(0xFF4545)
  339.   gpu.set(37,8,"RF:")
  340.   gpu.setForeground(0x4545FF)
  341.   gpu.set(37,9,"EU:")
  342.   gpu.setForeground(0x45FF45)
  343.   gpu.set(37,10,"kJ:")
  344. end
  345.  
  346.  
  347. local function finalRender()
  348.   inertScreen()
  349.   while true do
  350.     activeScreen()
  351.     os.sleep(1)
  352.   end
  353. end
  354.  
  355. local function finalTouch()
  356.   while true do
  357.     local junk1,junk2,tx,ty = event.pull("touch")
  358.     if tx >= 37 then if tx <= 58 then if ty >= 6 then if ty <= 12 then
  359.       nextPage()
  360.       activeScreen()
  361.     end end end end
  362.     if tx >= 3 then if tx <= 16 then if ty >= 12 then if ty <= 13 then
  363.       foldMe("l")
  364.       activeScreen()
  365.     end end end end
  366.     if tx >= 18 then if tx <= 30 then if ty >= 12 then if ty <= 13 then
  367.       foldMe("r")
  368.       activeScreen()
  369.     end end end end
  370.   end
  371. end
  372.  
  373. local function finalStartup()
  374.   intro()
  375.   gpu.set(3,4,"Searching for connected devices")
  376.   deviceDiscover()
  377.   gpu.set(3,6,"Search Complete. Please enjoy :D")
  378.   os.sleep(2)
  379.   clear()
  380.   inertScreen()
  381. end
  382.  
  383.  
  384. local function finalLogic()
  385.   while true do
  386.     RFc,RFp,EUc,EUp,kJc,kJp,filled = totalsBarLogic() --final logic
  387.     os.sleep(1)
  388.   end
  389. end
  390.  
  391. finalStartup()
  392.  
  393. thread.create(finalLogic)
  394. thread.create(finalTouch)
  395. thread.create(finalRender)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement